44e0dd08f985b63e69443960c1853b4c2cdc94e2
[vpp.git] / src / vpp-api / vom / types.cpp
1 /*
2  * Copyright (c) 2017 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 <algorithm>
17 #include <iomanip>
18 #include <iostream>
19 #include <sstream>
20
21 #include <boost/algorithm/string.hpp>
22
23 #include "vom/types.hpp"
24
25 namespace VOM {
26
27 rc_t::rc_t(int v, const std::string s)
28   : enum_base<rc_t>(v, s)
29 {
30 }
31 rc_t::~rc_t()
32 {
33 }
34
35 const rc_t&
36 rc_t::from_vpp_retval(int32_t rv)
37 {
38   if (0 == rv) {
39     return (rc_t::OK);
40   }
41   if (-68 == rv) {
42     // sub interface already exists
43     return (rc_t::OK);
44   }
45   if (-79 == rv) {
46     // interface already exists
47     return (rc_t::OK);
48   }
49
50   return (rc_t::INVALID);
51 }
52
53 const rc_t rc_t::UNSET(0, "un-set");
54 const rc_t rc_t::NOOP(1, "no-op");
55 const rc_t rc_t::OK(2, "ok");
56 const rc_t rc_t::INVALID(3, "invalid");
57 const rc_t rc_t::TIMEOUT(4, "timeout");
58
59 const handle_t handle_t::INVALID(~0);
60
61 handle_t::handle_t(int value)
62   : m_value(value)
63 {
64 }
65
66 handle_t::handle_t()
67   : m_value(~0)
68 {
69 }
70
71 std::string
72 handle_t::to_string() const
73 {
74   return (std::to_string(m_value));
75 }
76
77 bool
78 handle_t::operator==(const handle_t& other) const
79 {
80   return (m_value == other.m_value);
81 }
82
83 bool
84 handle_t::operator!=(const handle_t& other) const
85 {
86   return (!(*this == other));
87 }
88
89 bool
90 handle_t::operator<(const handle_t& other) const
91 {
92   return (m_value < other.m_value);
93 }
94
95 uint32_t
96 handle_t::value() const
97 {
98   return (m_value);
99 }
100
101 void
102 handle_t::reset()
103 {
104   m_value = ~0;
105 }
106
107 std::ostream&
108 operator<<(std::ostream& os, const handle_t& h)
109 {
110   os << h.value();
111
112   return (os);
113 }
114
115 mac_address_t::mac_address_t(uint8_t b[6])
116 {
117   std::copy(b, b + 6, std::begin(bytes));
118 }
119
120 mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
121 {
122   std::copy(i.begin(), i.end(), std::begin(bytes));
123 }
124
125 mac_address_t::mac_address_t(const std::string& str)
126 {
127   std::vector<std::string> parts;
128
129   boost::split(parts, str, boost::is_any_of(":"));
130
131   size_t n_bytes = std::min(bytes.size(), parts.size());
132
133   for (uint32_t ii = 0; ii < n_bytes; ii++) {
134     bytes[ii] = std::stoul(parts[ii], nullptr, 16);
135   }
136 }
137
138 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
139
140 const mac_address_t mac_address_t::ZERO({ 0x0 });
141
142 void
143 mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
144 {
145   for (int i = 0; i < 6 && i < len; i++) {
146     array[i] = bytes[i];
147   }
148 }
149
150 std::string
151 mac_address_t::to_string() const
152 {
153   std::ostringstream s;
154   bool first = true;
155
156   s.fill('0');
157   s << std::hex;
158   for (auto byte : bytes) {
159     if (first)
160       first = false;
161     else
162       s << ":";
163     s << std::setw(2) << static_cast<unsigned int>(byte);
164   }
165
166   return (s.str());
167 }
168
169 bool
170 mac_address_t::operator==(const mac_address_t& mac) const
171 {
172   return (bytes == mac.bytes);
173 }
174 bool
175 mac_address_t::operator<(const mac_address_t& m) const
176 {
177   return (bytes < m.bytes);
178 }
179
180 std::ostream&
181 operator<<(std::ostream& os, const mac_address_t& mac)
182 {
183   os << mac.to_string();
184
185   return (os);
186 }
187
188 l2_address_t::l2_address_t(const uint8_t b[8], uint8_t n_bytes)
189   : bytes(n_bytes)
190 {
191   std::copy_n(b, n_bytes, std::begin(bytes));
192 }
193
194 l2_address_t::l2_address_t(std::initializer_list<uint8_t> i)
195   : bytes(i)
196 {
197 }
198
199 l2_address_t::l2_address_t(const mac_address_t& mac)
200   : bytes(6)
201 {
202   std::copy(begin(mac.bytes), std::end(mac.bytes), std::begin(bytes));
203 }
204
205 const l2_address_t l2_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
206                                        0xff });
207
208 const l2_address_t l2_address_t::ZERO({ 0x0 });
209
210 void
211 l2_address_t::to_bytes(uint8_t* array, uint8_t len) const
212 {
213   for (uint8_t i = 0; i < bytes.size() && i < len; i++) {
214     array[i] = bytes[i];
215   }
216 }
217
218 mac_address_t
219 l2_address_t::to_mac() const
220 {
221   mac_address_t mac({});
222
223   std::copy_n(bytes.begin(), mac.bytes.size(), mac.bytes.begin());
224
225   return (mac);
226 }
227
228 std::string
229 l2_address_t::to_string() const
230 {
231   std::ostringstream s;
232   bool first = true;
233
234   s.fill('0');
235   s << std::hex;
236   for (auto byte : bytes) {
237     if (first)
238       first = false;
239     else
240       s << ":";
241     s << std::setw(2) << static_cast<unsigned int>(byte);
242   }
243
244   return (s.str());
245 }
246
247 bool
248 l2_address_t::operator==(const l2_address_t& l2) const
249 {
250   return (bytes == l2.bytes);
251 }
252
253 bool
254 l2_address_t::operator!=(const l2_address_t& l2) const
255 {
256   return (bytes != l2.bytes);
257 }
258
259 std::ostream&
260 operator<<(std::ostream& os, const l2_address_t& l2)
261 {
262   os << l2.to_string();
263
264   return (os);
265 }
266
267 const direction_t direction_t::INPUT(1, "input");
268 const direction_t direction_t::OUTPUT(0, "output");
269
270 direction_t::direction_t(int v, const std::string s)
271   : enum_base(v, s)
272 {
273 }
274 std::ostream&
275 operator<<(std::ostream& os, const direction_t& dir)
276 {
277   os << dir.to_string();
278   return os;
279 }
280
281 const ethertype_t ethertype_t::ARP(0x0806, "arp");
282 const ethertype_t ethertype_t::FCOE(0x8906, "fcoe");
283 const ethertype_t ethertype_t::IPV4(0x0800, "ipv4");
284 const ethertype_t ethertype_t::IPV6(0x86DD, "ipv6");
285 const ethertype_t ethertype_t::MAC_SECURITY(0x88E5, "mac-security");
286 const ethertype_t ethertype_t::MPLS_UNICAST(0x8847, "mpls-unicast");
287 const ethertype_t ethertype_t::TRILL(0x22F3, "trill");
288 const ethertype_t ethertype_t::UNSPECIFIED(0x0, "unspecified");
289
290 ethertype_t::ethertype_t(int v, const std::string s)
291   : enum_base(v, s)
292 {
293 }
294
295 std::ostream&
296 operator<<(std::ostream& os, const ethertype_t& ether)
297 {
298   os << ether.to_string();
299   return os;
300 }
301
302 const ethertype_t&
303 ethertype_t::from_numeric_val(uint16_t numeric)
304 {
305   if (0x0806 == numeric) {
306     return (ethertype_t::ARP);
307   }
308   if (0x8906 == numeric) {
309     return (ethertype_t::FCOE);
310   }
311   if (0x0800 == numeric) {
312     return (ethertype_t::IPV4);
313   }
314   if (0x86DD == numeric) {
315     return (ethertype_t::IPV6);
316   }
317   if (0x88E5 == numeric) {
318     return (ethertype_t::MAC_SECURITY);
319   }
320   if (0x8847 == numeric) {
321     return (ethertype_t::MPLS_UNICAST);
322   }
323   if (0x22F3 == numeric) {
324     return (ethertype_t::TRILL);
325   }
326
327   return (ethertype_t::UNSPECIFIED);
328 }
329
330 }; // namespace VOM
331
332 /*
333  * fd.io coding-style-patch-verification: ON
334  *
335  * Local Variables:
336  * eval: (c-set-style "mozilla")
337  * End:
338  */