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 1085812 : IFMapNode::IFMapNode(IFMapTable *table) 19 1085812 : : table_(table) { 20 1085812 : } 21 : 22 : struct IFMapObjectDeleter { 23 107073 : void operator()(IFMapObject *obj) const { 24 107073 : intrusive_ptr_release(obj); 25 107073 : } 26 : }; 27 : 28 2171624 : IFMapNode::~IFMapNode() { 29 1085812 : list_.clear_and_dispose(IFMapObjectDeleter()); 30 2171624 : } 31 : 32 2117797 : string IFMapNode::ToString() const { 33 2117797 : string repr(table_->Typename()); 34 2117797 : repr += ":"; 35 2117797 : repr += name_; 36 2117797 : return repr; 37 0 : } 38 : 39 139583 : IFMapObject *IFMapNode::Find(IFMapOrigin origin) { 40 279548 : for (ObjectList::iterator iter = list_.begin(); iter != list_.end(); 41 : ++iter) { 42 40018 : IFMapObject *object = iter.operator->(); 43 40018 : if (object->origin() == origin) { 44 39827 : return object; 45 : } 46 : } 47 99756 : return NULL; 48 : } 49 : 50 135029 : void IFMapNode::Insert(IFMapObject *obj) { 51 135029 : intrusive_ptr_add_ref(obj); 52 135029 : if (obj->origin().IsOriginXmpp()) { 53 80 : list_.push_back(*obj); 54 : } else { 55 134949 : list_.push_front(*obj); 56 : } 57 135029 : } 58 : 59 27956 : void IFMapNode::Remove(IFMapObject *obj) { 60 55912 : list_.erase(list_.iterator_to(*obj)); 61 27956 : intrusive_ptr_release(obj); 62 27956 : } 63 : 64 2018352 : IFMapObject *IFMapNode::GetObject() { 65 2018352 : if (list_.empty()) { 66 191901 : return NULL; 67 : } 68 1826451 : return &list_.front(); 69 : } 70 : 71 51620 : const IFMapObject *IFMapNode::GetObject() const { 72 51620 : if (list_.empty()) { 73 24 : return NULL; 74 : } 75 51596 : return &list_.front(); 76 : } 77 : 78 3592 : IFMapNode::crc32type IFMapNode::GetConfigCrc() { 79 3592 : IFMapNode::crc32type crc = 0; 80 3592 : IFMapObject *object = Find(IFMapOrigin(IFMapOrigin::CASSANDRA)); 81 3592 : if (object) { 82 3061 : crc = object->CalculateCrc(); 83 3061 : if (crc == 0) { 84 510 : crc = 0xffffffff; 85 : } 86 : } 87 : 88 3592 : 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 442 : void IFMapNode::EncodeNodeDetail(pugi::xml_node *parent) const { 101 442 : xml_node node = parent->append_child("node"); 102 442 : node.append_attribute("type") = table_->Typename(); 103 442 : node.append_child("name").text().set(name_.c_str()); 104 442 : const IFMapObject *object = GetObject(); 105 442 : if (object != NULL) { 106 418 : object->EncodeUpdate(&node); 107 : } 108 442 : } 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 1764 : DBEntryBase::KeyPtr IFMapNode::GetDBRequestKey() const { 123 1764 : IFMapTable::RequestKey *keyptr = new IFMapTable::RequestKey(); 124 1764 : keyptr->id_name = name_; 125 1764 : return KeyPtr(keyptr); 126 : } 127 : 128 1085812 : void IFMapNode::SetKey(const DBRequestKey *genkey) { 129 1085812 : const IFMapTable::RequestKey *keyptr = 130 : static_cast<const IFMapTable::RequestKey *>(genkey); 131 1085812 : name_ = keyptr->id_name; 132 1085812 : } 133 : 134 132010 : IFMapNode *IFMapNode::DescriptorLookup( 135 : DB *db, const IFMapNode::Descriptor &descriptor) { 136 132010 : if (db == NULL) { 137 0 : return NULL; 138 : } 139 132010 : IFMapTable *table = IFMapTable::FindTable(db, descriptor.first); 140 132010 : if (table == NULL) { 141 0 : return NULL; 142 : } 143 132010 : return table->FindNode(descriptor.second); 144 : }