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