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