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_uuid_mapper.h" 7 : 8 : #include "db/db.h" 9 : #include "db/db_table_partition.h" 10 : #include "ifmap/ifmap_node.h" 11 : #include "ifmap/ifmap_server.h" 12 : #include "ifmap/ifmap_server_table.h" 13 : #include "ifmap/ifmap_table.h" 14 : #include "schema/vnc_cfg_types.h" 15 : 16 98 : void IFMapUuidMapper::SetUuid(uint64_t ms_long, uint64_t ls_long, 17 : boost::uuids::uuid &uu_id) { 18 882 : for (int i = 0; i < 8; i++) { 19 784 : uu_id.data[7 - i] = ms_long & 0xFF; 20 784 : ms_long = ms_long >> 8; 21 : } 22 : 23 882 : for (int i = 0; i < 8; i++) { 24 784 : uu_id.data[15 - i] = ls_long & 0xFF; 25 784 : ls_long = ls_long >> 8; 26 : } 27 98 : } 28 : 29 98 : std::string IFMapUuidMapper::UuidToString(const boost::uuids::uuid &id) { 30 98 : std::stringstream uuid_str; 31 98 : uuid_str << id; 32 196 : return uuid_str.str(); 33 98 : } 34 : 35 98 : std::string IFMapUuidMapper::Add(uint64_t ms_long, uint64_t ls_long, 36 : IFMapNode *node) { 37 : boost::uuids::uuid uu_id; 38 98 : SetUuid(ms_long, ls_long, uu_id); 39 98 : std::string uuid_str = UuidToString(uu_id); 40 98 : uuid_node_map_.insert(std::make_pair(uuid_str, node)); 41 196 : return uuid_str; 42 0 : } 43 : 44 2 : void IFMapUuidMapper::Delete(const std::string &uuid_str) { 45 2 : uuid_node_map_.erase(uuid_str); 46 2 : } 47 : 48 98 : IFMapNode *IFMapUuidMapper::Find(const std::string &uuid_str) { 49 98 : UuidNodeMap::iterator loc = uuid_node_map_.find(uuid_str); 50 98 : if (loc != uuid_node_map_.end()) { 51 82 : return loc->second;; 52 : } 53 16 : return NULL; 54 : } 55 : 56 54 : bool IFMapUuidMapper::Exists(const std::string &uuid_str) { 57 54 : UuidNodeMap::iterator loc = uuid_node_map_.find(uuid_str); 58 54 : if (loc != uuid_node_map_.end()) { 59 40 : return true; 60 : } 61 14 : return false; 62 : } 63 : 64 0 : void IFMapUuidMapper::PrintAllMappedEntries() { 65 0 : std::cout << "Printing all UUID mapper entries - UUID : type:NODE-FQN\n"; 66 0 : for (UuidNodeMap::iterator iter = uuid_node_map_.begin(); 67 0 : iter != uuid_node_map_.end(); ++iter) { 68 0 : IFMapNode *node = iter->second; 69 0 : std::cout << iter->first << " : " << node->ToString() << std::endl; 70 : } 71 0 : } 72 : 73 : // Routines for class IFMapVmUuidMapper 74 : 75 183 : IFMapVmUuidMapper::IFMapVmUuidMapper(DB *db, IFMapServer *server) 76 183 : : db_(db), ifmap_server_(server), vm_table_(NULL), registered(false) { 77 183 : } 78 : 79 183 : IFMapVmUuidMapper::~IFMapVmUuidMapper() { 80 183 : Shutdown(); 81 183 : } 82 : 83 57 : void IFMapVmUuidMapper::Initialize() { 84 57 : vm_table_ = static_cast<IFMapServerTable *>( 85 57 : db_->FindTable("__ifmap__.virtual_machine.0")); 86 57 : assert(vm_table_ != NULL); 87 57 : vm_lid_ = vm_table_->Register(boost::bind(&IFMapVmUuidMapper::VmNodeProcess, 88 : this, _1, _2)); 89 57 : registered = true; 90 57 : } 91 : 92 346 : void IFMapVmUuidMapper::Shutdown() { 93 346 : if (registered) { 94 57 : vm_table_->Unregister(vm_lid_); 95 57 : registered = false; 96 : } 97 346 : } 98 : 99 230 : void IFMapVmUuidMapper::VmNodeProcess(DBTablePartBase *partition, 100 : DBEntryBase *entry) { 101 230 : IFMapNode *vm_node = static_cast<IFMapNode *>(entry); 102 230 : std::string tname = vm_node->table()->Typename(); 103 230 : assert(tname.compare("virtual-machine") == 0); 104 : 105 230 : if (!IsFeasible(vm_node)) { 106 2 : std::string vm_uuid; 107 2 : bool val = NodeToUuid(vm_node, &vm_uuid); 108 : 109 : // Its possible that the add came without any properties i.e no object 110 : // and hence no entry in node_uuid_map_ 111 2 : if (val) { 112 2 : node_uuid_map_.erase(vm_node); 113 2 : uuid_mapper_.Delete(vm_uuid); 114 : } 115 2 : return; 116 2 : } 117 : 118 : // Ignore 'change' if the 'add' has already been processed. 119 228 : if (NodeProcessed(vm_node)) { 120 56 : return; 121 : } 122 : 123 172 : IFMapObject *object = vm_node->Find(IFMapOrigin(IFMapOrigin::CASSANDRA)); 124 172 : if (object) { 125 : // Insert into the uuid-node-mapper 126 98 : autogen::VirtualMachine *vm = static_cast<autogen::VirtualMachine *> 127 : (object); 128 98 : if (vm->IsPropertySet(autogen::VirtualMachine::ID_PERMS)) { 129 98 : autogen::UuidType uuid = vm->id_perms().uuid; 130 : std::string vm_uuid = 131 98 : uuid_mapper_.Add(uuid.uuid_mslong, uuid.uuid_lslong, vm_node); 132 : 133 : // Insert into the node-uuid-map 134 98 : node_uuid_map_.insert(make_pair(vm_node, vm_uuid)); 135 : 136 : // Check if there were any vm-reg's for this VM whose processing we 137 : // had deferred since the vm-node did not exist then. 138 98 : std::string vr_name; 139 98 : bool exists = PendingVmRegExists(vm_uuid, &vr_name); 140 98 : if (exists) { 141 14 : bool subscribe = true; 142 14 : ifmap_server_->ProcessVmSubscribe(vr_name, vm_uuid, subscribe); 143 14 : pending_vmreg_map_.erase(vm_uuid); 144 : } 145 98 : } 146 : } 147 230 : } 148 : 149 98 : IFMapNode *IFMapVmUuidMapper::GetVmNodeByUuid(const std::string &vm_uuid) { 150 98 : return uuid_mapper_.Find(vm_uuid); 151 : } 152 : 153 54 : bool IFMapVmUuidMapper::VmNodeExists(const std::string &vm_uuid) { 154 54 : return uuid_mapper_.Exists(vm_uuid); 155 : } 156 : 157 0 : void IFMapVmUuidMapper::PrintAllUuidMapperEntries() { 158 0 : uuid_mapper_.PrintAllMappedEntries(); 159 0 : } 160 : 161 18 : void IFMapVmUuidMapper::ProcessVmRegAsPending(std::string vm_uuid, 162 : std::string vr_name, bool subscribe) { 163 18 : if (subscribe) { 164 18 : pending_vmreg_map_.insert(make_pair(vm_uuid, vr_name)); 165 : } else { 166 0 : pending_vmreg_map_.erase(vm_uuid); 167 : } 168 18 : } 169 : 170 110 : bool IFMapVmUuidMapper::PendingVmRegExists(const std::string &vm_uuid, 171 : std::string *vr_name) { 172 110 : PendingVmRegMap::iterator loc = pending_vmreg_map_.find(vm_uuid); 173 110 : if (loc != pending_vmreg_map_.end()) { 174 26 : *vr_name = loc->second; 175 26 : return true; 176 : } 177 84 : return false; 178 : } 179 : 180 0 : void IFMapVmUuidMapper::PrintAllPendingVmRegEntries() { 181 0 : std::cout << "Printing all pending vm-reg entries - VM-UUID : VR-FQN\n"; 182 0 : for (PendingVmRegMap::iterator iter = pending_vmreg_map_.begin(); 183 0 : iter != pending_vmreg_map_.end(); ++iter) { 184 0 : std::cout << iter->first << " : " << iter->second << std::endl; 185 : } 186 0 : } 187 : 188 230 : bool IFMapVmUuidMapper::IsFeasible(IFMapNode *node) { 189 230 : if (node->IsDeleted()) { 190 2 : return false; 191 : } 192 228 : return true; 193 : } 194 : 195 2 : bool IFMapVmUuidMapper::NodeToUuid(IFMapNode *vm_node, std::string *vm_uuid) { 196 2 : NodeUuidMap::iterator loc = node_uuid_map_.find(vm_node); 197 2 : if (loc != node_uuid_map_.end()) { 198 2 : *vm_uuid = loc->second; 199 2 : return true; 200 : } 201 0 : return false; 202 : } 203 : 204 228 : bool IFMapVmUuidMapper::NodeProcessed(IFMapNode *vm_node) { 205 228 : NodeUuidMap::iterator loc = node_uuid_map_.find(vm_node); 206 228 : if (loc != node_uuid_map_.end()) { 207 56 : return true; 208 : } 209 172 : return false; 210 : } 211 : 212 0 : void IFMapVmUuidMapper::PrintAllNodeUuidMappedEntries() { 213 0 : std::cout << "Printing all node-UUID entries - type:NODE-FQN : uuid\n"; 214 0 : for (NodeUuidMap::iterator iter = node_uuid_map_.begin(); 215 0 : iter != node_uuid_map_.end(); ++iter) { 216 0 : IFMapNode *node = iter->first; 217 0 : std::cout << node->ToString() << " : " << iter->second << std::endl; 218 : } 219 0 : } 220 :