Line data Source code
1 : /* 2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef config_client_manager_h 7 : #define config_client_manager_h 8 : 9 : #include <atomic> 10 : #include <set> 11 : #include <map> 12 : #include <mutex> 13 : #include <condition_variable> 14 : 15 : #include <boost/function.hpp> 16 : #include <boost/scoped_ptr.hpp> 17 : #include <boost/shared_ptr.hpp> 18 : 19 : #include "config_client_options.h" 20 : #include "config_factory.h" 21 : #include <rapidjson/document.h> 22 : 23 : struct AutogenProperty; 24 : class ConfigAmqpClient; 25 : class ConfigDbClient; 26 : struct DBRequest; 27 : class EventManager; 28 : class TaskTrigger; 29 : class ConfigJsonParserBase; 30 : class ConfigCass2JsonAdapter; 31 : struct ConfigClientManagerInfo; 32 : 33 : /* 34 : * This class is the manager that over-sees the retrieval of user configuration. 35 : * It interacts with the rabbit-mq client, the database-client and the parser 36 : * that parses the configuration received from the database-client. Its the 37 : * coordinator between these 3 pieces. 38 : */ 39 : class ConfigClientManager { 40 : public: 41 : static const int kNumConfigReaderTasks = 8; 42 : static const std::set<std::string> skip_properties; 43 : 44 : typedef std::set<std::string> ObjectTypeList; 45 : 46 : ConfigClientManager(EventManager *evm, 47 : ConfigJsonParserBase *cfg_json_base, 48 : std::string hostname, 49 : std::string module_name, 50 : const ConfigClientOptions& config_options); 51 : 52 : ~ConfigClientManager(); 53 : 54 : void Initialize(); 55 : ConfigAmqpClient *config_amqp_client() const; 56 : ConfigDbClient *config_db_client() const; 57 : void EnqueueUUIDRequest(std::string oper, std::string obj_type, 58 : std::string uuid_str); 59 : 60 43926 : ConfigAmqpClient *config_amqp_client() { return config_amqp_client_.get(); } 61 61503 : ConfigJsonParserBase *config_json_parser() { return config_json_parser_.get(); } 62 5620 : ConfigDbClient *config_db_client() { return config_db_client_.get(); } 63 : 64 : static int GetNumConfigReader(); 65 : 66 : static int GetNumWorkers() { 67 : // AMQP reader and ConfigDB readers 68 : return GetNumConfigReader() + 1; 69 : } 70 : 71 : void EndOfConfig(); 72 : void WaitForEndOfConfig(); 73 : void GetClientManagerInfo(ConfigClientManagerInfo &info) const; 74 : 75 : // Generation number identifies the current version of the config 76 : // available with ifmap server. While enqueuing the DB request to create 77 : // ifmap entries(node/link), config client manager uses the current 78 : // generation number to tag the config version in each ifmap config db entru 79 7738 : uint64_t GetGenerationNumber() const { 80 7738 : return generation_number_; 81 : } 82 : 83 : // Increment the generation number on reinit trigger 84 : // IFMap server identifies stale config db entries based on this generation 85 : // number. Any entry(node/link) which has generation number less than the 86 : // current generation number will be cleaned with "stale entry cleanup" 87 : // background task 88 0 : uint64_t IncrementGenerationNumber() { 89 0 : return generation_number_++; 90 : } 91 : 92 : // Reinit trigger with update of config parameter 93 : void ReinitConfigClient(const ConfigClientOptions &config); 94 : 95 : // Reinit trigger without update of config parameter. 96 : // Either from introspect or to force re-reading of cassandra 97 : void ReinitConfigClient(); 98 : 99 : // This task trigger handles with init and reinit of config client manager 100 : bool InitConfigClient(); 101 : 102 258558 : bool is_reinit_triggered() { 103 258558 : return reinit_triggered_; 104 : } 105 : static bool end_of_rib_computed() { return end_of_rib_computed_; } 106 6332 : static void set_end_of_rib_computed(bool end_of_rib_computed) { 107 6332 : end_of_rib_computed_ = end_of_rib_computed; 108 6332 : } 109 : 110 : bool GetEndOfRibComputed() const; 111 : uint64_t GetEndOfRibComputedAt() const; 112 : void PostShutdown(); 113 : 114 : private: 115 : typedef std::pair<std::string, std::string> LinkMemberPair; 116 : typedef std::pair<std::string, bool> LinkDataPair; 117 : typedef std::map<LinkMemberPair, std::string> ParentNameMap; 118 : typedef std::map<LinkMemberPair, LinkDataPair> LinkNameMap; 119 : typedef std::map<std::string, std::string> WrapperFieldMap; 120 : 121 : void SetUp(ConfigJsonParserBase*); 122 : void SetDefaultSchedulingPolicy(); 123 : 124 : LinkNameMap link_name_map_; 125 : ParentNameMap parent_name_map_; 126 : EventManager *evm_; 127 : boost::scoped_ptr<ConfigJsonParserBase> config_json_parser_; 128 : boost::scoped_ptr<ConfigDbClient> config_db_client_; 129 : boost::scoped_ptr<ConfigAmqpClient> config_amqp_client_; 130 : int thread_count_; 131 : uint64_t generation_number_; 132 : 133 : mutable std::mutex end_of_rib_sync_mutex_; 134 : std::condition_variable cond_var_; 135 : uint64_t end_of_rib_computed_at_; 136 : std::string hostname_; 137 : std::string module_name_; 138 : ConfigClientOptions config_options_; 139 : std::atomic<bool> reinit_triggered_; 140 : boost::scoped_ptr<TaskTrigger> init_trigger_; 141 : static bool end_of_rib_computed_; 142 : }; 143 : 144 : #endif // config_client_manager_h