OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vrf_assign.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <cmn/agent_cmn.h>
6 #include <boost/uuid/uuid_io.hpp>
7 
9 #include <oper/mirror_table.h>
10 #include <oper/vrf.h>
11 #include <oper/vrf_assign.h>
12 #include <oper/nexthop.h>
13 #include <oper/agent_sandesh.h>
14 
15 using namespace std;
16 using namespace boost::uuids;
17 
19 
21 // VRF Assignment table routines
23 std::unique_ptr<DBEntry> VrfAssignTable::AllocEntry(const DBRequestKey *k) const {
24  return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(AllocWithKey(k)));
25 }
26 
28  const VrfAssign::VrfAssignKey *key =
29  static_cast<const VrfAssign::VrfAssignKey *>(k);
30  VrfAssign *assign = NULL;
31 
33  Interface *intrface = static_cast<Interface *>
34  (agent()->interface_table()->Find(&intf_key, true));
35 
36  switch (key->type_) {
37  case VrfAssign::VLAN: {
38  VlanVrfAssign *vlan_assign =
39  new VlanVrfAssign(intrface, key->vlan_tag_);
40  assign = static_cast<VrfAssign *>(vlan_assign);
41  }
42  break;
43 
44  default:
45  assert(0);
46  break;
47  }
48 
49  return assign;
50 }
51 
53  const VrfAssign::VrfAssignKey *key =
54  static_cast<const VrfAssign::VrfAssignKey *>(req->key.get());
55  VrfAssign *rule = AllocWithKey(key);
56  if (rule->interface_.get() == NULL) {
57  delete rule;
58  return NULL;
59  }
60 
61  OnChange(rule, req);
62  return rule;
63 }
64 
65 // Assigned VRF can potentially change
66 bool VrfAssignTable::OnChange(DBEntry *entry, const DBRequest *req) {
67  return (static_cast<VrfAssign *>(entry))->Change(req);
68 }
69 
70 DBTableBase *VrfAssignTable::CreateTable(DB *db, const string &name) {
71  vrf_assign_table_ = new VrfAssignTable(db, name);
72  vrf_assign_table_->Init();
73  return vrf_assign_table_;
74 }
75 
77  VmInterfaceKey key(AgentKey::ADD_DEL_CHANGE, intf_uuid, "");
78  return static_cast<Interface *>
80 }
81 
82 VrfEntry *VrfAssignTable::FindVrf(const string &name) {
83  return Agent::GetInstance()->vrf_table()->FindVrfFromName(name);
84 }
85 
87  const std::string &vrf_name, uint16_t vlan_tag) {
88  DBRequest req;
90 
92  key->VlanInit(intf_uuid, vlan_tag);
93  req.key.reset(key);
94 
96  req.data.reset(data);
97 
98  vrf_assign_table_->Enqueue(&req);
99  return;
100 }
101 
103  uint16_t vlan_tag) {
104  DBRequest req;
106 
108  key->VlanInit(intf_uuid, vlan_tag);
109  req.key.reset(key);
110 
111  req.data.reset(NULL);
112  vrf_assign_table_->Enqueue(&req);
113  return;
114 }
115 
117  const std::string &vrf_name, uint16_t vlan_tag) {
118  DBRequest req;
120 
122  key->VlanInit(intf_uuid, vlan_tag);
123  req.key.reset(key);
124 
126  req.data.reset(data);
127 
128  vrf_assign_table_->Process(req);
129  return;
130 }
131 
133  uint16_t vlan_tag) {
134  DBRequest req;
136 
138  key->VlanInit(intf_uuid, vlan_tag);
139  req.key.reset(key);
140 
141  req.data.reset(NULL);
142  vrf_assign_table_->Process(req);
143  return;
144 }
145 
147  uint16_t vlan_tag) {
149  key.VlanInit(intf_uuid, vlan_tag);
150  return static_cast<VrfAssign *>(vrf_assign_table_->FindActiveEntry(&key));
151 }
152 
154 // VRF Assignment routines
156 bool VrfAssign::IsLess(const DBEntry &rhs) const {
157  const VrfAssign &vassign = static_cast<const VrfAssign &>(rhs);
158  if (type_ != vassign.type_) {
159  return type_ < vassign.type_;
160  }
161 
162  if (interface_.get() != vassign.interface_.get()) {
163  return interface_.get() < vassign.interface_.get();
164  }
165 
166  return VrfAssignIsLess(vassign);
167 }
168 
169 bool VrfAssign::Change(const DBRequest *req) {
170  bool ret = false;
172  static_cast<VrfAssign::VrfAssignData *>(req->data.get());
173 
175  if (vrf_.get() != vrf) {
176  vrf_ = vrf;
177  ret = true;
178  }
179 
180  if (VrfAssignChange(req)) {
181  ret = true;
182  }
183 
184  return ret;
185 }
186 
188  bool ret = false;
189  const VrfAssign::VrfAssignKey *key =
190  static_cast<const VrfAssign::VrfAssignKey *>(req->key.get());
191  VlanNHKey nh_key(key->intf_uuid_, key->vlan_tag_);
192  Agent *agent = Agent::GetInstance();
193  const NextHop *nh = static_cast<NextHop *>
194  (agent->nexthop_table()->FindActiveEntry(&nh_key));
195  assert(nh);
196  if (nh_ != nh) {
197  nh_ = nh;
198  ret = true;
199  }
200  return ret;
201 }
202 
204 // VLAN based VRF Assignment routines
207  const VlanVrfAssign &rhs_vassign = static_cast<const VlanVrfAssign &>(rhs);
208  return (vlan_tag_ < rhs_vassign.vlan_tag_);
209 }
210 
212  VrfAssignKey *key = new VrfAssignKey();
213  key->VlanInit(interface_->GetUuid(), vlan_tag_);
214  return DBEntryBase::KeyPtr(key);
215 }
216 
218  const VrfAssignKey *key = static_cast<const VrfAssignKey *>(k);
219  type_ = key->type_;
221  vlan_tag_ = key->vlan_tag_;
222 }
223 
224 bool VlanVrfAssign::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
225  VrfAssignResp *resp = static_cast<VrfAssignResp *> (sresp);
226 
227  if (!name.empty() && interface_->name().find(name) != string::npos) {
228  return false;
229  }
230 
231  VrfAssignSandeshData data;
232  assert(type_ == VrfAssign::VLAN);
233 
234  data.set_vlan_tag(vlan_tag_);
235  data.set_itf(interface_->name());
236  if (vrf_.get()) {
237  data.set_vrf(vrf_->GetName());
238  } else {
239  data.set_vrf("--ERROR--");
240  }
241  std::vector<VrfAssignSandeshData> &list =
242  const_cast<std::vector<VrfAssignSandeshData>&>
243  (resp->get_vrf_assign_list());
244  list.push_back(data);
245  return true;
246 }
247 
248 void VrfAssignReq::HandleRequest() const {
249  AgentSandeshPtr sand(new AgentVrfAssignSandesh(context(), get_uuid()));
250  sand->DoSandesh(sand);
251 }
252 
254  const std::string &context) {
255  return AgentSandeshPtr(new AgentVrfAssignSandesh(context,
256  args->GetString("name")));
257 }
static Agent * GetInstance()
Definition: agent.h:436
static void DeleteVlan(const boost::uuids::uuid &intf_uuid, uint16_t vlan_tag)
Definition: vrf_assign.cc:132
static VrfEntry * FindVrf(const std::string &name)
Definition: vrf_assign.cc:82
Definition: vrf.h:86
uint16_t vlan_tag_
Definition: vrf_assign.h:93
std::string GetString(const std::string &key) const
VrfEntry * FindVrfFromName(const string &name)
Definition: vrf.cc:873
NextHopTable * nexthop_table() const
Definition: agent.h:475
const std::string vrf_name_
Definition: vrf_assign.h:41
static VrfAssignTable * GetInstance()
Definition: vrf_assign.h:116
std::unique_ptr< DBRequestData > data
Definition: db_table.h:49
AgentDBEntry * FindActiveEntry(const DBEntry *key)
Definition: agent_db.cc:110
InterfaceTable * interface_table() const
Definition: agent.h:465
boost::uuids::uuid uuid
boost::uuids::uuid intf_uuid_
Definition: vrf_assign.h:28
virtual bool OnChange(DBEntry *entry, const DBRequest *req)
Definition: vrf_assign.cc:66
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
bool Change(const DBRequest *req)
Definition: vrf_assign.cc:169
void SetKey(const DBRequestKey *key)
Definition: vrf_assign.cc:217
Definition: db.h:24
void VlanInit(const boost::uuids::uuid intf_uuid, uint16_t vlan_tag)
Definition: vrf_assign.h:31
static DBTableBase * CreateTable(DB *db, const std::string &name)
Definition: vrf_assign.cc:70
Definition: agent.h:358
virtual bool VrfAssignChange(const DBRequest *req)
Definition: vrf_assign.cc:187
static VrfAssign * FindVlanReq(const boost::uuids::uuid &intf_uuid, uint16_t vlan_tag)
Definition: vrf_assign.cc:146
bool IsLess(const DBEntry &rhs) const
Definition: vrf_assign.cc:156
std::unique_ptr< DBRequestKey > key
Definition: db_table.h:48
KeyPtr GetDBRequestKey() const
Definition: vrf_assign.cc:211
DBOperation oper
Definition: db_table.h:42
class boost::shared_ptr< AgentSandesh > AgentSandeshPtr
Definition: agent_db.h:18
static void DeleteVlanReq(const boost::uuids::uuid &intf_uuid, uint16_t vlan_tag)
Definition: vrf_assign.cc:102
bool DBEntrySandesh(Sandesh *sresp, std::string &name) const
Definition: vrf_assign.cc:224
VrfTable * vrf_table() const
Definition: agent.h:485
NextHopRef nh_
Definition: vrf.h:240
virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, const std::string &context)
Definition: vrf_assign.cc:253
static void CreateVlanReq(const boost::uuids::uuid &intf_uuid, const std::string &vrf_name, uint16_t vlan_tag)
Definition: vrf_assign.cc:86
static VrfAssignTable * vrf_assign_table_
Definition: vrf_assign.h:133
VrfAssign::Type type_
Definition: vrf_assign.h:25
InterfaceRef interface_
Definition: vrf_assign.h:67
static void CreateVlan(const boost::uuids::uuid &intf_uuid, const std::string &vrf_name, uint16_t vlan_tag)
Definition: vrf_assign.cc:116
Type type_
Definition: vrf_assign.h:65
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *k) const
Definition: vrf_assign.cc:23
static Interface * FindInterface(const boost::uuids::uuid &intf_uuid)
Definition: vrf_assign.cc:76
bool VrfAssignIsLess(const VrfAssign &rhs) const
Definition: vrf_assign.cc:206
VrfAssign * AllocWithKey(const DBRequestKey *k) const
Definition: vrf_assign.cc:27
virtual DBEntry * Add(const DBRequest *req)
Definition: vrf_assign.cc:52