9 #include <boost/algorithm/string/predicate.hpp>
10 #include <boost/assign/list_of.hpp>
17 #include "config_client_log_types.h"
18 #include "rapidjson/stringbuffer.h"
19 #include "rapidjson/writer.h"
21 using boost::assign::list_of;
22 using contrail_rapidjson::Document;
23 using contrail_rapidjson::StringBuffer;
24 using contrail_rapidjson::Value;
25 using contrail_rapidjson::Writer;
40 list_of(prop_prefix)(map_prop_prefix)
41 (list_prop_prefix)(ref_prefix)(parent_prefix);
44 getenv(
"CONTRAIL_CONFIG_ASSERT_ON_PARSE_ERROR") != NULL;
46 #define CONFIG_PARSE_ASSERT(t, condition) \
50 CONFIG_CLIENT_WARN_LOG(ConfigurationMalformed ## t ## Warning ## Log, \
51 Category::CONFIG_CLIENT, c.key, c.value, type_, uuid_); \
52 CONFIG_CLIENT_TRACE(ConfigurationMalformed ## t ## Warning ## Trace, \
53 c.key, c.value, type_, uuid_); \
54 cout << "CONFIG_PARSE_ERROR " << __FILE__ << ":" << __LINE__ << " "; \
55 cout << type_ << " " << c.key << " " << c.value << " "; \
56 cout << uuid_ << endl; \
57 if (assert_on_parse_error_) \
66 CreateJsonString(obj_type, cdvec);
70 const string &
type,
const Document &doc) : uuid_(
uuid) {
74 Document::AllocatorType &a = json_document_.GetAllocator();
79 json_document_.SetObject().AddMember(vk.SetString(
type.c_str(), a),
85 Writer<StringBuffer> writer(buffer);
86 attr_value.Accept(writer);
87 return buffer.GetString();
92 Document::AllocatorType &a) {
96 string c_value = c.
value;
97 if (c.
key ==
"prop:bgpaas_session_attributes")
99 Document prop_document(&a);
100 prop_document.Parse<0>(c_value.c_str());
103 jsonObject->AddMember(
113 size_t from_front_pos = c.
key.find(
':');
114 size_t from_back_pos = c.
key.rfind(
':');
115 string prop_map = c.
key.substr(from_front_pos + 1,
116 from_back_pos - from_front_pos - 1);
118 config_json_parser()->GetWrapperFieldName(
type_,
120 if (wrapper.empty()) {
123 if (!jsonObject->HasMember(prop_map.c_str())) {
126 jsonObject->AddMember(
127 vk.SetString(prop_map.c_str(), a), v.SetObject(), a);
130 (*jsonObject)[prop_map.c_str()].AddMember(
131 vak.SetString(wrapper.c_str(), a), va.SetArray(), a);
134 Document map_document(&a);
135 map_document.Parse<0>(c.
value.c_str());
141 (*jsonObject)[prop_map.c_str()][wrapper.c_str()].PushBack(
147 size_t from_front_pos = c.
key.find(
':');
148 size_t from_back_pos = c.
key.rfind(
':');
151 string ref_type = c.
key.substr(from_front_pos + 1,
152 from_back_pos-from_front_pos - 1);
153 string ref_uuid = c.
key.substr(from_back_pos + 1);
155 string r = ref_type +
"_refs";
156 if (!jsonObject->HasMember(r.c_str())) {
159 jsonObject->AddMember(vk.SetString(r.c_str(), a), v.SetArray(), a);
167 v.AddMember(
"to", vs1.SetString(fq_name_ref.c_str(), a), a);
168 v.AddMember(
"uuid", vs2.SetString(ref_uuid.c_str(), a), a);
170 bool link_with_attr =
172 if (link_with_attr) {
173 Document ref_document(&a);
174 ref_document.Parse<0>(c.
value.c_str());
176 !ref_document.HasParseError());
178 ref_document.HasMember(
"attr"));
179 Value &attr_value = ref_document[
"attr"];
180 if (!attr_value.IsNull()) {
181 v.AddMember(
"attr", attr_value, a);
184 v.AddMember(
"attr", vm.SetObject(), a);
188 v.AddMember(
"attr", vm.SetObject(), a);
190 (*jsonObject)[r.c_str()].PushBack(v, a);
195 size_t pos = c.
key.rfind(
':');
197 size_t type_pos = c.
key.find(
':');
202 v.SetString(c.
key.substr(type_pos + 1,
203 pos-type_pos - 1).c_str(), a), a);
208 Document fq_name_document(&a);
209 fq_name_document.Parse<0>(c.
value.c_str());
212 jsonObject->AddMember(vk.SetString(c.
key.c_str(), a),
213 fq_name_document, a);
217 if (!c.
key.compare(
"type")) {
233 jsonObject.SetObject();
237 size_t type_index = -1;
238 size_t i = cdvec.size();
240 if (!cdvec[i].key.compare(
"type")) {
250 Category::CONFIG_CLIENT,
uuid_, obj_type);
256 for (
size_t i = 0; i < cdvec.size(); ++i) {
265 static const std::set< std::string > allowed_properties
static const std::string parent_type_prefix
static std::string GetJsonString(const contrail_rapidjson::Value &attr_value)
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_
static const std::string parent_prefix
ConfigCass2JsonAdapter(const std::string &uuid, ConfigCassandraClient *cassandra_client, const std::string &obj_type, const CassColumnKVVec &cdvec)
void CreateJsonString(const std::string &obj_type, const CassColumnKVVec &cdvec)
static const std::string prop_prefix
static const std::string map_prop_prefix
contrail_rapidjson::Document json_document_
static const std::string ref_prefix
static const std::string fq_name_prefix
static const std::string list_prop_prefix
ConfigJsonParserBase * config_json_parser()
ConfigClientManager * mgr()
#define CONFIG_PARSE_ASSERT(t, condition)
#define CONFIG_CLIENT_WARN_LOG(obj, category,...)
#define CONFIG_CLIENT_TRACE(obj,...)
std::vector< JsonAdapterDataType > CassColumnKVVec