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