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 __ctrlplane__ifmap_server__ 7 : #define __ctrlplane__ifmap_server__ 8 : 9 : #include <map> 10 : #include <deque> 11 : #include <vector> 12 : 13 : #include <boost/asio/io_service.hpp> 14 : #include <boost/scoped_ptr.hpp> 15 : 16 : #include "base/bitset.h" 17 : #include "base/timer.h" 18 : #include "base/address.h" 19 : #include "base/queue_task.h" 20 : #include "config-client-mgr/config_client_manager.h" 21 : 22 : class BgpRouterState; 23 : class DB; 24 : class DBGraph; 25 : class DBGraphEdge; 26 : class DBGraphVertex; 27 : class IFMapChannelManager; 28 : class IFMapClient; 29 : class IFMapExporter; 30 : class IFMapNode; 31 : class IFMapUpdateQueue; 32 : class IFMapUpdateSender; 33 : class IFMapVmUuidMapper; 34 : class IFMapServerShowClientMap; 35 : class IFMapServerShowIndexMap; 36 : class IFMapServerClientHistoryList; 37 : class IFMapTableListEntry; 38 : class IFMapNodeTableListShowEntry; 39 : class IFMapServerInfoUI; 40 : 41 : class IFMapServer { 42 : public: 43 : struct ClientHistoryInfo { 44 6 : ClientHistoryInfo(const std::string &name, int id, uint64_t ctime, 45 : uint64_t htime) 46 6 : : client_name(name), client_index(id), client_created_at(ctime), 47 6 : history_created_at(htime) { 48 6 : } 49 : const std::string client_created_at_str() const; 50 : const std::string history_created_at_str() const; 51 : 52 : std::string client_name; 53 : int client_index; 54 : uint64_t client_created_at; 55 : uint64_t history_created_at; 56 : }; 57 : 58 : static const int kClientHistorySize = 5000; 59 : typedef std::map<std::string, IFMapClient *> ClientMap; 60 : typedef std::map<int, IFMapClient *> IndexMap; 61 : typedef std::deque<ClientHistoryInfo> ClientHistory; 62 : typedef ClientMap::size_type CmSz_t; 63 : typedef IndexMap::size_type ImSz_t; 64 : IFMapServer(DB *db, DBGraph *graph, boost::asio::io_context *io_service); 65 : virtual ~IFMapServer(); 66 : 67 : // Must be called after the __ifmap__ tables are registered with the 68 : // database. 69 : void Initialize(); 70 : void Shutdown(); 71 : 72 : void ClientRegister(IFMapClient *client); 73 : void ClientUnregister(IFMapClient *client); 74 : bool ProcessClientWork(bool add, IFMapClient *client); 75 : 76 : IFMapClient *FindClient(const std::string &id); 77 : IFMapClient *GetClient(int index); 78 : 79 : void AddClient(IFMapClient *client); 80 : void DeleteClient(IFMapClient *client); 81 : void ClientExporterSetup(IFMapClient *client); 82 : 83 8382 : DB *database() { return db_; } 84 216 : DBGraph *graph() { return graph_; } 85 10227 : IFMapUpdateQueue *queue() { return queue_.get(); } 86 1312 : IFMapUpdateSender *sender() { return sender_.get(); } 87 904 : IFMapExporter *exporter() { return exporter_.get(); } 88 26 : IFMapVmUuidMapper *vm_uuid_mapper() { return vm_uuid_mapper_.get(); } 89 : boost::asio::io_context *io_service() { return io_service_; } 90 161 : void set_config_manager(ConfigClientManager *manager) { 91 161 : config_manager_ = manager; 92 161 : } 93 233 : ConfigClientManager *get_config_manager() { return config_manager_; } 94 177 : ConfigClientManager *get_config_manager() const { return config_manager_; } 95 5 : void set_ifmap_channel_manager(IFMapChannelManager *manager) { 96 5 : ifmap_channel_manager_ = manager; 97 5 : } 98 0 : IFMapChannelManager *get_ifmap_channel_manager() { 99 0 : return ifmap_channel_manager_; 100 : } 101 : 102 0 : uint64_t get_config_generation_number() { 103 0 : return config_manager_->GetGenerationNumber(); 104 : } 105 : void ProcessVmSubscribe(std::string vr_name, std::string vm_uuid, 106 : bool subscribe, bool has_vms); 107 : void ProcessVmSubscribe(std::string vr_name, std::string vm_uuid, 108 : bool subscribe); 109 : 110 : class IFMapStaleEntriesCleaner; 111 : class IFMapVmSubscribe; 112 : 113 : void ProcessVmRegAsPending(std::string vm_uuid, std::string vr_name, 114 : bool subscribe); 115 : IFMapNode *GetVmNodeByUuid(const std::string &vm_uuid); 116 : 117 : void FillClientMap(IFMapServerShowClientMap *out_map, 118 : const std::string &search_string); 119 : void FillIndexMap(IFMapServerShowIndexMap *out_map, 120 : const std::string &search_string); 121 : void FillClientHistory(IFMapServerClientHistoryList *out_list, 122 : const std::string &search_string); 123 177 : const CmSz_t GetClientMapSize() const { return client_map_.size(); } 124 : const CmSz_t GetIndexMapSize() const { return index_map_.size(); } 125 : void GetUIInfo(IFMapServerInfoUI *server_info) const; 126 : bool ClientNameToIndex(const std::string &id, int *index); 127 : void CleanupStaleEntries(); 128 : bool CollectStats(BgpRouterState *state, bool first) const; 129 : 130 : private: 131 : friend class IFMapServerTest; 132 : friend class IFMapRestartTest; 133 : friend class ShowIFMapXmppClientInfo; 134 : friend class XmppIfmapTest; 135 : friend class IFMapExporterTest; 136 : friend class IFMapVmUuidMapperTest; 137 : 138 : enum QueueOp { 139 : ADD = 1, 140 : DEL = 2 141 : }; 142 : struct QueueEntry { 143 : QueueOp op; 144 : IFMapClient *client; 145 : }; 146 : bool ClientWorker(QueueEntry work_entry); 147 : void ClientGraphDownload(IFMapClient *client); 148 : void RemoveSelfAddedLinksAndObjects(IFMapClient *client); 149 : void CleanupUuidMapper(IFMapClient *client); 150 : void ClientExporterCleanup(int index); 151 0 : const ClientMap &GetClientMap() const { return client_map_; } 152 : void SimulateDeleteClient(IFMapClient *client); 153 : void SaveClientHistory(IFMapClient *client); 154 : 155 : DB *db_; 156 : DBGraph *graph_; 157 : boost::scoped_ptr<IFMapUpdateQueue> queue_; 158 : boost::scoped_ptr<IFMapExporter> exporter_; 159 : boost::scoped_ptr<IFMapUpdateSender> sender_; 160 : boost::scoped_ptr<IFMapVmUuidMapper> vm_uuid_mapper_; 161 : BitSet client_indexes_; 162 : ClientMap client_map_; 163 : IndexMap index_map_; 164 : WorkQueue<QueueEntry> work_queue_; 165 : boost::asio::io_context *io_service_; 166 : ConfigClientManager *config_manager_; 167 : IFMapChannelManager *ifmap_channel_manager_; 168 : ClientHistory client_history_; 169 : }; 170 : 171 : #endif /* defined(__ctrlplane__ifmap_server__) */