13 using contrail_rapidjson::Document;
14 using contrail_rapidjson::Value;
20 #define FLAG_DEBUG(args...) if (process::debug_) LOG(DEBUG, ##args)
21 #define FLAG_WARN(args...) if (process::debug_) LOG(WARN, ##args)
22 #define FLAG_ERROR(args...) if (process::debug_) LOG(ERROR, ##args)
33 Flag::Flag(FlagManager *manager,
const string &name,
34 const string &description,
bool enabled,
37 description_(description),
39 context_infos_(context_infos),
46 manager_->Register(
this);
51 description_(flag.description_),
52 enabled_(flag.enabled_),
53 context_infos_(flag.context_infos_),
54 flag_state_cb_(callback),
55 manager_(flag.manager_)
92 return !(*
this == rhs);
105 : flag_manager_(manager),
106 flag_uve_cb_(flag_uve_cb) {
158 : flag_manager_(manager) {
170 if ( d.Parse<0>(build_info.c_str()).HasParseError() ) {
173 const Value &v = d[
"build-info"];
174 version_ = v[0][
"build-version"].GetString();
186 const string &
version,
bool enabled,
195 FLAG_DEBUG(
"Flag " << name <<
"'s version " << version <<
196 " does not match with module version " <<
version_ <<
247 bool changed =
false;
249 FlagConfig flag_cfg(name, version, enabled, state, context_infos);
251 tbb::mutex::scoped_lock lock(
mutex_);
260 FLAG_DEBUG(
"Flag " << name <<
"Already Present\n");
262 <<
" Version: " << old_cfg.
version()
263 <<
" Enabled: " << string(old_cfg.
enabled() ?
"True " :
"False ")
267 for (c_itr = c_vec.begin(); c_itr != c_vec.end(); ++c_itr) {
269 <<
" Value: " << c_itr->value);
272 if (old_cfg != flag_cfg) {
275 fmap_itr->second = flag_cfg;
277 FLAG_DEBUG(
"No change in flag " << name <<
" configuration\n");
284 FLAG_DEBUG(
"New flag configured. Name: " << name);
285 flag_map_.insert(make_pair(name, flag_cfg));
289 <<
" Version: " << flag_cfg.
version()
290 <<
" Enabled: " << string(flag_cfg.
enabled() ?
"True " :
"False ")
294 for (c_itr = c_vec.begin(); c_itr != c_vec.end(); ++c_itr) {
296 <<
" Value: " << c_itr->value);
309 pair <int_map_itr, int_map_itr> ret;
311 for (
int_map_itr it = ret.first; it != ret.second; ++it) {
313 Flag *f = it->second;
330 pair <int_map_itr, int_map_itr> ret;
332 for (
int_map_itr it = ret.first; it != ret.second; ++it) {
333 Flag *f = it->second;
359 FLAG_DEBUG(
"Checking if flag with name: " << name
360 <<
" is enabled for context");
361 for (c_itr = c_vec.begin(); c_itr != c_vec.end(); ++c_itr) {
363 <<
" Value: " << c_itr->value);
384 if (c_vec.empty() && !f_vec.empty()) {
387 for (c_itr = c_vec.begin(); c_itr != c_vec.end(); ++c_itr) {
388 f_itr = find(f_vec.begin(), f_vec.end(), *c_itr);
389 if (f_itr == f_vec.end()) {
401 result = default_state;
404 FLAG_DEBUG(
"Flag enabled: " <<
string(result ?
"True" :
"False"));
413 tbb::mutex::scoped_lock lock(
mutex_);
415 string name = flag->
name();
417 FLAG_DEBUG(
"Module interested in flag. Name: " << name <<
418 " \nAdding to InterestMap");
428 " already present in FlagMap");
438 itr =
int_map_.insert(make_pair(name, flag));
442 pair <int_map_itr, int_map_itr> ret;
443 const string name = flag->
name();
446 FLAG_DEBUG(
"Module not interested in flag. Name: " << name <<
447 " \nRemoving from InterestMap");
449 tbb::mutex::scoped_lock lock(
mutex_);
452 while (it != ret.second) {
453 if (it->second == flag) {
463 pair <int_map_const_itr, int_map_const_itr> ret;
465 tbb::mutex::scoped_lock lock(
mutex_);
469 if (it->second == flag) {
493 flag_infos.push_back(flag_cfg);
501 tbb::mutex::scoped_lock lock(
mutex_);
const FlagState::Type & state() const
static void Initialize(const string &build_info)
const std::string & version() const
void Unset(const std::string &name)
void Set(const std::string &name, const std::string &version, bool enabled, FlagState::Type state, ContextVec &context_infos)
static FlagUveManager * GetInstance()
static boost::scoped_ptr< FlagUveManager > instance_
FlagManager * flag_manager_
size_t GetIntMapCount() const
void Unset(const std::string &name)
static FlagConfigManager * GetInstance()
std::map< std::string, FlagConfig >::const_iterator flag_map_citr
ContextVec context_infos_
FlagConfigVec GetFlagInfos() const
static std::string ToString(Type state)
void Unregister(const Flag *flag)
const ContextVec & context_infos() const
boost::function< void(void)> FlagUveCb
void Register(Flag *flag)
FlagManager * flag_manager_
std::multimap< std::string, Flag * >::const_iterator int_map_const_itr
const bool enabled() const
size_t GetFlagMapCount() const
#define FLAG_DEBUG(args...)
std::map< std::string, FlagConfig >::iterator flag_map_itr
void Set(const std::string &name, const std::string &version_info, bool enabled, FlagState::Type state, ContextVec &context_infos)
static FlagManager * GetInstance()
FlagConfigVec GetFlagInfosUnlocked() const
std::vector< FlagConfig > FlagConfigVec
void set_enabled(const bool val)
static void CreateInstance(FlagManager *mgr, FlagUveCb send_uve_cb)
FlagConfigVec GetFlagInfos(bool lock) const
const ContextVec & context_infos() const
FlagStateCb flag_state_cb_
std::multimap< std::string, Flag * >::iterator int_map_itr
bool operator==(const Flag &rhs) const
static std::string version_
bool IsRegistered(const Flag *flag) const
bool operator!=(const Flag &rhs) const
const std::string & name() const
FlagUveManager(FlagManager *manager, FlagUveCb send_uve_cb)
boost::function< void()> FlagStateCb
std::vector< FlagContext > ContextVec
std::vector< FlagContext >::const_iterator context_iterator
static boost::scoped_ptr< FlagManager > instance_
bool IsFlagEnabled(const std::string &name, bool default_state, const ContextVec &c_vec) const
FlagConfigManager(FlagManager *manager)
static boost::scoped_ptr< FlagConfigManager > instance_
const std::string & name() const
#define FLAG_WARN(args...)