VPP Object Model (VOM)
[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 "vom/types.hpp"
22
23 namespace VOM {
24
25 rc_t::rc_t(int v, const std::string s)
26   : enum_base<rc_t>(v, s)
27 {
28 }
29 rc_t::~rc_t()
30 {
31 }
32
33 const rc_t&
34 rc_t::from_vpp_retval(int32_t rv)
35 {
36   if (0 == rv) {
37     return (rc_t::OK);
38   }
39   if (-68 == rv) {
40     // interface laready exists
41     return (rc_t::OK);
42   }
43
44   return (rc_t::INVALID);
45 }
46
47 const rc_t rc_t::UNSET(0, "un-set");
48 const rc_t rc_t::NOOP(1, "no-op");
49 const rc_t rc_t::OK(2, "ok");
50 const rc_t rc_t::INPROGRESS(3, "in-progess");
51 const rc_t rc_t::INVALID(4, "invalid");
52 const rc_t rc_t::TIMEOUT(5, "timeout");
53
54 const handle_t handle_t::INVALID(~0);
55
56 handle_t::handle_t(int value)
57   : m_value(value)
58 {
59 }
60
61 handle_t::handle_t()
62   : m_value(~0)
63 {
64 }
65
66 std::string
67 handle_t::to_string() const
68 {
69   return (std::to_string(m_value));
70 }
71
72 bool
73 handle_t::operator==(const handle_t& other) const
74 {
75   return (m_value == other.m_value);
76 }
77
78 bool
79 handle_t::operator!=(const handle_t& other) const
80 {
81   return (!(*this == other));
82 }
83
84 bool
85 handle_t::operator<(const handle_t& other) const
86 {
87   return (m_value < other.m_value);
88 }
89
90 uint32_t
91 handle_t::value() const
92 {
93   return (m_value);
94 }
95
96 std::ostream&
97 operator<<(std::ostream& os, const handle_t& h)
98 {
99   os << h.value();
100
101   return (os);
102 }
103
104 mac_address_t::mac_address_t(uint64_t address)
105 {
106   uint8_t mac[6];
107
108   std::memcpy(mac, &address, 6);
109   for (int i = 0; i < 6; i++) {
110     bytes[i] = mac[5 - i];
111   }
112 }
113
114 mac_address_t::mac_address_t(uint8_t b[6])
115 {
116   std::copy(b, b + 6, std::begin(bytes));
117 }
118
119 mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
120 {
121   std::copy(i.begin(), i.end(), std::begin(bytes));
122 }
123
124 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
125
126 const mac_address_t mac_address_t::ZERO({ 0x0 });
127
128 void
129 mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
130 {
131   for (int i = 0; i < 6 && i < len; i++) {
132     array[i] = bytes[i];
133   }
134 }
135
136 uint64_t
137 mac_address_t::to_u64() const
138 {
139   uint64_t mac6 = 0;
140   uint8_t* b = reinterpret_cast<uint8_t*>(&mac6);
141
142   // whack hack. the vapi will byte swap.
143   b[2] = bytes[5];
144   b[3] = bytes[4];
145   b[4] = bytes[3];
146   b[5] = bytes[2];
147   b[6] = bytes[1];
148   b[7] = bytes[0];
149
150   return (mac6);
151 }
152
153 std::string
154 mac_address_t::to_string() const
155 {
156   std::ostringstream s;
157   bool first = true;
158
159   s.fill('0');
160   s << std::hex;
161   s << "mac:[";
162   for (auto byte : bytes) {
163     if (first)
164       first = false;
165     else
166       s << ":";
167     s << std::setw(2) << static_cast<unsigned int>(byte);
168   }
169   s << "]";
170
171   return (s.str());
172 }
173
174 bool
175 mac_address_t::operator==(const mac_address_t& mac) const
176 {
177   return (bytes == mac.bytes);
178 }
179 bool
180 mac_address_t::operator<(const mac_address_t& m) const
181 {
182   return (bytes < m.bytes);
183 }
184
185 std::ostream&
186 operator<<(std::ostream& os, const mac_address_t& mac)
187 {
188   os << mac.to_string();
189
190   return (os);
191 }
192
193 l2_address_t::l2_address_t(const uint8_t b[8], uint8_t n_bytes)
194   : bytes(n_bytes)
195 {
196   std::copy_n(b, n_bytes, std::begin(bytes));
197 }
198
199 l2_address_t::l2_address_t(std::initializer_list<uint8_t> i)
200   : bytes(i)
201 {
202 }
203
204 l2_address_t::l2_address_t(const mac_address_t& mac)
205   : bytes(6)
206 {
207   std::copy(begin(mac.bytes), std::end(mac.bytes), std::begin(bytes));
208 }
209
210 const l2_address_t l2_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
211                                        0xff });
212
213 const l2_address_t l2_address_t::ZERO({ 0x0 });
214
215 void
216 l2_address_t::to_bytes(uint8_t* array, uint8_t len) const
217 {
218   for (uint8_t i = 0; i < bytes.size() && i < len; i++) {
219     array[i] = bytes[i];
220   }
221 }
222
223 mac_address_t
224 l2_address_t::to_mac() const
225 {
226   mac_address_t mac({});
227
228   std::copy_n(bytes.begin(), mac.bytes.size(), mac.bytes.begin());
229
230   return (mac);
231 }
232
233 std::string
234 l2_address_t::to_string() const
235 {
236   std::ostringstream s;
237   bool first = true;
238
239   s.fill('0');
240   s << std::hex;
241   for (auto byte : bytes) {
242     if (first)
243       first = false;
244     else
245       s << ":";
246     s << std::setw(2) << static_cast<unsigned int>(byte);
247   }
248
249   return (s.str());
250 }
251
252 bool
253 l2_address_t::operator==(const l2_address_t& l2) const
254 {
255   return (bytes == l2.bytes);
256 }
257
258 bool
259 l2_address_t::operator!=(const l2_address_t& l2) const
260 {
261   return (bytes != l2.bytes);
262 }
263
264 std::ostream&
265 operator<<(std::ostream& os, const l2_address_t& l2)
266 {
267   os << l2.to_string();
268
269   return (os);
270 }
271
272 const direction_t direction_t::INPUT(1, "input");
273 const direction_t direction_t::OUTPUT(0, "output");
274
275 direction_t::direction_t(int v, const std::string s)
276   : enum_base(v, s)
277 {
278 }
279 }
280
281 /*
282  * fd.io coding-style-patch-verification: ON
283  *
284  * Local Variables:
285  * eval: (c-set-style "mozilla")
286  * End:
287  */