OpenSDN source code
controller_ifmap.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>
7 #include <string.h>
8 #include <pugixml/pugixml.hpp>
9 #include <xml/xml_base.h>
10 #include <xml/xml_pugi.h>
11 
12 #include <base/util.h>
13 #include <base/logging.h>
14 #include <xmpp/xmpp_channel.h>
16 
17 #include <cmn/agent_cmn.h>
18 #include <cmn/agent_stats.h>
19 
23 #include <controller/controller_types.h>
24 
25 
27 
29  uint8_t cnt) : channel_(channel),
30  xs_idx_(cnt),
31  agent_(agent) {
34  this, _1));
35  config_cleanup_timer_.reset(new ConfigCleanupTimer(agent));
36  end_of_config_timer_.reset(new EndOfConfigTimer(agent, this));
37 }
38 
42  config_cleanup_timer_.reset();
43  end_of_config_timer_.reset();
44 }
45 
47 
48  seq_number_++;
49 
50  if (seq_number_ == 0) {
51  return ++seq_number_;
52  }
53 
54  CONTROLLER_TRACE(IFMapSeqTrace, GetSeqNumber(), seq_number_, "New Config Seq Num");
55  return seq_number_;
56 }
57 
59  return config_cleanup_timer_.get();
60 }
61 
63  return end_of_config_timer_.get();
64 }
65 
66 bool AgentIfMapXmppChannel::SendUpdate(const std::string &msg) {
67  if (!channel_) return false;
68  return channel_->Send((const uint8_t *)msg.data(), msg.size(), xmps::CONFIG,
69  boost::bind(&AgentIfMapXmppChannel::WriteReadyCb, this, _1));
70 }
71 
73  if (msg && msg->type == XmppStanza::IQ_STANZA) {
74  std::unique_ptr<XmlBase> impl(XmppXmlImplFactory::Instance()->GetXmlImpl());
75  XmlPugi *pugi = reinterpret_cast<XmlPugi *>(impl.get());
76  XmlPugi *msg_pugi = reinterpret_cast<XmlPugi *>(msg->dom.get());
77  pugi->LoadXmlDoc(msg_pugi->doc());
78  boost::shared_ptr<ControllerXmppData> data(new ControllerXmppData(xmps::CONFIG,
80  xs_idx_,
81  std::move(impl),
82  true));
83  agent_->controller()->Enqueue(data);
85  }
86 }
87 
88 void AgentIfMapXmppChannel::ReceiveConfigMessage(std::unique_ptr<XmlBase> impl) {
89 
91  LOG(WARN, "IFMap config on non primary channel");
92  return;
93  }
94 
95  XmlPugi *pugi = reinterpret_cast<XmlPugi *>(impl.get());
96  pugi::xml_node node = pugi->FindNode("config");
98  assert(parser);
99  parser->ConfigParse(node, seq_number_);
100 }
101 
103  if (agent_->stats())
105  ReceiveUpdate(msg);
106 }
107 
108 std::string AgentIfMapXmppChannel::ToString() const {
109  return channel_->ToString();
110 }
111 
112 void AgentIfMapXmppChannel::WriteReadyCb(const boost::system::error_code &ec) {
113 }
114 
116  //First start for end of config identification.
118 }
119 
121  //First start for end of config identification.
123 }
124 
127 }
128 
131 }
132 
134  EndOfConfigDataPtr data(new EndOfConfigData(this));
136  boost::static_pointer_cast<ControllerWorkQueueData>(data);
137  agent_->controller()->Enqueue(base_data);
138 }
139 
144 }
145 
147  ControllerWorkQueueData(), channel_(ch) {
148 }
149 
150 // Get active xmpp-peer
152  int active_index = agent->ifmap_active_xmpp_server_index();
153  if (active_index == -1) {
154  return NULL;
155  }
156 
157  AgentXmppChannel *peer = agent->controller_xmpp_channel(active_index);
158  if (peer == NULL)
159  return NULL;
160 
161  if (AgentXmppChannel::IsBgpPeerActive(agent, peer) == false) {
162  return NULL;
163  }
164 
165  return peer;
166 }
167 
168 
170 (Agent *agent, struct AgentIfMapVmExport::VmExportInfo *info) {
171  int active_index = agent->ifmap_active_xmpp_server_index();
172  AgentIfMapXmppChannel *ifmap = agent->ifmap_xmpp_channel(active_index);
173  // Peer is valid, but channel may be down
174  if (ifmap == NULL)
175  return NULL;
176 
177  if (info && (info->seq_number_ != ifmap->GetSeqNumber())) {
178  return NULL;
179  }
180 
181  return ifmap;
182 }
183 
185  VmMap::iterator vm_it = vm_map_.find(entry->vm_uuid_);
186  if (vm_map_.end() == vm_it) {
187  return;
188  }
189 
190  struct VmExportInfo *info = vm_it->second;
191  //If delete already processed, neglect the delete
192  if (!info)
193  return;
194 
195  // Find VMI-UUID in the list of VMI
196  UuidList::const_iterator vmi_it = std::find(info->vmi_list_.begin(),
197  info->vmi_list_.end(),
198  entry->vmi_uuid_);
199  if (vmi_it == info->vmi_list_.end())
200  return;
201 
202  CONTROLLER_TRACE(IFMapVmExportTrace, UuidToString(entry->vm_uuid_),
203  UuidToString(entry->vmi_uuid_), "Delete");
204  info->vmi_list_.remove(entry->vmi_uuid_);
205  // Stop here if VM has more interfaces
206  if (info->vmi_list_.size() != 0) {
207  return;
208  }
209 
210  // All interfaces deleted. Remove the VM entry
211  vm_map_.erase(entry->vm_uuid_);
212 
213  // Unsubscribe from config if we have active channel
215  if (peer == NULL) {
216  delete info;
217  CONTROLLER_TRACE(IFMapVmExportTrace, UuidToString(entry->vm_uuid_), "",
218  "Peer NULL skipped Unsubscribe ");
219  return;
220  }
221 
223  delete info;
224  if (ifmap == NULL) {
225  CONTROLLER_TRACE(IFMapVmExportTrace, UuidToString(entry->vm_uuid_), "",
226  "Channel NULL skipped Unsubscribe ");
227  return;
228  }
229 
230  CONTROLLER_TRACE(IFMapVmExportTrace, UuidToString(entry->vm_uuid_), "",
231  "Unsubscribe ");
233  false);
234  return;
235 }
236 
238  struct VmExportInfo *info = NULL;
239  VmMap::iterator vm_it = vm_map_.find(entry->vm_uuid_);
240  if (vm_map_.end() != vm_it) {
241  info = vm_it->second;
242  } else {
243  //If first VMI create the data
244  info = new VmExportInfo(0);
245  vm_map_[entry->vm_uuid_] = info;
246  }
247 
248  //If VMI is not found, insert in the list
249  UuidList::const_iterator vmi_it = std::find(info->vmi_list_.begin(),
250  info->vmi_list_.end(),
251  entry->vmi_uuid_);
252  if (vmi_it == info->vmi_list_.end()) {
253  CONTROLLER_TRACE(IFMapVmExportTrace, UuidToString(entry->vm_uuid_),
254  UuidToString(entry->vmi_uuid_), "Add");
255  info->vmi_list_.push_back(entry->vmi_uuid_);
256  }
257 
258  // Ensure that peer exists and is in active state
260  if (peer == NULL) {
261  return;
262  }
263 
264  // Ensure that channel is valid
266  if (ifmap == NULL) {
267  return;
268  }
269 
270  //We have already sent the subscribe
271  if (info->seq_number_ == ifmap->GetSeqNumber()) {
272  return;
273  }
274 
275  CONTROLLER_TRACE(IFMapVmExportTrace, UuidToString(entry->vm_uuid_),
276  UuidToString(entry->vmi_uuid_), "Subscribe");
278 
279  //Update the sequence number
280  info->seq_number_ = ifmap->GetSeqNumber();
281 }
282 
284  if (entry->del_) {
285  VmiDelete(entry);
286  return;
287  }
288 
289  VmiAdd(entry);
290  return;
291 }
292 
294  VmMap::iterator vm_it;
295  AgentIfMapXmppChannel *ifmap = NULL;
296  struct VmExportInfo *info = NULL;
297  Agent *agent = peer->agent();
298 
300  return;
301  }
302 
304  if (!ifmap) {
305  return;
306  }
307 
308  //We have all the required data. Send config subscribe for all VM's
309  AgentIfMapVmExport *agent_ifmap_vm_export = peer->agent()->controller()->
310  agent_ifmap_vm_export();
311  vm_it = agent_ifmap_vm_export->vm_map_.begin();
312  for(; vm_it != agent_ifmap_vm_export->vm_map_.end(); vm_it++) {
313  info = vm_it->second;
314  if (info->seq_number_ == ifmap->GetSeqNumber()) {
315  continue;
316  }
317 
318  std::stringstream vmid;
319  vmid << vm_it->first;
320  CONTROLLER_TRACE(IFMapVmExportTrace, vmid.str(), "",
321  "Subscribe");
323  vm_it->first, true);
324 
325  //Update the sequence number so that we dont send duplicate
326  //subscribe
327  info->seq_number_ = ifmap->GetSeqNumber();
328  }
329 
330  return;
331 }
332 
334  agent_(agent) {
335 }
336 
338  VmMap::iterator vm_it;
339  struct VmExportInfo *info = NULL;
340 
341  //Destroy the vm_map and vmi list
342  for(vm_it = vm_map_.begin(); vm_it != vm_map_.end(); vm_it++) {
343  info = vm_it->second;
344  delete info;
345  }
346 
347  vm_map_.clear();
348 }
void NotifyAll(AgentXmppChannel *peer)
void VmiAdd(const ControllerVmiSubscribeData *entry)
AgentIfMapVmExport(Agent *agent)
void VmiEvent(const ControllerVmiSubscribeData *entry)
void VmiDelete(const ControllerVmiSubscribeData *entry)
Agent * agent() const
static uint64_t GetSeqNumber()
boost::scoped_ptr< EndOfConfigTimer > end_of_config_timer_
static uint64_t NewSeqNumber()
virtual void ReceiveUpdate(const XmppStanza::XmppMessage *msg)
AgentIfMapXmppChannel(Agent *agent, XmppChannel *channel, uint8_t count)
void ReceiveInternal(const XmppStanza::XmppMessage *msg)
boost::shared_ptr< EndOfConfigData > EndOfConfigDataPtr
virtual void WriteReadyCb(const boost::system::error_code &ec)
virtual std::string ToString() const
boost::scoped_ptr< ConfigCleanupTimer > config_cleanup_timer_
ConfigCleanupTimer * config_cleanup_timer()
virtual bool SendUpdate(const std::string &msg)
EndOfConfigTimer * end_of_config_timer()
void ReceiveConfigMessage(std::unique_ptr< XmlBase > impl)
static uint64_t seq_number_
void incr_xmpp_config_in_msgs(uint8_t idx)
Definition: agent_stats.h:70
Agent * agent() const
static bool IsBgpPeerActive(const Agent *agent, AgentXmppChannel *peer)
static bool ControllerSendVmCfgSubscribe(AgentXmppChannel *peer, const boost::uuids::uuid &vm_id, bool subscribe)
Definition: agent.h:361
AgentXmppChannel * controller_xmpp_channel(uint8_t idx) const
Definition: agent.h:812
VNController * controller() const
Definition: agent.cc:985
IFMapAgentParser * ifmap_parser() const
Definition: agent.h:1175
AgentStats * stats() const
Definition: agent.cc:885
AgentIfMapXmppChannel * ifmap_xmpp_channel(uint8_t idx) const
Definition: agent.h:795
const int8_t & ifmap_active_xmpp_server_index() const
Definition: agent.h:783
boost::uuids::uuid vm_uuid_
boost::uuids::uuid vmi_uuid_
EndOfConfigData(AgentIfMapXmppChannel *ch)
void ConfigParse(const pugi::xml_node config, uint64_t seq)
boost::shared_ptr< ControllerWorkQueueData > ControllerWorkQueueDataType
void StartEndOfRibTxTimer()
void Enqueue(ControllerWorkQueueDataType data)
const pugi::xml_document & doc()
Definition: xml_pugi.h:69
virtual void RegisterReceive(xmps::PeerId, ReceiveCb)=0
virtual void UnRegisterReceive(xmps::PeerId)=0
virtual const std::string & ToString() const =0
virtual void UnRegisterWriteReady(xmps::PeerId id)=0
virtual bool Send(const uint8_t *, size_t, xmps::PeerId, SendReadyCb)=0
XmppMessageType type
Definition: xmpp_proto.h:58
std::unique_ptr< XmlBase > dom
Definition: xmpp_proto.h:63
static AgentIfMapXmppChannel * GetActiveChannel(Agent *agent, struct AgentIfMapVmExport::VmExportInfo *info)
static AgentXmppChannel * GetActivePeer(Agent *agent)
#define CONTROLLER_TRACE(obj,...)
#define LOG(_Level, _Msg)
Definition: logging.h:35
@ CONFIG
Definition: xmpp_channel.h:24
@ UNKNOWN
Definition: xmpp_channel.h:17
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
virtual void Start(AgentXmppChannel *agent_xmpp_channel)
virtual void Start(AgentXmppChannel *agent_xmpp_channel)
uint64_t end_of_config_processed_time_
uint64_t last_config_receive_time_
static XmppXmlImplFactory * Instance()
Definition: xml_base.cc:19
static uint64_t UTCTimestampUsec()
Definition: time_util.h:14