OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
physical_device_vn.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <boost/uuid/uuid_io.hpp>
6 #include <vnc_cfg_types.h>
7 #include <cmn/agent_cmn.h>
8 
9 #include <ifmap/ifmap_node.h>
10 #include <cfg/cfg_init.h>
11 #include <oper/agent_sandesh.h>
12 #include <oper/vn.h>
14 #include <oper/physical_device.h>
16 #include <oper/config_manager.h>
17 
18 using std::string;
19 using boost::assign::map_list_of;
20 using boost::assign::list_of;
21 
23 // PhysicalDeviceVn routines
25 bool PhysicalDeviceVn::IsLess(const DBEntry &rhs) const {
26  const PhysicalDeviceVn &a =
27  static_cast<const PhysicalDeviceVn &>(rhs);
28  if (device_uuid_ != a.device_uuid_) {
29  return (device_uuid_ < a.device_uuid_);
30  }
31  return (vn_uuid_ < a.vn_uuid_);
32 }
33 
36 }
37 
40  return DBEntryBase::KeyPtr(key);
41 }
42 
44  const PhysicalDeviceVnKey *key =
45  static_cast<const PhysicalDeviceVnKey *>(k);
46 
48  vn_uuid_ = key->vn_uuid_;
49 }
50 
52  const PhysicalDeviceVnData *data) {
53  bool ret = false;
54 
55  PhysicalDevice *dev =
57  if (dev != device_.get()) {
58  device_.reset(dev);
59  ret = true;
60  }
61 
62  if (dev && (dev->ip() != tor_ip_)) {
63  tor_ip_ = dev->ip();
64  ret = true;
65  }
66 
67  if (dev && (dev->name() != device_display_name_)) {
68  device_display_name_ = dev->name();
69  ret = true;
70  }
71 
72  VnEntry *vn = table->agent()->vn_table()->Find(vn_uuid_);
73  if (vn != vn_.get()) {
74  vn_.reset(vn);
75  ret = true;
76  }
77 
78  int vxlan_id = vn ? vn->GetVxLanId() : 0;
79  if (vxlan_id != vxlan_id_) {
81  ret = true;
82  }
83 
84  return ret;
85 }
86 
88 // PhysicalDeviceVnTable routines
90 PhysicalDeviceVnTable::PhysicalDeviceVnTable(DB *db, const std::string &name) :
91  AgentDBTable(db, name) {
94  this, _1, _2),
96  this, _1, _2));
97 }
98 
99 std::unique_ptr<DBEntry> PhysicalDeviceVnTable::AllocEntry(const DBRequestKey *k)
100  const {
101  const PhysicalDeviceVnKey *key =
102  static_cast<const PhysicalDeviceVnKey *>(k);
103 
105  key->vn_uuid_);
106  return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(entry));
107 }
108 
110  PhysicalDeviceVnKey *key =
111  static_cast<PhysicalDeviceVnKey *>(req->key.get());
112  PhysicalDeviceVnData *data =
113  static_cast<PhysicalDeviceVnData *>(req->data.get());
114 
116  key->vn_uuid_);
117  entry->Copy(this, data);
118  entry->SendObjectLog(AgentLogEvent::ADD);
119  return entry;
120 }
121 
123  PhysicalDeviceVn *entry = static_cast<PhysicalDeviceVn *>(e);
124  PhysicalDeviceVnData *data =
125  static_cast<PhysicalDeviceVnData *>(req->data.get());
126  bool ret = entry->Copy(this, data);
127  return ret;
128 }
129 
131  PhysicalDeviceVn *entry = static_cast<PhysicalDeviceVn *>(e);
132  PhysicalDeviceVnData *data =
133  static_cast<PhysicalDeviceVnData *>(req->data.get());
134  bool ret = entry->Copy(this, data);
135  return ret;
136 }
137 
141 }
142 
144  PhysicalDeviceVn *entry = static_cast<PhysicalDeviceVn *>(e);
145  entry->SendObjectLog(AgentLogEvent::DEL);
146  return true;
147 }
148 
150  const std::string &name) {
151  PhysicalDeviceVnTable *table = new PhysicalDeviceVnTable(db, name);
152  table->Init();
153  return table;
154 }
155 
157  DBEntryBase *entry) {
158  PhysicalDeviceVn *dev_vn = static_cast<PhysicalDeviceVn *>(entry);
159  DBEntryBase::KeyPtr db_key = dev_vn->GetDBRequestKey();
160  PhysicalDeviceVnKey *key =
161  static_cast<PhysicalDeviceVnKey *>(db_key.release());
162  key->sub_op_ = AgentKey::RESYNC;
163  DBRequest req;
165  req.key.reset(key);
166  req.data.reset(NULL);
167  Process(req);
168  return true;
169 }
170 
172  DBTableBase *part) {
173 }
174 
177 }
178 
180 // Vmi Config handling routines
183  const boost::uuids::uuid &vn) {
185  req.key.reset(new PhysicalDeviceVnKey(dev, vn));
186  req.data.reset(new PhysicalDeviceVnData());
187  Enqueue(&req);
188  return;
189 }
190 
192  const boost::uuids::uuid &dev,
193  const boost::uuids::uuid &vn) {
194  using boost::uuids::nil_uuid;
195 
196  // Sanity checks. Needed since VMInterface is not checking for nil_uuid
197  if (vmi == nil_uuid() || dev == nil_uuid() || vn == nil_uuid())
198  return false;
199 
200  config_tree_.insert(PhysicalDeviceVnToVmi(dev, vn, vmi));
202  return true;
203 }
204 
206  const boost::uuids::uuid &dev,
207  const boost::uuids::uuid &vn) {
208  using boost::uuids::nil_uuid;
209 
210  // Sanity checks. Needed since VMInterface is not checking for nil_uuid
211  if (vmi == nil_uuid() || dev == nil_uuid() || vn == nil_uuid())
212  return false;
213 
214  config_tree_.erase(PhysicalDeviceVnToVmi(dev, vn, vmi));
215  // Dont delete physical-device-vn entry if there are more entries in
216  // config-tree with given dev and vn
217  ConfigTree::iterator it =
218  config_tree_.upper_bound(PhysicalDeviceVnToVmi(dev, vn, nil_uuid()));
219  bool del_entry = false;
220  if (it == config_tree_.end())
221  del_entry = true;
222  else if (it->dev_ != dev)
223  del_entry = true;
224  else if (it->vn_ != vn)
225  del_entry = true;
226 
227  if (del_entry) {
230  req.key.reset(new PhysicalDeviceVnKey(dev, vn));
231  Enqueue(&req);
232  }
233 
234  return del_entry;
235 }
236 
238  const boost::uuids::uuid &u) {
239  return false;
240 }
241 
243 // Sandesh routines
246  public:
248  const std::string &dev,
249  const std::string &vn)
250  : AgentSandesh(context, ""), dev_str_(dev), vn_str_(vn) {
251  dev_uuid_ = StringToUuid(dev);
252  vn_uuid_ = StringToUuid(vn);
253  }
254 
255  virtual bool Filter(const DBEntryBase *entry) {
256  const PhysicalDeviceVn *dev_vn =
257  static_cast<const PhysicalDeviceVn *>(entry);
258  if (dev_str_.empty() == false) {
259  if (dev_vn->device_uuid() != dev_uuid_)
260  return false;
261  }
262 
263  if (vn_str_.empty() == false) {
264  if (dev_vn->vn_uuid() != vn_uuid_)
265  return false;
266  }
267 
268  return true;
269  }
270 
271  virtual bool FilterToArgs(AgentSandeshArguments *args) {
272  args->Add("device", dev_str_);
273  args->Add("vn", vn_str_);
274  return true;
275  }
276 
277  private:
279  Agent *agent = Agent::GetInstance();
280  return static_cast<DBTable *>
281  (agent->physical_device_vn_table());
282  }
283  void Alloc() {
284  resp_ = new SandeshPhysicalDeviceVnListResp();
285  }
286 
287  std::string dev_str_;
289  std::string vn_str_;
291 };
292 
294  SandeshPhysicalDeviceVn *data) {
295  data->set_device_uuid(UuidToString(entry->device_uuid()));
296  if (entry->device()) {
297  data->set_device(entry->device()->name());
298  } else {
299  data->set_device("INVALID");
300  }
301  data->set_vn_uuid(UuidToString(entry->vn_uuid()));
302  if (entry->vn()) {
303  data->set_vn(entry->vn()->GetName());
304  } else {
305  data->set_vn("INVALID");
306  }
307  data->set_vxlan_id(entry->vxlan_id());
308 }
309 
310 bool PhysicalDeviceVn::DBEntrySandesh(Sandesh *resp, std::string &name)
311  const {
312  SandeshPhysicalDeviceVnListResp *port_resp =
313  static_cast<SandeshPhysicalDeviceVnListResp *>(resp);
314 
315  SandeshPhysicalDeviceVn data;
316  SetPhysicalDeviceVnSandeshData(this, &data);
317  std::vector<SandeshPhysicalDeviceVn> &list =
318  const_cast<std::vector<SandeshPhysicalDeviceVn>&>
319  (port_resp->get_port_list());
320  list.push_back(data);
321  return true;
322 }
323 
324 void SandeshPhysicalDeviceVnReq::HandleRequest() const {
326  get_device(),
327  get_vn()));
328  sand->DoSandesh(sand);
329 }
330 
332 (const AgentSandeshArguments *args, const std::string &context){
333  return AgentSandeshPtr
334  (new AgentPhysicalDeviceVnSandesh(context, args->GetString("device"),
335  args->GetString("vn")));
336 }
337 
339  PhysicalDeviceVnObjectLogInfo info;
340 
341  string str;
342  switch (event) {
343  case AgentLogEvent::ADD:
344  str.assign("Addition ");
345  break;
346  case AgentLogEvent::DEL:
347  str.assign("Deletion ");
348  break;
349  case AgentLogEvent::CHANGE:
350  str.assign("Modification ");
351  break;
352  default:
353  str.assign("INVALID");
354  break;
355  }
356  info.set_event(str);
357 
358  info.set_device_uuid(UuidToString(device_uuid_));
359  if (device_.get()) {
360  info.set_device(device_->name());
361  } else {
362  info.set_device("INVALID");
363  }
364  info.set_vn_uuid(UuidToString(vn_uuid_));
365  if (vn_) {
366  info.set_vn(vn_->GetName());
367  } else {
368  info.set_vn("INVALID");
369  }
370  info.set_ref_count(GetRefCount());
371  PHYSICAL_DEVICE_VN_OBJECT_LOG_LOG("PhysicalDeviceVn",
372  SandeshLevel::SYS_INFO, info);
373 }
374 
376 // Sandesh routines to dump config tree
379  public:
381  const string &key, const string &context) :
382  Task(agent->task_scheduler()->GetTaskId(AGENT_SANDESH_TASKNAME), 0),
383  table_(table), key_(key), context_(context) { }
385  virtual bool Run();
386  std::string Description() const { return "ConfigPhysicalDeviceVnSandesh"; }
387 
388  private:
390  string key_;
391  string context_;
393 };
394 
396  SandeshConfigPhysicalDeviceVnListResp *resp =
397  new SandeshConfigPhysicalDeviceVnListResp();
398  std::vector<SandeshConfigPhysicalDeviceVn> &list =
399  const_cast<std::vector<SandeshConfigPhysicalDeviceVn>&>
400  (resp->get_device_vn_list());
401 
402  PhysicalDeviceVnTable::ConfigTree::const_iterator it =
403  table_->config_tree().begin();
404  while (it != table_->config_tree().end()){
405  SandeshConfigPhysicalDeviceVn entry;
406  entry.set_device_uuid(UuidToString(it->dev_));
407  entry.set_vn_uuid(UuidToString(it->vn_));
408  entry.set_vmi_uuid(UuidToString(it->vmi_));
409  list.push_back(entry);
410  it++;
411  }
412  resp->set_context(context_);
413  resp->set_more(false);
414  resp->Response();
415  return true;
416 }
417 
418 void SandeshConfigPhysicalDeviceVnReq::HandleRequest() const {
419  Agent *agent = Agent::GetInstance();
422  (agent, agent->physical_device_vn_table(),
423  get_device(), context());
424  agent->task_scheduler()->Enqueue(task);
425 }
int GetVxLanId() const
Definition: vn.cc:727
DBTable::DBTableWalkRef vxlan_id_walk_ref_
AgentPhysicalDeviceVnSandesh(const std::string &context, const std::string &dev, const std::string &vn)
virtual bool Run()
Code to execute. Returns true if task is completed. Return false to reschedule the task...
void DeviceVnWalkDone(DBTable::DBTableWalkRef walk_ref, DBTableBase *partition)
const boost::uuids::uuid & vn_uuid() const
void ProcessConfig(const boost::uuids::uuid &dev, const boost::uuids::uuid &vn)
static Agent * GetInstance()
Definition: agent.h:436
static boost::uuids::uuid StringToUuid(const std::string &str)
Definition: string_util.h:145
std::string device_display_name_
std::string GetString(const std::string &key) const
PhysicalDeviceTable * physical_device_table() const
Definition: agent.h:628
ConfigManager * config_manager() const
Definition: agent.cc:889
uint8_t sub_op_
Definition: agent_db.h:106
DBEntryBase::KeyPtr GetDBRequestKey() const
Agent * agent() const
Definition: agent_db.h:213
virtual DBEntry * Add(const DBRequest *req)
std::unique_ptr< DBRequestData > data
Definition: db_table.h:49
DBTableWalkRef AllocWalker(WalkFn walk_fn, WalkCompleteFn walk_complete)
Definition: db_table.cc:613
bool Enqueue(DBRequest *req)
Definition: db_table.cc:194
boost::uuids::uuid uuid
VnTable * vn_table() const
Definition: agent.h:495
Definition: task_int.h:10
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *k) const
void AddPhysicalDeviceVn(const boost::uuids::uuid &dev, const boost::uuids::uuid &vn)
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:138
bool DeviceVnWalk(DBTablePartBase *partition, DBEntryBase *entry)
int vxlan_id() const
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
boost::uuids::uuid vn_uuid_
const IpAddress & ip() const
void ReleaseWalker(DBTableWalkRef &walk)
Definition: db_table.cc:619
bool DBEntrySandesh(Sandesh *sresp, std::string &name) const
virtual std::string ToString() const
PhysicalDevice * device() const
Definition: db.h:24
PhysicalDevice * Find(const boost::uuids::uuid &u)
void WalkAgain(DBTableWalkRef walk)
Definition: db_table.cc:631
ConfigPhysicalDeviceVnSandesh(Agent *agent, PhysicalDeviceVnTable *table, const string &key, const string &context)
void Init()
Definition: db_table.cc:387
TaskScheduler * task_scheduler() const
Definition: agent.h:1120
uint8_t type
Definition: load_balance.h:109
PhysicalDeviceRef device_
Definition: agent.h:358
PhysicalDeviceVnTable * table_
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:636
boost::uuids::uuid device_uuid_
std::unique_ptr< DBRequestKey > key
Definition: db_table.h:48
virtual bool Filter(const DBEntryBase *entry)
void SendObjectLog(AgentLogEvent::type event) const
DBOperation oper
Definition: db_table.h:42
PhysicalDeviceVnTable * physical_device_vn_table() const
Definition: agent.h:635
bool Copy(PhysicalDeviceVnTable *table, const PhysicalDeviceVnData *data)
virtual void Clear()
Definition: agent_db.cc:215
virtual bool IFNodeToReq(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
class boost::shared_ptr< AgentSandesh > AgentSandeshPtr
Definition: agent_db.h:18
virtual void Process(DBRequest &req)
Definition: agent_db.cc:231
bool Add(const std::string &key, const std::string &val)
static void SetPhysicalDeviceVnSandeshData(const PhysicalDeviceVn *entry, SandeshPhysicalDeviceVn *data)
const ConfigTree & config_tree() const
VnEntry * Find(const boost::uuids::uuid &vn_uuid)
Definition: vn.cc:759
virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, const std::string &context)
Definition: vn.h:151
bool AddConfigEntry(const boost::uuids::uuid &vmi, const boost::uuids::uuid &dev, const boost::uuids::uuid &vn)
virtual void SetKey(const DBRequestKey *key)
virtual bool OnChange(DBEntry *entry, const DBRequest *req)
virtual bool Delete(DBEntry *entry, const DBRequest *req)
uint32_t GetRefCount() const
int GetTaskId() const
Definition: task.h:118
const std::string & name() const
virtual bool IsLess(const DBEntry &rhs) const
virtual bool FilterToArgs(AgentSandeshArguments *args)
virtual bool Resync(DBEntry *entry, const DBRequest *req)
#define AGENT_SANDESH_TASKNAME
Definition: agent.h:301
boost::uuids::uuid device_uuid_
void DelPhysicalDeviceVn(const boost::uuids::uuid &dev, const boost::uuids::uuid &vn)
const string & GetName() const
Definition: vn.h:162
boost::intrusive_ptr< DBTableWalk > DBTableWalkRef
Definition: db_table.h:169
DISALLOW_COPY_AND_ASSIGN(ConfigPhysicalDeviceVnSandesh)
bool DeleteConfigEntry(const boost::uuids::uuid &vmi, const boost::uuids::uuid &dev, const boost::uuids::uuid &vn)
Task is a wrapper over tbb::task to support policies.
Definition: task.h:86
std::string context() const
static DBTableBase * CreateTable(DB *db, const std::string &name)
SandeshResponse * resp_
VnEntry * vn() const
const boost::uuids::uuid & device_uuid() const
boost::uuids::uuid vn_uuid_
PhysicalDeviceVnTable(DB *db, const std::string &name)