Line data Source code
1 : /*
2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include "config_cass2json_adapter.h"
7 :
8 : #include <assert.h>
9 : #include <boost/algorithm/string/predicate.hpp>
10 : #include <boost/assign/list_of.hpp>
11 : #include <string>
12 : #include <iostream>
13 :
14 : #include "base/string_util.h"
15 : #include "config_cassandra_client.h"
16 : #include "config_client_log.h"
17 : #include "config_client_log_types.h"
18 : #include "rapidjson/stringbuffer.h"
19 : #include "rapidjson/writer.h"
20 :
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;
26 : using std::cout;
27 : using std::endl;
28 : using std::set;
29 : using std::string;
30 :
31 : const string ConfigCass2JsonAdapter::fq_name_prefix = "fq_name";
32 : const string ConfigCass2JsonAdapter::prop_prefix = "prop:";
33 : const string ConfigCass2JsonAdapter::list_prop_prefix = "propl:";
34 : const string ConfigCass2JsonAdapter::map_prop_prefix = "propm:";
35 : const string ConfigCass2JsonAdapter::ref_prefix = "ref:";
36 : const string ConfigCass2JsonAdapter::parent_prefix = "parent:";
37 : const string ConfigCass2JsonAdapter::parent_type_prefix = "parent_type";
38 :
39 : const set<string> ConfigCass2JsonAdapter::allowed_properties =
40 : list_of(prop_prefix)(map_prop_prefix)
41 : (list_prop_prefix)(ref_prefix)(parent_prefix);
42 :
43 : bool ConfigCass2JsonAdapter::assert_on_parse_error_ =
44 : getenv("CONTRAIL_CONFIG_ASSERT_ON_PARSE_ERROR") != NULL;
45 :
46 : #define CONFIG_PARSE_ASSERT(t, condition) \
47 : do { \
48 : if (condition) \
49 : break; \
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_) \
58 : assert(false); \
59 : return; \
60 : } while (false)
61 :
62 2447 : ConfigCass2JsonAdapter::ConfigCass2JsonAdapter(const string &uuid,
63 : ConfigCassandraClient *cassandra_client, const string &obj_type,
64 2447 : const CassColumnKVVec &cdvec) : cassandra_client_(cassandra_client),
65 2447 : uuid_(uuid) {
66 2447 : CreateJsonString(obj_type, cdvec);
67 2447 : }
68 :
69 0 : ConfigCass2JsonAdapter::ConfigCass2JsonAdapter(const string &uuid,
70 0 : const string &type, const Document &doc) : uuid_(uuid) {
71 : /**
72 : * Construct the final Json Document.
73 : */
74 0 : Document::AllocatorType &a = json_document_.GetAllocator();
75 0 : Value v;
76 0 : v.CopyFrom(doc, a);
77 :
78 0 : Value vk;
79 0 : json_document_.SetObject().AddMember(vk.SetString(type.c_str(), a),
80 : v, a);
81 0 : }
82 :
83 0 : string ConfigCass2JsonAdapter::GetJsonString(const Value &attr_value) {
84 0 : StringBuffer buffer;
85 0 : Writer<StringBuffer> writer(buffer);
86 0 : attr_value.Accept(writer);
87 0 : return buffer.GetString();
88 0 : }
89 :
90 12366 : void ConfigCass2JsonAdapter::AddOneEntry(Value *jsonObject,
91 : const string &obj_type, const JsonAdapterDataType &c,
92 : Document::AllocatorType &a) {
93 :
94 : // Process scalar property values.
95 12366 : if (boost::starts_with(c.key, prop_prefix)) {
96 3633 : string c_value = c.value;
97 3633 : if (c.key == "prop:bgpaas_session_attributes")
98 0 : c_value = "\"\"";
99 3633 : Document prop_document(&a);
100 3633 : prop_document.Parse<0>(c_value.c_str());
101 3633 : CONFIG_PARSE_ASSERT(Property, !prop_document.HasParseError());
102 3633 : Value vk;
103 3633 : jsonObject->AddMember(
104 7266 : vk.SetString(c.key.substr(prop_prefix.size()).c_str(), a),
105 : prop_document, a);
106 3633 : return;
107 3633 : }
108 :
109 : // Process property list and property map values.
110 8732 : bool is_map = false;
111 17259 : if ((is_map = boost::starts_with(c.key, map_prop_prefix)) ||
112 8527 : boost::starts_with(c.key, list_prop_prefix)) {
113 416 : size_t from_front_pos = c.key.find(':');
114 416 : size_t from_back_pos = c.key.rfind(':');
115 : string prop_map = c.key.substr(from_front_pos + 1,
116 416 : from_back_pos - from_front_pos - 1);
117 416 : string wrapper = cassandra_client_->mgr()-> \
118 416 : config_json_parser()->GetWrapperFieldName(type_,
119 416 : prop_map);
120 416 : if (wrapper.empty()) {
121 0 : return;
122 : }
123 416 : if (!jsonObject->HasMember(prop_map.c_str())) {
124 220 : Value v;
125 220 : Value vk;
126 220 : jsonObject->AddMember(
127 : vk.SetString(prop_map.c_str(), a), v.SetObject(), a);
128 220 : Value va;
129 220 : Value vak;
130 220 : (*jsonObject)[prop_map.c_str()].AddMember(
131 : vak.SetString(wrapper.c_str(), a), va.SetArray(), a);
132 220 : }
133 :
134 416 : Document map_document(&a);
135 416 : map_document.Parse<0>(c.value.c_str());
136 416 : if (is_map)
137 206 : CONFIG_PARSE_ASSERT(PropertyMap, !map_document.HasParseError());
138 : else
139 210 : CONFIG_PARSE_ASSERT(PropertyList, !map_document.HasParseError());
140 :
141 416 : (*jsonObject)[prop_map.c_str()][wrapper.c_str()].PushBack(
142 : map_document, a);
143 416 : return;
144 416 : }
145 :
146 8317 : if (boost::starts_with(c.key, ref_prefix)) {
147 941 : size_t from_front_pos = c.key.find(':');
148 941 : size_t from_back_pos = c.key.rfind(':');
149 941 : CONFIG_PARSE_ASSERT(Reference, from_front_pos != string::npos);
150 941 : CONFIG_PARSE_ASSERT(Reference, from_back_pos != string::npos);
151 : string ref_type = c.key.substr(from_front_pos + 1,
152 941 : from_back_pos-from_front_pos - 1);
153 941 : string ref_uuid = c.key.substr(from_back_pos + 1);
154 941 : string fq_name_ref = c.ref_fq_name;
155 941 : string r = ref_type + "_refs";
156 941 : if (!jsonObject->HasMember(r.c_str())) {
157 921 : Value v;
158 921 : Value vk;
159 921 : jsonObject->AddMember(vk.SetString(r.c_str(), a), v.SetArray(), a);
160 921 : }
161 :
162 941 : Value v;
163 941 : v.SetObject();
164 :
165 941 : Value vs1;
166 941 : Value vs2;
167 941 : v.AddMember("to", vs1.SetString(fq_name_ref.c_str(), a), a);
168 941 : v.AddMember("uuid", vs2.SetString(ref_uuid.c_str(), a), a);
169 :
170 : bool link_with_attr =
171 941 : cassandra_client_->mgr()->config_json_parser()->\
172 941 : IsLinkWithAttr(obj_type, ref_type);
173 941 : if (link_with_attr) {
174 382 : Document ref_document(&a);
175 382 : ref_document.Parse<0>(c.value.c_str());
176 382 : CONFIG_PARSE_ASSERT(ReferenceLinkAttributes,
177 : !ref_document.HasParseError());
178 382 : CONFIG_PARSE_ASSERT(ReferenceLinkAttributes,
179 : ref_document.HasMember("attr"));
180 382 : Value &attr_value = ref_document["attr"];
181 382 : if (!attr_value.IsNull()) {
182 378 : v.AddMember("attr", attr_value, a);
183 : } else {
184 4 : Value vm;
185 4 : v.AddMember("attr", vm.SetObject(), a);
186 4 : }
187 382 : } else {
188 559 : Value vm;
189 559 : v.AddMember("attr", vm.SetObject(), a);
190 559 : }
191 941 : (*jsonObject)[r.c_str()].PushBack(v, a);
192 941 : return;
193 941 : }
194 :
195 7376 : if (boost::starts_with(c.key, parent_prefix)) {
196 1412 : size_t pos = c.key.rfind(':');
197 1412 : CONFIG_PARSE_ASSERT(Parent, pos != string::npos);
198 1412 : size_t type_pos = c.key.find(':');
199 1412 : CONFIG_PARSE_ASSERT(Parent, type_pos != string::npos);
200 1412 : Value v;
201 1412 : Value vk;
202 1412 : jsonObject->AddMember(vk.SetString(parent_type_prefix.c_str(), a),
203 1412 : v.SetString(c.key.substr(type_pos + 1,
204 1412 : pos-type_pos - 1).c_str(), a), a);
205 1412 : return;
206 1412 : }
207 :
208 5964 : if (!c.key.compare(fq_name_prefix)) {
209 2447 : Document fq_name_document(&a);
210 2447 : fq_name_document.Parse<0>(c.value.c_str());
211 2447 : CONFIG_PARSE_ASSERT(FqName, !fq_name_document.HasParseError());
212 2447 : Value vk;
213 2447 : jsonObject->AddMember(vk.SetString(c.key.c_str(), a),
214 : fq_name_document, a);
215 2447 : return;
216 2447 : }
217 :
218 3517 : if (!c.key.compare("type")) {
219 : // Prepend the 'type'. This is "our key", with value being the json
220 : // sub-document containing all other columns.
221 2447 : CONFIG_PARSE_ASSERT(Type, type_ != obj_type);
222 2447 : type_ = c.value;
223 2447 : type_.erase(remove(type_.begin(), type_.end(), '\"'), type_.end());
224 2447 : return;
225 : }
226 : }
227 :
228 2447 : void ConfigCass2JsonAdapter::CreateJsonString(const string &obj_type,
229 : const CassColumnKVVec &cdvec) {
230 2447 : if (cdvec.empty())
231 0 : return;
232 2447 : Document::AllocatorType &a = json_document_.GetAllocator();
233 2447 : Value jsonObject;
234 2447 : jsonObject.SetObject();
235 :
236 : // First look for and part "type" field. We usually expect it to be at the
237 : // end as column keys are suppose to be allways sorted lexicographically.
238 2447 : size_t type_index = -1;
239 2447 : size_t i = cdvec.size();
240 6714 : while (i--) {
241 6714 : if (!cdvec[i].key.compare("type")) {
242 2447 : AddOneEntry(&jsonObject, obj_type, cdvec[i], a);
243 2447 : type_index = i;
244 2447 : break;
245 : }
246 : }
247 :
248 2447 : if (type_.empty()) {
249 :
250 0 : CONFIG_CLIENT_WARN_LOG(ConfigurationMissingTypeWarningLog,
251 : Category::CONFIG_CLIENT, uuid_, obj_type);
252 0 : CONFIG_CLIENT_TRACE(ConfigurationMissingTypeWarningTrace, uuid_,
253 : obj_type);
254 0 : return;
255 : }
256 :
257 14813 : for (size_t i = 0; i < cdvec.size(); ++i) {
258 12366 : if (i != type_index)
259 9919 : AddOneEntry(&jsonObject, obj_type, cdvec[i], a);
260 : }
261 :
262 2447 : Value vk;
263 2447 : json_document_.SetObject().AddMember(vk.SetString(type_.c_str(), a),
264 : jsonObject, a);
265 2447 : }
|