OpenSDN source code
policy_set.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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 <agent_types.h>
9 #include <cfg/cfg_init.h>
10 #include <ifmap/ifmap_node.h>
12 #include "base/logging.h"
13 #include "filter/acl.h"
14 #include "filter/policy_set.h"
15 #include "oper/config_manager.h"
16 #include "oper/agent_sandesh.h"
17 
19  uuid_(uuid), global_(false) {
20 }
21 
23  fw_policy_list_.clear();
24 }
25 
26 bool PolicySet::IsLess(const DBEntry &db) const {
27  const PolicySet &rhs = static_cast<const PolicySet&>(db);
28  return uuid_ < rhs.uuid_;
29 }
30 
31 std::string PolicySet::ToString() const {
32  std::string str = "Policy set ";
33  str.append(name_);
34  str.append(" Uuid :");
35  str.append(UuidToString(uuid_));
36  return str;
37 }
38 
40  PolicySetKey *key = new PolicySetKey(uuid_);
41  return DBEntryBase::KeyPtr(key);
42 }
43 
45  const PolicySetKey *key = static_cast<const PolicySetKey *>(k);
46  uuid_ = key->uuid_;
47 }
48 
49 bool PolicySet::Change(PolicySetTable *table, const PolicySetData *data) {
50  bool ret = false;
51  bool resync_intf = false;
52 
55  ret = true;
56  fw_policy_list_.clear();
57  FirewallPolicyUuidList::const_iterator it = fw_policy_uuid_list_.begin();
58  for(; it != fw_policy_uuid_list_.end(); it++) {
59  AclKey key(it->second);
60  AclDBEntry *acl = static_cast<AclDBEntry *>(table->agent()->
61  acl_table()->FindActiveEntry(&key));
62  if (acl) {
63  fw_policy_list_.push_back(acl);
64  }
65  }
66  }
67 
68  if (data->global_ != global_) {
69  global_ = data->global_;
70  ret = true;
71  }
72 
73  if (global_) {
74  if (table->global_policy_set() != this) {
75  table->set_global_policy_set(this);
76  }
77 
78  if (ret) {
79  resync_intf = true;
80  }
81  } else if (global_ == false && table->global_policy_set() == this) {
82  table->set_global_policy_set(NULL);
83  resync_intf = true;
84  }
85 
86  if (resync_intf) {
88  }
89 
90  return ret;
91 }
92 
93 bool PolicySet::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
94  ApplicationPolicySetResp *resp = static_cast<ApplicationPolicySetResp*>(sresp);
95 
96  ApplicationPolicySetSandeshData apsd;
97  apsd.set_name(name_);
98  apsd.set_uuid(UuidToString(uuid_));
99 
100  std::vector<PolicyLinkData> acl_list;
101  FirewallPolicyList::const_iterator it = fw_policy_list_.begin();
102  for(; it != fw_policy_list_.end(); it++) {
103  PolicyLinkData pld;
104  pld.set_firewall_policy(UuidToString((*it)->GetUuid()));
105  acl_list.push_back(pld);
106  }
107 
108  apsd.set_firewall_policy_list(acl_list);
109  apsd.set_all_applications(global_);
110 
111  std::vector<ApplicationPolicySetSandeshData> &list =
112  const_cast<std::vector<ApplicationPolicySetSandeshData>&>(
113  resp->get_application_policy_set_list());
114  list.push_back(apsd);
115  return true;
116 }
117 
118 std::unique_ptr<DBEntry>
120  const PolicySetKey *psk = static_cast<const PolicySetKey *>(key);
121  PolicySet *ps = new PolicySet(psk->uuid_);
122  return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(ps));
123 }
124 
126  return new PolicySetKey(u);
127 }
128 
130  Agent *agent, IFMapNode *node, boost::uuids::uuid &fp_uuid) {
131 
132  IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
133  DBGraph *graph = table->GetGraph();
134 
135  for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
136  iter != node->end(table->GetGraph()); ++iter) {
137 
138  IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
139  if (agent->config_manager()->SkipNode(adj_node,
140  agent->cfg()->cfg_firewall_policy_table())) {
141  continue;
142  }
143 
144  autogen::FirewallPolicy *fp =
145  static_cast<autogen::FirewallPolicy *>(adj_node->GetObject());
146  autogen::IdPermsType id_perms = fp->id_perms();
147  CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong,
148  fp_uuid);
149  return true;
150  }
151 
152  return false;
153 }
154 
155 static PolicySetData* BuildData(Agent *agent, IFMapNode *node,
156  const autogen::ApplicationPolicySet *ps) {
157 
158  IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
159  DBGraph *graph = table->GetGraph();
161  bool global = ps->all_applications();
162 
163  for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
164  iter != node->end(table->GetGraph()); ++iter) {
165 
166  IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
167  if (agent->config_manager()->SkipNode(adj_node,
169  continue;
170  }
171 
172  boost::uuids::uuid fp_uuid = boost::uuids::nil_uuid();
173  autogen::ApplicationPolicySetFirewallPolicy *aps_fp =
174  static_cast<autogen::ApplicationPolicySetFirewallPolicy *>
175  (adj_node->GetObject());
176 
177  if (aps_fp->data().sequence == Agent::NullString()) {
178  continue;
179  }
180 
181  if (BuildFirewallPolicy(agent, adj_node, fp_uuid) == false) {
182  continue;
183  }
184 
185  list.insert(FirewallPolicyPair(aps_fp->data().sequence, fp_uuid));
186  }
187 
188  return new PolicySetData(agent, node, node->name(), global, list);
189 }
190 
192  const boost::uuids::uuid &u) {
193  autogen::ApplicationPolicySet *ps =
194  static_cast<autogen::ApplicationPolicySet *>(node->GetObject());
195  assert(ps);
196 
197  assert(!u.is_nil());
198 
199  req.key.reset(BuildKey(u));
200  if ((req.oper == DBRequest::DB_ENTRY_DELETE) || node->IsDeleted()) {
202  return true;
203  }
204 
206  return false;
207 }
208 
210  const boost::uuids::uuid &u) {
211  autogen::ApplicationPolicySet *ps =
212  static_cast <autogen::ApplicationPolicySet *>(node->GetObject());
213  assert(ps);
214 
215  req.key.reset(BuildKey(u));
216  if (node->IsDeleted()) {
217  return false;
218  }
219 
221  req.data.reset(BuildData(agent(), node, ps));
222  return true;
223 }
224 
226  autogen::ApplicationPolicySet *ps =
227  static_cast<autogen::ApplicationPolicySet *>(node->GetObject());
228  autogen::IdPermsType id_perms = ps->id_perms();
229  CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, u);
230  return true;
231 }
232 
234  PolicySetKey *key = static_cast<PolicySetKey *>(req->key.get());
235  PolicySetData *data = static_cast<PolicySetData *>(req->data.get());
236  PolicySet *ps = new PolicySet(key->uuid_);
237  ps->name_ = data->name_;
238  ps->Change(this, data);
239  return ps;
240 }
241 
243  PolicySetData *data = static_cast<PolicySetData *>(req->data.get());
244  PolicySet *ps = static_cast<PolicySet *>(entry);
245  return ps->Change(this, data);
246 }
247 
249  return OperDBResync(entry, req);
250 }
251 
253  const DBRequest *req) {
254  PolicySet *ps = static_cast<PolicySet *>(entry);
255  if (ps->global() && global_policy_set() == ps) {
256  set_global_policy_set(NULL);
257  }
258  return true;
259 }
260 
262 PolicySetTable::CreateTable(DB *db, const std::string &name) {
263 
264  PolicySetTable *table = new PolicySetTable(db, name);
265  table->Init();
266  return table;
267 }
268 
269 void ApplicationPolicySetReq::HandleRequest() const {
270  AgentSandeshPtr sand(new AgentPolicySetSandesh(context(), get_uuid(),
271  get_name()));
272  sand->DoSandesh(sand);
273 }
274 
276  const std::string &context) {
277  return AgentSandeshPtr(new AgentPolicySetSandesh(context,
278  args->GetString("name"), args->GetString("uuid")));
279 }
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
Definition: acl.h:92
IFMapAgentTable * cfg_policy_set_firewall_policy_table() const
Definition: cfg_init.h:139
IFMapAgentTable * cfg_firewall_policy_table() const
Definition: cfg_init.h:115
IFMapAgentTable * cfg_vm_interface_table() const
Definition: cfg_init.h:23
Agent * agent() const
Definition: agent_db.h:216
std::string GetString(const std::string &key) const
Definition: agent.h:361
ConfigManager * config_manager() const
Definition: agent.cc:893
AgentConfig * cfg() const
Definition: agent.cc:869
static const std::string & NullString()
Definition: agent.h:440
void AddPolicySetNode(IFMapNode *node)
bool SkipNode(IFMapNode *node)
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)
const std::string & name() const
Definition: db_table.h:111
void Init()
Definition: db_table.cc:414
void NotifyAllEntries()
Definition: db_table.cc:623
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
void set_global_policy_set(PolicySet *ps)
Definition: policy_set.h:145
virtual DBEntry * OperDBAdd(const DBRequest *req)
Definition: policy_set.cc:233
virtual bool OperDBOnChange(DBEntry *entry, const DBRequest *req)
Definition: policy_set.cc:248
virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, const std::string &context)
Definition: policy_set.cc:275
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *key) const
Definition: policy_set.cc:119
static DBTableBase * CreateTable(DB *db, const std::string &name)
Definition: policy_set.cc:262
PolicySet * global_policy_set() const
Definition: policy_set.h:141
bool ProcessConfig(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
Definition: policy_set.cc:209
virtual bool OperDBDelete(DBEntry *entry, const DBRequest *req)
Definition: policy_set.cc:252
virtual bool IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u)
Definition: policy_set.cc:225
virtual bool IFNodeToReq(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
Definition: policy_set.cc:191
PolicySetTable(DB *db, const std::string &name)
Definition: policy_set.h:115
virtual bool OperDBResync(DBEntry *entry, const DBRequest *req)
Definition: policy_set.cc:242
bool global_
Definition: policy_set.h:109
virtual void SetKey(const DBRequestKey *key)
Definition: policy_set.cc:44
bool DBEntrySandesh(Sandesh *resp, std::string &name) const
Definition: policy_set.cc:93
virtual std::string ToString() const
Definition: policy_set.cc:31
bool Change(PolicySetTable *table, const PolicySetData *data)
Definition: policy_set.cc:49
boost::uuids::uuid uuid_
Definition: policy_set.h:105
bool global() const
Definition: policy_set.h:99
virtual bool IsLess(const DBEntry &rhs) const
Definition: policy_set.cc:26
FirewallPolicyList fw_policy_list_
Definition: policy_set.h:108
PolicySet(const boost::uuids::uuid &uuid)
Definition: policy_set.cc:18
FirewallPolicyUuidList fw_policy_uuid_list_
Definition: policy_set.h:107
std::string name_
Definition: policy_set.h:106
virtual KeyPtr GetDBRequestKey() const
Definition: policy_set.cc:39
static PolicySetData * BuildData(Agent *agent, IFMapNode *node, const autogen::ApplicationPolicySet *ps)
Definition: policy_set.cc:155
static bool BuildFirewallPolicy(Agent *agent, IFMapNode *node, boost::uuids::uuid &fp_uuid)
Definition: policy_set.cc:129
static PolicySetKey * BuildKey(const boost::uuids::uuid &u)
Definition: policy_set.cc:125
std::map< std::string, boost::uuids::uuid > FirewallPolicyUuidList
Definition: policy_set.h:51
std::pair< std::string, boost::uuids::uuid > FirewallPolicyPair
Definition: policy_set.h:52
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:139
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
const std::string name_
Definition: policy_set.h:61
FirewallPolicyUuidList fw_policy_uuid_list_
Definition: policy_set.h:63
boost::uuids::uuid uuid_
Definition: policy_set.h:43
boost::uuids::uuid uuid