OpenSDN source code
routing_policy_match.h
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 
6 #ifndef SRC_BGP_ROUTING_POLICY_ROUTING_POLICY_MATCH_H_
7 #define SRC_BGP_ROUTING_POLICY_ROUTING_POLICY_MATCH_H_
8 
9 #include <stdint.h>
10 
11 #include <set>
12 #include <string>
13 #include <typeinfo>
14 #include <utility>
15 #include <vector>
16 
17 #include "base/regex.h"
18 #include "bgp/bgp_config.h"
19 #include "bgp/inet/inet_route.h"
20 #include "bgp/inet6/inet6_route.h"
21 
22 class BgpAttr;
23 class BgpPath;
24 class BgpRoute;
25 class Community;
26 
28 public:
29  virtual std::string ToString() const = 0;
30  virtual ~RoutingPolicyMatch() {
31  }
32  bool operator()(const BgpRoute *route,
33  const BgpPath *path, const BgpAttr *attr) const {
34  return Match(route, path, attr);
35  }
36  virtual bool Match(const BgpRoute *route,
37  const BgpPath *path, const BgpAttr *attr) const = 0;
38  virtual bool operator==(const RoutingPolicyMatch &match) const {
39  if (typeid(match) == typeid(*this))
40  return(IsEqual(match));
41  return false;
42  }
43  virtual bool operator!=(const RoutingPolicyMatch &match) const {
44  return !operator==(match);
45  }
46  virtual bool IsEqual(const RoutingPolicyMatch &match) const = 0;
47 };
48 
50 public:
51  typedef std::set<uint32_t> CommunityList;
52  typedef std::vector<std::string> CommunityRegexStringList;
53  typedef std::vector<contrail::regex> CommunityRegexList;
54 
55  MatchCommunity(const std::vector<std::string> &communities, bool match_all);
56  virtual ~MatchCommunity();
57  virtual bool Match(const BgpRoute *route,
58  const BgpPath *path, const BgpAttr *attr) const;
59  virtual std::string ToString() const;
60  virtual bool IsEqual(const RoutingPolicyMatch &community) const;
61  bool match_all() const { return match_all_; }
62  const CommunityList &communities() const { return to_match_; }
65  }
66  const CommunityRegexList &regexs() const { return to_match_regexs_; }
67 
68 private:
69  bool MatchAll(const BgpAttr *attr) const;
70  bool MatchAny(const BgpAttr *attr) const;
71 
72  bool match_all_;
76 };
77 
79 public:
80  typedef std::vector<std::string> CommunityRegexStringList;
81  typedef std::vector<contrail::regex> CommunityRegexList;
82 
83  MatchExtCommunity(const std::vector<std::string> &communities,
84  bool match_all);
85  virtual ~MatchExtCommunity();
86  virtual bool Match(const BgpRoute *route,
87  const BgpPath *path, const BgpAttr *attr) const;
88  virtual std::string ToString() const;
89  virtual bool IsEqual(const RoutingPolicyMatch &community) const;
90  bool match_all() const { return match_all_; }
92  return to_match_;
93  }
96  }
97  const CommunityRegexList &regexs() const { return to_match_regexs_; }
98  bool Find(const ExtCommunity::ExtCommunityValue &community) const;
99 
100 private:
101  bool MatchAll(const BgpAttr *attr) const;
102  bool MatchAny(const BgpAttr *attr) const;
104  const std::string &comm);
105 
110 };
111 
113 public:
124  BGPaaS
125  };
126  typedef std::vector<MatchProtocolType> PathSourceList;
127  explicit MatchProtocol(const std::vector<std::string> &protocols);
128  virtual ~MatchProtocol();
129  virtual bool Match(const BgpRoute *route,
130  const BgpPath *path, const BgpAttr *attr) const;
131  virtual std::string ToString() const;
132  virtual bool IsEqual(const RoutingPolicyMatch &community) const;
133  const PathSourceList &protocols() const {
134  return to_match_;
135  }
136 
137 private:
139 };
140 const std::string MatchProtocolToString(MatchProtocol::MatchProtocolType protocol);
141 
142 template <typename T1, typename T2>
144  typedef T1 RouteT;
145  typedef T2 PrefixT;
146 };
147 
150 
151 template <typename T>
153 public:
154  typedef typename T::RouteT RouteT;
155  typedef typename T::PrefixT PrefixT;
156  enum MatchType {
160  };
161 
162  class PrefixMatch {
163  public:
166  }
167  bool operator==(const PrefixMatch &rhs) const {
168  return (prefix == rhs.prefix && match_type == rhs.match_type);
169  }
170  bool operator<(const PrefixMatch &rhs) const {
173  return false;
174  }
175 
178  };
179 
180  explicit MatchPrefix(const PrefixMatchConfigList &match_config_list);
181  virtual ~MatchPrefix();
182  virtual bool Match(const BgpRoute *route,
183  const BgpPath *path, const BgpAttr *attr) const;
184  virtual std::string ToString() const;
185  virtual bool IsEqual(const RoutingPolicyMatch &prefix) const;
186 
187  static MatchType GetMatchType(const std::string &match_type_str);
188 
189 private:
190  template <typename U> friend class MatchPrefixTest;
191  typedef std::vector<PrefixMatch> PrefixMatchList;
192 
194 };
195 
198 
199 #endif // SRC_BGP_ROUTING_POLICY_ROUTING_POLICY_MATCH_H_
std::vector< PrefixMatchConfig > PrefixMatchConfigList
Definition: bgp_config.h:359
std::vector< ExtCommunityValue > ExtCommunityList
Definition: community.h:156
boost::array< uint8_t, 8 > ExtCommunityValue
Definition: community.h:155
std::set< uint32_t > CommunityList
virtual bool Match(const BgpRoute *route, const BgpPath *path, const BgpAttr *attr) const
const CommunityRegexStringList & regex_strings() const
bool MatchAny(const BgpAttr *attr) const
const CommunityRegexList & regexs() const
CommunityList to_match_
bool match_all() const
virtual bool IsEqual(const RoutingPolicyMatch &community) const
std::vector< std::string > CommunityRegexStringList
MatchCommunity(const std::vector< std::string > &communities, bool match_all)
const CommunityList & communities() const
CommunityRegexStringList to_match_regex_strings_
std::vector< contrail::regex > CommunityRegexList
CommunityRegexList to_match_regexs_
bool MatchAll(const BgpAttr *attr) const
virtual std::string ToString() const
CommunityRegexList to_match_regexs_
virtual bool Match(const BgpRoute *route, const BgpPath *path, const BgpAttr *attr) const
const ExtCommunity::ExtCommunityList ExtCommunityFromString(const std::string &comm)
std::vector< contrail::regex > CommunityRegexList
const CommunityRegexStringList & regex_strings() const
const CommunityRegexList & regexs() const
bool Find(const ExtCommunity::ExtCommunityValue &community) const
CommunityRegexStringList to_match_regex_strings_
ExtCommunity::ExtCommunityList to_match_
bool MatchAny(const BgpAttr *attr) const
virtual std::string ToString() const
MatchExtCommunity(const std::vector< std::string > &communities, bool match_all)
virtual bool IsEqual(const RoutingPolicyMatch &community) const
bool MatchAll(const BgpAttr *attr) const
std::vector< std::string > CommunityRegexStringList
const ExtCommunity::ExtCommunityList & communities() const
bool operator==(const PrefixMatch &rhs) const
bool operator<(const PrefixMatch &rhs) const
PrefixMatch(PrefixT prefix, MatchType match_type)
std::vector< PrefixMatch > PrefixMatchList
virtual bool IsEqual(const RoutingPolicyMatch &prefix) const
virtual std::string ToString() const
PrefixMatchList match_list_
MatchPrefix(const PrefixMatchConfigList &match_config_list)
friend class MatchPrefixTest
virtual bool Match(const BgpRoute *route, const BgpPath *path, const BgpAttr *attr) const
static MatchType GetMatchType(const std::string &match_type_str)
std::vector< MatchProtocolType > PathSourceList
virtual std::string ToString() const
const PathSourceList & protocols() const
virtual bool Match(const BgpRoute *route, const BgpPath *path, const BgpAttr *attr) const
PathSourceList to_match_
virtual bool IsEqual(const RoutingPolicyMatch &community) const
MatchProtocol(const std::vector< std::string > &protocols)
virtual bool Match(const BgpRoute *route, const BgpPath *path, const BgpAttr *attr) const =0
virtual bool operator!=(const RoutingPolicyMatch &match) const
virtual bool operator==(const RoutingPolicyMatch &match) const
virtual bool IsEqual(const RoutingPolicyMatch &match) const =0
bool operator()(const BgpRoute *route, const BgpPath *path, const BgpAttr *attr) const
virtual std::string ToString() const =0
const std::string MatchProtocolToString(MatchProtocol::MatchProtocolType protocol)
MatchPrefix< PrefixMatchInet6 > MatchPrefixInet6
PrefixMatchBase< Inet6Route, Inet6Prefix > PrefixMatchInet6
PrefixMatchBase< InetRoute, Ip4Prefix > PrefixMatchInet
MatchPrefix< PrefixMatchInet > MatchPrefixInet
#define BOOL_KEY_COMPARE(x, y)
Definition: util.h:65