OpenSDN source code
ifmap_agent_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 
7 
8 #include <vector>
9 #include <pugixml/pugixml.hpp>
10 #include "base/logging.h"
11 #include "db/db_entry.h"
13 
14 using namespace std;
15 using namespace pugi;
16 
17 void IFMapAgentParser::NodeRegister(const string &node, NodeParseFn parser) {
18  pair<NodeParseMap::iterator, bool> result =
19  node_map_.insert(make_pair(node, parser));
20  assert(result.second);
21 }
22 
24  node_map_.clear();
25 }
26 
27 void IFMapAgentParser::NodeParse(xml_node &node, DBRequest::DBOperation oper, uint64_t seq) {
28 
29  const char *name = node.attribute("type").value();
30  int msg_type;
32  msg_type = UPDATE;
33  else
34  msg_type = DEL;
35 
36  IFMapTable *table;
37  table = IFMapTable::FindTable(db_, name);
38  if(!table) {
39  node_parse_errors_[msg_type]++;
40  return;
41  }
42 
43  // Locate the decode function using id_name
44  NodeParseMap::const_iterator loc = node_map_.find(name);
45  if (loc == node_map_.end()) {
46  node_parse_errors_[msg_type]++;
47  return;
48  }
49 
50  IFMapObject *obj;
52 
53  // Invoke the decode routine
54  req_key->id_type = name;
55  req_key->id_seq_num = seq;
56  obj = loc->second(node, db_, &req_key->id_name);
57  if (!obj) {
58  node_parse_errors_[msg_type]++;
59  delete req_key;
60  return;
61  }
62 
64  req_data->content.reset(obj);
65 
66  unique_ptr<DBRequest> request(new DBRequest);
67  request->oper = oper;
68  request->data.reset(req_data);
69  request->key.reset(req_key);
70  table->Enqueue(request.get());
71 }
72 
73 void IFMapAgentParser::LinkParse(xml_node &link, DBRequest::DBOperation oper, uint64_t seq) {
74 
75  xml_node first_node;
76  xml_node second_node;
77  xml_node name_node1;
78  xml_node name_node2;
79  const char *name1;
80  const char *name2;
81  IFMapTable *table;
82  IFMapAgentLinkTable *link_table;
83 
84  int msg_type;
86  msg_type = UPDATE;
87  else
88  msg_type = DEL;
89 
90  link_table = static_cast<IFMapAgentLinkTable *>(
91  db_->FindTable(IFMAP_AGENT_LINK_DB_NAME));
92 
93  assert(link_table);
94 
95  // Get both first and second node and its corresponding tables
96  first_node = link.first_child();
97  if (!first_node) {
98  link_parse_errors_[msg_type]++;
99  return;
100  }
101 
102  second_node = first_node.next_sibling();
103  if (!second_node) {
104  link_parse_errors_[msg_type]++;
105  return;
106  }
107 
108  name1 = first_node.attribute("type").value();
109  table = IFMapTable::FindTable(db_, name1);
110  if(!table) {
111  link_parse_errors_[msg_type]++;
112  return;
113  }
114 
115  name2 = second_node.attribute("type").value();
116  table = IFMapTable::FindTable(db_, name2);
117  if(!table) {
118  link_parse_errors_[msg_type]++;
119  return;
120  }
121 
122  // Get id_name of both the nodes
123  name_node1 = first_node.first_child();
124  if (!name_node1) {
125  link_parse_errors_[msg_type]++;
126  return;
127  }
128 
129  if (strcmp(name_node1.name(), "name") != 0) {
130  link_parse_errors_[msg_type]++;
131  return;
132  }
133 
134  name_node2 = second_node.first_child();
135  if (!name_node2) {
136  link_parse_errors_[msg_type]++;
137  return;
138  }
139 
140  if (strcmp(name_node2.name(), "name") != 0) {
141  link_parse_errors_[msg_type]++;
142  return;
143  }
144 
145  // Create both the request keys
146  unique_ptr <IFMapAgentLinkTable::RequestKey> req_key (new IFMapAgentLinkTable::RequestKey);
147  req_key->left_key.id_name = name_node1.child_value();
148  req_key->left_key.id_type = name1;
149  req_key->left_key.id_seq_num = seq;
150 
151  req_key->right_key.id_name = name_node2.child_value();
152  req_key->right_key.id_type = name2;
153  req_key->right_key.id_seq_num = seq;
154 
155  xml_node metadata = link.child("metadata");
156  if (metadata) {
157  req_key->metadata = metadata.attribute("type").value();
158  }
159 
160  unique_ptr <DBRequest> req (new DBRequest);
161  req->oper = oper;
162  req->key = std::move(req_key);
163 
164  link_table->Enqueue(req.get());
165 }
166 
167 void IFMapAgentParser::ConfigParse(const xml_node config, const uint64_t seq) {
168 
170 
171  for (xml_node node = config.first_child(); node;
172  node = node.next_sibling()) {
173 
174  int msg_type;
175  if (strcmp(node.name(), "update") == 0) {
177  msg_type = UPDATE;
178  } else if (strcmp(node.name(), "delete") == 0) {
180  msg_type = DEL;
181  } else {
182  continue;
183  }
184 
185  for(xml_node chld = node.first_child(); chld; chld = chld.next_sibling()) {
186 
187  // Handle the links between the nodes
188  if (strcmp(chld.name(), "link") == 0) {
189  links_processed_[msg_type]++;
190  LinkParse(chld, oper, seq);
191  continue;
192  }
193 
194  if (strcmp(chld.name(), "node") == 0) {
195  nodes_processed_[msg_type]++;
196  NodeParse(chld, oper, seq);
197  }
198  }
199  }
200 }
bool Enqueue(DBRequest *req)
Definition: db_table.cc:221
boost::function< IFMapObject *(const pugi::xml_node, DB *, std::string *id_name) > NodeParseFn
void NodeParse(pugi::xml_node &node, DBRequest::DBOperation oper, uint64_t seq)
void LinkParse(pugi::xml_node &node, DBRequest::DBOperation oper, uint64_t seq)
void ConfigParse(const pugi::xml_node config, uint64_t seq)
void NodeRegister(const std::string &node, NodeParseFn parser)
static IFMapTable * FindTable(DB *db, const std::string &element_type)
Definition: ifmap_table.cc:40
#define IFMAP_AGENT_LINK_DB_NAME
@ DB_ENTRY_DELETE
Definition: db_table.h:40
@ DB_ENTRY_ADD_CHANGE
Definition: db_table.h:39
std::unique_ptr< IFMapObject > content