c5325d287a5f4bdd8b3f3f953ea167144b038727
[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::INPROGRESS(3, "in-progess");
53 const rc_t rc_t::INVALID(4, "invalid");
54 const rc_t rc_t::TIMEOUT(5, "timeout");
55
56 const handle_t handle_t::INVALID(~0);
57
58 handle_t::handle_t(int value)
59   : m_value(value)
60 {
61 }
62
63 handle_t::handle_t()
64   : m_value(~0)
65 {
66 }
67
68 std::string
69 handle_t::to_string() const
70 {
71   return (std::to_string(m_value));
72 }
73
74 bool
75 handle_t::operator==(const handle_t& other) const
76 {
77   return (m_value == other.m_value);
78 }
79
80 bool
81 handle_t::operator!=(const handle_t& other) const
82 {
83   return (!(*this == other));
84 }
85
86 bool
87 handle_t::operator<(const handle_t& other) const
88 {
89   return (m_value < other.m_value);
90 }
91
92 uint32_t
93 handle_t::value() const
94 {
95   return (m_value);
96 }
97
98 std::ostream&
99 operator<<(std::ostream& os, const handle_t& h)
100 {
101   os << h.value();
102
103   return (os);
104 }
105
106 mac_address_t::mac_address_t(uint8_t b[6])
107 {
108   std::copy(b, b + 6, std::begin(bytes));
109 }
110
111 mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
112 {
113   std::copy(i.begin(), i.end(), std::begin(bytes));
114 }
115
116 mac_address_t::mac_address_t(const std::string& str)
117 {
118   std::vector<std::string> parts;
119
120   boost::split(parts, str, boost::is_any_of(":"));
121
122   size_t n_bytes = std::min(bytes.size(), parts.size());
123
124   for (uint32_t ii = 0; ii < n_bytes; ii++) {
125     bytes[ii] = std::stoul(parts[ii], nullptr, 16);
126   }
127 }
128
129 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
130
131 const mac_address_t mac_address_t::ZERO({ 0x0 });
132
133 void
134 mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
135 {
136   for (int i = 0; i < 6 && i < len; i++) {
137     array[i] = bytes[i];
138   }
139 }
140
141 std::string
142 mac_address_t::to_string() const
143 {
144   std::ostringstream s;
145   bool first = true;
146
147   s.fill('0');
148   s << std::hex;
149   for (auto byte : bytes) {
150     if (first)
151       first = false;
152     else
153       s << ":";
154     s << std::setw(2) << static_cast<unsigned int>(byte);
155   }
156
157   return (s.str());
158 }
159
160 bool
161 mac_address_t::operator==(const mac_address_t& mac) const
162 {
163   return (bytes == mac.bytes);
164 }
165 bool
166 mac_address_t::operator<(const mac_address_t& m) const
167 {
168   return (bytes < m.bytes);
169 }
170
171 std::ostream&
172 operator<<(std::ostream& os, const mac_address_t& mac)
173 {
174   os << mac.to_string();
175
176   return (os);
177 }
178
179 l2_address_t::l2_address_t(const uint8_t b[8], uint8_t n_bytes)
180   : bytes(n_bytes)
181 {
182   std::copy_n(b, n_bytes, std::begin(bytes));
183 }
184
185 l2_address_t::l2_address_t(std::initializer_list<uint8_t> i)
186   : bytes(i)
187 {
188 }
189
190 l2_address_t::l2_address_t(const mac_address_t& mac)
191   : bytes(6)
192 {
193   std::copy(begin(mac.bytes), std::end(mac.bytes), std::begin(bytes));
194 }
195
196 const l2_address_t l2_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
197                                        0xff });
198
199 const l2_address_t l2_address_t::ZERO({ 0x0 });
200
201 void
202 l2_address_t::to_bytes(uint8_t* array, uint8_t len) const
203 {
204   for (uint8_t i = 0; i < bytes.size() && i < len; i++) {
205     array[i] = bytes[i];
206   }
207 }
208
209 mac_address_t
210 l2_address_t::to_mac() const
211 {
212   mac_address_t mac({});
213
214   std::copy_n(bytes.begin(), mac.bytes.size(), mac.bytes.begin());
215
216   return (mac);
217 }
218
219 std::string
220 l2_address_t::to_string() const
221 {
222   std::ostringstream s;
223   bool first = true;
224
225   s.fill('0');
226   s << std::hex;
227   for (auto byte : bytes) {
228     if (first)
229       first = false;
230     else
231       s << ":";
232     s << std::setw(2) << static_cast<unsigned int>(byte);
233   }
234
235   return (s.str());
236 }
237
238 bool
239 l2_address_t::operator==(const l2_address_t& l2) const
240 {
241   return (bytes == l2.bytes);
242 }
243
244 bool
245 l2_address_t::operator!=(const l2_address_t& l2) const
246 {
247   return (bytes != l2.bytes);
248 }
249
250 std::ostream&
251 operator<<(std::ostream& os, const l2_address_t& l2)
252 {
253   os << l2.to_string();
254
255   return (os);
256 }
257
258 const direction_t direction_t::INPUT(1, "input");
259 const direction_t direction_t::OUTPUT(0, "output");
260
261 direction_t::direction_t(int v, const std::string s)
262   : enum_base(v, s)
263 {
264 }
265 }
266
267 /*
268  * fd.io coding-style-patch-verification: ON
269  *
270  * Local Variables:
271  * eval: (c-set-style "mozilla")
272  * End:
273  */