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_node.h" 7 : 8 : #include <iostream> 9 : 10 : #include <pugixml/pugixml.hpp> 11 : 12 : #include "ifmap/ifmap_table.h" 13 : 14 : using namespace std; 15 : using pugi::xml_node; 16 : 17 : 18 1069984 : IFMapNode::IFMapNode(IFMapTable *table) 19 1069984 : : table_(table) { 20 1069984 : } 21 : 22 : struct IFMapObjectDeleter { 23 106103 : void operator()(IFMapObject *obj) const { 24 106103 : intrusive_ptr_release(obj); 25 106103 : } 26 : }; 27 : 28 2139968 : IFMapNode::~IFMapNode() { 29 1069984 : list_.clear_and_dispose(IFMapObjectDeleter()); 30 2139968 : } 31 : 32 2103049 : string IFMapNode::ToString() const { 33 2103049 : string repr(table_->Typename()); 34 2103049 : repr += ":"; 35 2103049 : repr += name_; 36 2103049 : return repr; 37 0 : } 38 : 39 139523 : IFMapObject *IFMapNode::Find(IFMapOrigin origin) { 40 279416 : for (ObjectList::iterator iter = list_.begin(); iter != list_.end(); 41 : ++iter) { 42 40000 : IFMapObject *object = iter.operator->(); 43 40000 : if (object->origin() == origin) { 44 39815 : return object; 45 : } 46 : } 47 99708 : return NULL; 48 : } 49 : 50 133816 : void IFMapNode::Insert(IFMapObject *obj) { 51 133816 : intrusive_ptr_add_ref(obj); 52 133816 : if (obj->origin().IsOriginXmpp()) { 53 80 : list_.push_back(*obj); 54 : } else { 55 133736 : list_.push_front(*obj); 56 : } 57 133816 : } 58 : 59 27713 : void IFMapNode::Remove(IFMapObject *obj) { 60 55426 : list_.erase(list_.iterator_to(*obj)); 61 27713 : intrusive_ptr_release(obj); 62 27713 : } 63 : 64 1991563 : IFMapObject *IFMapNode::GetObject() { 65 1991563 : if (list_.empty()) { 66 191873 : return NULL; 67 : } 68 1799690 : return &list_.front(); 69 : } 70 : 71 49425 : const IFMapObject *IFMapNode::GetObject() const { 72 49425 : if (list_.empty()) { 73 25 : return NULL; 74 : } 75 49400 : return &list_.front(); 76 : } 77 : 78 3543 : IFMapNode::crc32type IFMapNode::GetConfigCrc() { 79 3543 : IFMapNode::crc32type crc = 0; 80 3543 : IFMapObject *object = Find(IFMapOrigin(IFMapOrigin::CASSANDRA)); 81 3543 : if (object) { 82 3058 : crc = object->CalculateCrc(); 83 3058 : if (crc == 0) { 84 510 : crc = 0xffffffff; 85 : } 86 : } 87 : 88 3543 : return crc; 89 : } 90 : 91 0 : void IFMapNode::PrintAllObjects() { 92 0 : cout << name_ << ": " << list_.size() << " objects" << endl; 93 0 : for (ObjectList::iterator iter = list_.begin(); iter != list_.end(); 94 : ++iter) { 95 0 : IFMapObject *object = iter.operator->(); 96 0 : cout << "\t" << object->origin().ToString() << endl; 97 : } 98 0 : } 99 : 100 443 : void IFMapNode::EncodeNodeDetail(pugi::xml_node *parent) const { 101 443 : xml_node node = parent->append_child("node"); 102 443 : node.append_attribute("type") = table_->Typename(); 103 443 : node.append_child("name").text().set(name_.c_str()); 104 443 : const IFMapObject *object = GetObject(); 105 443 : if (object != NULL) { 106 418 : object->EncodeUpdate(&node); 107 : } 108 443 : } 109 : 110 0 : void IFMapNode::EncodeNode(xml_node *parent) const { 111 0 : xml_node node = parent->append_child("node"); 112 0 : node.append_attribute("type") = table_->Typename(); 113 0 : node.append_child("name").text().set(name_.c_str()); 114 0 : } 115 : 116 916 : void IFMapNode::EncodeNode(const Descriptor &descriptor, xml_node *parent) { 117 916 : xml_node node = parent->append_child("node"); 118 916 : node.append_attribute("type") = descriptor.first.c_str(); 119 916 : node.append_child("name").text().set(descriptor.second.c_str()); 120 916 : } 121 : 122 1766 : DBEntryBase::KeyPtr IFMapNode::GetDBRequestKey() const { 123 1766 : IFMapTable::RequestKey *keyptr = new IFMapTable::RequestKey(); 124 1766 : keyptr->id_name = name_; 125 1766 : return KeyPtr(keyptr); 126 : } 127 : 128 1069984 : void IFMapNode::SetKey(const DBRequestKey *genkey) { 129 1069984 : const IFMapTable::RequestKey *keyptr = 130 : static_cast<const IFMapTable::RequestKey *>(genkey); 131 1069984 : name_ = keyptr->id_name; 132 1069984 : } 133 : 134 129474 : IFMapNode *IFMapNode::DescriptorLookup( 135 : DB *db, const IFMapNode::Descriptor &descriptor) { 136 129474 : if (db == NULL) { 137 0 : return NULL; 138 : } 139 129474 : IFMapTable *table = IFMapTable::FindTable(db, descriptor.first); 140 129474 : if (table == NULL) { 141 0 : return NULL; 142 : } 143 129474 : return table->FindNode(descriptor.second); 144 : }