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_server_table.h"
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 :
24 170561 : IFMapServerTable::RequestData::RequestData() {
25 170561 : }
26 :
27 8096 : IFMapServerTable::RequestData::RequestData(IFMapOrigin::Origin orig,
28 8096 : const string &type, const string &name)
29 8096 : : origin(orig), id_type(type), id_name(name) {
30 8096 : }
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.
37 357314 : IFMapServerTable::RequestData::~RequestData() {
38 : #if defined(__GNUC__)
39 : #if (__GNUC_PREREQ(4, 2) > 0)
40 : boost::has_virtual_destructor<AutogenProperty>::type has_destructor;
41 178657 : assert(has_destructor);
42 : #endif
43 : #endif
44 178657 : boost::checked_delete(content.release());
45 357314 : }
46 :
47 :
48 1225233 : IFMapServerTable::IFMapServerTable(DB *db, const string &name, DBGraph *graph)
49 1225233 : : IFMapTable(db, name, graph) {
50 1225233 : }
51 :
52 653358 : unique_ptr<DBEntry> IFMapServerTable::AllocEntry(const DBRequestKey *key) const {
53 : unique_ptr<DBEntry> entry(
54 653358 : new IFMapNode(const_cast<IFMapServerTable *>(this)));
55 653358 : entry->SetKey(key);
56 653358 : return entry;
57 0 : }
58 :
59 246566 : static IFMapServerTable *TableFind(DB *db, const string &metadata) {
60 246566 : string name = metadata;
61 246566 : std::replace(name.begin(), name.end(), '-', '_');
62 246566 : name = "__ifmap__." + name + ".0";
63 : IFMapServerTable *table =
64 246566 : static_cast<IFMapServerTable *>(db->FindTable(name));
65 246566 : return table;
66 246566 : }
67 :
68 58893 : IFMapNode *IFMapServerTable::EntryLookup(RequestKey *request) {
69 58893 : unique_ptr<DBEntry> key(AllocEntry(request));
70 58893 : IFMapNode *node = static_cast<IFMapNode *>(Find(key.get()));
71 58893 : if ((node == NULL) || node->IsDeleted()) {
72 111 : return NULL;
73 : }
74 58782 : return node;
75 58893 : }
76 :
77 322987 : IFMapNode *IFMapServerTable::EntryLocate(RequestKey *request, bool *changep) {
78 322987 : unique_ptr<DBEntry> key(AllocEntry(request));
79 322987 : IFMapNode *node = static_cast<IFMapNode *>(Find(key.get()));
80 322987 : if (node != NULL) {
81 133856 : if (node->IsDeleted()) {
82 4 : node->ClearDelete();
83 4 : graph()->AddNode(node);
84 4 : IFMAP_DEBUG(IFMapNodeOperation, "Re-creating", node->ToString());
85 4 : *changep = true;
86 : }
87 133856 : return node;
88 : }
89 189131 : *changep = true;
90 : node = const_cast<IFMapNode *>(
91 189131 : static_cast<const IFMapNode *>(key.release()));
92 : DBTablePartition *partition =
93 189131 : static_cast<DBTablePartition *>(GetTablePartition(0));
94 189131 : partition->Add(node);
95 189131 : graph()->AddNode(node);
96 189131 : IFMAP_DEBUG(IFMapNodeOperation, "Creating", node->ToString());
97 189131 : return node;
98 322987 : }
99 :
100 33356 : IFMapNode *IFMapServerTable::TableEntryLookup(IFMapServerTable *table,
101 : const string &id_name) {
102 33356 : RequestKey request;
103 33356 : request.id_name = id_name;
104 66712 : return table->EntryLookup(&request);
105 33356 : }
106 :
107 169771 : IFMapNode *IFMapServerTable::TableEntryLocate(IFMapServerTable *table,
108 : const string &id_name,
109 : bool *changep) {
110 169771 : RequestKey request;
111 169771 : request.id_name = id_name;
112 339542 : return table->EntryLocate(&request, changep);
113 169771 : }
114 :
115 203125 : IFMapLink *IFMapServerTable::FindLinkNode(IFMapNode *first, IFMapNode *second,
116 : const string &metadata) {
117 : IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
118 203125 : database()->FindTable("__ifmap_metadata__.0"));
119 203125 : assert(table != NULL);
120 203125 : IFMapLink *link = table->FindLink(metadata, first, second);
121 203125 : return (link ? (link->IsDeleted() ? NULL : link) : NULL);
122 : }
123 :
124 159868 : IFMapLink *IFMapServerTable::LinkNodeAdd(IFMapNode *first, IFMapNode *second,
125 : const string &metadata,
126 : uint64_t sequence_number,
127 : const IFMapOrigin &origin) {
128 : IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
129 159868 : database()->FindTable("__ifmap_metadata__.0"));
130 159868 : assert(table != NULL);
131 159868 : IFMAP_DEBUG(IFMapLinkOperation, "Creating", metadata);
132 159868 : return table->AddLink(first, second, metadata, sequence_number, origin);
133 : }
134 :
135 9951 : void IFMapServerTable::LinkNodeUpdate(IFMapLink *link, uint64_t sequence_number,
136 : const IFMapOrigin &origin) {
137 9951 : link->set_last_change_at_to_now();
138 9951 : link->UpdateProperties(origin, sequence_number);
139 9951 : }
140 :
141 33299 : void IFMapServerTable::LinkNodeDelete(IFMapLink *link,
142 : const IFMapOrigin &origin) {
143 : IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
144 33299 : database()->FindTable("__ifmap_metadata__.0"));
145 33299 : assert(table != NULL);
146 33299 : IFMAP_DEBUG(IFMapLinkOperation, "Deleting", link->ToString());
147 33299 : table->DeleteLink(link, origin);
148 33299 : }
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 79869 : std::string IFMapServerTable::LinkAttrKey(IFMapNode *first, IFMapNode *second) {
153 79869 : ostringstream oss;
154 79869 : oss << "attr(";
155 79869 : if (first->IsLess(*second)) {
156 67372 : oss << first->name() << "," << second->name();
157 : } else {
158 12497 : oss << second->name() << "," << first->name();
159 : }
160 79869 : oss << ")";
161 159738 : return oss.str();
162 79869 : }
163 :
164 41781 : void IFMapServerTable::DeleteNode(IFMapNode *node) {
165 41781 : IFMAP_DEBUG(IFMapNodeOperation, "Deleting", node->ToString());
166 : DBTablePartition *partition =
167 41781 : static_cast<DBTablePartition *>(GetTablePartition(0));
168 41781 : graph()->RemoveNode(node);
169 41781 : partition->Delete(node);
170 41781 : }
171 :
172 120756 : void IFMapServerTable::Notify(IFMapNode *node) {
173 : DBTablePartition *partition =
174 120756 : static_cast<DBTablePartition *>(GetTablePartition(0));
175 120756 : partition->Change(node);
176 120756 : }
177 :
178 58616 : bool IFMapServerTable::DeleteIfEmpty(IFMapNode *node) {
179 58616 : if ((node->GetObject() == NULL) && !node->HasAdjacencies(graph())) {
180 41781 : DeleteNode(node);
181 41781 : return true;
182 : }
183 16835 : return false;
184 : }
185 :
186 114589 : IFMapObject *IFMapServerTable::LocateObject(IFMapNode *node,
187 : IFMapOrigin origin) {
188 114589 : IFMapObject *object = node->Find(origin);
189 114589 : if (object == NULL) {
190 99120 : object = AllocObject();
191 99120 : object->set_origin(origin);
192 99120 : node->Insert(object);
193 : }
194 114589 : return object;
195 : }
196 :
197 49069 : IFMapIdentifier *IFMapServerTable::LocateIdentifier(IFMapNode *node,
198 : IFMapOrigin origin,
199 : uint64_t sequence_number) {
200 49069 : IFMapObject *object = LocateObject(node, origin);
201 49069 : 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 49069 : if (object->sequence_number() != sequence_number) {
207 16 : IFMapIdentifier *identifier = static_cast<IFMapIdentifier *>(object);
208 16 : identifier->TransferPropertyToOldProperty();
209 16 : object->set_sequence_number(sequence_number);
210 : }
211 49069 : return static_cast<IFMapIdentifier *>(object);
212 : }
213 :
214 65520 : IFMapLinkAttr *IFMapServerTable::LocateLinkAttr(IFMapNode *node,
215 : IFMapOrigin origin,
216 : uint64_t sequence_number) {
217 65520 : IFMapObject *object = LocateObject(node, origin);
218 65520 : assert(object);
219 65520 : object->set_sequence_number(sequence_number);
220 :
221 65520 : return static_cast<IFMapLinkAttr *>(object);
222 : }
223 :
224 178649 : void IFMapServerTable::Input(DBTablePartition *partition, DBClient *client,
225 : DBRequest *request) {
226 178649 : assert(request->oper == DBRequest::DB_ENTRY_ADD_CHANGE ||
227 : request->oper == DBRequest::DB_ENTRY_DELETE);
228 178649 : RequestKey *key = static_cast<RequestKey *>(request->key.get());
229 178649 : RequestData *data = static_cast<RequestData *>(request->data.get());
230 178649 : assert(data != NULL);
231 :
232 178649 : IFMapServerTable *rtable = NULL;
233 178649 : IFMapServerTable *mtable = NULL;
234 :
235 : // Sanity checks before allocation resources.
236 178649 : if (!data->id_name.empty()) {
237 123283 : rtable = TableFind(database(), data->id_type);
238 123283 : if (!rtable) {
239 0 : IFMAP_TRACE(IFMapTblNotFoundTrace, "Cant find table",
240 : data->id_type);
241 55427 : return;
242 : }
243 123283 : mtable = TableFind(database(), data->metadata);
244 162014 : if (mtable == NULL && request->oper == DBRequest::DB_ENTRY_ADD_CHANGE &&
245 38731 : data->content.get() != NULL) {
246 0 : IFMAP_TRACE(IFMapTblNotFoundTrace, "Cant find table",
247 : data->metadata);
248 0 : return;
249 : }
250 : }
251 :
252 178649 : IFMapNode *first = NULL;
253 178649 : bool lchanged = false;
254 178649 : if (request->oper == DBRequest::DB_ENTRY_DELETE) {
255 25433 : first = EntryLookup(key);
256 25433 : if (first == NULL) {
257 74 : IFMAP_WARN(IFMapIdentifierNotFound, "Cant find identifier",
258 : key->id_name);
259 74 : return;
260 : }
261 : } else {
262 153216 : first = EntryLocate(key, &lchanged);
263 : }
264 :
265 178575 : if (data->id_name.empty()) {
266 : // property
267 55316 : first->set_last_change_at_to_now();
268 55316 : if (request->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
269 48965 : IFMapIdentifier *identifier = LocateIdentifier(first, data->origin,
270 : key->id_seq_num);
271 48965 : identifier->SetProperty(data->metadata, data->content.get());
272 48965 : partition->Change(first);
273 : } else {
274 : IFMapIdentifier *identifier = static_cast<IFMapIdentifier *>(
275 6351 : first->Find(data->origin));
276 6351 : if (identifier == NULL) {
277 8 : return;
278 : }
279 6343 : identifier->ClearProperty(data->metadata);
280 : // Figure out whether to delete the identifier.
281 6343 : if (identifier->empty()) {
282 5254 : first->Remove(identifier);
283 : }
284 6343 : if (DeleteIfEmpty(first) == false) {
285 5886 : partition->Change(first);
286 : }
287 : }
288 55308 : return;
289 : }
290 :
291 123259 : IFMapNode *second = NULL;
292 123259 : bool rchanged = false;
293 123259 : if (request->oper == DBRequest::DB_ENTRY_DELETE) {
294 19008 : second = TableEntryLookup(rtable, data->id_name);
295 19008 : if (second == NULL) {
296 14 : IFMAP_WARN(IFMapIdentifierNotFound, "Cant find identifier",
297 : data->id_name);
298 14 : return;
299 : }
300 : } else {
301 104251 : second = TableEntryLocate(rtable, data->id_name, &rchanged);
302 : }
303 :
304 123245 : IFMapNode *midnode = NULL;
305 123245 : bool mchanged = false;
306 :
307 123245 : if (mtable != NULL) {
308 : // link with attribute
309 79868 : string id_mid = LinkAttrKey(first, second);
310 79868 : if (request->oper == DBRequest::DB_ENTRY_DELETE) {
311 14348 : midnode = TableEntryLookup(mtable, id_mid);
312 14348 : if (midnode == NULL) {
313 23 : IFMAP_WARN(IFMapIdentifierNotFound, "Cant find identifier",
314 : id_mid);
315 23 : return;
316 : }
317 : } else {
318 65520 : midnode = TableEntryLocate(mtable, id_mid, &mchanged);
319 : }
320 79845 : midnode->set_last_change_at_to_now();
321 79845 : if (request->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
322 : IFMapLink *glink =
323 131040 : static_cast<IFMapLink *>(FindLinkNode(first, midnode,
324 65520 : data->metadata));
325 65520 : if (glink == NULL) {
326 125488 : glink = LinkNodeAdd(first, midnode, data->metadata,
327 62744 : key->id_seq_num, data->origin);
328 62744 : graph()->Link(first, midnode, glink);
329 : } else {
330 2776 : LinkNodeUpdate(glink, key->id_seq_num, data->origin);
331 : }
332 131040 : glink = static_cast<IFMapLink *>(FindLinkNode(midnode, second,
333 65520 : data->metadata));
334 65520 : if (glink == NULL) {
335 125484 : glink = LinkNodeAdd(midnode, second, data->metadata,
336 62742 : key->id_seq_num, data->origin);
337 62742 : graph()->Link(midnode, second, glink);
338 : } else {
339 2778 : LinkNodeUpdate(glink, key->id_seq_num, data->origin);
340 : }
341 65520 : IFMapLinkAttr *link_attr = mtable->LocateLinkAttr(midnode,
342 : data->origin,
343 : key->id_seq_num);
344 65520 : mchanged |= link_attr->SetData(data->content.get());
345 : } else {
346 14325 : IFMapObject *object = midnode->Find(data->origin);
347 14325 : if (object == NULL) {
348 0 : return;
349 : }
350 14325 : midnode->Remove(object);
351 14325 : if (midnode->GetObject() != NULL) {
352 0 : return;
353 : }
354 14325 : IFMapOrigin origin(data->origin);
355 : IFMapLink *glink =
356 28650 : static_cast<IFMapLink *>(FindLinkNode(first, midnode,
357 14325 : data->metadata));
358 14325 : if (glink) LinkNodeDelete(glink, origin);
359 28650 : glink = static_cast<IFMapLink *>(FindLinkNode(midnode, second,
360 14325 : data->metadata));
361 14325 : if (glink) LinkNodeDelete(glink, origin);
362 14325 : DeleteIfEmpty(first);
363 14325 : rtable->DeleteIfEmpty(second);
364 14325 : mtable->DeleteIfEmpty(midnode);
365 : }
366 79868 : } else {
367 : // link
368 43377 : if (request->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
369 : // Link is added if not present
370 : IFMapLink *glink =
371 77462 : static_cast<IFMapLink *>(FindLinkNode(first, second,
372 38731 : data->metadata));
373 38731 : if (glink == NULL) {
374 68668 : glink = LinkNodeAdd(first, second, data->metadata,
375 34334 : key->id_seq_num, data->origin);
376 34334 : graph()->Link(first, second, glink);
377 : } else {
378 4397 : LinkNodeUpdate(glink, key->id_seq_num, data->origin);
379 : }
380 : } else {
381 : // TODO: check if the edge is present and ignore otherwise.
382 4646 : IFMapLink *glink = FindLinkNode(first, second, data->metadata);
383 4646 : if (glink != NULL) {
384 4643 : IFMapOrigin origin(data->origin);
385 4643 : LinkNodeDelete(glink, origin);
386 : // check whether any of the identifiers can be deleted.
387 4643 : DeleteIfEmpty(first);
388 4643 : rtable->DeleteIfEmpty(second);
389 : }
390 : }
391 : }
392 :
393 123222 : if (lchanged) {
394 48442 : partition->Change(first);
395 : }
396 123222 : if (rchanged) {
397 55624 : rtable->Notify(second);
398 : }
399 123222 : if (mchanged) {
400 65132 : mtable->Notify(midnode);
401 : }
402 : }
403 :
404 1222513 : void IFMapServerTable::Clear() {
405 : DBTablePartition *partition = static_cast<DBTablePartition *>(
406 1222513 : GetTablePartition(0));
407 1222513 : assert(!HasListeners());
408 1222513 : for (IFMapNode *node = static_cast<IFMapNode *>(partition->GetFirst()),
409 1222513 : *next = NULL;
410 1369867 : node != NULL; node = next) {
411 147354 : next = static_cast<IFMapNode *>(partition->GetNext(node));
412 147354 : if (node->IsDeleted()) {
413 0 : continue;
414 : }
415 147354 : graph()->RemoveNode(node);
416 147354 : partition->Delete(node);
417 : }
418 1222513 : }
419 :
420 : // This is called in the context of the virtual_router table i.e. 'this' points
421 : // to __ifmap__.virtual_router.0
422 52 : void IFMapServerTable::IFMapVmSubscribe(const std::string &vr_name,
423 : const std::string &vm_name,
424 : bool subscribe, bool has_vms) {
425 52 : if (subscribe) {
426 52 : IFMapProcVmSubscribe(vr_name, vm_name);
427 : } else {
428 0 : IFMapProcVmUnsubscribe(vr_name, vm_name, has_vms);
429 : }
430 52 : }
431 :
432 52 : void IFMapServerTable::IFMapAddVrVmLink(IFMapNode *vr_node,
433 : IFMapNode *vm_node) {
434 : // Add the link if it does not exist. If it does, add XMPP as origin
435 52 : uint64_t sequence_number = 0;
436 52 : IFMapOrigin origin(IFMapOrigin::XMPP);
437 :
438 52 : std::string metadata = std::string("virtual-router-virtual-machine");
439 : IFMapLink *glink =
440 52 : static_cast<IFMapLink *>(FindLinkNode(vr_node, vm_node, metadata));
441 52 : if (glink == NULL) {
442 48 : glink = LinkNodeAdd(vr_node, vm_node, metadata, sequence_number, origin);
443 48 : graph()->Link(vr_node, vm_node, glink);
444 : } else {
445 4 : glink->AddOriginInfo(origin, sequence_number);
446 : }
447 52 : }
448 :
449 : // Process the vm-subscribe only after a config-add of the vm
450 52 : void IFMapServerTable::IFMapProcVmSubscribe(const std::string &vr_name,
451 : const std::string &vm_name) {
452 52 : bool changed = false;
453 :
454 : // Lookup the node corresponding to vr_name
455 52 : RequestKey request;
456 52 : request.id_name = vr_name;
457 52 : IFMapNode *vr_node = EntryLookup(&request);
458 52 : if (vr_node == NULL) {
459 0 : vr_node = EntryLocate(&request, &changed);
460 : }
461 52 : LocateIdentifier(vr_node, IFMapOrigin(IFMapOrigin::XMPP), 0);
462 :
463 : // Lookup the node corresponding to vm_name
464 : IFMapServerTable *vm_table = static_cast<IFMapServerTable *>(
465 52 : database()->FindTable("__ifmap__.virtual_machine.0"));
466 52 : assert(vm_table != NULL);
467 52 : request.id_name = vm_name;
468 52 : IFMapNode *vm_node = vm_table->EntryLookup(&request);
469 52 : assert(vm_node != NULL);
470 52 : vm_table->LocateIdentifier(vm_node, IFMapOrigin(IFMapOrigin::XMPP), 0);
471 :
472 52 : IFMapAddVrVmLink(vr_node, vm_node);
473 52 : }
474 :
475 6 : void IFMapServerTable::IFMapRemoveVrVmLink(IFMapNode *vr_node,
476 : IFMapNode *vm_node) {
477 : // Remove XMPP as origin. If there are no more origin's, delete the link.
478 6 : IFMapOrigin origin(IFMapOrigin::XMPP);
479 6 : std::string metadata = std::string("virtual-router-virtual-machine");
480 : IFMapLink *glink =
481 6 : static_cast<IFMapLink *>(FindLinkNode(vr_node, vm_node, metadata));
482 6 : LinkNodeDelete(glink, origin);
483 6 : }
484 :
485 0 : 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 0 : RequestKey request;
490 0 : request.id_name = vr_name;
491 0 : IFMapNode *vr_node = EntryLookup(&request);
492 0 : assert(vr_node != NULL);
493 :
494 : // Lookup the node corresponding to vm_name
495 : IFMapServerTable *vm_table = static_cast<IFMapServerTable *>(
496 0 : database()->FindTable("__ifmap__.virtual_machine.0"));
497 0 : assert(vm_table != NULL);
498 0 : request.id_name = vm_name;
499 0 : IFMapNode *vm_node = vm_table->EntryLookup(&request);
500 0 : assert(vm_node != NULL);
501 :
502 0 : IFMapRemoveVrVmLink(vr_node, vm_node);
503 :
504 0 : IFMapOrigin origin(IFMapOrigin::XMPP);
505 0 : RemoveObjectAndDeleteNode(vm_node, origin);
506 :
507 : // Remove XMPP as origin from the VR only if all the VMs are gone
508 0 : if (!has_vms) {
509 0 : RemoveObjectAndDeleteNode(vr_node, origin);
510 : }
511 0 : }
512 :
513 12 : void IFMapServerTable::RemoveObjectAndDeleteNode(IFMapNode *node,
514 : const IFMapOrigin &origin) {
515 12 : IFMapServerTable *table = static_cast<IFMapServerTable *>(node->table());
516 12 : assert(table);
517 12 : IFMapObject *object = node->Find(origin);
518 12 : if (object) {
519 0 : node->Remove(object);
520 : }
521 12 : table->DeleteIfEmpty(node);
522 12 : }
523 :
|