8 #include <boost/algorithm/string/predicate.hpp>
9 #include <boost/assign/list_of.hpp>
16 #include "config_client_log_types.h"
17 #include "rapidjson/stringbuffer.h"
18 #include "rapidjson/writer.h"
20 using boost::assign::list_of;
21 using contrail_rapidjson::Document;
22 using contrail_rapidjson::StringBuffer;
23 using contrail_rapidjson::Value;
24 using contrail_rapidjson::Writer;
39 list_of(prop_prefix)(map_prop_prefix)
40 (list_prop_prefix)(ref_prefix)(parent_prefix);
43 getenv(
"CONTRAIL_CONFIG_ASSERT_ON_PARSE_ERROR") != NULL;
45 #define CONFIG_PARSE_ASSERT(t, condition) \
49 CONFIG_CLIENT_WARN_LOG(ConfigurationMalformed ## t ## Warning ## Log, \
50 Category::CONFIG_CLIENT, c.key, c.value, type_, uuid_); \
51 CONFIG_CLIENT_TRACE(ConfigurationMalformed ## t ## Warning ## Trace, \
52 c.key, c.value, type_, uuid_); \
53 cout << "CONFIG_PARSE_ERROR " << __FILE__ << ":" << __LINE__ << " "; \
54 cout << type_ << " " << c.key << " " << c.value << " "; \
55 cout << uuid_ << endl; \
56 if (assert_on_parse_error_) \
65 CreateJsonString(obj_type, cdvec);
69 const string &
type,
const Document &doc) : uuid_(uuid) {
73 Document::AllocatorType &a = json_document_.GetAllocator();
78 json_document_.SetObject().AddMember(vk.SetString(type.c_str(), a),
84 Writer<StringBuffer> writer(buffer);
85 attr_value.Accept(writer);
86 return buffer.GetString();
91 Document::AllocatorType &a) {
95 string c_value = c.
value;
96 if (c.
key ==
"prop:bgpaas_session_attributes")
98 Document prop_document(&a);
99 prop_document.Parse<0>(c_value.c_str());
102 jsonObject->AddMember(
112 size_t from_front_pos = c.
key.find(
':');
113 size_t from_back_pos = c.
key.rfind(
':');
114 string prop_map = c.
key.substr(from_front_pos + 1,
115 from_back_pos - from_front_pos - 1);
117 config_json_parser()->GetWrapperFieldName(
type_,
119 if (wrapper.empty()) {
122 if (!jsonObject->HasMember(prop_map.c_str())) {
125 jsonObject->AddMember(
126 vk.SetString(prop_map.c_str(), a), v.SetObject(), a);
129 (*jsonObject)[prop_map.c_str()].AddMember(
130 vak.SetString(wrapper.c_str(), a), va.SetArray(), a);
133 Document map_document(&a);
134 map_document.Parse<0>(c.
value.c_str());
140 (*jsonObject)[prop_map.c_str()][wrapper.c_str()].PushBack(
146 size_t from_front_pos = c.
key.find(
':');
147 size_t from_back_pos = c.
key.rfind(
':');
150 string ref_type = c.
key.substr(from_front_pos + 1,
151 from_back_pos-from_front_pos - 1);
152 string ref_uuid = c.
key.substr(from_back_pos + 1);
154 string r = ref_type +
"_refs";
155 if (!jsonObject->HasMember(r.c_str())) {
158 jsonObject->AddMember(vk.SetString(r.c_str(), a), v.SetArray(), a);
166 v.AddMember(
"to", vs1.SetString(fq_name_ref.c_str(), a), a);
167 v.AddMember(
"uuid", vs2.SetString(ref_uuid.c_str(), a), a);
169 bool link_with_attr =
171 if (link_with_attr) {
172 Document ref_document(&a);
173 ref_document.Parse<0>(c.
value.c_str());
175 !ref_document.HasParseError());
177 ref_document.HasMember(
"attr"));
178 Value &attr_value = ref_document[
"attr"];
179 if (!attr_value.IsNull()) {
180 v.AddMember(
"attr", attr_value, a);
183 v.AddMember(
"attr", vm.SetObject(), a);
187 v.AddMember(
"attr", vm.SetObject(), a);
189 (*jsonObject)[r.c_str()].PushBack(v, a);
194 size_t pos = c.
key.rfind(
':');
196 size_t type_pos = c.
key.find(
':');
201 v.SetString(c.
key.substr(type_pos + 1,
202 pos-type_pos - 1).c_str(), a), a);
207 Document fq_name_document(&a);
208 fq_name_document.Parse<0>(c.
value.c_str());
211 jsonObject->AddMember(vk.SetString(c.
key.c_str(), a),
212 fq_name_document, a);
216 if (!c.
key.compare(
"type")) {
232 jsonObject.SetObject();
236 size_t type_index = -1;
237 size_t i = cdvec.size();
239 if (!cdvec[i].key.compare(
"type")) {
249 Category::CONFIG_CLIENT,
uuid_, obj_type);
255 for (
size_t i = 0; i < cdvec.size(); ++i) {
264 static const std::string ref_prefix
ConfigJsonParserBase * config_json_parser()
#define CONFIG_CLIENT_WARN_LOG(obj, category,...)
static std::string GetJsonString(const contrail_rapidjson::Value &attr_value)
static const std::string list_prop_prefix
ConfigClientManager * mgr()
static const std::string prop_prefix
contrail_rapidjson::Document json_document_
static const std::set< std::string > allowed_properties
#define CONFIG_CLIENT_TRACE(obj,...)
static const std::string parent_type_prefix
void AddOneEntry(contrail_rapidjson::Value *jsonObject, const std::string &obj_type, const JsonAdapterDataType &c, contrail_rapidjson::Document::AllocatorType &a)
ConfigCassandraClient * cassandra_client_
static bool assert_on_parse_error_
std::vector< JsonAdapterDataType > CassColumnKVVec
static const std::string fq_name_prefix
static const std::string parent_prefix
static const std::string map_prop_prefix
void CreateJsonString(const std::string &obj_type, const CassColumnKVVec &cdvec)
ConfigCass2JsonAdapter(const std::string &uuid, ConfigCassandraClient *cassandra_client, const std::string &obj_type, const CassColumnKVVec &cdvec)
#define CONFIG_PARSE_ASSERT(t, condition)