OpenSDN source code
physical_device_vn.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <boost/uuid/uuid_io.hpp>
7 #include <vnc_cfg_types.h>
8 #include <cmn/agent_cmn.h>
9 
10 #include <ifmap/ifmap_node.h>
11 #include <cfg/cfg_init.h>
12 #include <oper/agent_sandesh.h>
13 #include <oper/vn.h>
15 #include <oper/physical_device.h>
17 #include <oper/config_manager.h>
18 
19 using std::string;
20 using boost::assign::map_list_of;
21 using boost::assign::list_of;
22 
24 // PhysicalDeviceVn routines
26 bool PhysicalDeviceVn::IsLess(const DBEntry &rhs) const {
27  const PhysicalDeviceVn &a =
28  static_cast<const PhysicalDeviceVn &>(rhs);
29  if (device_uuid_ != a.device_uuid_) {
30  return (device_uuid_ < a.device_uuid_);
31  }
32  return (vn_uuid_ < a.vn_uuid_);
33 }
34 
37 }
38 
41  return DBEntryBase::KeyPtr(key);
42 }
43 
45  const PhysicalDeviceVnKey *key =
46  static_cast<const PhysicalDeviceVnKey *>(k);
47 
49  vn_uuid_ = key->vn_uuid_;
50 }
51 
53  const PhysicalDeviceVnData *data) {
54  bool ret = false;
55 
56  PhysicalDevice *dev =
58  if (dev != device_.get()) {
59  device_.reset(dev);
60  ret = true;
61  }
62 
63  if (dev && (dev->ip() != tor_ip_)) {
64  tor_ip_ = dev->ip();
65  ret = true;
66  }
67 
68  if (dev && (dev->name() != device_display_name_)) {
69  device_display_name_ = dev->name();
70  ret = true;
71  }
72 
73  VnEntry *vn = table->agent()->vn_table()->Find(vn_uuid_);
74  if (vn != vn_.get()) {
75  vn_.reset(vn);
76  ret = true;
77  }
78 
79  int vxlan_id = vn ? vn->GetVxLanId() : 0;
80  if (vxlan_id != vxlan_id_) {
82  ret = true;
83  }
84 
85  return ret;
86 }
87 
89 // PhysicalDeviceVnTable routines
91 PhysicalDeviceVnTable::PhysicalDeviceVnTable(DB *db, const std::string &name) :
92  AgentDBTable(db, name) {
95  this, _1, _2),
97  this, _1, _2));
98 }
99 
100 std::unique_ptr<DBEntry> PhysicalDeviceVnTable::AllocEntry(const DBRequestKey *k)
101  const {
102  const PhysicalDeviceVnKey *key =
103  static_cast<const PhysicalDeviceVnKey *>(k);
104 
106  key->vn_uuid_);
107  return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(entry));
108 }
109 
111  PhysicalDeviceVnKey *key =
112  static_cast<PhysicalDeviceVnKey *>(req->key.get());
113  PhysicalDeviceVnData *data =
114  static_cast<PhysicalDeviceVnData *>(req->data.get());
115 
117  key->vn_uuid_);
118  entry->Copy(this, data);
119  entry->SendObjectLog(AgentLogEvent::ADD);
120  return entry;
121 }
122 
124  PhysicalDeviceVn *entry = static_cast<PhysicalDeviceVn *>(e);
125  PhysicalDeviceVnData *data =
126  static_cast<PhysicalDeviceVnData *>(req->data.get());
127  bool ret = entry->Copy(this, data);
128  return ret;
129 }
130 
132  PhysicalDeviceVn *entry = static_cast<PhysicalDeviceVn *>(e);
133  PhysicalDeviceVnData *data =
134  static_cast<PhysicalDeviceVnData *>(req->data.get());
135  bool ret = entry->Copy(this, data);
136  return ret;
137 }
138 
142 }
143 
145  PhysicalDeviceVn *entry = static_cast<PhysicalDeviceVn *>(e);
146  entry->SendObjectLog(AgentLogEvent::DEL);
147  return true;
148 }
149 
151  const std::string &name) {
153  table->Init();
154  return table;
155 }
156 
158  DBEntryBase *entry) {
159  PhysicalDeviceVn *dev_vn = static_cast<PhysicalDeviceVn *>(entry);
160  DBEntryBase::KeyPtr db_key = dev_vn->GetDBRequestKey();
161  PhysicalDeviceVnKey *key =
162  static_cast<PhysicalDeviceVnKey *>(db_key.release());
163  key->sub_op_ = AgentKey::RESYNC;
164  DBRequest req;
166  req.key.reset(key);
167  req.data.reset(NULL);
168  Process(req);
169  return true;
170 }
171 
173  DBTableBase *part) {
174 }
175 
178 }
179 
181 // Vmi Config handling routines
184  const boost::uuids::uuid &vn) {
186  req.key.reset(new PhysicalDeviceVnKey(dev, vn));
187  req.data.reset(new PhysicalDeviceVnData());
188  Enqueue(&req);
189  return;
190 }
191 
193  const boost::uuids::uuid &dev,
194  const boost::uuids::uuid &vn) {
195  using boost::uuids::nil_uuid;
196 
197  // Sanity checks. Needed since VMInterface is not checking for nil_uuid
198  if (vmi == nil_uuid() || dev == nil_uuid() || vn == nil_uuid())
199  return false;
200 
201  config_tree_.insert(PhysicalDeviceVnToVmi(dev, vn, vmi));
203  return true;
204 }
205 
207  const boost::uuids::uuid &dev,
208  const boost::uuids::uuid &vn) {
209  using boost::uuids::nil_uuid;
210 
211  // Sanity checks. Needed since VMInterface is not checking for nil_uuid
212  if (vmi == nil_uuid() || dev == nil_uuid() || vn == nil_uuid())
213  return false;
214 
215  config_tree_.erase(PhysicalDeviceVnToVmi(dev, vn, vmi));
216  // Dont delete physical-device-vn entry if there are more entries in
217  // config-tree with given dev and vn
218  ConfigTree::iterator it =
219  config_tree_.upper_bound(PhysicalDeviceVnToVmi(dev, vn, nil_uuid()));
220  bool del_entry = false;
221  if (it == config_tree_.end())
222  del_entry = true;
223  else if (it->dev_ != dev)
224  del_entry = true;
225  else if (it->vn_ != vn)
226  del_entry = true;
227 
228  if (del_entry) {
231  req.key.reset(new PhysicalDeviceVnKey(dev, vn));
232  Enqueue(&req);
233  }
234 
235  return del_entry;
236 }
237 
239  const boost::uuids::uuid &u) {
240  return false;
241 }
242 
244 // Sandesh routines
247  public:
249  const std::string &dev,
250  const std::string &vn)
251  : AgentSandesh(context, ""), dev_str_(dev), vn_str_(vn) {
252  dev_uuid_ = StringToUuid(dev);
253  vn_uuid_ = StringToUuid(vn);
254  }
255 
256  virtual bool Filter(const DBEntryBase *entry) {
257  const PhysicalDeviceVn *dev_vn =
258  static_cast<const PhysicalDeviceVn *>(entry);
259  if (dev_str_.empty() == false) {
260  if (dev_vn->device_uuid() != dev_uuid_)
261  return false;
262  }
263 
264  if (vn_str_.empty() == false) {
265  if (dev_vn->vn_uuid() != vn_uuid_)
266  return false;
267  }
268 
269  return true;
270  }
271 
272  virtual bool FilterToArgs(AgentSandeshArguments *args) {
273  args->Add("device", dev_str_);
274  args->Add("vn", vn_str_);
275  return true;
276  }
277 
278  private:
280  Agent *agent = Agent::GetInstance();
281  return static_cast<DBTable *>
282  (agent->physical_device_vn_table());
283  }
284  void Alloc() {
285  resp_ = new SandeshPhysicalDeviceVnListResp();
286  }
287 
288  std::string dev_str_;
290  std::string vn_str_;
292 };
293 
295  SandeshPhysicalDeviceVn *data) {
296  data->set_device_uuid(UuidToString(entry->device_uuid()));
297  if (entry->device()) {
298  data->set_device(entry->device()->name());
299  } else {
300  data->set_device("INVALID");
301  }
302  data->set_vn_uuid(UuidToString(entry->vn_uuid()));
303  if (entry->vn()) {
304  data->set_vn(entry->vn()->GetName());
305  } else {
306  data->set_vn("INVALID");
307  }
308  data->set_vxlan_id(entry->vxlan_id());
309 }
310 
311 bool PhysicalDeviceVn::DBEntrySandesh(Sandesh *resp, std::string &name)
312  const {
313  SandeshPhysicalDeviceVnListResp *port_resp =
314  static_cast<SandeshPhysicalDeviceVnListResp *>(resp);
315 
316  SandeshPhysicalDeviceVn data;
317  SetPhysicalDeviceVnSandeshData(this, &data);
318  std::vector<SandeshPhysicalDeviceVn> &list =
319  const_cast<std::vector<SandeshPhysicalDeviceVn>&>
320  (port_resp->get_port_list());
321  list.push_back(data);
322  return true;
323 }
324 
325 void SandeshPhysicalDeviceVnReq::HandleRequest() const {
327  get_device(),
328  get_vn()));
329  sand->DoSandesh(sand);
330 }
331 
333 (const AgentSandeshArguments *args, const std::string &context){
334  return AgentSandeshPtr
335  (new AgentPhysicalDeviceVnSandesh(context, args->GetString("device"),
336  args->GetString("vn")));
337 }
338 
340  PhysicalDeviceVnObjectLogInfo info;
341 
342  string str;
343  switch (event) {
344  case AgentLogEvent::ADD:
345  str.assign("Addition ");
346  break;
347  case AgentLogEvent::DEL:
348  str.assign("Deletion ");
349  break;
350  case AgentLogEvent::CHANGE:
351  str.assign("Modification ");
352  break;
353  default:
354  str.assign("INVALID");
355  break;
356  }
357  info.set_event(str);
358 
359  info.set_device_uuid(UuidToString(device_uuid_));
360  if (device_.get()) {
361  info.set_device(device_->name());
362  } else {
363  info.set_device("INVALID");
364  }
365  info.set_vn_uuid(UuidToString(vn_uuid_));
366  if (vn_) {
367  info.set_vn(vn_->GetName());
368  } else {
369  info.set_vn("INVALID");
370  }
371  info.set_ref_count(GetRefCount());
372  PHYSICAL_DEVICE_VN_OBJECT_LOG_LOG("PhysicalDeviceVn",
373  SandeshLevel::SYS_INFO, info);
374 }
375 
377 // Sandesh routines to dump config tree
380  public:
382  const string &key, const string &context) :
383  Task(agent->task_scheduler()->GetTaskId(AGENT_SANDESH_TASKNAME), 0),
384  table_(table), key_(key), context_(context) { }
386  virtual bool Run();
387  std::string Description() const { return "ConfigPhysicalDeviceVnSandesh"; }
388 
389  private:
391  string key_;
392  string context_;
394 };
395 
397  SandeshConfigPhysicalDeviceVnListResp *resp =
398  new SandeshConfigPhysicalDeviceVnListResp();
399  std::vector<SandeshConfigPhysicalDeviceVn> &list =
400  const_cast<std::vector<SandeshConfigPhysicalDeviceVn>&>
401  (resp->get_device_vn_list());
402 
403  PhysicalDeviceVnTable::ConfigTree::const_iterator it =
404  table_->config_tree().begin();
405  while (it != table_->config_tree().end()){
406  SandeshConfigPhysicalDeviceVn entry;
407  entry.set_device_uuid(UuidToString(it->dev_));
408  entry.set_vn_uuid(UuidToString(it->vn_));
409  entry.set_vmi_uuid(UuidToString(it->vmi_));
410  list.push_back(entry);
411  it++;
412  }
413  resp->set_context(context_);
414  resp->set_more(false);
415  resp->Response();
416  return true;
417 }
418 
419 void SandeshConfigPhysicalDeviceVnReq::HandleRequest() const {
420  Agent *agent = Agent::GetInstance();
423  (agent, agent->physical_device_vn_table(),
424  get_device(), context());
425  agent->task_scheduler()->Enqueue(task);
426 }
#define AGENT_SANDESH_TASKNAME
Definition: agent.h:302
class boost::shared_ptr< AgentSandesh > AgentSandeshPtr
Definition: agent_db.h:21
virtual void Clear()
Definition: agent_db.cc:216
virtual void Process(DBRequest &req)
Definition: agent_db.cc:232
Agent * agent() const
Definition: agent_db.h:216
virtual bool FilterToArgs(AgentSandeshArguments *args)
AgentPhysicalDeviceVnSandesh(const std::string &context, const std::string &dev, const std::string &vn)
virtual bool Filter(const DBEntryBase *entry)
bool Add(const std::string &key, const std::string &val)
std::string GetString(const std::string &key) const
SandeshResponse * resp_
std::string context() const
Definition: agent.h:361
ConfigManager * config_manager() const
Definition: agent.cc:893
VnTable * vn_table() const
Definition: agent.h:498
PhysicalDeviceVnTable * physical_device_vn_table() const
Definition: agent.h:638
PhysicalDeviceTable * physical_device_table() const
Definition: agent.h:631
TaskScheduler * task_scheduler() const
Definition: agent.h:1123
static Agent * GetInstance()
Definition: agent.h:439
void AddPhysicalDeviceVn(const boost::uuids::uuid &dev, const boost::uuids::uuid &vn)
void DelPhysicalDeviceVn(const boost::uuids::uuid &dev, const boost::uuids::uuid &vn)
ConfigPhysicalDeviceVnSandesh(Agent *agent, PhysicalDeviceVnTable *table, const string &key, const string &context)
DISALLOW_COPY_AND_ASSIGN(ConfigPhysicalDeviceVnSandesh)
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
PhysicalDeviceVnTable * table_
std::string Description() const
Gives a description of the task.
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
bool Enqueue(DBRequest *req)
Definition: db_table.cc:221
const std::string & name() const
Definition: db_table.h:111
void WalkAgain(DBTableWalkRef walk)
Definition: db_table.cc:658
void Init()
Definition: db_table.cc:414
DBTableWalkRef AllocWalker(WalkFn walk_fn, WalkCompleteFn walk_complete)
Definition: db_table.cc:640
void ReleaseWalker(DBTableWalkRef &walk)
Definition: db_table.cc:646
boost::intrusive_ptr< DBTableWalk > DBTableWalkRef
Definition: db_table.h:170
Definition: db.h:25
PhysicalDevice * Find(const boost::uuids::uuid &u)
DBTable::DBTableWalkRef vxlan_id_walk_ref_
void DeviceVnWalkDone(DBTable::DBTableWalkRef walk_ref, DBTableBase *partition)
virtual bool Resync(DBEntry *entry, const DBRequest *req)
static DBTableBase * CreateTable(DB *db, const std::string &name)
const ConfigTree & config_tree() const
virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, const std::string &context)
bool DeviceVnWalk(DBTablePartBase *partition, DBEntryBase *entry)
void ProcessConfig(const boost::uuids::uuid &dev, const boost::uuids::uuid &vn)
bool DeleteConfigEntry(const boost::uuids::uuid &vmi, const boost::uuids::uuid &dev, const boost::uuids::uuid &vn)
virtual bool OnChange(DBEntry *entry, const DBRequest *req)
PhysicalDeviceVnTable(DB *db, const std::string &name)
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *k) const
virtual DBEntry * Add(const DBRequest *req)
bool AddConfigEntry(const boost::uuids::uuid &vmi, const boost::uuids::uuid &dev, const boost::uuids::uuid &vn)
virtual bool Delete(DBEntry *entry, const DBRequest *req)
virtual bool IFNodeToReq(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
DBEntryBase::KeyPtr GetDBRequestKey() const
uint32_t GetRefCount() const
bool DBEntrySandesh(Sandesh *sresp, std::string &name) const
PhysicalDevice * device() const
boost::uuids::uuid device_uuid_
const boost::uuids::uuid & vn_uuid() const
virtual std::string ToString() const
virtual void SetKey(const DBRequestKey *key)
PhysicalDeviceRef device_
void SendObjectLog(AgentLogEvent::type event) const
boost::uuids::uuid vn_uuid_
VnEntry * vn() const
virtual bool IsLess(const DBEntry &rhs) const
const boost::uuids::uuid & device_uuid() const
std::string device_display_name_
bool Copy(PhysicalDeviceVnTable *table, const PhysicalDeviceVnData *data)
const IpAddress & ip() const
const std::string & name() const
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
Definition: task.cc:662
Task is a class to describe a computational task within OpenSDN control plane applications....
Definition: task.h:82
Definition: vn.h:152
int GetVxLanId() const
Definition: vn.cc:729
const string & GetName() const
Definition: vn.h:163
VnEntry * Find(const boost::uuids::uuid &vn_uuid)
Definition: vn.cc:771
uint8_t type
Definition: load_balance.h:2
static void SetPhysicalDeviceVnSandeshData(const PhysicalDeviceVn *entry, SandeshPhysicalDeviceVn *data)
static boost::uuids::uuid StringToUuid(const std::string &str)
Definition: string_util.h:146
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
@ RESYNC
Definition: agent_db.h:104
uint8_t sub_op_
Definition: agent_db.h:109
DBOperation oper
Definition: db_table.h:43
@ DB_ENTRY_DELETE
Definition: db_table.h:40
@ DB_ENTRY_ADD_CHANGE
Definition: db_table.h:39
std::unique_ptr< DBRequestKey > key
Definition: db_table.h:49
std::unique_ptr< DBRequestData > data
Definition: db_table.h:50
boost::uuids::uuid vn_uuid_
boost::uuids::uuid device_uuid_
Definition: task_int.h:11
boost::uuids::uuid uuid