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