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