Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "ifmap_node.h" 6 : 7 : #include <iostream> 8 : 9 : #include <pugixml/pugixml.hpp> 10 : 11 : #include "ifmap/ifmap_table.h" 12 : 13 : using namespace std; 14 : using pugi::xml_node; 15 : 16 : 17 1063548 : IFMapNode::IFMapNode(IFMapTable *table) 18 1063548 : : table_(table) { 19 1063548 : } 20 : 21 : struct IFMapObjectDeleter { 22 105645 : void operator()(IFMapObject *obj) const { 23 105645 : intrusive_ptr_release(obj); 24 105645 : } 25 : }; 26 : 27 2127096 : IFMapNode::~IFMapNode() { 28 1063548 : list_.clear_and_dispose(IFMapObjectDeleter()); 29 2127096 : } 30 : 31 2096191 : string IFMapNode::ToString() const { 32 2096191 : string repr(table_->Typename()); 33 2096191 : repr += ":"; 34 2096191 : repr += name_; 35 2096191 : return repr; 36 0 : } 37 : 38 139564 : IFMapObject *IFMapNode::Find(IFMapOrigin origin) { 39 279510 : for (ObjectList::iterator iter = list_.begin(); iter != list_.end(); 40 : ++iter) { 41 39978 : IFMapObject *object = iter.operator->(); 42 39978 : if (object->origin() == origin) { 43 39787 : return object; 44 : } 45 : } 46 99777 : return NULL; 47 : } 48 : 49 133281 : void IFMapNode::Insert(IFMapObject *obj) { 50 133281 : intrusive_ptr_add_ref(obj); 51 133281 : if (obj->origin().IsOriginXmpp()) { 52 80 : list_.push_back(*obj); 53 : } else { 54 133201 : list_.push_front(*obj); 55 : } 56 133281 : } 57 : 58 27636 : void IFMapNode::Remove(IFMapObject *obj) { 59 55272 : list_.erase(list_.iterator_to(*obj)); 60 27636 : intrusive_ptr_release(obj); 61 27636 : } 62 : 63 1976135 : IFMapObject *IFMapNode::GetObject() { 64 1976135 : if (list_.empty()) { 65 192827 : return NULL; 66 : } 67 1783308 : return &list_.front(); 68 : } 69 : 70 48461 : const IFMapObject *IFMapNode::GetObject() const { 71 48461 : if (list_.empty()) { 72 24 : return NULL; 73 : } 74 48437 : return &list_.front(); 75 : } 76 : 77 3622 : IFMapNode::crc32type IFMapNode::GetConfigCrc() { 78 3622 : IFMapNode::crc32type crc = 0; 79 3622 : IFMapObject *object = Find(IFMapOrigin(IFMapOrigin::CASSANDRA)); 80 3622 : if (object) { 81 3026 : crc = object->CalculateCrc(); 82 3026 : if (crc == 0) { 83 510 : crc = 0xffffffff; 84 : } 85 : } 86 : 87 3622 : return crc; 88 : } 89 : 90 0 : void IFMapNode::PrintAllObjects() { 91 0 : cout << name_ << ": " << list_.size() << " objects" << endl; 92 0 : for (ObjectList::iterator iter = list_.begin(); iter != list_.end(); 93 : ++iter) { 94 0 : IFMapObject *object = iter.operator->(); 95 0 : cout << "\t" << object->origin().ToString() << endl; 96 : } 97 0 : } 98 : 99 443 : void IFMapNode::EncodeNodeDetail(pugi::xml_node *parent) const { 100 443 : xml_node node = parent->append_child("node"); 101 443 : node.append_attribute("type") = table_->Typename(); 102 443 : node.append_child("name").text().set(name_.c_str()); 103 443 : const IFMapObject *object = GetObject(); 104 443 : if (object != NULL) { 105 419 : object->EncodeUpdate(&node); 106 : } 107 443 : } 108 : 109 0 : void IFMapNode::EncodeNode(xml_node *parent) const { 110 0 : xml_node node = parent->append_child("node"); 111 0 : node.append_attribute("type") = table_->Typename(); 112 0 : node.append_child("name").text().set(name_.c_str()); 113 0 : } 114 : 115 936 : void IFMapNode::EncodeNode(const Descriptor &descriptor, xml_node *parent) { 116 936 : xml_node node = parent->append_child("node"); 117 936 : node.append_attribute("type") = descriptor.first.c_str(); 118 936 : node.append_child("name").text().set(descriptor.second.c_str()); 119 936 : } 120 : 121 1736 : DBEntryBase::KeyPtr IFMapNode::GetDBRequestKey() const { 122 1736 : IFMapTable::RequestKey *keyptr = new IFMapTable::RequestKey(); 123 1736 : keyptr->id_name = name_; 124 1736 : return KeyPtr(keyptr); 125 : } 126 : 127 1063548 : void IFMapNode::SetKey(const DBRequestKey *genkey) { 128 1063548 : const IFMapTable::RequestKey *keyptr = 129 : static_cast<const IFMapTable::RequestKey *>(genkey); 130 1063548 : name_ = keyptr->id_name; 131 1063548 : } 132 : 133 128320 : IFMapNode *IFMapNode::DescriptorLookup( 134 : DB *db, const IFMapNode::Descriptor &descriptor) { 135 128320 : if (db == NULL) { 136 0 : return NULL; 137 : } 138 128320 : IFMapTable *table = IFMapTable::FindTable(db, descriptor.first); 139 128320 : if (table == NULL) { 140 0 : return NULL; 141 : } 142 128320 : return table->FindNode(descriptor.second); 143 : }