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 1125 : IFMapServerTable::RequestData::RequestData() {
25 1125 : }
26 :
27 6174 : IFMapServerTable::RequestData::RequestData(IFMapOrigin::Origin orig,
28 6174 : const string &type, const string &name)
29 6174 : : origin(orig), id_type(type), id_name(name) {
30 6174 : }
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 14598 : IFMapServerTable::RequestData::~RequestData() {
38 : #if defined(__GNUC__)
39 : #if (__GNUC_PREREQ(4, 2) > 0)
40 : boost::has_virtual_destructor<AutogenProperty>::type has_destructor;
41 7299 : assert(has_destructor);
42 : #endif
43 : #endif
44 7299 : boost::checked_delete(content.release());
45 14598 : }
46 :
47 :
48 31594 : IFMapServerTable::IFMapServerTable(DB *db, const string &name, DBGraph *graph)
49 31594 : : IFMapTable(db, name, graph) {
50 31594 : }
51 :
52 14176 : unique_ptr<DBEntry> IFMapServerTable::AllocEntry(const DBRequestKey *key) const {
53 : unique_ptr<DBEntry> entry(
54 14176 : new IFMapNode(const_cast<IFMapServerTable *>(this)));
55 14176 : entry->SetKey(key);
56 14176 : return entry;
57 0 : }
58 :
59 6374 : static IFMapServerTable *TableFind(DB *db, const string &metadata) {
60 6374 : string name = metadata;
61 6374 : std::replace(name.begin(), name.end(), '-', '_');
62 6374 : name = "__ifmap__." + name + ".0";
63 : IFMapServerTable *table =
64 6374 : static_cast<IFMapServerTable *>(db->FindTable(name));
65 6374 : return table;
66 6374 : }
67 :
68 619 : IFMapNode *IFMapServerTable::EntryLookup(RequestKey *request) {
69 619 : unique_ptr<DBEntry> key(AllocEntry(request));
70 619 : IFMapNode *node = static_cast<IFMapNode *>(Find(key.get()));
71 619 : if ((node == NULL) || node->IsDeleted()) {
72 4 : return NULL;
73 : }
74 615 : return node;
75 619 : }
76 :
77 10807 : IFMapNode *IFMapServerTable::EntryLocate(RequestKey *request, bool *changep) {
78 10807 : unique_ptr<DBEntry> key(AllocEntry(request));
79 10807 : IFMapNode *node = static_cast<IFMapNode *>(Find(key.get()));
80 10807 : if (node != NULL) {
81 7314 : 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 7314 : return node;
88 : }
89 3493 : *changep = true;
90 : node = const_cast<IFMapNode *>(
91 3493 : static_cast<const IFMapNode *>(key.release()));
92 : DBTablePartition *partition =
93 3493 : static_cast<DBTablePartition *>(GetTablePartition(0));
94 3493 : partition->Add(node);
95 3493 : graph()->AddNode(node);
96 3493 : IFMAP_DEBUG(IFMapNodeOperation, "Creating", node->ToString());
97 3493 : return node;
98 10807 : }
99 :
100 165 : IFMapNode *IFMapServerTable::TableEntryLookup(IFMapServerTable *table,
101 : const string &id_name) {
102 165 : RequestKey request;
103 165 : request.id_name = id_name;
104 330 : return table->EntryLookup(&request);
105 165 : }
106 :
107 3866 : IFMapNode *IFMapServerTable::TableEntryLocate(IFMapServerTable *table,
108 : const string &id_name,
109 : bool *changep) {
110 3866 : RequestKey request;
111 3866 : request.id_name = id_name;
112 7732 : return table->EntryLocate(&request, changep);
113 3866 : }
114 :
115 4089 : IFMapLink *IFMapServerTable::FindLinkNode(IFMapNode *first, IFMapNode *second,
116 : const string &metadata) {
117 : IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
118 4089 : database()->FindTable("__ifmap_metadata__.0"));
119 4089 : assert(table != NULL);
120 4089 : IFMapLink *link = table->FindLink(metadata, first, second);
121 4089 : return (link ? (link->IsDeleted() ? NULL : link) : NULL);
122 : }
123 :
124 3797 : 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 3797 : database()->FindTable("__ifmap_metadata__.0"));
130 3797 : assert(table != NULL);
131 3797 : IFMAP_DEBUG(IFMapLinkOperation, "Creating", metadata);
132 3797 : return table->AddLink(first, second, metadata, sequence_number, origin);
133 : }
134 :
135 117 : void IFMapServerTable::LinkNodeUpdate(IFMapLink *link, uint64_t sequence_number,
136 : const IFMapOrigin &origin) {
137 117 : link->set_last_change_at_to_now();
138 117 : link->UpdateProperties(origin, sequence_number);
139 117 : }
140 :
141 171 : void IFMapServerTable::LinkNodeDelete(IFMapLink *link,
142 : const IFMapOrigin &origin) {
143 : IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
144 171 : database()->FindTable("__ifmap_metadata__.0"));
145 171 : assert(table != NULL);
146 171 : IFMAP_DEBUG(IFMapLinkOperation, "Deleting", link->ToString());
147 171 : table->DeleteLink(link, origin);
148 171 : }
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 847 : std::string IFMapServerTable::LinkAttrKey(IFMapNode *first, IFMapNode *second) {
153 847 : ostringstream oss;
154 847 : oss << "attr(";
155 847 : if (first->IsLess(*second)) {
156 699 : oss << first->name() << "," << second->name();
157 : } else {
158 148 : oss << second->name() << "," << first->name();
159 : }
160 847 : oss << ")";
161 1694 : return oss.str();
162 847 : }
163 :
164 232 : void IFMapServerTable::DeleteNode(IFMapNode *node) {
165 232 : IFMAP_DEBUG(IFMapNodeOperation, "Deleting", node->ToString());
166 : DBTablePartition *partition =
167 232 : static_cast<DBTablePartition *>(GetTablePartition(0));
168 232 : graph()->RemoveNode(node);
169 232 : partition->Delete(node);
170 232 : }
171 :
172 1631 : void IFMapServerTable::Notify(IFMapNode *node) {
173 : DBTablePartition *partition =
174 1631 : static_cast<DBTablePartition *>(GetTablePartition(0));
175 1631 : partition->Change(node);
176 1631 : }
177 :
178 523 : bool IFMapServerTable::DeleteIfEmpty(IFMapNode *node) {
179 523 : if ((node->GetObject() == NULL) && !node->HasAdjacencies(graph())) {
180 232 : DeleteNode(node);
181 232 : return true;
182 : }
183 291 : return false;
184 : }
185 :
186 4831 : IFMapObject *IFMapServerTable::LocateObject(IFMapNode *node,
187 : IFMapOrigin origin) {
188 4831 : IFMapObject *object = node->Find(origin);
189 4831 : if (object == NULL) {
190 3216 : object = AllocObject();
191 3216 : object->set_origin(origin);
192 3216 : node->Insert(object);
193 : }
194 4831 : return object;
195 : }
196 :
197 4005 : IFMapIdentifier *IFMapServerTable::LocateIdentifier(IFMapNode *node,
198 : IFMapOrigin origin,
199 : uint64_t sequence_number) {
200 4005 : IFMapObject *object = LocateObject(node, origin);
201 4005 : 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 4005 : 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 4005 : return static_cast<IFMapIdentifier *>(object);
212 : }
213 :
214 826 : IFMapLinkAttr *IFMapServerTable::LocateLinkAttr(IFMapNode *node,
215 : IFMapOrigin origin,
216 : uint64_t sequence_number) {
217 826 : IFMapObject *object = LocateObject(node, origin);
218 826 : assert(object);
219 826 : object->set_sequence_number(sequence_number);
220 :
221 826 : return static_cast<IFMapLinkAttr *>(object);
222 : }
223 :
224 7291 : void IFMapServerTable::Input(DBTablePartition *partition, DBClient *client,
225 : DBRequest *request) {
226 7291 : assert(request->oper == DBRequest::DB_ENTRY_ADD_CHANGE ||
227 : request->oper == DBRequest::DB_ENTRY_DELETE);
228 7291 : RequestKey *key = static_cast<RequestKey *>(request->key.get());
229 7291 : RequestData *data = static_cast<RequestData *>(request->data.get());
230 7291 : assert(data != NULL);
231 :
232 7291 : IFMapServerTable *rtable = NULL;
233 7291 : IFMapServerTable *mtable = NULL;
234 :
235 : // Sanity checks before allocation resources.
236 7291 : if (!data->id_name.empty()) {
237 3187 : rtable = TableFind(database(), data->id_type);
238 3187 : if (!rtable) {
239 0 : IFMAP_TRACE(IFMapTblNotFoundTrace, "Cant find table",
240 : data->id_type);
241 4106 : return;
242 : }
243 3187 : mtable = TableFind(database(), data->metadata);
244 5401 : if (mtable == NULL && request->oper == DBRequest::DB_ENTRY_ADD_CHANGE &&
245 2214 : data->content.get() != NULL) {
246 0 : IFMAP_TRACE(IFMapTblNotFoundTrace, "Cant find table",
247 : data->metadata);
248 0 : return;
249 : }
250 : }
251 :
252 7291 : IFMapNode *first = NULL;
253 7291 : bool lchanged = false;
254 7291 : if (request->oper == DBRequest::DB_ENTRY_DELETE) {
255 350 : first = EntryLookup(key);
256 350 : if (first == NULL) {
257 4 : IFMAP_WARN(IFMapIdentifierNotFound, "Cant find identifier",
258 : key->id_name);
259 4 : return;
260 : }
261 : } else {
262 6941 : first = EntryLocate(key, &lchanged);
263 : }
264 :
265 7287 : if (data->id_name.empty()) {
266 : // property
267 4102 : first->set_last_change_at_to_now();
268 4102 : if (request->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
269 3901 : IFMapIdentifier *identifier = LocateIdentifier(first, data->origin,
270 : key->id_seq_num);
271 3901 : identifier->SetProperty(data->metadata, data->content.get());
272 3901 : partition->Change(first);
273 : } else {
274 : IFMapIdentifier *identifier = static_cast<IFMapIdentifier *>(
275 201 : first->Find(data->origin));
276 201 : if (identifier == NULL) {
277 0 : return;
278 : }
279 201 : identifier->ClearProperty(data->metadata);
280 : // Figure out whether to delete the identifier.
281 201 : if (identifier->empty()) {
282 161 : first->Remove(identifier);
283 : }
284 201 : if (DeleteIfEmpty(first) == false) {
285 138 : partition->Change(first);
286 : }
287 : }
288 4102 : return;
289 : }
290 :
291 3185 : IFMapNode *second = NULL;
292 3185 : bool rchanged = false;
293 3185 : if (request->oper == DBRequest::DB_ENTRY_DELETE) {
294 145 : second = TableEntryLookup(rtable, data->id_name);
295 145 : if (second == NULL) {
296 0 : IFMAP_WARN(IFMapIdentifierNotFound, "Cant find identifier",
297 : data->id_name);
298 0 : return;
299 : }
300 : } else {
301 3040 : second = TableEntryLocate(rtable, data->id_name, &rchanged);
302 : }
303 :
304 3185 : IFMapNode *midnode = NULL;
305 3185 : bool mchanged = false;
306 :
307 3185 : if (mtable != NULL) {
308 : // link with attribute
309 846 : string id_mid = LinkAttrKey(first, second);
310 846 : if (request->oper == DBRequest::DB_ENTRY_DELETE) {
311 20 : midnode = TableEntryLookup(mtable, id_mid);
312 20 : if (midnode == NULL) {
313 0 : IFMAP_WARN(IFMapIdentifierNotFound, "Cant find identifier",
314 : id_mid);
315 0 : return;
316 : }
317 : } else {
318 826 : midnode = TableEntryLocate(mtable, id_mid, &mchanged);
319 : }
320 846 : midnode->set_last_change_at_to_now();
321 846 : if (request->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
322 : IFMapLink *glink =
323 1652 : static_cast<IFMapLink *>(FindLinkNode(first, midnode,
324 826 : data->metadata));
325 826 : if (glink == NULL) {
326 1612 : glink = LinkNodeAdd(first, midnode, data->metadata,
327 806 : key->id_seq_num, data->origin);
328 806 : graph()->Link(first, midnode, glink);
329 : } else {
330 20 : LinkNodeUpdate(glink, key->id_seq_num, data->origin);
331 : }
332 1652 : glink = static_cast<IFMapLink *>(FindLinkNode(midnode, second,
333 826 : data->metadata));
334 826 : if (glink == NULL) {
335 1608 : glink = LinkNodeAdd(midnode, second, data->metadata,
336 804 : key->id_seq_num, data->origin);
337 804 : graph()->Link(midnode, second, glink);
338 : } else {
339 22 : LinkNodeUpdate(glink, key->id_seq_num, data->origin);
340 : }
341 826 : IFMapLinkAttr *link_attr = mtable->LocateLinkAttr(midnode,
342 : data->origin,
343 : key->id_seq_num);
344 826 : mchanged |= link_attr->SetData(data->content.get());
345 : } else {
346 20 : IFMapObject *object = midnode->Find(data->origin);
347 20 : if (object == NULL) {
348 0 : return;
349 : }
350 20 : midnode->Remove(object);
351 20 : if (midnode->GetObject() != NULL) {
352 0 : return;
353 : }
354 20 : IFMapOrigin origin(data->origin);
355 : IFMapLink *glink =
356 40 : static_cast<IFMapLink *>(FindLinkNode(first, midnode,
357 20 : data->metadata));
358 20 : if (glink) LinkNodeDelete(glink, origin);
359 40 : glink = static_cast<IFMapLink *>(FindLinkNode(midnode, second,
360 20 : data->metadata));
361 20 : if (glink) LinkNodeDelete(glink, origin);
362 20 : DeleteIfEmpty(first);
363 20 : rtable->DeleteIfEmpty(second);
364 20 : mtable->DeleteIfEmpty(midnode);
365 : }
366 846 : } else {
367 : // link
368 2339 : if (request->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
369 : // Link is added if not present
370 : IFMapLink *glink =
371 4428 : static_cast<IFMapLink *>(FindLinkNode(first, second,
372 2214 : data->metadata));
373 2214 : if (glink == NULL) {
374 4278 : glink = LinkNodeAdd(first, second, data->metadata,
375 2139 : key->id_seq_num, data->origin);
376 2139 : graph()->Link(first, second, glink);
377 : } else {
378 75 : LinkNodeUpdate(glink, key->id_seq_num, data->origin);
379 : }
380 : } else {
381 : // TODO: check if the edge is present and ignore otherwise.
382 125 : IFMapLink *glink = FindLinkNode(first, second, data->metadata);
383 125 : if (glink != NULL) {
384 125 : IFMapOrigin origin(data->origin);
385 125 : LinkNodeDelete(glink, origin);
386 : // check whether any of the identifiers can be deleted.
387 125 : DeleteIfEmpty(first);
388 125 : rtable->DeleteIfEmpty(second);
389 : }
390 : }
391 : }
392 :
393 3185 : if (lchanged) {
394 206 : partition->Change(first);
395 : }
396 3185 : if (rchanged) {
397 805 : rtable->Notify(second);
398 : }
399 3185 : if (mchanged) {
400 826 : mtable->Notify(midnode);
401 : }
402 : }
403 :
404 28874 : void IFMapServerTable::Clear() {
405 : DBTablePartition *partition = static_cast<DBTablePartition *>(
406 28874 : GetTablePartition(0));
407 28874 : assert(!HasListeners());
408 28874 : for (IFMapNode *node = static_cast<IFMapNode *>(partition->GetFirst()),
409 28874 : *next = NULL;
410 32139 : node != NULL; node = next) {
411 3265 : next = static_cast<IFMapNode *>(partition->GetNext(node));
412 3265 : if (node->IsDeleted()) {
413 0 : continue;
414 : }
415 3265 : graph()->RemoveNode(node);
416 3265 : partition->Delete(node);
417 : }
418 28874 : }
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 :
|