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