OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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.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 
18 IFMapLinkTable::IFMapLinkTable(DB *db, const string &name, DBGraph *graph)
19  : DBGraphTable(db, name, graph) {
20 }
21 
23  DBRequest *req) {
24  assert(false);
25 }
26 
27 std::unique_ptr<DBEntry> IFMapLinkTable::AllocEntry(
28  const DBRequestKey *key) const {
29  const RequestKey *rkey = static_cast<const RequestKey *>(key);
30  unique_ptr<DBEntry> entry(new IFMapLink(rkey->name));
31  return entry;
32 }
33 
34 // Generate an unique name for the link node and it should
35 // be independent of the order in which the right and left nodes are specified
36 std::string IFMapLinkTable::LinkKey(const string &metadata,
37  IFMapNode *left, IFMapNode *right) {
38  ostringstream oss;
39  if (left->ToString() < right->ToString()) {
40  oss << metadata << "," << left->ToString() << "," << right->ToString();
41  } else {
42  oss << metadata << "," << right->ToString() << "," << left->ToString();
43  }
44  return oss.str();
45 }
46 
48  const string &metadata, uint64_t sequence_number,
49  const IFMapOrigin &origin) {
50  DBTablePartition *partition =
51  static_cast<DBTablePartition *>(GetTablePartition(0));
52 
53  string link_name = LinkKey(metadata, left, right);
54  IFMapLink *link = FindLink(link_name);
55  if (link) {
56  assert(link->IsDeleted());
57  link->SetLinkRevival(true);
58  link->ClearDelete();
60  partition->Change(link);
61  } else {
62  link = new IFMapLink(link_name);
63  partition->Add(link);
64  }
65  link->SetProperties(left, right, metadata, sequence_number, origin);
66  assert(dynamic_cast<IFMapNode *>(left));
67  assert(dynamic_cast<IFMapNode *>(right));
68  return link;
69 }
70 
71 IFMapLink *IFMapLinkTable::FindLink(const string &metadata, IFMapNode *left, IFMapNode *right) {
72  string link_name = LinkKey(metadata, left, right);
73  return FindLink(link_name);
74 }
75 
76 IFMapLink *IFMapLinkTable::FindLink(const string &name) {
77 
78  DBTablePartition *partition =
79  static_cast<DBTablePartition *>(GetTablePartition(0));
80  RequestKey key;
81  key.name = name;
82  return static_cast<IFMapLink *>(partition->Find(&key));
83 }
84 
86 
87  DBTablePartition *partition =
88  static_cast<DBTablePartition *>(GetTablePartition(0));
89  RequestKey key;
90  key.name = name;
91  return static_cast<IFMapLink *>(partition->FindNext(&key));
92 }
93 
95  DBGraphEdge *edge = static_cast<DBGraphEdge *>(link);
96  graph()->Unlink(edge);
98  link->ClearNodes();
99  DBTablePartition *partition =
100  static_cast<DBTablePartition *>(GetTablePartition(0));
101  partition->Delete(edge);
102 }
103 
105  link->RemoveOriginInfo(origin.origin);
106  if (link->is_origin_empty()) {
107  DeleteLink(link);
108  }
109 }
110 
111 DBTable *IFMapLinkTable::CreateTable(DB *db, const string &name,
112  DBGraph *graph) {
113  IFMapLinkTable *table = new IFMapLinkTable(db, name, graph);
114  table->Init();
115  return table;
116 }
117 
119  DBTablePartition *partition = static_cast<DBTablePartition *>(
120  GetTablePartition(0));
121 
122  assert(!HasListeners());
123  for (IFMapLink *link = static_cast<IFMapLink *>(partition->GetFirst()),
124  *next = NULL; link != NULL; link = next) {
125  next = static_cast<IFMapLink *>(partition->GetNext(link));
126  if (link->IsDeleted()) {
127  continue;
128  }
129  graph()->Unlink(link);
130  partition->Delete(link);
131  }
132 }
133 
134 void IFMapLinkTable_Init(DB *db, DBGraph *graph) {
135  DBTable *table =
136  IFMapLinkTable::CreateTable(db, "__ifmap_metadata__.0", graph);
137  db->AddTable(table);
138 }
139 
141  IFMapLinkTable *ltable = static_cast<IFMapLinkTable *>(
142  db->FindTable("__ifmap_metadata__.0"));
143  ltable->Clear();
144 }
const DBGraph * graph() const
std::string LinkKey(const std::string &metadata, IFMapNode *left, IFMapNode *right)
virtual std::string ToString() const
Definition: ifmap_node.cc:31
void set_last_change_at_to_now()
Definition: db_entry.cc:96
DBEntry * Find(const DBEntry *entry)
bool IsDeleted() const
Definition: db_entry.h:49
void Unlink(DBGraphEdge *link)
Definition: db_graph.cc:52
virtual DBEntry * GetNext(const DBEntryBase *entry)
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *key) const
void DeleteLink(IFMapLink *link, const IFMapOrigin &origin)
void Delete(DBEntryBase *)
IFMapLink * FindLink(const std::string &metadata, IFMapNode *left, IFMapNode *right)
virtual void Change(DBEntry *entry)
virtual void Input(DBTablePartition *partition, DBClient *client, DBRequest *req)
IFMapLink * AddLink(IFMapNode *left, IFMapNode *right, const std::string &metadata, uint64_t sequence_number, const IFMapOrigin &origin)
Definition: db.h:24
DBEntry * FindNext(const DBRequestKey *key)
IFMapLinkTable(DB *db, const std::string &name, DBGraph *graph)
void Init()
Definition: db_table.cc:387
static DBTable * CreateTable(DB *db, const std::string &name, DBGraph *graph)
const std::string & name() const
Definition: db_table.h:110
void ClearDelete()
Definition: db_entry.h:48
virtual DBTablePartBase * GetTablePartition(const DBRequestKey *key)
Definition: db_table.cc:436
Origin origin
Definition: ifmap_origin.h:41
bool HasListeners() const
Definition: db_table.cc:234
void AddTable(DBTableBase *tbl_base)
Definition: db.cc:81
virtual DBEntry * GetFirst()
virtual void Add(DBEntry *entry)
IFMapLink * FindNextLink(const std::string &name)
DBTableBase * FindTable(const std::string &name)
Definition: db.cc:68