OpenSDN source code
config_cass2json_adapter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
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"
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 
40  list_of(prop_prefix)(map_prop_prefix)
41  (list_prop_prefix)(ref_prefix)(parent_prefix);
42 
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 
63  ConfigCassandraClient *cassandra_client, const string &obj_type,
64  const CassColumnKVVec &cdvec) : cassandra_client_(cassandra_client),
65  uuid_(uuid) {
66  CreateJsonString(obj_type, cdvec);
67 }
68 
70  const string &type, const Document &doc) : uuid_(uuid) {
74  Document::AllocatorType &a = json_document_.GetAllocator();
75  Value v;
76  v.CopyFrom(doc, a);
77 
78  Value vk;
79  json_document_.SetObject().AddMember(vk.SetString(type.c_str(), a),
80  v, a);
81 }
82 
83 string ConfigCass2JsonAdapter::GetJsonString(const Value &attr_value) {
84  StringBuffer buffer;
85  Writer<StringBuffer> writer(buffer);
86  attr_value.Accept(writer);
87  return buffer.GetString();
88 }
89 
90 void ConfigCass2JsonAdapter::AddOneEntry(Value *jsonObject,
91  const string &obj_type, const JsonAdapterDataType &c,
92  Document::AllocatorType &a) {
93 
94  // Process scalar property values.
95  if (boost::starts_with(c.key, prop_prefix)) {
96  string c_value = c.value;
97  if (c.key == "prop:bgpaas_session_attributes")
98  c_value = "\"\"";
99  Document prop_document(&a);
100  prop_document.Parse<0>(c_value.c_str());
101  CONFIG_PARSE_ASSERT(Property, !prop_document.HasParseError());
102  Value vk;
103  jsonObject->AddMember(
104  vk.SetString(c.key.substr(prop_prefix.size()).c_str(), a),
105  prop_document, a);
106  return;
107  }
108 
109  // Process property list and property map values.
110  bool is_map = false;
111  if ((is_map = boost::starts_with(c.key, map_prop_prefix)) ||
112  boost::starts_with(c.key, list_prop_prefix)) {
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);
117  string wrapper = cassandra_client_->mgr()-> \
118  config_json_parser()->GetWrapperFieldName(type_,
119  prop_map);
120  if (wrapper.empty()) {
121  return;
122  }
123  if (!jsonObject->HasMember(prop_map.c_str())) {
124  Value v;
125  Value vk;
126  jsonObject->AddMember(
127  vk.SetString(prop_map.c_str(), a), v.SetObject(), a);
128  Value va;
129  Value vak;
130  (*jsonObject)[prop_map.c_str()].AddMember(
131  vak.SetString(wrapper.c_str(), a), va.SetArray(), a);
132  }
133 
134  Document map_document(&a);
135  map_document.Parse<0>(c.value.c_str());
136  if (is_map)
137  CONFIG_PARSE_ASSERT(PropertyMap, !map_document.HasParseError());
138  else
139  CONFIG_PARSE_ASSERT(PropertyList, !map_document.HasParseError());
140 
141  (*jsonObject)[prop_map.c_str()][wrapper.c_str()].PushBack(
142  map_document, a);
143  return;
144  }
145 
146  if (boost::starts_with(c.key, ref_prefix)) {
147  size_t from_front_pos = c.key.find(':');
148  size_t from_back_pos = c.key.rfind(':');
149  CONFIG_PARSE_ASSERT(Reference, from_front_pos != string::npos);
150  CONFIG_PARSE_ASSERT(Reference, from_back_pos != string::npos);
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);
154  string fq_name_ref = c.ref_fq_name;
155  string r = ref_type + "_refs";
156  if (!jsonObject->HasMember(r.c_str())) {
157  Value v;
158  Value vk;
159  jsonObject->AddMember(vk.SetString(r.c_str(), a), v.SetArray(), a);
160  }
161 
162  Value v;
163  v.SetObject();
164 
165  Value vs1;
166  Value vs2;
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);
169 
170  bool link_with_attr =
171  cassandra_client_->mgr()->config_json_parser()->\ IsLinkWithAttr(obj_type, ref_type);
172  if (link_with_attr) {
173  Document ref_document(&a);
174  ref_document.Parse<0>(c.value.c_str());
175  CONFIG_PARSE_ASSERT(ReferenceLinkAttributes,
176  !ref_document.HasParseError());
177  CONFIG_PARSE_ASSERT(ReferenceLinkAttributes,
178  ref_document.HasMember("attr"));
179  Value &attr_value = ref_document["attr"];
180  if (!attr_value.IsNull()) {
181  v.AddMember("attr", attr_value, a);
182  } else {
183  Value vm;
184  v.AddMember("attr", vm.SetObject(), a);
185  }
186  } else {
187  Value vm;
188  v.AddMember("attr", vm.SetObject(), a);
189  }
190  (*jsonObject)[r.c_str()].PushBack(v, a);
191  return;
192  }
193 
194  if (boost::starts_with(c.key, parent_prefix)) {
195  size_t pos = c.key.rfind(':');
196  CONFIG_PARSE_ASSERT(Parent, pos != string::npos);
197  size_t type_pos = c.key.find(':');
198  CONFIG_PARSE_ASSERT(Parent, type_pos != string::npos);
199  Value v;
200  Value vk;
201  jsonObject->AddMember(vk.SetString(parent_type_prefix.c_str(), a),
202  v.SetString(c.key.substr(type_pos + 1,
203  pos-type_pos - 1).c_str(), a), a);
204  return;
205  }
206 
207  if (!c.key.compare(fq_name_prefix)) {
208  Document fq_name_document(&a);
209  fq_name_document.Parse<0>(c.value.c_str());
210  CONFIG_PARSE_ASSERT(FqName, !fq_name_document.HasParseError());
211  Value vk;
212  jsonObject->AddMember(vk.SetString(c.key.c_str(), a),
213  fq_name_document, a);
214  return;
215  }
216 
217  if (!c.key.compare("type")) {
218  // Prepend the 'type'. This is "our key", with value being the json
219  // sub-document containing all other columns.
220  CONFIG_PARSE_ASSERT(Type, type_ != obj_type);
221  type_ = c.value;
222  type_.erase(remove(type_.begin(), type_.end(), '\"'), type_.end());
223  return;
224  }
225 }
226 
227 void ConfigCass2JsonAdapter::CreateJsonString(const string &obj_type,
228  const CassColumnKVVec &cdvec) {
229  if (cdvec.empty())
230  return;
231  Document::AllocatorType &a = json_document_.GetAllocator();
232  Value jsonObject;
233  jsonObject.SetObject();
234 
235  // First look for and part "type" field. We usually expect it to be at the
236  // end as column keys are suppose to be allways sorted lexicographically.
237  size_t type_index = -1;
238  size_t i = cdvec.size();
239  while (i--) {
240  if (!cdvec[i].key.compare("type")) {
241  AddOneEntry(&jsonObject, obj_type, cdvec[i], a);
242  type_index = i;
243  break;
244  }
245  }
246 
247  if (type_.empty()) {
248 
249  CONFIG_CLIENT_WARN_LOG(ConfigurationMissingTypeWarningLog,
250  Category::CONFIG_CLIENT, uuid_, obj_type);
251  CONFIG_CLIENT_TRACE(ConfigurationMissingTypeWarningTrace, uuid_,
252  obj_type);
253  return;
254  }
255 
256  for (size_t i = 0; i < cdvec.size(); ++i) {
257  if (i != type_index)
258  AddOneEntry(&jsonObject, obj_type, cdvec[i], a);
259  }
260 
261  Value vk;
262  json_document_.SetObject().AddMember(vk.SetString(type_.c_str(), a),
263  jsonObject, a);
264 }
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 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
uint8_t type
Definition: load_balance.h:2
boost::uuids::uuid uuid