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