LCOV - code coverage report
Current view: top level - ifmap - ifmap_agent_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 343 381 90.0 %
Date: 2026-11-30 02:00:48 Functions: 28 29 96.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "ifmap/ifmap_agent_table.h"
       7             : 
       8             : #include <boost/algorithm/string.hpp>
       9             : #include <boost/bind/bind.hpp>
      10             : #include <boost/format.hpp>
      11             : #include "base/logging.h"
      12             : #include "db/db.h"
      13             : #include "db/db_graph.h"
      14             : #include "db/db_table_partition.h"
      15             : #include "ifmap/ifmap_agent_parser.h"
      16             : #include "ifmap/ifmap_node.h"
      17             : #include "ifmap/ifmap_link.h"
      18             : #include "ifmap/ifmap_agent_types.h"
      19             : 
      20             : using namespace std;
      21             : using namespace boost::placeholders;
      22             : 
      23             : SandeshTraceBufferPtr
      24             : IFMapAgentTraceBuf(SandeshTraceBufferCreate("IFMapAgentTrace", 1000));
      25             : 
      26         149 : IFMapAgentTable::IFMapAgentTable(DB *db, const string &name, DBGraph *graph)
      27         149 :         : IFMapTable(db, name, graph), pre_filter_(NULL) {
      28         149 : }
      29             : 
      30         873 : unique_ptr<DBEntry> IFMapAgentTable::AllocEntry(const DBRequestKey *key) const {
      31             :     unique_ptr<DBEntry> entry(
      32         873 :         new IFMapNode(const_cast<IFMapAgentTable *>(this)));
      33         873 :     entry->SetKey(key);
      34         873 :     return entry;
      35           0 : }
      36             : 
      37         431 : IFMapNode* IFMapAgentTable::TableEntryLookup(DB *db, RequestKey *key) {
      38             : 
      39         431 :     IFMapTable *table = FindTable(db, key->id_type);
      40         431 :     if (!table) {
      41           0 :         return NULL;
      42             :     }
      43             : 
      44         431 :     unique_ptr<DBEntry> entry(new IFMapNode(table));
      45         431 :     entry->SetKey(key);
      46         431 :     IFMapNode *node = static_cast<IFMapNode *>(table->Find(entry.get()));
      47         431 :     return node;
      48         431 : }
      49             : 
      50             : 
      51         169 : IFMapAgentTable* IFMapAgentTable::TableFind(const string &node_name) {
      52         169 :     string name = node_name;
      53         169 :     std::replace(name.begin(), name.end(), '-', '_');
      54         169 :     name = "__ifmap__." + name + ".0";
      55             :     IFMapAgentTable *table =
      56         169 :             static_cast<IFMapAgentTable *>(database()->FindTable(name));
      57         169 :     return table;
      58         169 : }
      59             : 
      60         169 : IFMapNode *IFMapAgentTable::EntryLookup(RequestKey *request) {
      61         169 :     unique_ptr<DBEntry> key(AllocEntry(request));
      62         169 :     IFMapNode *node = static_cast<IFMapNode *>(Find(key.get()));
      63         169 :     return node;
      64         169 : }
      65             : 
      66          92 : IFMapNode *IFMapAgentTable::EntryLocate(IFMapNode *node, RequestKey *req) {
      67             : 
      68             :     IFMapObject *obj;
      69             : 
      70          92 :     if (node != NULL) {
      71             :         /* If delete marked, clear it now */
      72          10 :         if (node->IsDeleted()) {
      73           0 :             node->ClearDelete();
      74           0 :             graph()->AddNode(node);
      75             :         }
      76             : 
      77          10 :         obj = node->GetObject();
      78          10 :         assert(obj);
      79             :         //We dont accept lesser sequence number updates
      80          10 :         assert(obj->sequence_number() <= req->id_seq_num);
      81             : 
      82          10 :         node->Remove(obj);
      83             : 
      84             :     } else {
      85          82 :         unique_ptr<DBEntry> key(AllocEntry(req));
      86             :         node = const_cast<IFMapNode *>(
      87          82 :             static_cast<const IFMapNode *>(key.release()));
      88             :         DBTablePartition *partition =
      89          82 :             static_cast<DBTablePartition *>(GetTablePartition(0));
      90          82 :         partition->Add(node);
      91          82 :         graph()->AddNode(node);
      92          82 :     }
      93             : 
      94          92 :     return node;
      95             : }
      96             : 
      97             : // A node is deleted. Move all links for the node to defer-list
      98          28 : void IFMapAgentTable::HandlePendingLinks(IFMapNode *node) {
      99             : 
     100             :     IFMapNode *right;
     101             :     DBGraphEdge *edge;
     102             : 
     103             :     IFMapAgentLinkTable *ltable = static_cast<IFMapAgentLinkTable *>
     104          28 :         (database()->FindTable(IFMAP_AGENT_LINK_DB_NAME));
     105          28 :     assert(ltable != NULL);
     106             : 
     107          28 :     DBGraphVertex::edge_iterator iter;
     108             :     bool origin_exists;
     109             :     uint64_t seq;
     110          28 :     for (iter = node->edge_list_begin(graph());
     111          98 :          iter != node->edge_list_end(graph());) {
     112          70 :         edge = iter.operator->();
     113          70 :         IFMapLink *l = static_cast<IFMapLink *>(edge);
     114          70 :         right = static_cast<IFMapNode *>(iter.target());
     115          70 :         iter++;
     116          70 :         seq = l->sequence_number(IFMapOrigin::UNKNOWN, &origin_exists);
     117          70 :         assert(origin_exists);
     118             :         // Create both the request keys
     119          70 :         unique_ptr <IFMapAgentLinkTable::RequestKey> req_key (new IFMapAgentLinkTable::RequestKey);
     120          70 :         req_key->left_key.id_name = node->name();
     121          70 :         req_key->left_key.id_type = node->table()->Typename();
     122          70 :         req_key->left_key.id_seq_num = seq;
     123             : 
     124          70 :         req_key->right_key.id_name = right->name();
     125          70 :         req_key->right_key.id_type = right->table()->Typename();
     126          70 :         req_key->right_key.id_seq_num = seq;
     127          70 :         req_key->metadata = l->metadata();
     128             : 
     129          70 :         DBRequest req;
     130          70 :         req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
     131          70 :         req.key = std::move(req_key);
     132             : 
     133             :         //Add it to defer list
     134          70 :         ltable->LinkDefAdd(&req);
     135             : 
     136          70 :         ltable->DelLink(node, right, edge);
     137          70 :     }
     138          28 : }
     139             : 
     140          82 : void IFMapAgentTable::DeleteNode(IFMapNode *node) {
     141             : 
     142             : 
     143          82 :     if ((node->HasAdjacencies(graph()) == true)) {
     144          28 :         HandlePendingLinks(node);
     145             :     }
     146             : 
     147             :     //Now there should not be any more adjacencies
     148          82 :     assert((node->HasAdjacencies(graph()) == false));
     149             : 
     150             :     DBTablePartition *partition =
     151          82 :         static_cast<DBTablePartition *>(GetTablePartition(0));
     152          82 :     graph()->RemoveNode(node);
     153          82 :     partition->Delete(node);
     154          82 : }
     155             : 
     156          92 : void IFMapAgentTable::NotifyNode(IFMapNode *node) {
     157             :     DBTablePartition *partition =
     158          92 :         static_cast<DBTablePartition *>(GetTablePartition(0));
     159          92 :     partition->Change(node);
     160          92 : }
     161             : 
     162             : // Process link-defer list based for the request.
     163             : // If request is add, create left->right and right-left defer nodes
     164             : // If request is delete, remove left->right and right->left defer nodes
     165             : // The sequence number is valid only in the DeferrendNode entry
     166         153 : void IFMapAgentLinkTable::LinkDefAdd(DBRequest *request) {
     167         153 :     RequestKey *key = static_cast<RequestKey *>(request->key.get());
     168             : 
     169         153 :     std::list<DeferredNode>::iterator it;
     170             : 
     171         153 :     std::list<DeferredNode> *left = NULL;
     172         153 :     LinkDefMap::iterator left_it = link_def_map_.find(key->left_key);
     173         153 :     if (link_def_map_.end() != left_it)
     174         101 :         left = left_it->second;
     175             : 
     176         153 :     std::list<DeferredNode> *right = NULL;
     177         153 :     LinkDefMap::iterator right_it = link_def_map_.find(key->right_key);
     178         153 :     if (link_def_map_.end() != right_it)
     179          61 :         right = right_it->second;
     180             : 
     181         153 :     if (request->oper == DBRequest::DB_ENTRY_DELETE)  {
     182             :         //We need to delete the old sequence links as well
     183             :         // remove left->right entry
     184          66 :         if (left) {
     185         119 :             for(it = left->begin(); it != left->end(); it++) {
     186         172 :                 if (((*it).node_key.id_type == key->right_key.id_type) &&
     187          54 :                     ((*it).node_key.id_name == key->right_key.id_name)) {
     188          54 :                     left->erase(it);
     189          54 :                     break;
     190             :                 }
     191             :             }
     192          55 :             RemoveDefListEntry(&link_def_map_, left_it, NULL);
     193             :         }
     194             : 
     195             :         // remove right->left entry
     196          66 :         if (right) {
     197          55 :             for(it = right->begin(); it != right->end(); it++) {
     198         109 :                 if (((*it).node_key.id_type == key->left_key.id_type) &&
     199          54 :                     ((*it).node_key.id_name == key->left_key.id_name)) {
     200          54 :                     right->erase(it);
     201          54 :                     break;
     202             :                 }
     203             :             }
     204          54 :             RemoveDefListEntry(&link_def_map_, right_it, NULL);
     205             :         }
     206             : 
     207          66 :         return;
     208             :     }
     209             : 
     210          87 :     bool push_left = true;
     211             : 
     212             :     // Add/Update left->right entry
     213          87 :     if (left) {
     214             :         // If list already contains, just update the seq number
     215         126 :         for(it = left->begin(); it != left->end(); it++) {
     216          87 :             if (((*it).node_key.id_type == key->right_key.id_type) &&
     217           7 :                     ((*it).node_key.id_name == key->right_key.id_name)) {
     218           0 :                 (*it).node_key.id_seq_num = key->right_key.id_seq_num;
     219           0 :                 (*it).link_metadata = key->metadata;
     220           0 :                 push_left = false;
     221           0 :                 break;
     222             :             }
     223             :         }
     224             :     } else {
     225          41 :         left = new std::list<DeferredNode>();
     226          41 :         link_def_map_[key->left_key] = left;
     227             :     }
     228             : 
     229          87 :     bool push_right = true;
     230             :     // Add/Update right->left entry
     231          87 :     if (right) {
     232             :         // If list already contains, just update the seq number
     233          15 :         for(it = right->begin(); it != right->end(); it++) {
     234           9 :             if (((*it).node_key.id_type == key->left_key.id_type) &&
     235           1 :                     ((*it).node_key.id_name == key->left_key.id_name)) {
     236           0 :                 (*it).node_key.id_seq_num = key->left_key.id_seq_num;
     237           0 :                 (*it).link_metadata = key->metadata;
     238           0 :                 push_right = false;
     239           0 :                 break;
     240             :             }
     241             :         }
     242             :     } else {
     243          80 :         right = new std::list<DeferredNode>();
     244          80 :         link_def_map_[key->right_key] = right;
     245             :     }
     246             : 
     247             :     // Add it to the end of the list
     248          87 :     struct DeferredNode dn;
     249          87 :     dn.link_metadata = key->metadata;
     250          87 :     if (push_left) {
     251          87 :         dn.node_key = key->right_key;
     252          87 :         left->push_back(dn);
     253             :     }
     254          87 :     if (push_right) {
     255          87 :         dn.node_key = key->left_key;
     256          87 :         right->push_back(dn);
     257             :     }
     258          87 :     return;
     259          87 : }
     260             : 
     261         169 : void IFMapAgentTable::Input(DBTablePartition *partition, DBClient *client,
     262             :                              DBRequest *request) {
     263         169 :     RequestKey *key = static_cast<RequestKey *>(request->key.get());
     264         169 :     IFMapAgentTable *table = NULL;
     265             :     struct IFMapAgentData *req_data;
     266             :     IFMapObject *obj;
     267             : 
     268         169 :     table = TableFind(key->id_type);
     269         169 :     if (!table) {
     270           0 :         IFMAP_AGENT_TRACE(Trace, key->id_seq_num,
     271             :                 "Table " + key->id_type + " not found");
     272           0 :         return;
     273             :     }
     274             : 
     275         169 :     IFMapNode *node = EntryLookup(key);
     276         169 :     if (table->pre_filter_) {
     277          89 :         DBRequest::DBOperation old_oper = request->oper;
     278          89 :         if (table->pre_filter_(table, node, request) == false) {
     279           0 :             IFMAP_AGENT_TRACE(Trace, key->id_seq_num,
     280             :                     "Node " + key->id_name + " neglected as filter"
     281             :                     + "suppressed");
     282           0 :             return;
     283             :         }
     284          89 :         if ((old_oper != DBRequest::DB_ENTRY_DELETE) &&
     285          49 :                 (request->oper == DBRequest::DB_ENTRY_DELETE)) {
     286           0 :             IFMAP_AGENT_TRACE(Trace, key->id_seq_num,
     287             :                     "Node " + key->id_name + "ID_PERMS Null");
     288             :         }
     289             :     }
     290             : 
     291         169 :     if (request->oper == DBRequest::DB_ENTRY_DELETE) {
     292          75 :         if (node == NULL) {
     293           6 :             IFMAP_AGENT_TRACE(Trace, key->id_seq_num,
     294             :                     "Node " + key->id_name + " not found in Delete");
     295           6 :             return;
     296             :         }
     297             : 
     298          69 :         if (node->IsDeleted()) {
     299           0 :             IFMAP_AGENT_TRACE(Trace, key->id_seq_num,
     300             :                         "Node " + key->id_name + " already deleted");
     301           0 :             return;
     302             :         }
     303             : 
     304          69 :         obj = node->GetObject();
     305             :         //We dont accept lesser sequence number updates
     306          69 :         assert(obj->sequence_number() <= key->id_seq_num);
     307             : 
     308             :         //Upate the sequence number even for deletion of node
     309          69 :         obj->set_sequence_number(key->id_seq_num);
     310          69 :         DeleteNode(node);
     311          69 :         return;
     312             :     }
     313             : 
     314          94 :     if (request->oper == DBRequest::DB_ENTRY_NOTIFY) {
     315           2 :         if (node) {
     316           2 :             partition->Notify(node);
     317             :         }
     318           2 :         return;
     319             :     }
     320             : 
     321          92 :     node = EntryLocate(node, key);
     322          92 :     assert(node);
     323             : 
     324             :     //Get the data from request key and notify oper tables
     325          92 :     req_data = static_cast<struct IFMapAgentData *>(request->data.get());
     326          92 :     obj = static_cast<IFMapObject *>(req_data->content.release());
     327             : 
     328             :     //Set the sequence number of the object
     329          92 :     obj->set_sequence_number(key->id_seq_num);
     330             : 
     331          92 :     node->Insert(obj);
     332          92 :     NotifyNode(node);
     333             : 
     334             :     IFMapAgentLinkTable *link_table = static_cast<IFMapAgentLinkTable *>(
     335          92 :         database()->FindTable(IFMAP_AGENT_LINK_DB_NAME));
     336          92 :     link_table->EvalDefLink(key);
     337             : }
     338             : 
     339         149 : void IFMapAgentTable::Clear() {
     340         149 :     assert(!HasListeners());
     341             :     DBTablePartition *partition = static_cast<DBTablePartition *>(
     342         149 :         GetTablePartition(0));
     343         149 :     IFMapNode *next = NULL;
     344         149 :     for (IFMapNode *node = static_cast<IFMapNode *>(partition->GetFirst());
     345         149 :          node != NULL; node = next) {
     346           0 :         next = static_cast<IFMapNode *>(partition->GetNext(node));
     347           0 :         if (node->IsDeleted()) {
     348           0 :             continue;
     349             :         }
     350           0 :         graph()->RemoveNode(node);
     351           0 :         partition->Delete(node);
     352             :     }
     353         149 : }
     354             : 
     355             : 
     356             : // Agent link table routines
     357         130 : IFMapLink *IFMapAgentLinkTable::FindLink(IFMapNode *left, IFMapNode *right,
     358             :                                   const std::string &metadata) {
     359             : 
     360             :     IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
     361         130 :         database()->FindTable(IFMAP_AGENT_LINK_DB_NAME));
     362         130 :     assert(table != NULL);
     363         130 :     IFMapLink *link = table->FindLink(metadata, left, right);
     364         130 :     return (link ? (link->IsDeleted() ? NULL : link) : NULL);
     365             : }
     366             : 
     367         101 : void IFMapAgentLinkTable::AddLink(IFMapNode *left, IFMapNode *right,
     368             :                                   const std::string &metadata,
     369             :                                   uint64_t seq) {
     370             : 
     371             :     IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
     372         101 :         database()->FindTable(IFMAP_AGENT_LINK_DB_NAME));
     373         101 :     assert(table != NULL);
     374             : 
     375         101 :     IFMapLink *link = table->AddLink(left, right, metadata, seq,
     376         101 :                                      IFMapOrigin(IFMapOrigin::UNKNOWN));
     377         101 :     graph()->Link(left, right, (DBGraphEdge *)link);
     378         101 : }
     379             : 
     380          95 : void IFMapAgentLinkTable::DelLink(IFMapNode *left, IFMapNode *right, DBGraphEdge *edge) {
     381             :     IFMapAgentLinkTable *table = static_cast<IFMapAgentLinkTable *>(
     382          95 :         database()->FindTable(IFMAP_AGENT_LINK_DB_NAME));
     383          95 :     assert(table != NULL);
     384          95 :     table->DeleteLink(static_cast<IFMapLink *>(edge));
     385          95 : }
     386             : 
     387           1 : IFMapAgentLinkTable::IFMapAgentLinkTable(DB *db, const string &name, DBGraph *graph)
     388           1 :         : IFMapLinkTable(db, name, graph) {
     389           1 : }
     390             : 
     391           1 : DBTable *IFMapAgentLinkTable::CreateTable(DB *db, const string &name,
     392             :                                      DBGraph *graph) {
     393           1 :     IFMapAgentLinkTable *table = new IFMapAgentLinkTable(db, name, graph);
     394           1 :     table->Init();
     395           1 :     return table;
     396             : }
     397             : 
     398             : 
     399           1 : void IFMapAgentLinkTable_Init(DB *db, DBGraph *graph) {
     400           1 :     db->RegisterFactory(IFMAP_AGENT_LINK_DB_NAME,
     401             :         boost::bind(&IFMapAgentLinkTable::CreateTable, _1, _2, graph));
     402           1 :     db->CreateTable(IFMAP_AGENT_LINK_DB_NAME);
     403           1 : }
     404             : 
     405         213 : void IFMapAgentLinkTable::Input(DBTablePartition *partition, DBClient *client,
     406             :                            DBRequest *req) {
     407             : 
     408         213 :     RequestKey *key = static_cast<RequestKey *>(req->key.get());
     409             : 
     410             :     IFMapNode *left;
     411         213 :     left = IFMapAgentTable::TableEntryLookup(database(), &key->left_key);
     412         213 :     if (!left) {
     413          31 :         IFMAP_AGENT_TRACE(Trace, key->left_key.id_seq_num,
     414             :                 key->left_key.id_type + ":" + key->left_key.id_name +
     415             :                 " not present for link to " + key->right_key.id_type +
     416             :                 ":" + key->right_key.id_name);
     417          31 :         LinkDefAdd(req);
     418          31 :         return;
     419             :     }
     420             : 
     421             :     IFMapNode *right;
     422         182 :     right = IFMapAgentTable::TableEntryLookup(database(), &key->right_key);
     423         182 :     if (!right) {
     424           9 :         IFMAP_AGENT_TRACE(Trace, key->left_key.id_seq_num,
     425             :                 key->right_key.id_type + " : " + key->right_key.id_name +
     426             :                 " not present for link to " + key->left_key.id_type + " : " +
     427             :                 key->left_key.id_name);
     428           9 :         LinkDefAdd(req);
     429           9 :         return;
     430             :     }
     431             : 
     432         173 :     if (left->IsDeleted()) {
     433          43 :         IFMAP_AGENT_TRACE(Trace, key->left_key.id_seq_num,
     434             :             "Adding Link" + key->left_key.id_type + ":" +
     435             :             key->left_key.id_name + "->" + key->right_key.id_type +
     436             :                             ":" + key->right_key.id_name + " to defer "
     437             :                             "list as left is deleted marked");
     438          43 :         LinkDefAdd(req);
     439          43 :         return;
     440             :     }
     441             : 
     442         130 :     if (right->IsDeleted()) {
     443           0 :         IFMAP_AGENT_TRACE(Trace, key->left_key.id_seq_num,
     444             :             "Adding Link" + key->left_key.id_type + ":" +
     445             :             key->left_key.id_name + "->" + key->right_key.id_type +
     446             :                             ":" + key->right_key.id_name + " to defer "
     447             :                             "list as right is deleted marked");
     448           0 :         LinkDefAdd(req);
     449           0 :         return;
     450             :     }
     451             : 
     452         130 :     IFMapObject *obj = left->GetObject();
     453         130 :     if (obj->sequence_number() < key->left_key.id_seq_num) {
     454           0 :         IFMAP_AGENT_TRACE(Trace, key->left_key.id_seq_num,
     455             :             "IFMap Link " + left->name() + right->name() +
     456             :             " with wrong seq number");
     457           0 :         LinkDefAdd(req);
     458           0 :         return;
     459             :     }
     460             : 
     461         130 :     obj = right->GetObject();
     462         130 :     if (obj->sequence_number() < key->left_key.id_seq_num) {
     463           0 :         IFMAP_AGENT_TRACE(Trace, key->left_key.id_seq_num,
     464             :             "IFMap Link " + left->name() + right->name() +
     465             :             " with wrong seq number");
     466           0 :         LinkDefAdd(req);
     467           0 :         return;
     468             :     }
     469             : 
     470         130 :     DBGraphEdge *link = FindLink(left, right, key->metadata);
     471             : 
     472         130 :     if (req->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
     473         105 :         if (link == NULL) {
     474         101 :             AddLink(left, right, key->metadata, key->left_key.id_seq_num);
     475             :         } else {
     476           4 :             IFMapOrigin origin(IFMapOrigin::UNKNOWN);
     477           4 :             IFMapLink *l = static_cast<IFMapLink *>(link);
     478           4 :             l->UpdateProperties(origin, key->left_key.id_seq_num);
     479             :         }
     480             :     } else {
     481          25 :         if (link == NULL) {
     482           0 :             return;
     483             :         }
     484          25 :         DelLink(left, right, link);
     485             :     }
     486             : }
     487             : 
     488         163 : bool IFMapAgentLinkTable::RemoveDefListEntry
     489             :     (LinkDefMap *map, LinkDefMap::iterator &map_it,
     490             :      std::list<DeferredNode>::iterator *list_it) {
     491             : 
     492         163 :     std::list<DeferredNode> *list = map_it->second;
     493         163 :     if (list_it) {
     494          40 :         list->erase(*list_it);
     495             :     }
     496             : 
     497         163 :     if (list->size()) {
     498          42 :         return false;
     499             :     }
     500         121 :     map->erase(map_it);
     501         121 :     delete list;
     502         121 :     return true;
     503             : }
     504             : 
     505             : // For every link there are 2 entries,
     506             : //  left->right
     507             : //  right->left
     508             : //
     509             : //  If both left and right node are available, remove the entries and try to
     510             : //  add the link
     511          92 : void IFMapAgentLinkTable::EvalDefLink(IFMapTable::RequestKey *key) {
     512          92 :     LinkDefMap::iterator link_defmap_it = link_def_map_.find(*key);
     513          92 :     if (link_def_map_.end() == link_defmap_it)
     514          78 :         return;
     515             : 
     516          14 :     std::list<DeferredNode> *left_list = link_defmap_it->second;
     517          14 :     std::list<DeferredNode>::iterator left_it, left_list_entry;
     518          41 :     for(left_it = left_list->begin(); left_it != left_list->end();) {
     519          27 :         left_list_entry = left_it++;
     520             : 
     521             :         // If link seq is older, dont consider the link.
     522          27 :         if ((*left_list_entry).node_key.id_seq_num < key->id_seq_num)
     523           1 :             continue;
     524             : 
     525             :         // Skip if right-node is not yet present
     526          27 :         IFMapNode *node = IFMapAgentTable::TableEntryLookup(database(),
     527          27 :                     &((*left_list_entry).node_key));
     528          27 :         if (!node)
     529           1 :             continue;
     530             : 
     531             :         //If the other end of the node is not from active control node,
     532             :         //dont consider the link
     533          26 :         IFMapObject *obj = node->GetObject();
     534          26 :         if (obj->sequence_number() < key->id_seq_num)
     535           0 :             continue;
     536             : 
     537             : 
     538             :         // left->right entry found defer-list. Find the right->left entry
     539             :         LinkDefMap::iterator right_defmap_it =
     540          26 :             link_def_map_.find((*left_list_entry).node_key);
     541          26 :         assert(link_def_map_.end() != right_defmap_it);
     542             : 
     543          26 :         std::list<DeferredNode> *right_list = right_defmap_it->second;
     544          26 :         std::list<DeferredNode>::iterator right_it, right_list_entry;
     545          26 :         bool removed_something = false;
     546          52 :         for(right_it = right_list->begin(); right_it !=
     547          52 :                 right_list->end(); right_it++) {
     548             : 
     549             :             // If link seq is older, dont consider the link.
     550          26 :             if ((*right_it).node_key.id_seq_num < key->id_seq_num)
     551           0 :                 continue;
     552             : 
     553          52 :             if ((*right_it).node_key.id_type == key->id_type &&
     554          26 :                     (*right_it).node_key.id_name == key->id_name) {
     555          26 :                 RemoveDefListEntry(&link_def_map_, right_defmap_it, &right_it);
     556          26 :                 removed_something = true;
     557          26 :                 break;
     558             :             }
     559             :         }
     560             : 
     561             :         //We should have removed something in the above iteration
     562          26 :         assert(removed_something);
     563             : 
     564             :         //Remove from deferred list before enqueing
     565          26 :         unique_ptr <RequestKey> req_key (new RequestKey);
     566          26 :         req_key->left_key = *key;
     567          26 :         req_key->right_key = (*left_list_entry).node_key;
     568          26 :         req_key->metadata = (*left_list_entry).link_metadata;
     569             :         // Dont delete left_list_entry. Its passed in req structure
     570          26 :         left_list->erase(left_list_entry);
     571             : 
     572          26 :         DBRequest req;
     573          26 :         req.key = std::move(req_key);
     574          26 :         req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
     575          26 :         Enqueue(&req);
     576          26 :     }
     577             : 
     578             :     // If list does not have any entries, delete the list
     579          14 :     RemoveDefListEntry(&link_def_map_, link_defmap_it, NULL);
     580             : }
     581             : 
     582           2 : void IFMapAgentLinkTable::DestroyDefLink(uint64_t seq) {
     583             :     std::list<DeferredNode> *ent;
     584           2 :     std::list<DeferredNode>::iterator it, list_entry;
     585           2 :     IFMapAgentLinkTable::LinkDefMap::iterator dlist_it, temp;
     586             : 
     587          13 :     for(dlist_it = link_def_map_.begin(); dlist_it != link_def_map_.end(); ) {
     588          11 :         temp = dlist_it++;
     589          11 :         ent = temp->second;
     590          14 :         for(it = ent->begin(); it != ent->end();) {
     591          14 :             list_entry = it++;
     592             : 
     593             :             //Delete the deferred link if it is old seq
     594          14 :             if ((*list_entry).node_key.id_seq_num < seq) {
     595          14 :                 if (RemoveDefListEntry(&link_def_map_, temp,
     596          14 :                             &list_entry) == true) {
     597             :                     //The list has been deleted. Move to the next map
     598             :                     //entry
     599          11 :                     break;
     600             :                 }
     601             :             }
     602             :         }
     603             :     }
     604           2 : }
     605             : 
     606             : //Stale Cleaner functionality
     607             : class IFMapAgentStaleCleaner::IFMapAgentStaleCleanerWorker : public Task {
     608             : public:
     609             : 
     610           2 :     IFMapAgentStaleCleanerWorker(DB *db, DBGraph *graph, uint64_t seq):
     611             :         Task(TaskScheduler::GetInstance()->GetTaskId("db::DBTable"), 0),
     612           2 :         db_(db), graph_(graph), seq_(seq) {
     613           2 :     }
     614             : 
     615           2 :     bool Run() {
     616           2 :         IFMAP_AGENT_TRACE(Trace, seq_,
     617             :                 "IFMap Config Audit start:");
     618             :         //Handle the links first
     619           2 :         DBGraph::edge_iterator e_next(graph_);
     620           2 :         for (DBGraph::edge_iterator e_iter = graph_->edge_list_begin();
     621           8 :             e_iter != graph_->edge_list_end(); e_iter = e_next) {
     622             : 
     623           6 :             const DBGraph::DBEdgeInfo &tuple = *e_iter;
     624             : 
     625           6 :             e_next = ++e_iter;
     626             : 
     627           6 :             IFMapNode *lhs = static_cast<IFMapNode *>(boost::get<0>(tuple));
     628           6 :             IFMapNode *rhs = static_cast<IFMapNode *>(boost::get<1>(tuple));
     629           6 :             IFMapLink *link = static_cast<IFMapLink *>(boost::get<2>(tuple));
     630           6 :             assert(link);
     631             : 
     632           6 :             bool exists = false;
     633             :             IFMapLink::LinkOriginInfo origin_info =
     634           6 :                 link->GetOriginInfo(IFMapOrigin::UNKNOWN, &exists);
     635           6 :             if (exists && (origin_info.sequence_number < seq_ )) {
     636             :                 IFMapAgentLinkTable *ltable = static_cast<IFMapAgentLinkTable *>(
     637           6 :                     db_->FindTable(IFMAP_AGENT_LINK_DB_NAME));
     638           6 :                 IFMAP_AGENT_TRACE(Trace,
     639             :                      origin_info.sequence_number, "Deleting Link between " +
     640             :                      lhs->name() + rhs->name());
     641           6 :                 ltable->DeleteLink(link);
     642             :             }
     643             :         }
     644             : 
     645             :         //Handle the vertices now
     646           2 :         DBGraph::vertex_iterator v_next(graph_);
     647           2 :         for (DBGraph::vertex_iterator v_iter = graph_->vertex_list_begin();
     648          15 :             v_iter != graph_->vertex_list_end(); v_iter = v_next) {
     649             : 
     650          13 :             IFMapNode *node = static_cast<IFMapNode *>(v_iter.operator->());
     651          13 :             v_next = ++v_iter;
     652             : 
     653          13 :             IFMapObject *obj = node->GetObject();
     654          13 :             assert(obj);
     655          13 :             if (obj->sequence_number() < seq_) {
     656          13 :                 IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
     657          13 :                 IFMAP_AGENT_TRACE(Trace, obj->sequence_number(),
     658             :                         "Deleting node " + node->name());
     659          13 :                 table->DeleteNode(node);
     660             :             }
     661             :         }
     662             : 
     663             :         //Handle deferred list
     664             :         IFMapAgentLinkTable *table = static_cast<IFMapAgentLinkTable *>(
     665           2 :                     db_->FindTable(IFMAP_AGENT_LINK_DB_NAME));
     666           2 :         table->DestroyDefLink(seq_);
     667             : 
     668           2 :         return true;
     669             :     }
     670           0 :     std::string Description() const {
     671           0 :         return "IFMapAgentStaleCleaner::IFMapAgentStaleCleanerWorker";
     672             :     }
     673             : 
     674             : private:
     675             :     DB *db_;
     676             :     DBGraph *graph_;
     677             :     uint64_t seq_;
     678             : };
     679             : 
     680           1 : IFMapAgentStaleCleaner::~IFMapAgentStaleCleaner() {
     681           1 : }
     682             : 
     683           1 : IFMapAgentStaleCleaner::IFMapAgentStaleCleaner(DB *db, DBGraph *graph) :
     684           1 :         db_(db), graph_(graph) {
     685           1 : }
     686             : 
     687           2 : bool IFMapAgentStaleCleaner::StaleTimeout(uint64_t seq) {
     688           2 :     seq_ = seq;
     689           2 :     IFMapAgentStaleCleanerWorker *cleaner = new IFMapAgentStaleCleanerWorker(db_, graph_, seq_);
     690           2 :     TaskScheduler *sch = TaskScheduler::GetInstance();
     691           2 :     sch->Enqueue(cleaner);
     692           2 :     return false;
     693             : }
     694             : 
     695           1 : void IFMapAgentStaleCleaner::Clear() {
     696             :     IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
     697           1 :         db_->FindTable(IFMAP_AGENT_LINK_DB_NAME));
     698           1 :     table->Clear();
     699           1 :     IFMapTable::ClearTables(db_);
     700           1 : }

Generated by: LCOV version 1.14