From 53cc6cd9e2abd87ce691b4c4f43be68cd208cbff Mon Sep 17 00:00:00 2001 From: Varun Lodaya Date: Tue, 18 Aug 2015 22:32:04 -0700 Subject: [PATCH] Allow custom configs with LBaaS This fix commits the vrouter agent code to read the custom_attributes from ifmap node and copy it to config.json file which the haproxy parser would read. Added missing '}'. Incorporating the comments Closes-Bug: #1475393 Change-Id: I6f22f4f537c97c48b2283971b2959c9be5931361 --- src/vnsw/agent/oper/loadbalancer.cc | 1 + src/vnsw/agent/oper/loadbalancer_config.cc | 29 ++++++++++- src/vnsw/agent/oper/loadbalancer_config.h | 2 + .../agent/oper/loadbalancer_properties.cc | 50 +++++++++++++++++++ src/vnsw/agent/oper/loadbalancer_properties.h | 9 ++++ 5 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/vnsw/agent/oper/loadbalancer.cc b/src/vnsw/agent/oper/loadbalancer.cc index 7954be8bb1f..668b3f8f570 100644 --- a/src/vnsw/agent/oper/loadbalancer.cc +++ b/src/vnsw/agent/oper/loadbalancer.cc @@ -95,6 +95,7 @@ void Loadbalancer::CalculateProperties(DBGraph *graph, Properties *properties) { autogen::LoadbalancerPool *pool = static_cast(node_->GetObject()); properties->set_pool_properties(pool->properties()); + properties->set_custom_attributes(pool->custom_attributes()); for (DBGraphVertex::adjacency_iterator iter = node_->begin(graph); iter != node_->end(graph); ++iter) { diff --git a/src/vnsw/agent/oper/loadbalancer_config.cc b/src/vnsw/agent/oper/loadbalancer_config.cc index fff858e4160..b048ce2f5c9 100644 --- a/src/vnsw/agent/oper/loadbalancer_config.cc +++ b/src/vnsw/agent/oper/loadbalancer_config.cc @@ -118,7 +118,33 @@ void LoadbalancerConfig::GenerateHealthMonitors( if (count) { ostr << endl; } - ostr << " ]" << endl; + ostr << " ]," << endl; + *out << ostr.str(); +} + +void LoadbalancerConfig::GenerateCustomAttributes( + ostream *out, const LoadbalancerProperties &props) const { + const std::vector + &custom_attributes = props.custom_attributes(); + ostringstream ostr; + autogen::KeyValuePairs::const_iterator curr_iter, next_iter, end_iter; + curr_iter = custom_attributes.begin(); + end_iter = custom_attributes.end(); + + ostr << " \"custom-attributes\":{" << endl; + + while (curr_iter != end_iter) { + next_iter = curr_iter + 1; + const autogen::KeyValuePair element = (*curr_iter); + ostr << " \"" << element.key << "\":\"" << element.value << "\""; + if (next_iter == end_iter) { + ostr << endl; + break; + } + ostr << "," << endl; + curr_iter = next_iter; + } + ostr << " }" << endl; *out << ostr.str(); } @@ -137,6 +163,7 @@ void LoadbalancerConfig::GenerateConfig( GenerateVip(&fs, props); GenerateMembers(&fs, props); GenerateHealthMonitors(&fs, props); + GenerateCustomAttributes(&fs, props); fs << "}" << endl; fs.close(); } diff --git a/src/vnsw/agent/oper/loadbalancer_config.h b/src/vnsw/agent/oper/loadbalancer_config.h index fbfff4662f4..55c6c3769a4 100644 --- a/src/vnsw/agent/oper/loadbalancer_config.h +++ b/src/vnsw/agent/oper/loadbalancer_config.h @@ -29,6 +29,8 @@ class LoadbalancerConfig { const LoadbalancerProperties &props) const; void GenerateHealthMonitors(std::ostream *out, const LoadbalancerProperties &props) const; + void GenerateCustomAttributes(std::ostream *out, + const LoadbalancerProperties &props) const; Agent *agent_; diff --git a/src/vnsw/agent/oper/loadbalancer_properties.cc b/src/vnsw/agent/oper/loadbalancer_properties.cc index e47429a9c22..5b9de3cc7ca 100644 --- a/src/vnsw/agent/oper/loadbalancer_properties.cc +++ b/src/vnsw/agent/oper/loadbalancer_properties.cc @@ -125,6 +125,48 @@ void MapDiff(const MapType &lhs, const MapType &rhs, Comparator comp, } } +int CustomAttributesCompare(const std::vector &lhs, + const std::vector &rhs) { + autogen::KeyValuePairs::const_iterator iter1 = lhs.begin(); + autogen::KeyValuePairs::const_iterator iter2 = rhs.begin(); + int ret = 0; + + while (iter1 != lhs.end() && iter2 != rhs.end()) { + if ((ret = iter1->key.compare(iter2->key)) != 0) { + return ret; + } + if ((ret = iter1->value.compare(iter2->value)) != 0) { + return ret; + } + ++iter1; + ++iter2; + } + + if (iter1 != lhs.end()) { + return 1; + } + if (iter2 != rhs.end()) { + return -1; + } + return 0; +} + +void CustomAttributesDiff(const std::vector &lhs, + const std::vector &rhs, + std::stringstream *ss) { + autogen::KeyValuePairs::const_iterator iter1 = lhs.begin(); + autogen::KeyValuePairs::const_iterator iter2 = rhs.begin(); + + while (iter1 != lhs.end()) { + *ss << " -" << iter1->key << ": " << iter1->value; + ++iter1; + } + while (iter2 != rhs.end()) { + *ss << " +" << iter2->key << ": " << iter2->value; + ++iter2; + } +} + #define COMPARE_PROPERTY(Xname) \ do { \ int res = compare(Xname, rhs.Xname); \ @@ -152,8 +194,14 @@ int LoadbalancerProperties::CompareTo(const LoadbalancerProperties &rhs) const { } result = MapCompare(monitors_, rhs.monitors_, CompareMonitor); + if (result) { + return result; + } + + result = CustomAttributesCompare(custom_attributes_, rhs.custom_attributes_); return result; } + #undef COMPARE_PROPERTY #define DIFF_PROPERTY(Xss, Xname)\ @@ -187,6 +235,8 @@ std::string LoadbalancerProperties::DiffString( MapDiff(current->members_, members_, CompareMember, &ss); ss << " Monitors:"; MapDiff(current->monitors_, monitors_, CompareMonitor, &ss); + ss << " Custom Attributes:"; + CustomAttributesDiff(current->custom_attributes_, custom_attributes_, &ss); return ss.str(); } diff --git a/src/vnsw/agent/oper/loadbalancer_properties.h b/src/vnsw/agent/oper/loadbalancer_properties.h index 673e6e2b036..522ccbf75f3 100644 --- a/src/vnsw/agent/oper/loadbalancer_properties.h +++ b/src/vnsw/agent/oper/loadbalancer_properties.h @@ -62,12 +62,21 @@ class LoadbalancerProperties { return &monitors_; } + const std::vector &custom_attributes() const { + return custom_attributes_; + } + + void set_custom_attributes(const std::vector &custom_attributes) { + custom_attributes_ = custom_attributes; + } + private: autogen::LoadbalancerPoolType pool_; boost::uuids::uuid vip_uuid_; autogen::VirtualIpType vip_; MemberMap members_; HealthmonitorMap monitors_; + std::vector custom_attributes_; }; #endif // VNSW_AGENT_OPER_LOADBALANCER_PROPERTIES_H__