OpenSDN source code
policy.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 <boost/uuid/uuid_io.hpp>
7 #include "base/logging.h"
8 #include "filter/policy.h"
9 #include "filter/acl.h"
11 
12 void PolicyData::Init(const PolicyConfigSpec &policy_cfg) {
13  vpc_id_ = policy_cfg.vpc_id;
14  policy_id_ = policy_cfg.vpc_id;
15  name_ = policy_cfg.name;
16  inbound_ = policy_cfg.inbound;
17  acl_id_ = policy_cfg.acl_id;
18 }
19 
20 Policy::Policy(const uuid id) : policy_id_(id)
21 {
22  std::stringstream ss;
23  ss << policy_id_;
24  LOG(DEBUG, "Create Policy - " << ss.str());
25  inbound_acls_.clear();
26  outbound_acls_.clear();
27 }
28 
30 {
31  AclPtrList::iterator it;
32  LOG(DEBUG, "Policy::~Policy");
33  for (it = inbound_acls_.begin(); it != inbound_acls_.end(); it++) {
34  (*it)->~Acl();
35  }
36  inbound_acls_.clear();
37  for (it = outbound_acls_.begin(); it != outbound_acls_.end(); it++) {
38  (*it)->~Acl();
39  }
40  outbound_acls_.clear();
41 }
42 
43 void Policy::Init(const PolicyData *policy_data)
44 {
45  vpc_id_ = policy_data->vpc_id_;
46  policy_id_ = policy_data->policy_id_;
47  name_ = policy_data->name_;
48 
49  std::stringstream ss;
50  ss << policy_id_;
51  LOG(DEBUG, "Init: " << ss.str() << " Name: " << name_);
52  AclPtr a = AclPtr(new Acl(policy_data->acl_id_));
53  if (policy_data->inbound_) {
54  inbound_acls_.push_back(a);
55  } else {
56  outbound_acls_.push_back(a);
57  }
58 }
59 
60 AclPtr Policy::FindAcl(const bool inbound, const uuid acl_id)
61 {
62  AclPtrList::iterator it;
63  AclPtrList *acls;
64  if (inbound) {
65  acls = &inbound_acls_;
66  } else {
67  acls = &outbound_acls_;
68  }
69  for (it = acls->begin(); it < acls->end(); it++) {
70  if (acl_id == (*it)->acl_id()) {
71  return (*it);
72  }
73  }
74  return NULL;
75 }
76 
77 void Policy::InsertAcl(const bool inbound, const AclPtr acl)
78 {
79  if (inbound) {
80  inbound_acls_.push_back(acl);
81  } else {
82  outbound_acls_.push_back(acl);
83  }
84 }
85 
86 void Policy::DeleteAcl(const bool inbound, const uuid acl_id)
87 {
88  AclPtrList::iterator it;
89  AclPtrList *acls;
90  if (inbound) {
91  acls = &inbound_acls_;
92  } else {
93  acls = &outbound_acls_;
94  }
95  for (it = acls->begin(); it < acls->end(); it++) {
96  if (acl_id == (*it)->acl_id()) {
97  acls->erase(it);
98  return;
99  }
100  }
101 }
102 
103 bool Policy::PacketMatch(const PacketHeader &packet_header,
104  const bool inbound,
106 {
107  AclPtrList::iterator it;
108  AclPtrList *acls;
109  bool terminal_rule;
110  bool ret_value = false;
111 
112  if (inbound) {
113  acls = &inbound_acls_;
114  } else {
115  acls = &outbound_acls_;
116  }
117  for (it = acls->begin(); it < acls->end(); it++) {
118  if ((*it)->PMatch(packet_header, sal, terminal_rule)) {
119  ret_value = true;
120  if (terminal_rule == true) {
121  return ret_value;
122  }
123  }
124  }
125  return ret_value;
126 }
127 
128 bool Policy::IsLess(const DBEntry &rhs) const {
129  const Policy &a = static_cast<const Policy &>(rhs);
130  return policy_id_ < a.policy_id_;
131 }
132 
133 std::string Policy::ToString() const {
134  std::string str = "Policy ";
135  str.append(name_);
136  return str;
137 }
138 
140  PolicyKey *key = new PolicyKey(policy_id_);
141  LOG(DEBUG, "Policy::GetDBRequestKey");
142  return DBEntryBase::KeyPtr(key);
143 }
144 
145 void Policy::SetKey(const DBRequestKey *key) {
146  const PolicyKey *k = static_cast<const PolicyKey *>(key);
147  LOG(DEBUG, "Policy::SetKey");
148  policy_id_ = k->id_;
149 }
150 
151 void Policy::SetName(const std::string str) {
152  name_ = str;
153 }
154 
155 std::unique_ptr<DBEntry> PolicyTable::AllocEntry(const DBRequestKey *key) const {
156  const PolicyKey *k = static_cast<const PolicyKey *>(key);
157  Policy *p = new Policy(k->id_);
158  return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(p));
159 }
160 
162  PolicyKey *key = static_cast<PolicyKey *>(req->key.get());
163  PolicyData *data = static_cast<PolicyData *>(req->data.get());
164  LOG(DEBUG, "PolicyTable::Add");
165  Policy *policy = new Policy(key->id_);
166  policy->Init(data);
167  return policy;
168 }
169 
170 bool PolicyTable::OnChange(DBEntry *entry, const DBRequest *req) {
171  PolicyData *data = static_cast<PolicyData *>(req->data.get());
172  Policy *p = static_cast<Policy *>(entry);
173 
174  LOG(DEBUG, "PolicyTable::Change");
175  p->SetName(data->name_);
176  if (p->FindAcl(data->inbound_, data->acl_id_) == NULL) {
177  AclPtr a = AclPtr(new Acl(data->acl_id_));
178  p->InsertAcl(data->inbound_, a);
179  }
180  return true;
181 }
182 
183 void PolicyTable::Delete(DBEntry *entry, const DBRequest *req) {
184  LOG(DEBUG, "PolicyTable::Delete");
185  return;
186 }
187 
188 DBTableBase *PolicyTable::CreateTable(DB *db, const std::string &name) {
189  PolicyTable *table = new PolicyTable(db, name);
190  LOG(DEBUG, "CreateTable" << name);
191  table->Init();
192  return table;
193 }
194 
197 }
std::list< TrafficAction * > ActionList
Definition: acl_entry.h:95
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
const std::string & name() const
Definition: db_table.h:111
void Init()
Definition: db_table.cc:414
Definition: db.h:25
static void RegisterFactory(const std::string &prefix, CreateFunction create_fn)
Definition: db.cc:24
static void Register()
Definition: policy.cc:195
DBEntry * Add(const DBRequest *req)
Definition: policy.cc:161
void Delete(DBEntry *entry, const DBRequest *req)
Definition: policy.cc:183
virtual bool OnChange(DBEntry *entry, const DBRequest *req)
Definition: policy.cc:170
static DBTableBase * CreateTable(DB *db, const std::string &name)
Definition: policy.cc:188
PolicyTable(DB *db, const std::string &name)
virtual std::unique_ptr< DBEntry > AllocEntry(const DBRequestKey *key) const
Definition: policy.cc:155
AclPtrList outbound_acls_
AclPtr FindAcl(const bool inbound, const uuid acl_id)
Definition: policy.cc:60
std::string name_
std::vector< AclPtr > AclPtrList
void DeleteAcl(const bool inbound, const uuid acl_id)
Definition: policy.cc:86
void SetName(const std::string str)
Definition: policy.cc:151
KeyPtr GetDBRequestKey() const
Definition: policy.cc:139
AclPtrList inbound_acls_
bool IsLess(const DBEntry &rhs) const
Definition: policy.cc:128
void Init(const PolicyData *req)
Definition: policy.cc:43
void InsertAcl(const bool inbound, const AclPtr acl)
Definition: policy.cc:77
bool PacketMatch(const PacketHeader &packet_header, const bool inbound, AclEntry::ActionList &sal)
Definition: policy.cc:103
std::string ToString() const
Definition: policy.cc:133
void SetKey(const DBRequestKey *key)
Definition: policy.cc:145
~Policy()
Definition: policy.cc:29
#define LOG(_Level, _Msg)
Definition: logging.h:35
std::unique_ptr< DBRequestKey > key
Definition: db_table.h:49
std::unique_ptr< DBRequestData > data
Definition: db_table.h:50
void Init(const PolicyConfigSpec &policy_cfg)
Definition: policy.cc:12
boost::uuids::uuid uuid