OpenSDN source code
sg.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 <algorithm>
7 #include <boost/uuid/uuid_io.hpp>
8 #include <base/parse_object.h>
9 #include <ifmap/ifmap_link.h>
10 #include <ifmap/ifmap_table.h>
11 #include <vnc_cfg_types.h>
12 
13 #include <cmn/agent_cmn.h>
14 #include <cfg/cfg_init.h>
15 #include <oper/sg.h>
16 #include <filter/acl.h>
17 
18 #include <oper/interface_common.h>
19 #include <oper/mirror_table.h>
20 #include <oper/agent_sandesh.h>
21 #include <oper/config_manager.h>
22 
23 using namespace autogen;
24 using namespace std;
25 
27 
28 bool SgEntry::IsLess(const DBEntry &rhs) const {
29  const SgEntry &a = static_cast<const SgEntry &>(rhs);
30  return (sg_uuid_ < a.sg_uuid_);
31 }
32 
33 string SgEntry::ToString() const {
34  std::stringstream uuidstring;
35  uuidstring << sg_uuid_;
36  return uuidstring.str();
37 }
38 
40  SgKey *key = new SgKey(sg_uuid_);
41  return DBEntryBase::KeyPtr(key);
42 }
43 
44 void SgEntry::SetKey(const DBRequestKey *key) {
45  const SgKey *k = static_cast<const SgKey *>(key);
46  sg_uuid_ = k->sg_uuid_;
47 }
48 
49 std::unique_ptr<DBEntry> SgTable::AllocEntry(const DBRequestKey *k) const {
50  const SgKey *key = static_cast<const SgKey *>(k);
51  SgEntry *sg = new SgEntry(key->sg_uuid_);
52  return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(sg));
53 }
54 
56  SgKey *key = static_cast<SgKey *>(req->key.get());
57  SgEntry *sg = new SgEntry(key->sg_uuid_);
58  ChangeHandler(sg, req);
59  sg->SendObjectLog(GetOperDBTraceBuf(), AgentLogEvent::ADD);
60  return sg;
61 }
62 
63 bool SgTable::OperDBOnChange(DBEntry *entry, const DBRequest *req) {
64  bool ret = ChangeHandler(entry, req);
65  SgEntry *sg = static_cast<SgEntry *>(entry);
66  sg->SendObjectLog(GetOperDBTraceBuf(), AgentLogEvent::CHANGE);
67  return ret;
68 }
69 
70 bool SgTable::ChangeHandler(DBEntry *entry, const DBRequest *req) {
71  bool ret = false;
72  SgEntry *sg = static_cast<SgEntry *>(entry);
73  SgData *data = static_cast<SgData *>(req->data.get());
74 
75  if (sg->sg_id_ != data->sg_id_) {
76  sg->sg_id_ = data->sg_id_;
77  ret = true;
78  }
79 
80  AclKey key(data->egress_acl_id_);
81  AclDBEntry *acl = static_cast<AclDBEntry *>(agent()->acl_table()->FindActiveEntry(&key));
82  if (sg->egress_acl_ != acl) {
83  sg->egress_acl_ = acl;
84  ret = true;
85  }
86  key = AclKey(data->ingress_acl_id_);
87  acl = static_cast<AclDBEntry *>(agent()->acl_table()->FindActiveEntry(&key));
88  if (sg->ingress_acl_ != acl) {
89  sg->ingress_acl_ = acl;
90  ret = true;
91  }
92  return ret;
93 }
94 
95 bool SgTable::OperDBDelete(DBEntry *entry, const DBRequest *req) {
96  SgEntry *sg = static_cast<SgEntry *>(entry);
97  sg->SendObjectLog(GetOperDBTraceBuf(), AgentLogEvent::DEL);
98  return true;
99 }
100 
101 DBTableBase *SgTable::CreateTable(DB *db, const std::string &name) {
102  sg_table_ = new SgTable(db, name);
103  sg_table_->Init();
104  return sg_table_;
105 };
106 
108  SecurityGroup *cfg = static_cast<SecurityGroup *>(node->GetObject());
109  assert(cfg);
110  autogen::IdPermsType id_perms = cfg->id_perms();
111  CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, u);
112  return true;
113 }
114 
116  const boost::uuids::uuid &u) {
117  SecurityGroup *cfg = static_cast<SecurityGroup *>(node->GetObject());
118  assert(cfg);
119 
120  assert(!u.is_nil());
121 
122  if ((req.oper == DBRequest::DB_ENTRY_DELETE) || node->IsDeleted()) {
124  req.key.reset(new SgKey(u));
125  agent()->sg_table()->Enqueue(&req);
126  return false;
127  }
128 
129  agent()->config_manager()->AddSgNode(node);
130  return false;
131 }
132 
134  const boost::uuids::uuid &u) {
135 
136  if (node->IsDeleted())
137  return false;
138 
139  SecurityGroup *cfg = static_cast<SecurityGroup *>(node->GetObject());
140  assert(cfg);
141 
143  uint32_t sg_id = cfg->id();
144  if (sg_id == SgTable::kInvalidSgId) {
145  OPER_TRACE(Sg, "Ignore SG id 0", UuidToString(u));
146  return false;
147  }
148 
149  SgKey *key = new SgKey(u);
150  SgData *data = NULL;
151 
152  boost::uuids::uuid egress_acl_uuid = boost::uuids::nil_uuid();
153  boost::uuids::uuid ingress_acl_uuid = boost::uuids::nil_uuid();
154  IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
156  node->begin(table->GetGraph());
157  iter != node->end(table->GetGraph()); ++iter) {
158  IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
159  if (agent()->config_manager()->SkipNode(adj_node)) {
160  continue;
161  }
162 
163  if (adj_node->table() == agent()->cfg()->cfg_acl_table()) {
164  AccessControlList *acl_cfg = static_cast<AccessControlList *>
165  (adj_node->GetObject());
166  assert(acl_cfg);
167  autogen::IdPermsType id_perms = acl_cfg->id_perms();
168  if (adj_node->name().find("egress-access-control-list") != std::string::npos) {
169  CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong,
170  egress_acl_uuid);
171  }
172  if (adj_node->name().find("ingress-access-control-list") != std::string::npos) {
173  CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong,
174  ingress_acl_uuid);
175  }
176  }
177  }
178  data = new SgData(agent(), node, sg_id, egress_acl_uuid, ingress_acl_uuid);
179  req.key.reset(key);
180  req.data.reset(data);
181  agent()->sg_table()->Enqueue(&req);
182  return false;
183 }
184 
185 bool SgEntry::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
186  SgListResp *resp = static_cast<SgListResp *>(sresp);
187  std::string str_uuid = UuidToString(GetSgUuid());
188  if (name.empty() ||
189  (str_uuid == name) ||
190  (integerToString(GetSgId()) == name)) {
191  SgSandeshData data;
192  data.set_ref_count(GetRefCount());
193  data.set_sg_uuid(str_uuid);
194  data.set_sg_id(GetSgId());
195  if (GetEgressAcl()) {
196  data.set_egress_acl_uuid(UuidToString(GetEgressAcl()->GetUuid()));
197  }
198  if (GetIngressAcl()) {
199  data.set_ingress_acl_uuid(UuidToString(GetIngressAcl()->GetUuid()));
200  }
201  std::vector<SgSandeshData> &list =
202  const_cast<std::vector<SgSandeshData>&>(resp->get_sg_list());
203  list.push_back(data);
204  return true;
205  }
206  return false;
207 }
208 
210  AgentLogEvent::type event) const {
211  SgObjectLogInfo info;
212 
213  string str;
214  switch(event) {
215  case AgentLogEvent::ADD:
216  str.assign("Addition");
217  break;
218  case AgentLogEvent::DEL:
219  str.assign("Deletion");
220  break;
221  case AgentLogEvent::CHANGE:
222  str.assign("Modification");
223  break;
224  default:
225  str.assign("");
226  break;
227  }
228  info.set_event(str);
229 
230  string sg_uuid = UuidToString(GetSgUuid());
231  info.set_uuid(sg_uuid);
232  info.set_id(GetSgId());
233  if (GetEgressAcl()) {
234  info.set_egress_acl_uuid(UuidToString(GetEgressAcl()->GetUuid()));
235  }
236  if (GetIngressAcl()) {
237  info.set_ingress_acl_uuid(UuidToString(GetIngressAcl()->GetUuid()));
238  }
239  info.set_ref_count(GetRefCount());
240  SG_OBJECT_LOG_LOG("AgentSg", SandeshLevel::SYS_INFO, info);
241  SG_OBJECT_TRACE_TRACE(buf, info);
242 }
243 
244 void SgListReq::HandleRequest() const {
245  AgentSandeshPtr sand(new AgentSgSandesh(context(), get_name()));
246  sand->DoSandesh(sand);
247 }
248 
250  const std::string &context) {
251  return AgentSandeshPtr(new AgentSgSandesh(context,
252  args->GetString("name")));
253 }
static void CfgUuidSet(uint64_t ms_long, uint64_t ls_long, boost::uuids::uuid &u)
Definition: agent_cmn.h:67
class boost::shared_ptr< AgentSandesh > AgentSandeshPtr
Definition: agent_db.h:21
#define OPER_TRACE(obj,...)
Definition: agent_db.h:232
Definition: acl.h:92
std::string GetString(const std::string &key) const
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
bool IsDeleted() const
Definition: db_entry.h:49
adjacency_iterator end(DBGraph *graph)
adjacency_iterator begin(DBGraph *graph)
Definition: db.h:25
const DBGraph * GetGraph() const
IFMapTable * table()
Definition: ifmap_node.h:30
const std::string & name() const
Definition: ifmap_node.h:49
IFMapObject * GetObject()
Definition: ifmap_node.cc:64
Definition: sg.h:36
bool DBEntrySandesh(Sandesh *sresp, std::string &name) const
Definition: sg.cc:185
AclDBEntryRef ingress_acl_
Definition: sg.h:69
uint32_t sg_id_
Definition: sg.h:67
AclDBEntryRef egress_acl_
Definition: sg.h:68
void SendObjectLog(SandeshTraceBufferPtr ptr, AgentLogEvent::type event) const
Definition: sg.cc:209
virtual void SetKey(const DBRequestKey *key)
Definition: sg.cc:44
virtual bool IsLess(const DBEntry &rhs) const
Definition: sg.cc:28
virtual string ToString() const
Definition: sg.cc:33
boost::uuids::uuid sg_uuid_
Definition: sg.h:66
virtual KeyPtr GetDBRequestKey() const
Definition: sg.cc:39
Definition: sg.h:73
bool ChangeHandler(DBEntry *entry, const DBRequest *req)
Definition: sg.cc:70
static DBTableBase * CreateTable(DB *db, const std::string &name)
Definition: sg.cc:101
virtual DBEntry * OperDBAdd(const DBRequest *req)
Definition: sg.cc:55
virtual bool OperDBDelete(DBEntry *entry, const DBRequest *req)
Definition: sg.cc:95
static SgTable * sg_table_
Definition: sg.h:96
static const uint32_t kInvalidSgId
Definition: sg.h:75
bool ProcessConfig(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
Definition: sg.cc:133
virtual bool OperDBOnChange(DBEntry *entry, const DBRequest *req)
Definition: sg.cc:63
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *k) const
Definition: sg.cc:49
virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, const std::string &context)
Definition: sg.cc:249
virtual bool IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u)
Definition: sg.cc:107
virtual bool IFNodeToReq(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
Definition: sg.cc:115
uint8_t type
Definition: load_balance.h:2
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:19
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:20
Definition: acl.h:62
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
Definition: sg.h:21
boost::uuids::uuid egress_acl_id_
Definition: sg.h:32
boost::uuids::uuid ingress_acl_id_
Definition: sg.h:33
uint32_t sg_id_
Definition: sg.h:31
Definition: sg.h:14
boost::uuids::uuid sg_uuid_
Definition: sg.h:18
boost::uuids::uuid uuid