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