OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
routing_policy_action.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 
7 #include <boost/foreach.hpp>
8 
9 #include <algorithm>
10 #include <sstream>
11 
12 #include "bgp/bgp_attr.h"
13 #include "bgp/bgp_server.h"
14 #include "bgp/community.h"
15 
16 using std::copy;
17 using std::ostringstream;
18 using std::string;
19 using std::vector;
20 
21 UpdateAsPath::UpdateAsPath(const vector<uint32_t> &asn_list)
22  : asn_list_(asn_list) {
23 }
24 
25 void UpdateAsPath::operator()(BgpAttr *attr) const {
26  if (!attr)
27  return;
28  BgpAttrDB *attr_db = attr->attr_db();
29  BgpServer *server = attr_db->server();
30  if (!server->enable_4byte_as()) {
31  const AsPath *as_path = attr->as_path();
32  if (as_path) {
33  const AsPathSpec &as_path_spec = as_path->path();
34  AsPathSpec *as_path_spec_ptr = as_path_spec.Add(asn_list_);
35  attr->set_as_path(as_path_spec_ptr);
36  delete as_path_spec_ptr;
37  } else {
38  AsPathSpec as_path_spec;
39  AsPathSpec *as_path_spec_ptr = as_path_spec.Add(asn_list_);
40  attr->set_as_path(as_path_spec_ptr);
41  delete as_path_spec_ptr;
42  }
43  } else {
44  const AsPath4Byte *as_path = attr->aspath_4byte();
45  if (as_path) {
46  const AsPath4ByteSpec &as_path_spec = as_path->path();
47  AsPath4ByteSpec *as_path_spec_ptr = as_path_spec.Add(asn_list_);
48  attr->set_aspath_4byte(as_path_spec_ptr);
49  delete as_path_spec_ptr;
50  } else {
51  AsPath4ByteSpec as_path_spec;
52  AsPath4ByteSpec *as_path_spec_ptr = as_path_spec.Add(asn_list_);
53  attr->set_aspath_4byte(as_path_spec_ptr);
54  delete as_path_spec_ptr;
55  }
56  }
57 }
58 
59 string UpdateAsPath::ToString() const {
60  ostringstream oss;
61  oss << "as-path expand [ ";
62  BOOST_FOREACH(uint32_t asn, asn_list_) {
63  oss << asn << ",";
64  }
65  oss.seekp(-1, oss.cur);
66  oss << " ]";
67  return oss.str();
68 }
69 
70 bool UpdateAsPath::IsEqual(const RoutingPolicyAction &as_path) const {
71  const UpdateAsPath in_as_path = static_cast<const UpdateAsPath&>(as_path);
72  return (asn_list_ == in_as_path.asn_list_);
73 }
74 
75 UpdateCommunity::UpdateCommunity(const std::vector<string> communities,
76  string op) {
77  BOOST_FOREACH(const string &community, communities) {
78  uint32_t value = CommunityType::CommunityFromString(community);
79  if (value) communities_.push_back(value);
80  }
81  std::sort(communities_.begin(), communities_.end());
82  std::vector<uint32_t>::iterator it =
83  std::unique(communities_.begin(), communities_.end());
84  communities_.erase(it, communities_.end());
85 
86  if (strcmp(op.c_str(), "add") == 0) {
87  op_ = ADD;
88  } else if (strcmp(op.c_str(), "remove") == 0) {
89  op_ = REMOVE;
90  } else if (strcmp(op.c_str(), "set") == 0) {
91  op_ = SET;
92  }
93 }
94 
96  if (!attr) return;
97  const Community *comm = attr->community();
98  BgpAttrDB *attr_db = attr->attr_db();
99  BgpServer *server = attr_db->server();
100  CommunityDB *comm_db = server->comm_db();
101  CommunityPtr new_community = NULL;
102  if (op_ == SET) {
103  new_community = comm_db->SetAndLocate(comm, communities_);
104  } else if (op_ == ADD) {
105  new_community = comm_db->AppendAndLocate(comm, communities_);
106  } else if (op_ == REMOVE) {
107  if (comm) new_community = comm_db->RemoveAndLocate(comm, communities_);
108  }
109  attr->set_community(new_community);
110 }
111 
113  ostringstream oss;
114  if (op_ == SET) oss << "community set [ ";
115  else if (op_ == ADD) oss << "community add [ ";
116  else if (op_ == REMOVE) oss << "community remove [ ";
117 
118  BOOST_FOREACH(uint32_t community, communities()) {
119  string name = CommunityType::CommunityToString(community);
120  oss << name << ",";
121  }
122  oss.seekp(-1, oss.cur);
123  oss << " ]";
124  return oss.str();
125 }
126 
127 bool UpdateCommunity::IsEqual(const RoutingPolicyAction &community) const {
128  const UpdateCommunity in_comm =
129  static_cast<const UpdateCommunity&>(community);
130  if (op_ == in_comm.op_)
131  return (communities() == in_comm.communities());
132  return false;
133 }
134 
135 UpdateExtCommunity::UpdateExtCommunity(const std::vector<string> &communities,
136  string op) {
137  BOOST_FOREACH(const string &community, communities) {
138  const ExtCommunity::ExtCommunityList list =
140  if (list.size())
141  communities_.push_back(list[0]);
142  }
143  std::sort(communities_.begin(), communities_.end());
144  ExtCommunity::ExtCommunityList::iterator it =
145  std::unique(communities_.begin(), communities_.end());
146  communities_.erase(it, communities_.end());
147 
148  if (strcmp(op.c_str(), "add") == 0) {
149  op_ = ADD;
150  } else if (strcmp(op.c_str(), "remove") == 0) {
151  op_ = REMOVE;
152  } else if (strcmp(op.c_str(), "set") == 0) {
153  op_ = SET;
154  }
155 }
156 
158  if (!attr) return;
159  const ExtCommunity *comm = attr->ext_community();
160  BgpAttrDB *attr_db = attr->attr_db();
161  BgpServer *server = attr_db->server();
162  ExtCommunityDB *comm_db = server->extcomm_db();
163  ExtCommunityPtr new_community = NULL;
164  if (op_ == SET) {
165  new_community = comm_db->SetAndLocate(comm, communities_);
166  } else if (op_ == ADD) {
167  new_community = comm_db->AppendAndLocate(comm, communities_);
168  } else if (op_ == REMOVE) {
169  new_community = comm_db->RemoveAndLocate(comm, communities_);
170  }
171  attr->set_ext_community(new_community);
172 }
173 
175  ostringstream oss;
176  if (op_ == SET) oss << "Extcommunity set [ ";
177  else if (op_ == ADD) oss << "Extcommunity add [ ";
178  else if (op_ == REMOVE) oss << "Extcommunity remove [ ";
179 
180  BOOST_FOREACH(ExtCommunity::ExtCommunityValue community, communities()) {
181  string name = ExtCommunity::ToString(community);
182  oss << name << ",";
183  }
184  oss.seekp(-1, oss.cur);
185  oss << " ]";
186  return oss.str();
187 }
188 
189 bool UpdateExtCommunity::IsEqual(const RoutingPolicyAction &community) const {
190  const UpdateExtCommunity in_comm =
191  static_cast<const UpdateExtCommunity&>(community);
192  if (op_ == in_comm.op_)
193  return (communities() == in_comm.communities());
194  return false;
195 }
196 
198  : local_pref_(local_pref) {
199 }
200 
203 }
204 
206  ostringstream oss;
207  oss << "local-pref " << local_pref_;
208  return oss.str();
209 }
210 
211 bool UpdateLocalPref::IsEqual(const RoutingPolicyAction &local_pref) const {
212  const UpdateLocalPref in_lp =
213  static_cast<const UpdateLocalPref&>(local_pref);
214  return (local_pref_ == in_lp.local_pref_);
215 }
216 
217 UpdateMed::UpdateMed(uint32_t med)
218  : med_(med) {
219 }
220 
221 void UpdateMed::operator()(BgpAttr *attr) const {
222  attr->set_med(med_);
223 }
224 
225 string UpdateMed::ToString() const {
226  ostringstream oss;
227  oss << "med " << med_;
228  return oss.str();
229 }
230 
231 bool UpdateMed::IsEqual(const RoutingPolicyAction &med) const {
232  const UpdateMed in_med =
233  static_cast<const UpdateMed&>(med);
234  return (med_ == in_med.med_);
235 }
const Community * community() const
Definition: bgp_attr.h:914
ExtCommunityPtr AppendAndLocate(const ExtCommunity *src, const ExtCommunity::ExtCommunityList &list)
Definition: community.cc:696
CommunityList communities_
boost::array< uint8_t, 8 > ExtCommunityValue
Definition: community.h:152
const AsPathSpec & path() const
Definition: bgp_aspath.h:127
virtual bool IsEqual(const RoutingPolicyAction &med) const
UpdateExtCommunity(const std::vector< std::string > &communities, std::string op)
ExtCommunity::ExtCommunityList communities_
AsPathSpec * Add(as2_t asn) const
Definition: bgp_aspath.cc:130
CommunityDB * comm_db()
Definition: bgp_server.h:184
virtual bool IsEqual(const RoutingPolicyAction &local_pref) const
static const std::string CommunityToString(uint32_t comm)
CommunityPtr RemoveAndLocate(const Community *src, const std::vector< uint32_t > &value)
Definition: community.cc:170
std::vector< uint32_t > asn_list_
void set_community(CommunityPtr comm)
Definition: bgp_attr.cc:999
ExtCommunityPtr RemoveAndLocate(const ExtCommunity *src, const ExtCommunity::ExtCommunityList &list)
Definition: community.cc:716
UpdateMed(uint32_t med)
void set_as_path(AsPathPtr aspath)
Definition: bgp_attr.cc:955
const ExtCommunity::ExtCommunityList & communities() const
BgpAttrDB * attr_db()
Definition: bgp_attr.h:928
virtual void operator()(BgpAttr *out_attr) const
std::string ToString() const
const AsPath4Byte * aspath_4byte() const
Definition: bgp_attr.h:902
virtual bool IsEqual(const RoutingPolicyAction &community) const
std::string ToString() const
ExtCommunityDB * extcomm_db()
Definition: bgp_server.h:187
CommunityPtr AppendAndLocate(const Community *src, uint32_t value)
Definition: community.cc:131
std::string ToString() const
boost::intrusive_ptr< const ExtCommunity > ExtCommunityPtr
Definition: community.h:448
CommunityPtr SetAndLocate(const Community *src, const std::vector< uint32_t > &value)
Definition: community.cc:157
static ExtCommunityList ExtCommunityFromString(const std::string &comm)
Definition: community.cc:305
virtual bool IsEqual(const RoutingPolicyAction &as_path) const
AsPath4ByteSpec * Add(as_t asn) const
Definition: bgp_aspath.cc:392
static std::string ToString(const ExtCommunityValue &val)
Definition: community.cc:361
BgpServer * server()
Definition: bgp_attr.h:1031
UpdateCommunity(const std::vector< std::string > communities, std::string op)
virtual void operator()(BgpAttr *out_attr) const
boost::intrusive_ptr< const Community > CommunityPtr
Definition: community.h:109
const AsPath * as_path() const
Definition: bgp_attr.h:899
void set_local_pref(uint32_t local_pref)
Definition: bgp_attr.h:839
virtual void operator()(BgpAttr *out_attr) const
const ExtCommunity * ext_community() const
Definition: bgp_attr.h:915
const AsPath4ByteSpec & path() const
Definition: bgp_aspath.h:285
std::string ToString() const
std::vector< ExtCommunityValue > ExtCommunityList
Definition: community.h:153
void set_med(uint32_t med)
Definition: bgp_attr.h:838
const CommunityList & communities() const
virtual bool IsEqual(const RoutingPolicyAction &community) const
UpdateLocalPref(uint32_t local_pref)
bool enable_4byte_as() const
Definition: bgp_server.cc:726
virtual void operator()(BgpAttr *out_attr) const
void set_ext_community(ExtCommunityPtr extcomm)
Definition: bgp_attr.cc:1011
virtual void operator()(BgpAttr *out_attr) const
void set_aspath_4byte(AsPath4BytePtr aspath)
Definition: bgp_attr.cc:979
static uint32_t CommunityFromString(const std::string &comm, boost::system::error_code *perr=NULL)
std::string ToString() const
ExtCommunityPtr SetAndLocate(const ExtCommunity *src, const ExtCommunity::ExtCommunityList &list)
Definition: community.cc:944
UpdateAsPath(const std::vector< uint32_t > &asn_list)
CommunityUpdateOp op_