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