OpenSDN source code
ifmap_agent_sandesh.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 <sandesh/request_pipeline.h>
7 
8 #include <boost/bind/bind.hpp>
9 #include <boost/assign/list_of.hpp>
10 #include <boost/uuid/uuid_io.hpp>
11 #include <boost/algorithm/string/trim.hpp>
12 
13 #include "base/logging.h"
14 #include "db/db.h"
15 #include "db/db_table_partition.h"
16 #include "base/bitset.h"
17 
18 #include <ifmap/ifmap_update.h>
19 #include <ifmap/ifmap_table.h>
21 #include <ifmap/ifmap_node.h>
22 #include <ifmap/ifmap_agent_types.h>
24 #include <pugixml/pugixml.hpp>
25 
26 using namespace boost::assign;
27 using namespace std;
28 using namespace boost::placeholders;
29 
31 public:
32  static DB *db_;
35  vector<string> send_buffer;
36  };
37 
39  return static_cast<RequestPipeline::InstData *>(new ShowData);
40  }
41 
42  void MakeNode(const string &name_sub_string,
43  const string &link_type_sub_string,
44  const string &link_node_sub_string,
45  string &dst, DBEntryBase *src);
46 
47  bool BufferStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps,
48  int stage, int instNum, RequestPipeline::InstData *data);
49  bool SendStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps,
50  int stage, int instNum, RequestPipeline::InstData *data);
51  void TableToBuffer(const ShowIFMapAgentReq *request, IFMapTable *table,
52  ShowData *show_data);
53 
54  bool BufferAllTables(const RequestPipeline::PipeSpec ps,
56  bool BufferSomeTables(const RequestPipeline::PipeSpec ps,
58 };
59 
62 
63 static inline void to_uuid(uint64_t ms_long, uint64_t ls_long,
64  boost::uuids::uuid &u) {
65  for (int i = 0; i < 8; i++) {
66  u.data[7 - i] = ms_long & 0xFF;
67  ms_long = ms_long >> 8;
68  }
69 
70  for (int i = 0; i < 8; i++) {
71  u.data[15 - i] = ls_long & 0xFF;
72  ls_long = ls_long >> 8;
73  }
74 }
75 
76 
77 void xml_parse(pugi::xml_node &node, string &s, int n) {
78  int child;
79  pugi::xml_node chld;
80  pugi::xml_node fchld;
81  string t(n, '\t');
82  static uint64_t uuid_ms;
83  static uint64_t uuid_ls;
84  static int ls_set = 0;
85  static int ms_set = 0;
86 
87  switch(node.type()) {
88  case pugi::node_element:
89  child = 0;
90  for(chld = node.first_child(); chld; chld = chld.next_sibling()) {
91  child++;
92  fchld = chld;
93  }
94 
95  if (strlen(node.child_value()) == 0) {
96  if (!child) {
97  break;
98  }
99  if ((child == 1) && (fchld.type() == pugi::node_pcdata) && (strlen(fchld.child_value()) == 0)) {
100  break;
101  }
102  }
103 
104  if (strcmp(node.name(), "uuid-mslong") == 0) {
105  string value(node.child_value());
106  boost::trim(value);
107  uuid_ms = strtoull(value.c_str(), NULL, 10);
108  ms_set = 1;
109  }
110 
111  if (strcmp(node.name(), "uuid-lslong") == 0) {
112  string value(node.child_value());
113  boost::trim(value);
114  uuid_ls = strtoull(value.c_str(), NULL, 10);
115  ls_set = 1;
116  }
117 
118  if (strcmp(node.name(), "config") && strcmp(node.name(), "node")) {
119 
120  if (strlen(node.child_value())) {
121  s = s + t + node.name() + ":" + node.child_value() + "\n";
122  } else {
123  s = s + t + node.name() + "\n";
124  }
125  }
126 
127  if (ms_set && ls_set) {
129  to_uuid(uuid_ms, uuid_ls, u);
130  string tmp = UuidToString(u);
131  s = s + t + "Uuid : " + tmp + "\n";
132  ms_set = ls_set = 0;
133  }
134 
135  for (pugi::xml_attribute_iterator ait = node.attributes_begin(); ait != node.attributes_end(); ++ait) {
136  s = s + t + ait->name() + ":" + ait->value() + "\n";
137  }
138 
139  for(pugi::xml_node chld = node.first_child(); chld; chld = chld.next_sibling()) {
140  xml_parse(chld, s, n + 1);
141  }
142  break;
143 
144  case pugi::node_pcdata:
145  s = s + node.child_value() + "\n";
146  break;
147 
148  default:
149  break;
150  }
151 }
152 void ShowIFMapAgentTable::MakeNode(const string &name_sub_string,
153  const string &link_node_sub_string,
154  const string &link_type_sub_string,
155  string &dst, DBEntryBase *src) {
156  IFMapNode *node = static_cast<IFMapNode *>(src);
157  pugi::xml_document doc;
158  pugi::xml_node config;
159 
160  if (name_sub_string.empty() == false) {
161  if (node->name().find(name_sub_string) == string::npos)
162  return;
163  }
164 
165  dst += "\n";
166  if (!node->IsDeleted()) {
167  config = doc.append_child("config");
168  } else {
169  config = doc.append_child("Delete Marked config");
170  }
171 
172  node->EncodeNodeDetail(&config);
173  xml_parse(config, dst, 1);
174 
175  if (node->IsDeleted()) {
176  return;
177  }
178 
179  // Display its adjacencies
180  dst = dst + "Adjacencies:\n";
181  IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
183  iter = node->begin(table->GetGraph());
184  iter != node->end(table->GetGraph()); ++iter) {
185  IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
186 
187  if (link_type_sub_string.empty() == false) {
188  // Check if filter passes the table-name for the adjacency
189  if (strstr(adj_node->table()->Typename(),
190  link_type_sub_string.c_str()) == NULL)
191  continue;
192  }
193 
194  if (link_node_sub_string.empty() == false) {
195  // Check if filter passes the node-name for the adjacency
196  if (adj_node->name().find(link_node_sub_string) == string::npos)
197  continue;
198  }
199 
200  dst = dst + "\t" + adj_node->table()->Typename() + " " +
201  adj_node->name() +"\n";
202  }
203 }
204 
205 void ShowIFMapAgentTable::TableToBuffer(const ShowIFMapAgentReq *request,
206  IFMapTable *table,
207  ShowData *show_data) {
208  for (int i = 0; i < IFMapTable::kPartitionCount; i++) {
209  DBTablePartBase *partition = table->GetTablePartition(i);
210  DBEntryBase *src = partition->GetFirst();
211  while (src) {
212  string dst;
213  MakeNode(request->get_node_sub_string(),
214  request->get_link_node_sub_string(),
215  request->get_link_type_sub_string(),
216  dst, src);
217  if (dst.empty() == false)
218  show_data->send_buffer.push_back(dst);
219  src = partition->GetNext(src);
220  }
221  }
222 }
223 
226  const ShowIFMapAgentReq *request =
227  static_cast<const ShowIFMapAgentReq *>(ps.snhRequest_.get());
228 
229  IFMapTable *table = IFMapTable::FindTable(db_, request->get_table_name());
230  if (table == NULL) {
231  LOG(DEBUG, "Invalid table name: " << request->get_table_name());
232  return true;
233  }
234 
235  if (table->name().find("__ifmap__.") != 0) {
236  LOG(DEBUG, "Invalid table name: " << request->get_table_name());
237  return true;
238  }
239 
240  ShowData *show_data = static_cast<ShowData *>(data);
241  TableToBuffer(request, table, show_data);
242  return true;
243 }
244 
247 
248  const ShowIFMapAgentReq *request =
249  static_cast<const ShowIFMapAgentReq *>(ps.snhRequest_.get());
250  for (DB::iterator iter = db_->lower_bound("__ifmap__.");
251  iter != db_->end(); ++iter) {
252  if (iter->first.find("__ifmap__.") != 0) {
253  break;
254  }
255  IFMapTable *table = static_cast<IFMapTable *>(iter->second);
256  ShowData *show_data = static_cast<ShowData *>(data);
257  TableToBuffer(request, table, show_data);
258  }
259 
260  return true;
261 }
262 
264  const RequestPipeline::PipeSpec ps,
265  int stage, int instNum,
267 
268  const ShowIFMapAgentReq *request =
269  static_cast<const ShowIFMapAgentReq *>(ps.snhRequest_.get());
270  // If table name has not been passed, print all tables
271  if (request->get_table_name().length()) {
272  return BufferSomeTables(ps, data);
273  } else {
274  return BufferAllTables(ps, data);
275  }
276 }
277 
279  const RequestPipeline::PipeSpec ps,
280  int stage, int instNum,
282  const RequestPipeline::StageData *stage_data = ps.GetStageData(0);
283  const ShowIFMapAgentTable::ShowData &show_data =
284  static_cast<const ShowIFMapAgentTable::ShowData &> (stage_data->at(0));
285  const ShowIFMapAgentReq *request =
286  static_cast<const ShowIFMapAgentReq *>(ps.snhRequest_.get());
287  ShowIFMapAgentResp *response = new ShowIFMapAgentResp();
288  response->set_table_data(show_data.send_buffer);
289  response->set_context(request->context());
290  response->set_more(false);
291  response->Response();
292  return true;
293 }
294 
295 void ShowIFMapAgentReq::HandleRequest() const {
296  ShowIFMapAgentTable show_table;
297 
300 
301  // 2 stages - first: gather/read, second: send
302 
303  s0.taskId_ = scheduler->GetTaskId("db::DBTable");
304  s0.allocFn_ = boost::bind(&ShowIFMapAgentTable::AllocData, &show_table, _1);
305  s0.cbFn_ = boost::bind(&ShowIFMapAgentTable::BufferStage, &show_table,
306  _1, _2, _3, _4, _5);
307  s0.instances_.push_back(0);
308 
309  // Agent ifmap show command task
310  s1.taskId_ = scheduler->GetTaskId("agent_ifmap::ShowCommand");
311  s1.allocFn_ = boost::bind(&ShowIFMapAgentTable::AllocData, &show_table, _1);
312  s1.cbFn_ = boost::bind(&ShowIFMapAgentTable::SendStage, &show_table,
313  _1, _2, _3, _4, _5);
314  s1.instances_.push_back(0);
315 
316  RequestPipeline::PipeSpec ps(this);
317  ps.stages_= list_of(s0)(s1)
318  .convert_to_container<vector<RequestPipeline::StageSpec> >();
319  RequestPipeline rp(ps);
320 }
321 
322 void ShowIFMapAgentDefLinkReq::HandleRequest() const {
323  ShowIFMapAgentDefLinkResp *resp;
324  resp = new ShowIFMapAgentDefLinkResp();
325 
326  //Get link table
327  IFMapAgentLinkTable *link_table = static_cast<IFMapAgentLinkTable *>(
329 
330  IFMapAgentLinkTable::LinkDefMap::const_iterator dlist_it;
331  std::list<IFMapAgentLinkTable::DeferredNode> *ent;
332  std::list<IFMapAgentLinkTable::DeferredNode>::iterator it;
333 
334  //Get linktables's deferred list
335  const IFMapAgentLinkTable::LinkDefMap &def_list = link_table->GetLinkDefMap();
336 
337  //Get Sandesh response's output list
338  std::vector<IFMapAgentDefLink> &list =
339  const_cast<std::vector<IFMapAgentDefLink>&>(resp->get_def_list());
340 
341  //Iterate left node list
342  for(dlist_it = def_list.begin(); dlist_it != def_list.end(); dlist_it++) {
343  const IFMapTable::RequestKey &temp = dlist_it->first;
344  ent = dlist_it->second;
345 
346  //Iterate the right nodes corresponding to above left node
347  for(it = ent->begin(); it != ent->end(); it++) {
348  IFMapAgentDefLink data;
349  data.set_seq_num((*it).node_key.id_seq_num);
350  data.set_left_node(temp.id_type + ":" +
351  temp.id_name);
352  data.set_metadata((*it).link_metadata);
353  data.set_right_node((*it).node_key.id_type + ":" +
354  (*it).node_key.id_name);
355  list.push_back(data);
356  }
357  }
358  resp->set_context(context());
359  resp->Response();
360  return;
361 }
362 
363 void ShowIFMapAgentStatsReq::HandleRequest() const {
364 
366 
367  if (!parser)
368  return;
369 
370  ShowIFMapAgentStatsResp *resp;
371  resp = new ShowIFMapAgentStatsResp();
372 
373  resp->set_node_updates_processed(parser->node_updates());
374  resp->set_node_deletes_processed(parser->node_deletes());
375  resp->set_link_updates_processed(parser->link_updates());
376  resp->set_link_deletes_processed(parser->link_deletes());
377  resp->set_node_update_parse_errors(parser->node_update_parse_errors());
378  resp->set_link_update_parse_errors(parser->link_update_parse_errors());
379  resp->set_node_delete_parse_errors(parser->node_delete_parse_errors());
380  resp->set_link_delete_parse_errors(parser->link_delete_parse_errors());
381 
382  resp->set_context(context());
383  resp->Response();
384  return;
385 }
386 
390 }
bool IsDeleted() const
Definition: db_entry.h:49
adjacency_iterator end(DBGraph *graph)
adjacency_iterator begin(DBGraph *graph)
const std::string & name() const
Definition: db_table.h:111
virtual DBEntryBase * GetNext(const DBEntryBase *)=0
virtual DBEntryBase * GetFirst()=0
virtual DBTablePartBase * GetTablePartition(const DBRequestKey *key)
Definition: db_table.cc:463
Definition: db.h:25
TableMap::iterator iterator
Definition: db.h:30
DBTableBase * FindTable(const std::string &name)
Definition: db.cc:68
std::map< IFMapTable::RequestKey, std::list< DeferredNode > *, comp > LinkDefMap
const LinkDefMap & GetLinkDefMap() const
uint64_t node_delete_parse_errors()
uint64_t link_update_parse_errors()
uint64_t node_update_parse_errors()
uint64_t link_delete_parse_errors()
const DBGraph * GetGraph() const
IFMapTable * table()
Definition: ifmap_node.h:30
const std::string & name() const
Definition: ifmap_node.h:49
void EncodeNodeDetail(pugi::xml_node *parent) const
Definition: ifmap_node.cc:100
static IFMapTable * FindTable(DB *db, const std::string &element_type)
Definition: ifmap_table.cc:40
virtual const char * Typename() const =0
static const int kPartitionCount
Definition: ifmap_table.h:17
boost::ptr_vector< InstData > StageData
bool SendStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps, int stage, int instNum, RequestPipeline::InstData *data)
bool BufferAllTables(const RequestPipeline::PipeSpec ps, RequestPipeline::InstData *data)
void MakeNode(const string &name_sub_string, const string &link_type_sub_string, const string &link_node_sub_string, string &dst, DBEntryBase *src)
RequestPipeline::InstData * AllocData(int stage)
bool BufferSomeTables(const RequestPipeline::PipeSpec ps, RequestPipeline::InstData *data)
static IFMapAgentParser * parser_
bool BufferStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps, int stage, int instNum, RequestPipeline::InstData *data)
void TableToBuffer(const ShowIFMapAgentReq *request, IFMapTable *table, ShowData *show_data)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
int GetTaskId(const std::string &name)
Definition: task.cc:884
static TaskScheduler * GetInstance()
Definition: task.cc:574
void IFMapAgentSandeshInit(DB *db, IFMapAgentParser *parser)
void xml_parse(pugi::xml_node &node, string &s, int n)
static void to_uuid(uint64_t ms_long, uint64_t ls_long, boost::uuids::uuid &u)
#define IFMAP_AGENT_LINK_DB_NAME
#define LOG(_Level, _Msg)
Definition: logging.h:35
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
boost::shared_ptr< const SandeshRequest > snhRequest_
const StageData * GetStageData(int stage) const
std::vector< int > instances_
boost::uuids::uuid uuid