LCOV - code coverage report
Current view: top level - vnsw/agent/oper - oper_db.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 117 128 91.4 %
Date: 2026-11-30 02:00:48 Functions: 25 31 80.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #ifndef SRC_VNSW_AGENT_OPER_OPER_DB_H_
       7             : #define SRC_VNSW_AGENT_OPER_OPER_DB_H_
       8             : 
       9             : /////////////////////////////////////////////////////////////////////////////
      10             : // This file defines set of common agent oper-db class with following
      11             : // - Support IFMap dependency tracking.
      12             : //      - The IFMapNode for a request is passed in the AgentOperDBData object
      13             : //      - AgentOperDBTable::Add allocates a AgentOperDBEntry and sets the
      14             : //        AgentOperDBEntry allocated as "state" for IFMapNode passed
      15             : //      - AgentOperDBTable::OnChange and AgentOperDBTable::Resync will check
      16             : //        if there is change in IFMapNode for the AgentOperDBEntry. If so,
      17             : //        it will release state from the previous IFMapNode and set state to
      18             : //        the new IFMapNode
      19             : //      - AgentOperDBTable::Delete will release the state added for IFMapNode
      20             : /////////////////////////////////////////////////////////////////////////////
      21             : #include <cmn/agent_cmn.h>
      22             : #include <cmn/agent_db.h>
      23             : #include <operdb_init.h>
      24             : #include <ifmap_dependency_manager.h>
      25             : #include <ifmap/ifmap_node.h>
      26             : 
      27             : struct AgentOperDBKey : public AgentKey {
      28        3342 :     AgentOperDBKey() : AgentKey() { }
      29       16291 :     AgentOperDBKey(DBSubOperation sub_op) : AgentKey(sub_op) { }
      30       19633 :     virtual ~AgentOperDBKey() { }
      31             : };
      32             : 
      33             : struct AgentOperDBData : public AgentData {
      34             : 
      35         361 :     AgentOperDBData(const Agent *agent, IFMapNode *node) :
      36         361 :         AgentData(), agent_(agent) {
      37         361 :         SetIFMapNode(node);
      38         361 :     }
      39         361 :     virtual ~AgentOperDBData() {
      40         361 :         SetIFMapNode(NULL);
      41         361 :     }
      42             : 
      43             : 
      44         810 :     void SetIFMapNode(IFMapNode *node) {
      45             : 
      46         810 :         if (node == NULL) {
      47         504 :             ifmap_node_state_ = NULL;
      48         504 :             return;
      49             :         }
      50             : 
      51         306 :         assert(agent_);
      52             : 
      53             :         // We dont allow changing the node
      54         306 :         assert(!ifmap_node_state_);
      55             : 
      56         306 :         IFMapDependencyManager *dep = agent_->oper_db()->dependency_manager();
      57         306 :         ifmap_node_state_ = dep->SetState(node);
      58             :     }
      59             : 
      60         718 :     IFMapNode *ifmap_node() const {
      61         718 :         if (!ifmap_node_state_)
      62          20 :             return NULL;
      63         698 :         IFMapNodeState *state = ifmap_node_state_.get();
      64         698 :         return state->node();
      65             :     }
      66         166 :     const Agent *agent() const {
      67         166 :         return agent_;
      68             :     }
      69             : private:
      70             :     const Agent *agent_;
      71             :     // IFMap Node pointer for the object
      72             :     IFMapDependencyManager::IFMapNodePtr ifmap_node_state_;
      73             : };
      74             : 
      75             : class AgentOperDBEntry : public AgentDBEntry {
      76             : public:
      77        3347 :     AgentOperDBEntry() : AgentDBEntry()  { }
      78        3347 :     virtual ~AgentOperDBEntry() { }
      79             : 
      80         414 :     IFMapNode *ifmap_node() const {
      81         414 :         if (!ifmap_node_state_)
      82          95 :             return NULL;
      83         319 :         IFMapNodeState *state = ifmap_node_state_.get();
      84         319 :         return state->node();
      85             :     }
      86             : 
      87         102 :     void SetIFMapNodeState(IFMapDependencyManager::IFMapNodePtr sref) {
      88         102 :         ifmap_node_state_  = sref;
      89         102 :     }
      90             : private:
      91             :     friend class AgentOperDBTable;
      92             :     IFMapDependencyManager::IFMapNodePtr ifmap_node_state_;
      93             :     DISALLOW_COPY_AND_ASSIGN(AgentOperDBEntry);
      94             : };
      95             : 
      96             : class AgentOperDBTable : public AgentDBTable {
      97             : public:
      98          16 :     AgentOperDBTable(DB *db, const std::string &name) :
      99          16 :         AgentDBTable(db, name) {
     100          16 :     }
     101             : 
     102             :     AgentOperDBTable(DB *db, const std::string &name,
     103             :                      bool del_on_zero_refcount) :
     104             :         AgentDBTable(db, name, del_on_zero_refcount) {
     105             :     }
     106          16 :     virtual ~AgentOperDBTable() { }
     107             : 
     108             :     // AgentOperDBTable implements Add/OnChange/Delete/Resync APIs for
     109             :     // AgentDBTable. They will in-turn invoke
     110             :     // OperDBAdd/OperDBOnChange/OperDBDelete/OperDBResync correspondingly
     111             :     virtual DBEntry *OperDBAdd(const DBRequest *req) = 0;
     112             :     virtual bool OperDBOnChange(DBEntry *entry, const DBRequest *req) = 0;
     113             :     virtual bool OperDBDelete(DBEntry *entry, const DBRequest *req) = 0;
     114           0 :     virtual bool OperDBResync(DBEntry *entry, const DBRequest *req) {
     115           0 :         return false;
     116             :     }
     117             : 
     118             :     // Default callback handler from IFMap Dependency tracker. We invoke
     119             :     // IFNodeToReq to keep all IFMap handling at one place
     120             : 
     121         545 :     virtual void ConfigEventHandler(IFMapNode *node, DBEntry *entry) {
     122         545 :         DBRequest req;
     123         545 :         IFMapDependencyManager *dep = agent()->oper_db()->dependency_manager();
     124         545 :         boost::uuids::uuid new_uuid = boost::uuids::nil_uuid();
     125         545 :         bool uuid_set = IFNodeToUuid(node, new_uuid);
     126         545 :         IFMapNodeState *state = dep->IFMapNodeGet(node);
     127         545 :         boost::uuids::uuid old_uuid = state->uuid();
     128             : 
     129         545 :         if (!node->IsDeleted()) {
     130         467 :             if (entry) {
     131         234 :                 if ((old_uuid != new_uuid)) {
     132           0 :                     if (old_uuid != boost::uuids::nil_uuid()) {
     133           0 :                         req.oper = DBRequest::DB_ENTRY_DELETE;
     134           0 :                         if (IFNodeToReq(node, req, old_uuid) == true) {
     135           0 :                             assert(req.oper == DBRequest::DB_ENTRY_DELETE);
     136           0 :                             Enqueue(&req);
     137             :                         }
     138             :                     }
     139             :                 }
     140             :             }
     141         467 :             if (uuid_set && dep->IsNodeIdentifiedByUuid(node)) {
     142         392 :                 assert(new_uuid != boost::uuids::nil_uuid());
     143             :             }
     144         467 :             state->set_uuid(new_uuid);
     145         467 :             state->set_oper_db_request_enqueued(true);
     146         467 :             req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
     147             :         } else {
     148          78 :             req.oper = DBRequest::DB_ENTRY_DELETE;
     149          78 :             if (uuid_set && (old_uuid == boost::uuids::nil_uuid()) &&
     150           0 :                 (dep->IsNodeIdentifiedByUuid(node))) {
     151             :                 //Node was never added so no point sending delete
     152           0 :                 return;
     153             :             }
     154          78 :             new_uuid = old_uuid;
     155          78 :             if (state) {
     156          78 :                 state->set_oper_db_request_enqueued(false);
     157             :             }
     158             :         }
     159             : 
     160         545 :         if (IFNodeToReq(node, req, new_uuid) == true) {
     161          20 :             Enqueue(&req);
     162             :         }
     163             : 
     164         545 :         return;
     165         545 :     }
     166             : 
     167             : protected:
     168             :     // Handle change in IFMapNode for a DBEntry
     169         327 :     void UpdateIfMapNode(AgentOperDBEntry *entry, IFMapNode *node) {
     170         327 :         if (entry == NULL)
     171           0 :             return;
     172             : 
     173         327 :         IFMapNode *old_node = entry->ifmap_node();
     174             : 
     175         327 :         if (old_node == node)
     176         225 :             return;
     177             : 
     178         102 :         IFMapDependencyManager *dep = agent()->oper_db()->dependency_manager();
     179         102 :         if (old_node) {
     180          51 :             dep->SetObject(old_node, NULL);
     181          51 :             entry->SetIFMapNodeState(NULL);
     182             :         }
     183             : 
     184         102 :         if (node) {
     185          51 :             entry->SetIFMapNodeState(dep->SetState(node));
     186          51 :             dep->SetObject(node, entry);
     187             :         }
     188             :     }
     189             : 
     190             :     // Implement Add, Delete, OnChange to provide common agent functionality
     191             :     // including,
     192          56 :     virtual DBEntry *Add(const DBRequest *req) {
     193             :         AgentOperDBEntry *entry = static_cast<AgentOperDBEntry *>
     194          56 :             (OperDBAdd(req));
     195          56 :         AgentOperDBData *data = static_cast<AgentOperDBData *>(req->data.get());
     196          56 :         if (data && data->ifmap_node())
     197          38 :             UpdateIfMapNode(entry, data->ifmap_node());
     198          56 :         return entry;
     199             :     }
     200             : 
     201         171 :     virtual bool OnChange(DBEntry *entry, const DBRequest *req) {
     202         171 :         AgentOperDBEntry *oper_entry = static_cast<AgentOperDBEntry *>(entry);
     203         171 :         bool ret = OperDBOnChange(entry, req);
     204             : 
     205         171 :         AgentOperDBData *data = static_cast<AgentOperDBData *>(req->data.get());
     206         171 :         if (data && data->ifmap_node())
     207         170 :             UpdateIfMapNode(oper_entry, data->ifmap_node());
     208         171 :         return ret;
     209             :     }
     210             : 
     211          41 :     virtual bool Resync(DBEntry *entry, const DBRequest *req) {
     212          41 :         AgentOperDBEntry *oper_entry = static_cast<AgentOperDBEntry *>(entry);
     213          41 :         bool ret = OperDBResync(entry, req);
     214             : 
     215          41 :         AgentOperDBData *data = static_cast<AgentOperDBData *>(req->data.get());
     216          41 :         if (data && data->ifmap_node())
     217          40 :             UpdateIfMapNode(oper_entry, data->ifmap_node());
     218          41 :         return ret;
     219             :     }
     220             : 
     221          79 :     virtual bool Delete(DBEntry *entry, const DBRequest *req) {
     222          79 :         AgentOperDBEntry *oper_entry = static_cast<AgentOperDBEntry *>(entry);
     223          79 :         bool ret = OperDBDelete(entry, req);
     224             : 
     225          79 :         UpdateIfMapNode(oper_entry, NULL);
     226          79 :         return ret;
     227             :     }
     228             : 
     229             : private:
     230             :     DISALLOW_COPY_AND_ASSIGN(AgentOperDBTable);
     231             : };
     232             : 
     233             : // Oper-DB works on few IFMap tables which do not have corresponding
     234             : // oper DBTable.
     235             : // Example : virtual-router, network-ipam etc...
     236             : // The IFMap nodes are registered with ifmap-dependency-manager. The class
     237             : // OperIFMapTable is responsible to manage notifications of such IFMap tables
     238             : class OperIFMapTable {
     239             : public:
     240             :     typedef std::map<IFMapNode *, IFMapDependencyManager::IFMapNodePtr> Tree;
     241           7 :     OperIFMapTable(Agent *agent) : agent_(agent), oper_(agent->oper_db()) { }
     242           7 :     virtual ~OperIFMapTable() {
     243           7 :         assert(tree_.size() == 0);
     244           7 :     }
     245             : 
     246          55 :     Agent *agent() const { return agent_; }
     247             :     OperDB *oper() const { return oper_; }
     248             :     uint32_t Size() const { return tree_.size(); }
     249             : 
     250             :     // Callback from ifmap-dependnecy-manager
     251          31 :     void ConfigEventHandler(IFMapNode *node, DBEntry *entry) {
     252          31 :         if (node->IsDeleted()) {
     253          15 :             ConfigDelete(node);
     254          15 :             tree_.erase(node);
     255          15 :             return;
     256             :         }
     257             : 
     258          16 :         ConfigManagerEnqueue(node);
     259          16 :         return;
     260             :     }
     261             : 
     262             :     // Callback from config-manager
     263          11 :     void ProcessConfig(IFMapNode *node) {
     264          11 :         if (node->IsDeleted()) {
     265           0 :             return;
     266             :         }
     267             : 
     268          11 :         IFMapDependencyManager *dep = agent_->oper_db()->dependency_manager();
     269          11 :         tree_.insert(std::make_pair(node, dep->SetState(node)));
     270          11 :         ConfigAddChange(node);
     271             :     }
     272             : 
     273             :     virtual void ConfigDelete(IFMapNode *node) = 0;
     274             :     virtual void ConfigAddChange(IFMapNode *node) = 0;
     275             :     virtual void ConfigManagerEnqueue(IFMapNode *node) = 0;
     276             : private:
     277             :     Agent *agent_;
     278             :     OperDB *oper_;
     279             :     Tree tree_;
     280             :     DISALLOW_COPY_AND_ASSIGN(OperIFMapTable);
     281             : };
     282             : #endif  // SRC_VNSW_AGENT_OPER_OPER_DB_H_

Generated by: LCOV version 1.14