LCOV - code coverage report
Current view: top level - vnsw/agent/cmn - agent_db.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 121 151 80.1 %
Date: 2026-08-17 02:09:53 Functions: 26 31 83.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #include "agent_cmn.h"
       6             : #include "agent_db.h"
       7             : #include <cfg/cfg_init.h>
       8             : 
       9       14211 : void AgentDBEntry::SetRefState() const {
      10       14211 :     AgentDBTable *table = static_cast<AgentDBTable *>(get_table());
      11             :     // Force calling SetState on a const object.
      12             :     // Ideally, SetState should be 'const method' and StateMap mutable
      13       14211 :     AgentDBEntry *entry = (AgentDBEntry *)this;
      14       14211 :     entry->SetState(table, table->GetRefListenerId(), new AgentDBState(this));
      15       14211 : }
      16             : 
      17       14210 : void AgentDBEntry::ClearRefState() const {
      18       14210 :     AgentDBTable *table = static_cast<AgentDBTable *>(get_table());
      19             :     // Force calling SetState on a const object.
      20             :     // Ideally, ClearState should be 'const method' and StateMap mutable
      21       14210 :     AgentDBEntry *entry = (AgentDBEntry *)this;
      22       14210 :     table->OnZeroRefcount(entry);
      23       14210 :     delete(entry->GetState(table, table->GetRefListenerId()));
      24       14210 :     entry->ClearState(table, table->GetRefListenerId());
      25       14210 : }
      26             : 
      27       83450 : bool AgentDBEntry::IsActive() const {
      28       83450 :     return !IsDeleted();
      29             : }
      30             : 
      31        2743 : DBState *AgentDBEntry::GetAgentDBEntryState(int listener_id) {
      32             :     DBState *state = dynamic_cast<DBState *>
      33        2743 :         (GetState(get_table(), listener_id));
      34        2743 :     return state;
      35             : }
      36             : 
      37        2090 : const DBState *AgentDBEntry::GetAgentDBEntryState(int listener_id) const {
      38             :     const DBState *state = dynamic_cast<const DBState *>
      39        2090 :         (GetState(get_table(), listener_id));
      40        2090 :     return state;
      41             : }
      42             : 
      43       13147 : void AgentDBEntry::AllocateResources(ResourceManager *resource_manager) {
      44       13147 : }
      45             : 
      46       13146 : void AgentDBEntry::FreeResources(ResourceManager *resource_manager) {
      47       13146 : }
      48             : 
      49        1951 : void AgentDBEntry::PostAdd() {
      50        1951 : }
      51             : 
      52          41 : static bool FlushNotify(DBTablePartBase *partition, DBEntryBase *e) {
      53          41 :     if (e->IsDeleted()) {
      54           8 :         return true;
      55             :     }
      56             : 
      57          33 :     DBRequest req(DBRequest::DB_ENTRY_DELETE);
      58          33 :     req.key = e->GetDBRequestKey();
      59          33 :     (static_cast<AgentDBTable *>(e->get_table()))->Process(req);
      60          33 :     return true;
      61          33 : }
      62             : 
      63          32 : static void FlushWalkDone(DBTable::DBTableWalkRef walk_ref,
      64             :                           DBTableBase *partition) {
      65          32 :     AgentDBTable *table = static_cast<AgentDBTable *>(partition);
      66          32 :     table->ReleaseWalker(walk_ref);
      67          32 :     table->reset_flush_walk_ref();
      68          32 : }
      69             : 
      70          96 : AgentDBTable::AgentDBTable(DB *db, const std::string &name) :
      71          96 :     DBTable(db, name), ref_listener_id_(-1), agent_(NULL),
      72          96 :     OperDBTraceBuf(SandeshTraceBufferCreate(("Oper " + name), 5000)),
      73         192 :     flush_walk_ref_() {
      74          96 :     ref_listener_id_ = Register(boost::bind(&AgentDBTable::Notify,
      75             :                                             this, _1, _2));
      76          96 : };
      77             : 
      78           0 : AgentDBTable::AgentDBTable(DB *db, const std::string &name,
      79           0 :                            bool del_on_zero_refcount) :
      80           0 :     DBTable(db, name), ref_listener_id_(-1) , agent_(NULL),
      81           0 :     OperDBTraceBuf(SandeshTraceBufferCreate(("Oper " + name), 5000)),
      82           0 :     flush_walk_ref_() {
      83           0 :     ref_listener_id_ = Register(boost::bind(&AgentDBTable::Notify,
      84             :                                             this, _1, _2));
      85           0 : };
      86             : 
      87          96 : AgentDBTable::~AgentDBTable() {
      88          96 :     assert(HasWalkers() == false);
      89          96 : }
      90             : 
      91           0 : void AgentDBTable::NotifyEntry(DBEntryBase *e) {
      92           0 :     agent_->ConcurrencyCheck();
      93             :     DBTablePartBase *tpart =
      94           0 :         static_cast<DBTablePartition *>(GetTablePartition(e));
      95           0 :     tpart->Notify(e);
      96           0 : }
      97             : 
      98          32 : void AgentDBTable::reset_flush_walk_ref() {
      99          32 :     flush_walk_ref_ = NULL;
     100          32 : }
     101             : 
     102           0 : AgentDBEntry *AgentDBTable::FindActiveEntryNoLock(const DBEntry *key) {
     103           0 :     AgentDBEntry *entry = static_cast<AgentDBEntry *> (FindNoLock(key));
     104           0 :     if (entry && (entry->IsActive() == false)) {
     105           0 :         return NULL;
     106             :     }
     107           0 :     return entry;
     108             : }
     109             : 
     110        5147 : AgentDBEntry *AgentDBTable::FindActiveEntry(const DBEntry *key) {
     111        5147 :     AgentDBEntry *entry = static_cast<AgentDBEntry *> (Find(key));
     112        5147 :     if (entry && (entry->IsActive() == false)) {
     113           0 :         return NULL;
     114             :     }
     115        5147 :     return entry;
     116             : }
     117             : 
     118           8 : AgentDBEntry *AgentDBTable::FindActiveEntryNoLock(const DBRequestKey *key) {
     119           8 :     AgentDBEntry *entry = static_cast<AgentDBEntry *>(FindNoLock(key));
     120           8 :     if (entry && (entry->IsActive() == false)) {
     121           0 :         return NULL;
     122             :     }
     123           8 :     return entry;
     124             : }
     125             : 
     126      101635 : AgentDBEntry *AgentDBTable::FindActiveEntry(const DBRequestKey *key) {
     127      101635 :     AgentDBEntry *entry = static_cast<AgentDBEntry *>(Find(key));
     128      101635 :     if (entry && (entry->IsActive() == false)) {
     129           6 :         return NULL;
     130             :     }
     131      101629 :     return entry;
     132             : }
     133             : 
     134           0 : AgentDBEntry *AgentDBTable::Find(const DBEntry *key, bool ret_del) {
     135           0 :     if (ret_del) {
     136           0 :         return Find(key);
     137             :     } else {
     138           0 :         return FindActiveEntry(key);
     139             :     }
     140             : }
     141             : 
     142      100433 : AgentDBEntry *AgentDBTable::Find(const DBRequestKey *key, bool ret_del) {
     143      100433 :     if (ret_del) {
     144       97583 :         return Find(key);
     145             :     } else {
     146        2850 :         return FindActiveEntry(key);
     147             :     }
     148             : }
     149             : 
     150        5147 : AgentDBEntry *AgentDBTable::Find(const DBEntry *key) {
     151        5147 :     return static_cast<AgentDBEntry *> (DBTable::Find(key));
     152             : }
     153             : 
     154      199218 : AgentDBEntry *AgentDBTable::Find(const DBRequestKey *key) {
     155      199218 :     return static_cast<AgentDBEntry *>(DBTable::Find(key));
     156             : }
     157             : 
     158       13147 : void AgentDBTablePartition::Add(DBEntry *entry) {
     159       13147 :     entry->set_table_partition(static_cast<DBTablePartBase *>(this));
     160       13147 :     Agent *agent = (static_cast<AgentDBTable *>(parent()))->agent();
     161       13147 :     if (agent) {
     162             :         (static_cast<AgentDBEntry *>(entry))->AllocateResources
     163       13147 :             (agent->resource_manager());
     164             :     }
     165       13147 :     DBTablePartition::Add(entry);
     166       13147 :     static_cast<AgentDBEntry *>(entry)->PostAdd();
     167       13147 : }
     168             : 
     169       13146 : void AgentDBTablePartition::Remove(DBEntryBase *entry) {
     170       13146 :     AgentDBEntry *agent_dbentry = static_cast<AgentDBEntry *>(entry);
     171       13146 :     if (agent_dbentry->GetRefCount() != 0) {
     172           0 :         agent_dbentry->ClearOnRemoveQ();
     173           0 :         return;
     174             :     }
     175       13146 :     Agent *agent = (static_cast<AgentDBTable *>(parent()))->agent();
     176       13146 :     if (agent) {
     177             :         (static_cast<AgentDBEntry *>(entry))->FreeResources
     178       13146 :             (agent->resource_manager());
     179             :     }
     180       13146 :     DBTablePartition::Remove(entry);
     181             : }
     182             : 
     183        1625 : bool AgentDBTable::IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &id) {
     184        1625 :     id = boost::uuids::nil_uuid();
     185        1625 :     return false;
     186             : }
     187             : 
     188       52419 : void AgentDBTable::Input(DBTablePartition *partition, DBClient *client,
     189             :                          DBRequest *req) {
     190       52419 :     AgentKey *key = static_cast<AgentKey *>(req->key.get());
     191             : 
     192       52419 :     if (key->sub_op_ == AgentKey::ADD_DEL_CHANGE) {
     193       49672 :         DBTable::Input(partition, client, req);
     194       49672 :         return;
     195             :     }
     196             : 
     197        2747 :     AgentDBEntry *entry = static_cast<AgentDBEntry *>(partition->Find(key));
     198        2747 :     if (entry && (entry->IsActive() == false)) {
     199          43 :         return;
     200             :     }
     201             : 
     202             :     // Dont create an entry on RESYNC sub_op
     203        2704 :     if (key->sub_op_ == AgentKey::RESYNC) {
     204        2704 :         if (entry == NULL) {
     205          78 :             return;
     206             :         }
     207        2626 :         if (Resync(entry, req)) {
     208        2337 :             partition->Change(entry);
     209             :         }
     210        2626 :         return;
     211             :     }
     212           0 :     return;
     213             : }
     214             : 
     215           4 : void AgentDBTable::Clear() {
     216           4 :     Unregister(ref_listener_id_);
     217           4 :     assert(!HasListeners());
     218             :     DBTablePartition *partition = static_cast<DBTablePartition *>(
     219           4 :         GetTablePartition(0));
     220             : 
     221           4 :     DBEntryBase *next = NULL;
     222           4 :     for (DBEntryBase *entry = partition->GetFirst(); entry; entry = next) {
     223           0 :         next = partition->GetNext(entry);
     224           0 :         if (entry->IsDeleted()) {
     225           0 :             continue;
     226             :         }
     227           0 :         partition->Delete(entry);
     228             :     }
     229           4 : }
     230             : 
     231          42 : void AgentDBTable::Process(DBRequest &req) {
     232          42 :     agent_->ConcurrencyCheck();
     233             :     DBTablePartition *tpart =
     234          42 :         static_cast<DBTablePartition *>(GetTablePartition(req.key.get()));
     235          42 :     tpart->Process(NULL, &req);
     236          42 : }
     237             : 
     238          32 : void AgentDBTable::Flush() {
     239          64 :     flush_walk_ref_ = AllocWalker(boost::bind(FlushNotify, _1, _2),
     240          32 :                                   boost::bind(FlushWalkDone, _1, _2));
     241          32 :     WalkAgain(flush_walk_ref_);
     242          32 : }

Generated by: LCOV version 1.14