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 : #ifndef __IFMAP_UUID_MAPPER_H__ 7 : #define __IFMAP_UUID_MAPPER_H__ 8 : 9 : #include <boost/bind/bind.hpp> 10 : #include <boost/uuid/uuid.hpp> 11 : #include <boost/uuid/uuid_io.hpp> 12 : 13 : #include "db/db_table.h" 14 : 15 : #include <map> 16 : #include <set> 17 : #include <string> 18 : 19 : using namespace boost::placeholders; 20 : 21 : class DB; 22 : class IFMapNode; 23 : class IFMapServer; 24 : class IFMapServerTable; 25 : 26 : // Maintains a mapping of [uuid, node] 27 : class IFMapUuidMapper { 28 : public: 29 : typedef std::map<std::string, IFMapNode *> UuidNodeMap; 30 : typedef UuidNodeMap::size_type Sz_t; 31 : 32 : std::string Add(uint64_t ms_long, uint64_t ls_long, IFMapNode *node); 33 : void Delete(const std::string &uuid_str); 34 : IFMapNode *Find(const std::string &uuid_str); 35 : bool Exists(const std::string &uuid_str); 36 : void PrintAllMappedEntries(); 37 22 : Sz_t Size() { return uuid_node_map_.size(); } 38 : 39 : private: 40 : friend class ShowIFMapUuidToNodeMapping; 41 : 42 : void SetUuid(uint64_t ms_long, uint64_t ls_long, boost::uuids::uuid &uu_id); 43 : std::string UuidToString(const boost::uuids::uuid &id); 44 : 45 : UuidNodeMap uuid_node_map_; 46 : }; 47 : 48 : class IFMapVmUuidMapper { 49 : public: 50 : // Store [vm-uuid, vr-name] from the vm-reg request 51 : // ADD: vm-reg-request, DELETE: vm-node add/xmpp-not-ready 52 : typedef std::map<std::string, std::string> PendingVmRegMap; 53 : // Store [vm-node, vm-uuid]. Used to clean-up uuid_mapper_'s 'vm-uuid' 54 : // entry when the vm-node becomes InFeasible. The objects would be gone by 55 : // then and the uuid would not be available from the node. 56 : // ADD: config vm-node add, DELETE: config vm-node delete 57 : typedef std::map<IFMapNode *, std::string> NodeUuidMap; 58 : 59 : explicit IFMapVmUuidMapper(DB *db, IFMapServer *server); 60 : ~IFMapVmUuidMapper(); 61 : 62 : void Initialize(); 63 : void Shutdown(); 64 22 : IFMapUuidMapper::Sz_t UuidMapperCount() { 65 22 : return uuid_mapper_.Size(); 66 : } 67 : 68 : DBTable::ListenerId vm_listener_id() { return vm_lid_; } 69 : DB *db() { return db_; } 70 : IFMapServer *server() { return ifmap_server_; } 71 : bool is_registered() { return registered; } 72 : 73 : void VmNodeProcess(DBTablePartBase *partition, DBEntryBase *entry); 74 : IFMapNode *GetVmNodeByUuid(const std::string &vm_uuid); 75 : bool VmNodeExists(const std::string &vm_uuid); 76 : void PrintAllUuidMapperEntries(); 77 : 78 : void ProcessVmRegAsPending(std::string vm_uuid, std::string vr_name, 79 : bool subscribe); 80 : bool PendingVmRegExists(const std::string &vm_uuid, std::string *vr_name); 81 26 : PendingVmRegMap::size_type PendingVmRegCount() { 82 26 : return pending_vmreg_map_.size(); 83 : } 84 0 : void CleanupPendingVmRegEntry(const std::string &vm_uuid) { 85 0 : pending_vmreg_map_.erase(vm_uuid); 86 0 : } 87 : void PrintAllPendingVmRegEntries(); 88 : 89 : bool NodeToUuid(IFMapNode *vm_node, std::string *vm_uuid); 90 : bool NodeProcessed(IFMapNode *node); 91 22 : NodeUuidMap::size_type NodeUuidMapCount() { 92 22 : return node_uuid_map_.size(); 93 : } 94 : void PrintAllNodeUuidMappedEntries(); 95 : 96 : private: 97 : friend class IFMapVmUuidMapperTest; 98 : friend class ShowIFMapPendingVmReg; 99 : friend class ShowIFMapNodeToUuidMapping; 100 : friend class ShowIFMapUuidToNodeMapping; 101 : 102 : DB *db_; 103 : IFMapServer *ifmap_server_; 104 : IFMapServerTable *vm_table_; 105 : DBTable::ListenerId vm_lid_; 106 : bool registered; 107 : // ADD: config vm-node add, DELETE: config vm-node delete 108 : IFMapUuidMapper uuid_mapper_; 109 : NodeUuidMap node_uuid_map_; 110 : PendingVmRegMap pending_vmreg_map_; 111 : 112 : // TODO: consider moving the common parts inside IFMapNode 113 : bool IsFeasible(IFMapNode *node); 114 : }; 115 : 116 : #endif /* __IFMAP_UUID_MAPPER_H__ */