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 209 : IFMapLinkTable::IFMapLinkTable(DB *db, const string &name, DBGraph *graph) 21 209 : : DBGraphTable(db, name, graph) { 22 209 : } 23 : 24 0 : void IFMapLinkTable::Input(DBTablePartition *partition, DBClient *client, 25 : DBRequest *req) { 26 0 : assert(false); 27 : } 28 : 29 11645 : std::unique_ptr<DBEntry> IFMapLinkTable::AllocEntry( 30 : const DBRequestKey *key) const { 31 11645 : const RequestKey *rkey = static_cast<const RequestKey *>(key); 32 11645 : unique_ptr<DBEntry> entry(new IFMapLink(rkey->name)); 33 11645 : 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 11615 : std::string IFMapLinkTable::LinkKey(const string &metadata, 39 : IFMapNode *left, IFMapNode *right) { 40 11615 : ostringstream oss; 41 11615 : if (left->ToString() < right->ToString()) { 42 5317 : oss << metadata << "," << left->ToString() << "," << right->ToString(); 43 : } else { 44 6298 : oss << metadata << "," << right->ToString() << "," << left->ToString(); 45 : } 46 23230 : return oss.str(); 47 11615 : } 48 : 49 5031 : IFMapLink *IFMapLinkTable::AddLink(IFMapNode *left, IFMapNode *right, 50 : const string &metadata, uint64_t sequence_number, 51 : const IFMapOrigin &origin) { 52 : DBTablePartition *partition = 53 5031 : static_cast<DBTablePartition *>(GetTablePartition(0)); 54 : 55 5031 : string link_name = LinkKey(metadata, left, right); 56 5031 : IFMapLink *link = FindLink(link_name); 57 5031 : 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 5027 : link = new IFMapLink(link_name); 65 5027 : partition->Add(link); 66 : } 67 5031 : link->SetProperties(left, right, metadata, sequence_number, origin); 68 5031 : assert(dynamic_cast<IFMapNode *>(left)); 69 5031 : assert(dynamic_cast<IFMapNode *>(right)); 70 5031 : return link; 71 5031 : } 72 : 73 6552 : IFMapLink *IFMapLinkTable::FindLink(const string &metadata, IFMapNode *left, IFMapNode *right) { 74 6552 : string link_name = LinkKey(metadata, left, right); 75 13104 : return FindLink(link_name); 76 6552 : } 77 : 78 11645 : IFMapLink *IFMapLinkTable::FindLink(const string &name) { 79 : 80 : DBTablePartition *partition = 81 11645 : static_cast<DBTablePartition *>(GetTablePartition(0)); 82 11645 : RequestKey key; 83 11645 : key.name = name; 84 23290 : return static_cast<IFMapLink *>(partition->Find(&key)); 85 11645 : } 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 1399 : void IFMapLinkTable::DeleteLink(IFMapLink *link) { 97 1399 : DBGraphEdge *edge = static_cast<DBGraphEdge *>(link); 98 1399 : graph()->Unlink(edge); 99 1399 : link->set_last_change_at_to_now(); 100 1399 : link->ClearNodes(); 101 : DBTablePartition *partition = 102 1399 : static_cast<DBTablePartition *>(GetTablePartition(0)); 103 1399 : partition->Delete(edge); 104 1399 : } 105 : 106 171 : void IFMapLinkTable::DeleteLink(IFMapLink *link, const IFMapOrigin &origin) { 107 171 : link->RemoveOriginInfo(origin.origin); 108 171 : if (link->is_origin_empty()) { 109 165 : DeleteLink(link); 110 : } 111 171 : } 112 : 113 207 : DBTable *IFMapLinkTable::CreateTable(DB *db, const string &name, 114 : DBGraph *graph) { 115 207 : IFMapLinkTable *table = new IFMapLinkTable(db, name, graph); 116 207 : table->Init(); 117 207 : return table; 118 : } 119 : 120 209 : void IFMapLinkTable::Clear() { 121 : DBTablePartition *partition = static_cast<DBTablePartition *>( 122 209 : GetTablePartition(0)); 123 : 124 209 : assert(!HasListeners()); 125 209 : for (IFMapLink *link = static_cast<IFMapLink *>(partition->GetFirst()), 126 3841 : *next = NULL; link != NULL; link = next) { 127 3632 : next = static_cast<IFMapLink *>(partition->GetNext(link)); 128 3632 : if (link->IsDeleted()) { 129 0 : continue; 130 : } 131 3632 : graph()->Unlink(link); 132 3632 : partition->Delete(link); 133 : } 134 209 : } 135 : 136 207 : void IFMapLinkTable_Init(DB *db, DBGraph *graph) { 137 : DBTable *table = 138 207 : IFMapLinkTable::CreateTable(db, "__ifmap_metadata__.0", graph); 139 207 : db->AddTable(table); 140 207 : } 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 : }