OpenSDN source code
ifmap_server_parser.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <stdint.h>
8 
9 #include <pugixml/pugixml.hpp>
10 #include "db/db.h"
12 #include "ifmap/ifmap_log.h"
13 #include "ifmap/ifmap_log_types.h"
14 
15 using namespace std;
16 using namespace pugi;
17 
19 
20 static const char *NodeName(const xml_node &node) {
21  const char *name = node.name();
22  // strip namespace
23  const char *dot = strchr(name, ':');
24  if (dot != NULL) {
25  name = dot + 1;
26  }
27  return name;
28 }
29 
31  ModuleMap::iterator loc = module_map_.find(module);
32  if (loc != module_map_.end()) {
33  return loc->second;
34  }
35  IFMapServerParser *parser = new IFMapServerParser();
36  module_map_.insert(make_pair(module, parser));
37  return parser;
38 }
39 
40 void IFMapServerParser::DeleteInstance(const string &module) {
41  ModuleMap::iterator loc = module_map_.find(module);
42  if (loc == module_map_.end()) {
43  return;
44  }
45 
46  delete loc->second;
47  module_map_.erase(loc);
48 }
49 
50 void IFMapServerParser::MetadataRegister(const string &metadata,
51  MetadataParseFn parser) {
52  pair<MetadataParseMap::iterator, bool> result =
53  metadata_map_.insert(make_pair(metadata, parser));
54  assert(result.second);
55 }
56 
57 void IFMapServerParser::MetadataClear(const string &module) {
58  metadata_map_.clear();
59  DeleteInstance(module);
60 }
61 
62 static string ParseIdentifier(const xml_node &node) {
63  string id(node.attribute("name").value());
64  return id;
65 }
66 
67 void IFMapServerParser::SetOrigin(struct DBRequest *result) const {
69  static_cast<IFMapServerTable::RequestData *>(result->data.get());
71 }
72 
73 bool IFMapServerParser::ParseMetadata(const pugi::xml_node &node,
74  struct DBRequest *result) const {
76  static_cast<IFMapServerTable::RequestData *>(result->data.get());
77  const char *name = NodeName(node);
78  MetadataParseMap::const_iterator loc = metadata_map_.find(name);
79  if (loc == metadata_map_.end()) {
80  return false;
81  }
82  std::unique_ptr<AutogenProperty> pvalue;
83  bool success = (loc->second)(node, &pvalue);
84  if (!success) {
85  return false;
86  }
87  if (data == NULL) {
88  data = new IFMapServerTable::RequestData();
89  result->data.reset(data);
90  }
91  data->metadata = name;
92  data->content.reset(pvalue.release());
93  return true;
94 }
95 
96 // Expect the identifier name to be [contrail:]type:name
97 static void IdentifierNsTypeName(const string &id, string *id_type,
98  string *id_name) {
99  size_t ns = id.find("contrail:");
100  size_t start = (ns == 0) ? sizeof("contrail:") - 1: 0;
101  size_t loc = id.find(':', start);
102  if (loc != string::npos) {
103  *id_type = string(id, start, loc - start);
104  *id_name = string(id, loc + 1, id.size() - (loc + 1));
105  } else {
106  *id_name = id;
107  }
108 }
109 
111  DBRequest *request = new DBRequest();
112  request->oper = src->oper;
113  IFMapTable::RequestKey *src_key =
114  static_cast<IFMapTable::RequestKey *>(src->key.get());
115  if (src_key != NULL) {
116  IFMapTable::RequestKey *dst_key =
118  dst_key->id_type = src_key->id_type;
119  dst_key->id_name = src_key->id_name;
120  request->key.reset(dst_key);
121  }
122 
124  static_cast<IFMapServerTable::RequestData *>(src->data.get());
125  if (src_data) {
128  dst_data->id_type = src_data->id_type;
129  dst_data->id_name = src_data->id_name;
130  request->data.reset(dst_data);
131  }
132  return request;
133 }
134 
136  const xml_node &parent, bool add_change, RequestList *list) const {
137  unique_ptr<DBRequest> request(new DBRequest);
138  request->oper = (add_change ? DBRequest::DB_ENTRY_ADD_CHANGE :
140  IFMapTable::RequestKey *key = NULL;
141  IFMapServerTable::RequestData *data = NULL;
142  int idcount = 0;
143  bool has_meta = false;
144  for (xml_node node = parent.first_child(); node;
145  node = node.next_sibling()) {
146  const char *name = NodeName(node);
147  if (strcmp(name, "identity") == 0) {
148  string id = ParseIdentifier(node);
149  switch (idcount) {
150  case 0:
151  key = new IFMapTable::RequestKey();
152  request->key.reset(key);
153  IdentifierNsTypeName(id, &key->id_type, &key->id_name);
154  break;
155  case 1:
156  data = new IFMapServerTable::RequestData();
157  request->data.reset(data);
158  IdentifierNsTypeName(id, &data->id_type, &data->id_name);
159  break;
160  default:
161  return false;
162  }
163  idcount++;
164  continue;
165  }
166  if (strcmp(name, "metadata") == 0) {
167  if (has_meta) {
168  return false;
169  }
170  has_meta = true;
171  for (xml_node meta = node.first_child(); meta;
172  meta = meta.next_sibling()) {
173  if (ParseMetadata(meta, request.get())) {
174  SetOrigin(request.get());
175  DBRequest *current = request.release();
176  if (meta.next_sibling()) {
177  request.reset(IFMapServerRequestClone(current));
178  }
179  list->push_back(current);
180  }
181  }
182  }
183  }
184 
185  if (idcount == 0 || !has_meta) {
186  return false;
187  }
188  return true;
189 }
190 
192  const xml_document &xdoc, RequestList *list) const {
193  xml_node current = xdoc.first_child();
194  while (current) {
195  bool add_change;
196  if (strcmp(NodeName(current), "updateResult") == 0 ||
197  strcmp(NodeName(current), "searchResult") == 0 ||
198  strcmp(NodeName(current), "deleteResult") == 0) {
199 
200  if (strcmp(NodeName(current), "deleteResult") == 0) {
201  add_change = false;
202  } else {
203  add_change = true;
204  }
205  xml_node result = current;
206  for (xml_node node = result.first_child(); node;
207  node = node.next_sibling()) {
208  ParseResultItem(node, add_change, list);
209  }
210  current = current.next_sibling();
211  } else {
212  current = current.first_child();
213  }
214  }
215 }
216 
217 // Called in the context of the ifmap client thread.
218 bool IFMapServerParser::Receive(DB *db, const char *data, size_t length,
219  uint64_t sequence_number) {
220  xml_document xdoc;
221  pugi::xml_parse_result result = xdoc.load_buffer(data, length);
222  if (!result) {
223  IFMAP_WARN(IFMapXmlLoadError, "Unable to load XML document", length);
224  return false;
225  }
226 
228  ParseResults(xdoc, &requests);
229 
230  while (!requests.empty()) {
231  unique_ptr<DBRequest> req(requests.front());
232  requests.pop_front();
233 
235  static_cast<IFMapTable::RequestKey *>(req->key.get());
236  key->id_seq_num = sequence_number;
237 
238  IFMapTable *table = IFMapTable::FindTable(db, key->id_type);
239  if (table != NULL) {
240  table->Enqueue(req.get());
241  } else {
242  IFMAP_TRACE(IFMapTblNotFoundTrace, "Cant find table", key->id_type);
243  }
244  }
245  return true;
246 }
bool Enqueue(DBRequest *req)
Definition: db_table.cc:221
Definition: db.h:25
void MetadataRegister(const std::string &metadata, MetadataParseFn parser)
void SetOrigin(struct DBRequest *result) const
bool Receive(DB *db, const char *data, size_t length, uint64_t sequence_number)
static IFMapServerParser * GetInstance(const std::string &module)
void ParseResults(const pugi::xml_document &xdoc, RequestList *list) const
std::map< std::string, IFMapServerParser * > ModuleMap
boost::function< bool(const pugi::xml_node &, std::unique_ptr< AutogenProperty > *) > MetadataParseFn
static void DeleteInstance(const std::string &module)
void MetadataClear(const std::string &module)
bool ParseMetadata(const pugi::xml_node &node, struct DBRequest *result) const
static ModuleMap module_map_
bool ParseResultItem(const pugi::xml_node &parent, bool add_change, RequestList *list) const
std::list< struct DBRequest * > RequestList
static IFMapTable * FindTable(DB *db, const std::string &element_type)
Definition: ifmap_table.cc:40
#define IFMAP_TRACE(obj,...)
Definition: ifmap_log.h:27
#define IFMAP_WARN(obj,...)
Definition: ifmap_log.h:73
static DBRequest * IFMapServerRequestClone(const DBRequest *src)
static const char * NodeName(const xml_node &node)
static string ParseIdentifier(const xml_node &node)
static void IdentifierNsTypeName(const string &id, string *id_type, string *id_name)
DBOperation oper
Definition: db_table.h:43
@ DB_ENTRY_DELETE
Definition: db_table.h:40
@ DB_ENTRY_ADD_CHANGE
Definition: db_table.h:39
std::unique_ptr< DBRequestKey > key
Definition: db_table.h:49
std::unique_ptr< DBRequestData > data
Definition: db_table.h:50
void set_origin(Origin in_origin)
Definition: ifmap_origin.h:22
std::unique_ptr< AutogenProperty > content