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