OpenSDN source code
ifmap_link_table.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 
7 #include <boost/bind/bind.hpp>
8 
9 #include "db/db.h"
10 #include "db/db_graph.h"
11 #include "db/db_table_partition.h"
12 #include "ifmap/ifmap_link.h"
13 #include "ifmap/ifmap_log.h"
14 #include "ifmap/ifmap_log_types.h"
15 
16 using namespace std;
17 using namespace boost::placeholders;
18 
19 IFMapLinkTable::IFMapLinkTable(DB *db, const string &name, DBGraph *graph)
20  : DBGraphTable(db, name, graph) {
21 }
22 
24  DBRequest *req) {
25  assert(false);
26 }
27 
28 std::unique_ptr<DBEntry> IFMapLinkTable::AllocEntry(
29  const DBRequestKey *key) const {
30  const RequestKey *rkey = static_cast<const RequestKey *>(key);
31  unique_ptr<DBEntry> entry(new IFMapLink(rkey->name));
32  return entry;
33 }
34 
35 // Generate an unique name for the link node and it should
36 // be independent of the order in which the right and left nodes are specified
37 std::string IFMapLinkTable::LinkKey(const string &metadata,
38  IFMapNode *left, IFMapNode *right) {
39  ostringstream oss;
40  if (left->ToString() < right->ToString()) {
41  oss << metadata << "," << left->ToString() << "," << right->ToString();
42  } else {
43  oss << metadata << "," << right->ToString() << "," << left->ToString();
44  }
45  return oss.str();
46 }
47 
49  const string &metadata, uint64_t sequence_number,
50  const IFMapOrigin &origin) {
51  DBTablePartition *partition =
52  static_cast<DBTablePartition *>(GetTablePartition(0));
53 
54  string link_name = LinkKey(metadata, left, right);
55  IFMapLink *link = FindLink(link_name);
56  if (link) {
57  assert(link->IsDeleted());
58  link->SetLinkRevival(true);
59  link->ClearDelete();
61  partition->Change(link);
62  } else {
63  link = new IFMapLink(link_name);
64  partition->Add(link);
65  }
66  link->SetProperties(left, right, metadata, sequence_number, origin);
67  assert(dynamic_cast<IFMapNode *>(left));
68  assert(dynamic_cast<IFMapNode *>(right));
69  return link;
70 }
71 
72 IFMapLink *IFMapLinkTable::FindLink(const string &metadata, IFMapNode *left, IFMapNode *right) {
73  string link_name = LinkKey(metadata, left, right);
74  return FindLink(link_name);
75 }
76 
77 IFMapLink *IFMapLinkTable::FindLink(const string &name) {
78 
79  DBTablePartition *partition =
80  static_cast<DBTablePartition *>(GetTablePartition(0));
81  RequestKey key;
82  key.name = name;
83  return static_cast<IFMapLink *>(partition->Find(&key));
84 }
85 
87 
88  DBTablePartition *partition =
89  static_cast<DBTablePartition *>(GetTablePartition(0));
90  RequestKey key;
91  key.name = name;
92  return static_cast<IFMapLink *>(partition->FindNext(&key));
93 }
94 
96  DBGraphEdge *edge = static_cast<DBGraphEdge *>(link);
97  graph()->Unlink(edge);
99  link->ClearNodes();
100  DBTablePartition *partition =
101  static_cast<DBTablePartition *>(GetTablePartition(0));
102  partition->Delete(edge);
103 }
104 
106  link->RemoveOriginInfo(origin.origin);
107  if (link->is_origin_empty()) {
108  DeleteLink(link);
109  }
110 }
111 
112 DBTable *IFMapLinkTable::CreateTable(DB *db, const string &name,
113  DBGraph *graph) {
114  IFMapLinkTable *table = new IFMapLinkTable(db, name, graph);
115  table->Init();
116  return table;
117 }
118 
120  DBTablePartition *partition = static_cast<DBTablePartition *>(
121  GetTablePartition(0));
122 
123  assert(!HasListeners());
124  for (IFMapLink *link = static_cast<IFMapLink *>(partition->GetFirst()),
125  *next = NULL; link != NULL; link = next) {
126  next = static_cast<IFMapLink *>(partition->GetNext(link));
127  if (link->IsDeleted()) {
128  continue;
129  }
130  graph()->Unlink(link);
131  partition->Delete(link);
132  }
133 }
134 
135 void IFMapLinkTable_Init(DB *db, DBGraph *graph) {
136  DBTable *table =
137  IFMapLinkTable::CreateTable(db, "__ifmap_metadata__.0", graph);
138  db->AddTable(table);
139 }
140 
142  IFMapLinkTable *ltable = static_cast<IFMapLinkTable *>(
143  db->FindTable("__ifmap_metadata__.0"));
144  ltable->Clear();
145 }
void ClearDelete()
Definition: db_entry.h:47
bool IsDeleted() const
Definition: db_entry.h:48
void set_last_change_at_to_now()
Definition: db_entry.cc:96
const DBGraph * graph() const
void Unlink(DBGraphEdge *link)
Definition: db_graph.cc:52
bool HasListeners() const
Definition: db_table.cc:260
const std::string & name() const
Definition: db_table.h:110
void Delete(DBEntryBase *)
virtual void Change(DBEntry *entry)
virtual void Add(DBEntry *entry)
DBEntry * FindNext(const DBRequestKey *key)
virtual DBEntry * GetNext(const DBEntryBase *entry)
DBEntry * Find(const DBEntry *entry)
virtual DBEntry * GetFirst()
void Init()
Definition: db_table.cc:413
virtual DBTablePartBase * GetTablePartition(const DBRequestKey *key)
Definition: db_table.cc:462
Definition: db.h:24
void AddTable(DBTableBase *tbl_base)
Definition: db.cc:81
DBTableBase * FindTable(const std::string &name)
Definition: db.cc:68
static DBTable * CreateTable(DB *db, const std::string &name, DBGraph *graph)
IFMapLink * FindLink(const std::string &metadata, IFMapNode *left, IFMapNode *right)
IFMapLink * AddLink(IFMapNode *left, IFMapNode *right, const std::string &metadata, uint64_t sequence_number, const IFMapOrigin &origin)
void DeleteLink(IFMapLink *link, const IFMapOrigin &origin)
std::string LinkKey(const std::string &metadata, IFMapNode *left, IFMapNode *right)
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *key) const
IFMapLink * FindNextLink(const std::string &name)
virtual void Input(DBTablePartition *partition, DBClient *client, DBRequest *req)
IFMapLinkTable(DB *db, const std::string &name, DBGraph *graph)
virtual std::string ToString() const
Definition: ifmap_node.cc:31
Origin origin
Definition: ifmap_origin.h:41