OpenSDN source code
ifmap_server_table.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
7 
8 #include <boost/algorithm/string.hpp>
9 #include <boost/checked_delete.hpp>
10 #include <boost/type_traits.hpp>
11 
12 #include "base/compiler.h"
13 #include "base/logging.h"
14 #include "db/db.h"
15 #include "db/db_graph.h"
16 #include "db/db_table_partition.h"
17 #include "ifmap/ifmap_link.h"
18 #include "ifmap/ifmap_link_table.h"
19 #include "ifmap/ifmap_log.h"
20 #include "ifmap/ifmap_log_types.h"
21 
22 using namespace std;
23 
25 }
26 
28  const string &type, const string &name)
29  : origin(orig), id_type(type), id_name(name) {
30 }
31 
32 // Warning: std::unique_ptr<> will not call the destructor if the type is
33 // incomplete at the time the unique_ptr destructor is generated. Depending
34 // on the compiler this may occur at different times. With clang the
35 // unique_ptr appears to be generated when needed by an enclosing type.
36 // gcc appears to behave differently.
38 #if defined(__GNUC__)
39 #if (__GNUC_PREREQ(4, 2) > 0)
41  assert(has_destructor);
42 #endif
43 #endif
44  boost::checked_delete(content.release());
45 }
46 
47 
49  : IFMapTable(db, name, graph) {
50 }
51 
52 unique_ptr<DBEntry> IFMapServerTable::AllocEntry(const DBRequestKey *key) const {
53  unique_ptr<DBEntry> entry(
54  new IFMapNode(const_cast<IFMapServerTable *>(this)));
55  entry->SetKey(key);
56  return entry;
57 }
58 
59 static IFMapServerTable *TableFind(DB *db, const string &metadata) {
60  string name = metadata;
61  std::replace(name.begin(), name.end(), '-', '_');
62  name = "__ifmap__." + name + ".0";
63  IFMapServerTable *table =
64  static_cast<IFMapServerTable *>(db->FindTable(name));
65  return table;
66 }
67 
69  unique_ptr<DBEntry> key(AllocEntry(request));
70  IFMapNode *node = static_cast<IFMapNode *>(Find(key.get()));
71  if ((node == NULL) || node->IsDeleted()) {
72  return NULL;
73  }
74  return node;
75 }
76 
78  unique_ptr<DBEntry> key(AllocEntry(request));
79  IFMapNode *node = static_cast<IFMapNode *>(Find(key.get()));
80  if (node != NULL) {
81  if (node->IsDeleted()) {
82  node->ClearDelete();
83  graph()->AddNode(node);
84  IFMAP_DEBUG(IFMapNodeOperation, "Re-creating", node->ToString());
85  *changep = true;
86  }
87  return node;
88  }
89  *changep = true;
90  node = const_cast<IFMapNode *>(
91  static_cast<const IFMapNode *>(key.release()));
92  DBTablePartition *partition =
93  static_cast<DBTablePartition *>(GetTablePartition(0));
94  partition->Add(node);
95  graph()->AddNode(node);
96  IFMAP_DEBUG(IFMapNodeOperation, "Creating", node->ToString());
97  return node;
98 }
99 
101  const string &id_name) {
102  RequestKey request;
103  request.id_name = id_name;
104  return table->EntryLookup(&request);
105 }
106 
108  const string &id_name,
109  bool *changep) {
110  RequestKey request;
111  request.id_name = id_name;
112  return table->EntryLocate(&request, changep);
113 }
114 
116  const string &metadata) {
117  IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
118  database()->FindTable("__ifmap_metadata__.0"));
119  assert(table != NULL);
120  IFMapLink *link = table->FindLink(metadata, first, second);
121  return (link ? (link->IsDeleted() ? NULL : link) : NULL);
122 }
123 
125  const string &metadata,
126  uint64_t sequence_number,
127  const IFMapOrigin &origin) {
128  IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
129  database()->FindTable("__ifmap_metadata__.0"));
130  assert(table != NULL);
131  IFMAP_DEBUG(IFMapLinkOperation, "Creating", metadata);
132  return table->AddLink(first, second, metadata, sequence_number, origin);
133 }
134 
135 void IFMapServerTable::LinkNodeUpdate(IFMapLink *link, uint64_t sequence_number,
136  const IFMapOrigin &origin) {
138  link->UpdateProperties(origin, sequence_number);
139 }
140 
142  const IFMapOrigin &origin) {
143  IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
144  database()->FindTable("__ifmap_metadata__.0"));
145  assert(table != NULL);
146  IFMAP_DEBUG(IFMapLinkOperation, "Deleting", link->ToString());
147  table->DeleteLink(link, origin);
148 }
149 
150 // Generate an unique key for a Link Attribute element. The generated key should
151 // be independent of the order in which the parameters are specified.
152 std::string IFMapServerTable::LinkAttrKey(IFMapNode *first, IFMapNode *second) {
153  ostringstream oss;
154  oss << "attr(";
155  if (first->IsLess(*second)) {
156  oss << first->name() << "," << second->name();
157  } else {
158  oss << second->name() << "," << first->name();
159  }
160  oss << ")";
161  return oss.str();
162 }
163 
165  IFMAP_DEBUG(IFMapNodeOperation, "Deleting", node->ToString());
166  DBTablePartition *partition =
167  static_cast<DBTablePartition *>(GetTablePartition(0));
168  graph()->RemoveNode(node);
169  partition->Delete(node);
170 }
171 
173  DBTablePartition *partition =
174  static_cast<DBTablePartition *>(GetTablePartition(0));
175  partition->Change(node);
176 }
177 
179  if ((node->GetObject() == NULL) && !node->HasAdjacencies(graph())) {
180  DeleteNode(node);
181  return true;
182  }
183  return false;
184 }
185 
187  IFMapOrigin origin) {
188  IFMapObject *object = node->Find(origin);
189  if (object == NULL) {
190  object = AllocObject();
191  object->set_origin(origin);
192  node->Insert(object);
193  }
194  return object;
195 }
196 
198  IFMapOrigin origin,
199  uint64_t sequence_number) {
200  IFMapObject *object = LocateObject(node, origin);
201  assert(object);
202 
203  // If the sequence number has changed, we are processing updates in a new
204  // connection to the ifmap server. Save the current properties and check
205  // later with the updated properties to find any stale ones.
206  if (object->sequence_number() != sequence_number) {
207  IFMapIdentifier *identifier = static_cast<IFMapIdentifier *>(object);
208  identifier->TransferPropertyToOldProperty();
209  object->set_sequence_number(sequence_number);
210  }
211  return static_cast<IFMapIdentifier *>(object);
212 }
213 
215  IFMapOrigin origin,
216  uint64_t sequence_number) {
217  IFMapObject *object = LocateObject(node, origin);
218  assert(object);
219  object->set_sequence_number(sequence_number);
220 
221  return static_cast<IFMapLinkAttr *>(object);
222 }
223 
225  DBRequest *request) {
226  assert(request->oper == DBRequest::DB_ENTRY_ADD_CHANGE ||
227  request->oper == DBRequest::DB_ENTRY_DELETE);
228  RequestKey *key = static_cast<RequestKey *>(request->key.get());
229  RequestData *data = static_cast<RequestData *>(request->data.get());
230  assert(data != NULL);
231 
232  IFMapServerTable *rtable = NULL;
233  IFMapServerTable *mtable = NULL;
234 
235  // Sanity checks before allocation resources.
236  if (!data->id_name.empty()) {
237  rtable = TableFind(database(), data->id_type);
238  if (!rtable) {
239  IFMAP_TRACE(IFMapTblNotFoundTrace, "Cant find table",
240  data->id_type);
241  return;
242  }
243  mtable = TableFind(database(), data->metadata);
244  if (mtable == NULL && request->oper == DBRequest::DB_ENTRY_ADD_CHANGE &&
245  data->content.get() != NULL) {
246  IFMAP_TRACE(IFMapTblNotFoundTrace, "Cant find table",
247  data->metadata);
248  return;
249  }
250  }
251 
252  IFMapNode *first = NULL;
253  bool lchanged = false;
254  if (request->oper == DBRequest::DB_ENTRY_DELETE) {
255  first = EntryLookup(key);
256  if (first == NULL) {
257  IFMAP_WARN(IFMapIdentifierNotFound, "Cant find identifier",
258  key->id_name);
259  return;
260  }
261  } else {
262  first = EntryLocate(key, &lchanged);
263  }
264 
265  if (data->id_name.empty()) {
266  // property
267  first->set_last_change_at_to_now();
268  if (request->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
269  IFMapIdentifier *identifier = LocateIdentifier(first, data->origin,
270  key->id_seq_num);
271  identifier->SetProperty(data->metadata, data->content.get());
272  partition->Change(first);
273  } else {
274  IFMapIdentifier *identifier = static_cast<IFMapIdentifier *>(
275  first->Find(data->origin));
276  if (identifier == NULL) {
277  return;
278  }
279  identifier->ClearProperty(data->metadata);
280  // Figure out whether to delete the identifier.
281  if (identifier->empty()) {
282  first->Remove(identifier);
283  }
284  if (DeleteIfEmpty(first) == false) {
285  partition->Change(first);
286  }
287  }
288  return;
289  }
290 
291  IFMapNode *second = NULL;
292  bool rchanged = false;
293  if (request->oper == DBRequest::DB_ENTRY_DELETE) {
294  second = TableEntryLookup(rtable, data->id_name);
295  if (second == NULL) {
296  IFMAP_WARN(IFMapIdentifierNotFound, "Cant find identifier",
297  data->id_name);
298  return;
299  }
300  } else {
301  second = TableEntryLocate(rtable, data->id_name, &rchanged);
302  }
303 
304  IFMapNode *midnode = NULL;
305  bool mchanged = false;
306 
307  if (mtable != NULL) {
308  // link with attribute
309  string id_mid = LinkAttrKey(first, second);
310  if (request->oper == DBRequest::DB_ENTRY_DELETE) {
311  midnode = TableEntryLookup(mtable, id_mid);
312  if (midnode == NULL) {
313  IFMAP_WARN(IFMapIdentifierNotFound, "Cant find identifier",
314  id_mid);
315  return;
316  }
317  } else {
318  midnode = TableEntryLocate(mtable, id_mid, &mchanged);
319  }
320  midnode->set_last_change_at_to_now();
321  if (request->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
322  IFMapLink *glink =
323  static_cast<IFMapLink *>(FindLinkNode(first, midnode,
324  data->metadata));
325  if (glink == NULL) {
326  glink = LinkNodeAdd(first, midnode, data->metadata,
327  key->id_seq_num, data->origin);
328  graph()->Link(first, midnode, glink);
329  } else {
330  LinkNodeUpdate(glink, key->id_seq_num, data->origin);
331  }
332  glink = static_cast<IFMapLink *>(FindLinkNode(midnode, second,
333  data->metadata));
334  if (glink == NULL) {
335  glink = LinkNodeAdd(midnode, second, data->metadata,
336  key->id_seq_num, data->origin);
337  graph()->Link(midnode, second, glink);
338  } else {
339  LinkNodeUpdate(glink, key->id_seq_num, data->origin);
340  }
341  IFMapLinkAttr *link_attr = mtable->LocateLinkAttr(midnode,
342  data->origin,
343  key->id_seq_num);
344  mchanged |= link_attr->SetData(data->content.get());
345  } else {
346  IFMapObject *object = midnode->Find(data->origin);
347  if (object == NULL) {
348  return;
349  }
350  midnode->Remove(object);
351  if (midnode->GetObject() != NULL) {
352  return;
353  }
354  IFMapOrigin origin(data->origin);
355  IFMapLink *glink =
356  static_cast<IFMapLink *>(FindLinkNode(first, midnode,
357  data->metadata));
358  if (glink) LinkNodeDelete(glink, origin);
359  glink = static_cast<IFMapLink *>(FindLinkNode(midnode, second,
360  data->metadata));
361  if (glink) LinkNodeDelete(glink, origin);
362  DeleteIfEmpty(first);
363  rtable->DeleteIfEmpty(second);
364  mtable->DeleteIfEmpty(midnode);
365  }
366  } else {
367  // link
368  if (request->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
369  // Link is added if not present
370  IFMapLink *glink =
371  static_cast<IFMapLink *>(FindLinkNode(first, second,
372  data->metadata));
373  if (glink == NULL) {
374  glink = LinkNodeAdd(first, second, data->metadata,
375  key->id_seq_num, data->origin);
376  graph()->Link(first, second, glink);
377  } else {
378  LinkNodeUpdate(glink, key->id_seq_num, data->origin);
379  }
380  } else {
381  // TODO: check if the edge is present and ignore otherwise.
382  IFMapLink *glink = FindLinkNode(first, second, data->metadata);
383  if (glink != NULL) {
384  IFMapOrigin origin(data->origin);
385  LinkNodeDelete(glink, origin);
386  // check whether any of the identifiers can be deleted.
387  DeleteIfEmpty(first);
388  rtable->DeleteIfEmpty(second);
389  }
390  }
391  }
392 
393  if (lchanged) {
394  partition->Change(first);
395  }
396  if (rchanged) {
397  rtable->Notify(second);
398  }
399  if (mchanged) {
400  mtable->Notify(midnode);
401  }
402 }
403 
405  DBTablePartition *partition = static_cast<DBTablePartition *>(
406  GetTablePartition(0));
407  assert(!HasListeners());
408  for (IFMapNode *node = static_cast<IFMapNode *>(partition->GetFirst()),
409  *next = NULL;
410  node != NULL; node = next) {
411  next = static_cast<IFMapNode *>(partition->GetNext(node));
412  if (node->IsDeleted()) {
413  continue;
414  }
415  graph()->RemoveNode(node);
416  partition->Delete(node);
417  }
418 }
419 
420 // This is called in the context of the virtual_router table i.e. 'this' points
421 // to __ifmap__.virtual_router.0
422 void IFMapServerTable::IFMapVmSubscribe(const std::string &vr_name,
423  const std::string &vm_name,
424  bool subscribe, bool has_vms) {
425  if (subscribe) {
426  IFMapProcVmSubscribe(vr_name, vm_name);
427  } else {
428  IFMapProcVmUnsubscribe(vr_name, vm_name, has_vms);
429  }
430 }
431 
433  IFMapNode *vm_node) {
434  // Add the link if it does not exist. If it does, add XMPP as origin
435  uint64_t sequence_number = 0;
437 
438  std::string metadata = std::string("virtual-router-virtual-machine");
439  IFMapLink *glink =
440  static_cast<IFMapLink *>(FindLinkNode(vr_node, vm_node, metadata));
441  if (glink == NULL) {
442  glink = LinkNodeAdd(vr_node, vm_node, metadata, sequence_number, origin);
443  graph()->Link(vr_node, vm_node, glink);
444  } else {
445  glink->AddOriginInfo(origin, sequence_number);
446  }
447 }
448 
449 // Process the vm-subscribe only after a config-add of the vm
450 void IFMapServerTable::IFMapProcVmSubscribe(const std::string &vr_name,
451  const std::string &vm_name) {
452  bool changed = false;
453 
454  // Lookup the node corresponding to vr_name
455  RequestKey request;
456  request.id_name = vr_name;
457  IFMapNode *vr_node = EntryLookup(&request);
458  if (vr_node == NULL) {
459  vr_node = EntryLocate(&request, &changed);
460  }
462 
463  // Lookup the node corresponding to vm_name
464  IFMapServerTable *vm_table = static_cast<IFMapServerTable *>(
465  database()->FindTable("__ifmap__.virtual_machine.0"));
466  assert(vm_table != NULL);
467  request.id_name = vm_name;
468  IFMapNode *vm_node = vm_table->EntryLookup(&request);
469  assert(vm_node != NULL);
470  vm_table->LocateIdentifier(vm_node, IFMapOrigin(IFMapOrigin::XMPP), 0);
471 
472  IFMapAddVrVmLink(vr_node, vm_node);
473 }
474 
476  IFMapNode *vm_node) {
477  // Remove XMPP as origin. If there are no more origin's, delete the link.
479  std::string metadata = std::string("virtual-router-virtual-machine");
480  IFMapLink *glink =
481  static_cast<IFMapLink *>(FindLinkNode(vr_node, vm_node, metadata));
482  LinkNodeDelete(glink, origin);
483 }
484 
485 void IFMapServerTable::IFMapProcVmUnsubscribe(const std::string &vr_name,
486  const std::string &vm_name,
487  bool has_vms) {
488  // Lookup the node corresponding to vr_name
489  RequestKey request;
490  request.id_name = vr_name;
491  IFMapNode *vr_node = EntryLookup(&request);
492  assert(vr_node != NULL);
493 
494  // Lookup the node corresponding to vm_name
495  IFMapServerTable *vm_table = static_cast<IFMapServerTable *>(
496  database()->FindTable("__ifmap__.virtual_machine.0"));
497  assert(vm_table != NULL);
498  request.id_name = vm_name;
499  IFMapNode *vm_node = vm_table->EntryLookup(&request);
500  assert(vm_node != NULL);
501 
502  IFMapRemoveVrVmLink(vr_node, vm_node);
503 
505  RemoveObjectAndDeleteNode(vm_node, origin);
506 
507  // Remove XMPP as origin from the VR only if all the VMs are gone
508  if (!has_vms) {
509  RemoveObjectAndDeleteNode(vr_node, origin);
510  }
511 }
512 
514  const IFMapOrigin &origin) {
515  IFMapServerTable *table = static_cast<IFMapServerTable *>(node->table());
516  assert(table);
517  IFMapObject *object = node->Find(origin);
518  if (object) {
519  node->Remove(object);
520  }
521  table->DeleteIfEmpty(node);
522 }
523 
void ClearDelete()
Definition: db_entry.h:48
bool IsDeleted() const
Definition: db_entry.h:49
void set_last_change_at_to_now()
Definition: db_entry.cc:97
const DBGraph * graph() const
bool HasAdjacencies(DBGraph *graph) const
Edge Link(DBGraphVertex *lhs, DBGraphVertex *rhs, DBGraphEdge *link)
Definition: db_graph.cc:42
void RemoveNode(DBGraphVertex *entry)
Definition: db_graph.cc:37
void AddNode(DBGraphVertex *entry)
Definition: db_graph.cc:31
bool HasListeners() const
Definition: db_table.cc:261
DB * database()
Definition: db_table.h:108
const std::string & name() const
Definition: db_table.h:111
void Delete(DBEntryBase *)
virtual void Change(DBEntry *entry)
virtual void Add(DBEntry *entry)
virtual DBEntry * GetNext(const DBEntryBase *entry)
virtual DBEntry * GetFirst()
DBEntry * Find(const DBEntry *entry)
Definition: db_table.cc:496
virtual DBTablePartBase * GetTablePartition(const DBRequestKey *key)
Definition: db_table.cc:463
Definition: db.h:25
DBTableBase * FindTable(const std::string &name)
Definition: db.cc:68
virtual void ClearProperty(const std::string &attr_key)=0
virtual bool empty() const
Definition: ifmap_object.h:94
virtual bool SetProperty(const std::string &attr_key, AutogenProperty *data)=0
void TransferPropertyToOldProperty()
Definition: ifmap_object.cc:28
virtual bool SetData(const AutogenProperty *data)=0
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)
IFMapTable * table()
Definition: ifmap_node.h:30
void Remove(IFMapObject *obj)
Definition: ifmap_node.cc:59
const std::string & name() const
Definition: ifmap_node.h:49
virtual bool IsLess(const DBEntry &db_entry) const
Definition: ifmap_node.h:37
IFMapObject * GetObject()
Definition: ifmap_node.cc:64
void Insert(IFMapObject *obj)
Definition: ifmap_node.cc:50
IFMapObject * Find(IFMapOrigin origin)
Definition: ifmap_node.cc:39
virtual std::string ToString() const
Definition: ifmap_node.cc:32
uint64_t sequence_number()
Definition: ifmap_object.h:34
void IFMapProcVmSubscribe(const std::string &vr_name, const std::string &vm_name)
IFMapLinkAttr * LocateLinkAttr(IFMapNode *node, IFMapOrigin origin, uint64_t sequence_number)
virtual void Clear()
static std::string LinkAttrKey(IFMapNode *first, IFMapNode *second)
static IFMapNode * TableEntryLookup(IFMapServerTable *table, const std::string &id_name)
void LinkNodeUpdate(IFMapLink *link, uint64_t sequence_number, const IFMapOrigin &origin)
void IFMapVmSubscribe(const std::string &vr_name, const std::string &vm_name, bool subscribe, bool has_vms)
IFMapNode * EntryLocate(RequestKey *key, bool *changep)
static void RemoveObjectAndDeleteNode(IFMapNode *node, const IFMapOrigin &origin)
IFMapLink * LinkNodeAdd(IFMapNode *first, IFMapNode *second, const std::string &metadata, uint64_t sequence_number, const IFMapOrigin &origin)
IFMapServerTable(DB *db, const std::string &name, DBGraph *graph)
static IFMapNode * TableEntryLocate(IFMapServerTable *table, const std::string &id_name, bool *changep)
void LinkNodeDelete(IFMapLink *link, const IFMapOrigin &origin)
IFMapNode * EntryLookup(RequestKey *key)
void IFMapRemoveVrVmLink(IFMapNode *vr_node, IFMapNode *vm_node)
void IFMapAddVrVmLink(IFMapNode *vr_node, IFMapNode *vm_node)
IFMapLink * FindLinkNode(IFMapNode *first, IFMapNode *second, const std::string &metadata)
void Notify(IFMapNode *node)
bool DeleteIfEmpty(IFMapNode *node)
IFMapIdentifier * LocateIdentifier(IFMapNode *node, IFMapOrigin origin, uint64_t sequence_number)
virtual void Input(DBTablePartition *partition, DBClient *client, DBRequest *req)
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *key) const
void DeleteNode(IFMapNode *node)
void IFMapProcVmUnsubscribe(const std::string &vr_name, const std::string &vm_name, bool has_vms)
IFMapObject * LocateObject(IFMapNode *node, IFMapOrigin origin)
virtual IFMapObject * AllocObject()=0
#define IFMAP_DEBUG(obj,...)
Definition: ifmap_log.h:34
#define IFMAP_TRACE(obj,...)
Definition: ifmap_log.h:27
#define IFMAP_WARN(obj,...)
Definition: ifmap_log.h:73
static IFMapServerTable * TableFind(DB *db, const string &metadata)
uint8_t type
Definition: load_balance.h:2
DBOperation oper
Definition: db_table.h:43
@ DB_ENTRY_DELETE
Definition: db_table.h:40
@ DB_ENTRY_ADD_CHANGE
Definition: db_table.h:39
std::unique_ptr< DBRequestKey > key
Definition: db_table.h:49
std::unique_ptr< DBRequestData > data
Definition: db_table.h:50
std::unique_ptr< AutogenProperty > content