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