OpenSDN source code
ifmap_encoder.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 "ifmap/ifmap_encoder.h"
7 
8 #include <sstream>
9 #include "ifmap/ifmap_link.h"
10 #include "ifmap/ifmap_object.h"
11 #include "ifmap/ifmap_update.h"
12 
13 using namespace pugi;
14 using namespace std;
15 
16 IFMapMessage::IFMapMessage() : op_type_(NONE), node_count_(0),
17  objects_per_message_(kObjectsPerMessage) {
18  // init empty document
19  Open();
20 }
21 
23  xml_node iq = doc_.append_child("iq");
24  // set iq (type, from, to) attributes
25  iq.append_attribute("type") = "set";
26  iq.append_attribute("from") = "network-control@contrailsystems.com";
27  iq.append_attribute("to") = "";
28  config_ = iq.append_child("config");
29 }
30 
32  ostringstream oss;
33  doc_.save(oss);
34  str_ = oss.str();
35 }
36 
37 void IFMapMessage::SetReceiverInMsg(const std::string &cli_identifier) {
38  xml_node iq = doc_.child("iq");
39  assert(iq);
40  xml_attribute iqattr = iq.attribute("to");
41  assert(iqattr);
42  std::string str(cli_identifier);
43  str += "/config";
44  iqattr.set_value(str.c_str());
45 }
46 
49 }
50 
52  // update is either of type UPDATE OR DELETE
53  if (update->IsUpdate()) {
54  if (op_type_ != UPDATE) {
55  op_node_ = config_.append_child("update");
56  op_type_ = UPDATE;
57  }
58  if (update->data().type == IFMapObjectPtr::NODE) {
59  EncodeNode(update);
60  } else if (update->data().type == IFMapObjectPtr::LINK) {
61  EncodeLink(update);
62  } else {
63  assert(0);
64  }
65  } else {
66  if (op_type_ != DEL) {
67  op_node_ = config_.append_child("delete");
68  op_type_ = DEL;
69  }
70  if (update->data().type == IFMapObjectPtr::NODE) {
71  EncodeNode(update);
72  } else if (update->data().type == IFMapObjectPtr::LINK) {
73  EncodeLink(update);
74  } else {
75  assert(0);
76  }
77  }
78  node_count_++;
79 }
80 
82  IFMapNode *node = update->data().u.node;
83  if (update->IsUpdate()) {
84  node->EncodeNodeDetail(&op_node_);
85  } else {
86  node->EncodeNode(&op_node_);
87  }
88 }
89 
91  xml_node link_node = op_node_.append_child("link");
92 
93  const IFMapLink *link = update->data().u.link;
94 
95  IFMapNode::EncodeNode(link->left_id(), &link_node);
96  IFMapNode::EncodeNode(link->right_id(), &link_node);
97  link->EncodeLinkInfo(&link_node);
98 
99  node_count_++;
100 }
101 
103  return ((node_count_ >= objects_per_message_) ? true : false);
104 }
105 
107  return ((node_count_ == 0) ? true : false);
108 }
109 
110 //
111 // Reset the IFMapMessage to initial state so that it can be used to build
112 // the next config message.
113 //
114 // Using remove_child to remove the only child of the document is a better
115 // way to clear the document than using reset. The pugixml library allocates
116 // memory for a document in increments of 32KB pages and then manages smaller
117 // allocations (nodes or attributes) using these pages. Calling reset method
118 // on a document frees all the pages. In contrast, removing the only child
119 // node of the document returns the smaller allocations to the free pool of
120 // memory for the document, but doesn't free the pages themselves. This lets
121 // the library reuse the same memory when building the tree for each message.
122 //
124  doc_.remove_child("iq");
125  node_count_ = 0;
126  op_type_ = NONE;
127  Open();
128 }
void SetObjectsPerMessage(int num)
pugi::xml_document doc_
Definition: ifmap_encoder.h:41
std::string str_
Definition: ifmap_encoder.h:45
void EncodeNode(const IFMapUpdate *update)
void SetReceiverInMsg(const std::string &cli_identifier)
void EncodeUpdate(const IFMapUpdate *update)
void EncodeLink(const IFMapUpdate *update)
pugi::xml_node config_
Definition: ifmap_encoder.h:42
int objects_per_message_
Definition: ifmap_encoder.h:47
pugi::xml_node op_node_
Definition: ifmap_encoder.h:44
void EncodeNodeDetail(pugi::xml_node *parent) const
Definition: ifmap_node.cc:100
void EncodeNode(pugi::xml_node *parent) const
const IFMapObjectPtr & data() const
Definition: ifmap_update.h:97
bool IsUpdate() const
Definition: ifmap_update.h:66
IFMapNode * node
Definition: ifmap_update.h:42
ObjectType type
Definition: ifmap_update.h:39
IFMapLink * link
Definition: ifmap_update.h:43
union IFMapObjectPtr::@5 u