vom: Add support for redirect contracts in gbp
[vpp.git] / extras / vom / vom / gbp_rule.cpp
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <sstream>
17
18 #include "vom/gbp_rule.hpp"
19
20 namespace VOM {
21 gbp_rule::next_hop_t::next_hop_t(const boost::asio::ip::address& ip,
22                                  const mac_address_t& mac,
23                                  uint32_t bd_id,
24                                  uint32_t rd_id)
25   : m_ip(ip)
26   , m_mac(mac)
27   , m_bd_id(bd_id)
28   , m_rd_id(rd_id)
29 {
30 }
31
32 std::string
33 gbp_rule::next_hop_t::to_string() const
34 {
35   std::ostringstream s;
36
37   s << "["
38     << "ip:" << m_ip << " mac:" << m_mac.to_string() << " bd:" << m_bd_id
39     << " rd:" << m_rd_id << "]";
40
41   return (s.str());
42 }
43
44 bool
45 gbp_rule::next_hop_t::operator<(const gbp_rule::next_hop_t& nh) const
46 {
47   return (nh.m_ip < m_ip);
48 }
49
50 bool
51 gbp_rule::next_hop_t::operator==(const gbp_rule::next_hop_t& nh) const
52 {
53   return ((m_ip == nh.m_ip) && (m_mac == nh.m_mac) && (m_bd_id == nh.m_bd_id) &&
54           (m_rd_id == nh.m_rd_id));
55 }
56
57 const boost::asio::ip::address&
58 gbp_rule::next_hop_t::getIp() const
59 {
60   return m_ip;
61 }
62
63 const mac_address_t&
64 gbp_rule::next_hop_t::getMac() const
65 {
66   return m_mac;
67 }
68
69 const uint32_t
70 gbp_rule::next_hop_t::getBdId() const
71 {
72   return m_bd_id;
73 }
74
75 const uint32_t
76 gbp_rule::next_hop_t::getRdId() const
77 {
78   return m_rd_id;
79 }
80
81 const gbp_rule::hash_mode_t gbp_rule::hash_mode_t::SRC_IP(1, "src-ip");
82 const gbp_rule::hash_mode_t gbp_rule::hash_mode_t::DST_IP(0, "dst-ip");
83 const gbp_rule::hash_mode_t gbp_rule::hash_mode_t::SYMMETRIC(2, "symmetric");
84
85 gbp_rule::hash_mode_t::hash_mode_t(int v, const std::string s)
86   : enum_base(v, s)
87 {
88 }
89
90 const gbp_rule::hash_mode_t&
91 gbp_rule::hash_mode_t::from_int(vapi_enum_gbp_hash_mode i)
92 {
93   if (i == GBP_API_HASH_MODE_SYMMETRIC)
94     return gbp_rule::hash_mode_t::SYMMETRIC;
95   else if (i == GBP_API_HASH_MODE_SRC_IP)
96     return gbp_rule::hash_mode_t::SRC_IP;
97
98   return gbp_rule::hash_mode_t::DST_IP;
99 }
100
101 gbp_rule::next_hop_set_t::next_hop_set_t(const gbp_rule::hash_mode_t& hm,
102                                          gbp_rule::next_hops_t& nhs)
103   : m_hm(hm)
104   , m_nhs(nhs)
105 {
106 }
107
108 std::string
109 gbp_rule::next_hop_set_t::to_string() const
110 {
111   std::ostringstream s;
112
113   s << "hash-mode:" << m_hm.to_string() << " next-hops:[";
114   auto it = m_nhs.cbegin();
115   while (it != m_nhs.cend()) {
116     s << " " << it->to_string();
117     ++it;
118   }
119   s << " ] next-hop-size:" << m_nhs.size();
120
121   return (s.str());
122 }
123
124 bool
125 gbp_rule::next_hop_set_t::operator==(const next_hop_set_t& nhs) const
126 {
127   return ((m_hm == nhs.m_hm) && (m_nhs == nhs.m_nhs));
128 }
129
130 const gbp_rule::hash_mode_t&
131 gbp_rule::next_hop_set_t::getHashMode() const
132 {
133   return m_hm;
134 }
135
136 const gbp_rule::next_hops_t&
137 gbp_rule::next_hop_set_t::getNextHops() const
138 {
139   return m_nhs;
140 }
141
142 const gbp_rule::action_t gbp_rule::action_t::REDIRECT(2, "redirect");
143 const gbp_rule::action_t gbp_rule::action_t::PERMIT(1, "permit");
144 const gbp_rule::action_t gbp_rule::action_t::DENY(0, "deny");
145
146 gbp_rule::action_t::action_t(int v, const std::string s)
147   : enum_base(v, s)
148 {
149 }
150
151 const gbp_rule::action_t&
152 gbp_rule::action_t::from_int(vapi_enum_gbp_rule_action i)
153 {
154   if (i == GBP_API_RULE_REDIRECT)
155     return gbp_rule::action_t::REDIRECT;
156   else if (i == GBP_API_RULE_PERMIT)
157     return gbp_rule::action_t::PERMIT;
158
159   return gbp_rule::action_t::DENY;
160 }
161
162 gbp_rule::gbp_rule(uint32_t priority,
163                    const gbp_rule::next_hop_set_t& nhs,
164                    const gbp_rule::action_t& a)
165   : m_priority(priority)
166   , m_nhs(nhs)
167   , m_action(a)
168 {
169 }
170
171 bool
172 gbp_rule::operator<(const gbp_rule& other) const
173 {
174   return (other.m_priority < m_priority);
175 }
176
177 bool
178 gbp_rule::operator==(const gbp_rule& rule) const
179 {
180   return ((m_action == rule.m_action) && (m_nhs == rule.m_nhs) &&
181           (m_priority == rule.m_priority));
182 }
183
184 std::string
185 gbp_rule::to_string() const
186 {
187   std::ostringstream s;
188
189   s << "gbp-rule:["
190     << "priority:" << m_priority << " action:" << m_action.to_string()
191     << " next-hop-set:[" << m_nhs.to_string() << "]]";
192
193   return (s.str());
194 }
195
196 uint32_t
197 gbp_rule::priority() const
198 {
199   return m_priority;
200 }
201
202 const gbp_rule::action_t&
203 gbp_rule::action() const
204 {
205   return m_action;
206 }
207
208 const gbp_rule::next_hop_set_t&
209 gbp_rule::nhs() const
210 {
211   return m_nhs;
212 }
213 }; // namespace VOM
214
215 /*
216  * fd.io coding-style-patch-verification: ON
217  *
218  * Local Variables:
219  * eval: (c-set-style "mozilla")
220  * End:
221  */