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