LCOV - code coverage report
Current view: top level - ifmap - ifmap_link_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 76 85 89.4 %
Date: 2026-11-23 02:04:52 Functions: 12 14 85.7 %
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_link_table.h"
       7             : 
       8             : #include <boost/bind/bind.hpp>
       9             : 
      10             : #include "db/db.h"
      11             : #include "db/db_graph.h"
      12             : #include "db/db_table_partition.h"
      13             : #include "ifmap/ifmap_link.h"
      14             : #include "ifmap/ifmap_log.h"
      15             : #include "ifmap/ifmap_log_types.h"
      16             : 
      17             : using namespace std;
      18             : using namespace boost::placeholders;
      19             : 
      20         255 : IFMapLinkTable::IFMapLinkTable(DB *db, const string &name, DBGraph *graph)
      21         255 :         : DBGraphTable(db, name, graph) {
      22         255 : }
      23             : 
      24           0 : void IFMapLinkTable::Input(DBTablePartition *partition, DBClient *client,
      25             :                            DBRequest *req) {
      26           0 :     assert(false);
      27             : }
      28             : 
      29        7737 : std::unique_ptr<DBEntry> IFMapLinkTable::AllocEntry(
      30             :         const DBRequestKey *key) const {
      31        7737 :     const RequestKey *rkey = static_cast<const RequestKey *>(key);
      32        7737 :     unique_ptr<DBEntry> entry(new IFMapLink(rkey->name));
      33        7737 :     return entry;
      34             : }
      35             : 
      36             : // Generate an unique name for the link node and it should
      37             : // be independent of the order in which the right and left nodes are specified
      38        7709 : std::string IFMapLinkTable::LinkKey(const string &metadata,
      39             :                                         IFMapNode *left, IFMapNode *right) {
      40        7709 :     ostringstream oss;
      41        7709 :     if (left->ToString() < right->ToString()) {
      42        3297 :         oss << metadata << "," << left->ToString() << "," << right->ToString();
      43             :     } else {
      44        4412 :         oss << metadata << "," << right->ToString() << "," << left->ToString();
      45             :     }
      46       15418 :     return oss.str();
      47        7709 : }
      48             : 
      49        3423 : IFMapLink *IFMapLinkTable::AddLink(IFMapNode *left, IFMapNode *right,
      50             :                              const string &metadata, uint64_t sequence_number,
      51             :                              const IFMapOrigin &origin) {
      52             :     DBTablePartition *partition =
      53        3423 :         static_cast<DBTablePartition *>(GetTablePartition(0));
      54             : 
      55        3423 :     string link_name = LinkKey(metadata, left, right);
      56        3423 :     IFMapLink *link = FindLink(link_name);
      57        3423 :     if (link) {
      58           4 :         assert(link->IsDeleted());
      59           4 :         link->SetLinkRevival(true);
      60           4 :         link->ClearDelete();
      61           4 :         link->set_last_change_at_to_now();
      62           4 :         partition->Change(link);
      63             :     } else {
      64        3419 :         link = new IFMapLink(link_name);
      65        3419 :         partition->Add(link);
      66             :     }
      67        3423 :     link->SetProperties(left, right, metadata, sequence_number, origin);
      68        3423 :     assert(dynamic_cast<IFMapNode *>(left));
      69        3423 :     assert(dynamic_cast<IFMapNode *>(right));
      70        3423 :     return link;
      71        3423 : }
      72             : 
      73        4254 : IFMapLink *IFMapLinkTable::FindLink(const string &metadata, IFMapNode *left, IFMapNode *right) {
      74        4254 :     string link_name = LinkKey(metadata, left, right);
      75        8508 :     return FindLink(link_name);
      76        4254 : }
      77             : 
      78        7737 : IFMapLink *IFMapLinkTable::FindLink(const string &name) {
      79             : 
      80             :     DBTablePartition *partition =
      81        7737 :         static_cast<DBTablePartition *>(GetTablePartition(0));
      82        7737 :     RequestKey key;
      83        7737 :     key.name = name;
      84       15474 :     return static_cast<IFMapLink *>(partition->Find(&key));
      85        7737 : }
      86             : 
      87           0 : IFMapLink *IFMapLinkTable::FindNextLink(const string &name) {
      88             : 
      89             :     DBTablePartition *partition =
      90           0 :         static_cast<DBTablePartition *>(GetTablePartition(0));
      91           0 :     RequestKey key;
      92           0 :     key.name = name;
      93           0 :     return static_cast<IFMapLink *>(partition->FindNext(&key));
      94           0 : }
      95             : 
      96         164 : void IFMapLinkTable::DeleteLink(IFMapLink *link) {
      97         164 :     DBGraphEdge *edge = static_cast<DBGraphEdge *>(link);
      98         164 :     graph()->Unlink(edge);
      99         164 :     link->set_last_change_at_to_now();
     100         164 :     link->ClearNodes();
     101             :     DBTablePartition *partition =
     102         164 :         static_cast<DBTablePartition *>(GetTablePartition(0));
     103         164 :     partition->Delete(edge);
     104         164 : }
     105             : 
     106         170 : void IFMapLinkTable::DeleteLink(IFMapLink *link, const IFMapOrigin &origin) {
     107         170 :     link->RemoveOriginInfo(origin.origin);
     108         170 :     if (link->is_origin_empty()) {
     109         164 :         DeleteLink(link);
     110             :     }
     111         170 : }
     112             : 
     113         255 : DBTable *IFMapLinkTable::CreateTable(DB *db, const string &name,
     114             :                                      DBGraph *graph) {
     115         255 :     IFMapLinkTable *table = new IFMapLinkTable(db, name, graph);
     116         255 :     table->Init();
     117         255 :     return table;
     118             : }
     119             : 
     120         255 : void IFMapLinkTable::Clear() {
     121             :     DBTablePartition *partition = static_cast<DBTablePartition *>(
     122         255 :         GetTablePartition(0));
     123             : 
     124         255 :     assert(!HasListeners());
     125         255 :     for (IFMapLink *link = static_cast<IFMapLink *>(partition->GetFirst()),
     126        3514 :                  *next = NULL; link != NULL; link = next) {
     127        3259 :         next = static_cast<IFMapLink *>(partition->GetNext(link));
     128        3259 :         if (link->IsDeleted()) {
     129           0 :             continue;
     130             :         }
     131        3259 :         graph()->Unlink(link);
     132        3259 :         partition->Delete(link);
     133             :     }
     134         255 : }
     135             : 
     136         255 : void IFMapLinkTable_Init(DB *db, DBGraph *graph) {
     137             :     DBTable *table =
     138         255 :             IFMapLinkTable::CreateTable(db, "__ifmap_metadata__.0", graph);
     139         255 :     db->AddTable(table);
     140         255 : }
     141             : 
     142         160 : void IFMapLinkTable_Clear(DB *db) {
     143             :     IFMapLinkTable *ltable = static_cast<IFMapLinkTable *>(
     144         160 :             db->FindTable("__ifmap_metadata__.0"));
     145         160 :     ltable->Clear();
     146         160 : }

Generated by: LCOV version 1.14