-
Notifications
You must be signed in to change notification settings - Fork 40
/
EosSdk.i
467 lines (424 loc) · 16.6 KB
/
EosSdk.i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Copyright (c) 2013 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.
%module(directors="1") eossdk
// Importing this module allows us to produce better stack traces when
// a Python exception bubbles up into the C++ code.
%pythonbegin %{
try:
import _Tac
except ImportError:
pass
%}
%include "cpointer.i"
%include "stdint.i"
%include "std_list.i"
%include "std_map.i"
%include "std_pair.i"
%include "std_set.i"
%include "std_string.i"
%include "std_unordered_set.i"
%include "std_vector.i"
%import "SwigUtils.i"
%include "typemaps.i"
// Change all some_class_t names to just SomeClass:
// The <<""< uses automatic string concatination to get around the
// conflict marker limitation.
%rename("%(command:python3 $SRCDIR/SwigRenamer.py --classname <<""<)s", %$isclass) "";
// Don't do anything for enum values, functions or variables.
// Cause SWIG to produce basic documentation in the generated eossdk.py
// For functions, the docstrings describe argument type and return types.
// All constructors are listed in the __init__ function for the type.
// These docstrings are later examined by sphinx.
%feature("autodoc", "1");
// Ignored conversions:
#define SWIG_FILE_WITH_INIT
%import "eos/base.h"
// Tell swig about our custom types (copied from base.h)
typedef double seconds_t;
typedef uint16_t uint16_be_t;
typedef uint32_t uint32_be_t;
typedef uint64_t uint64_be_t;
%typemap(in) void *eossdk_context {
$1 = PyLong_AsVoidPtr($input);
}
%typemap(in) eos::ByteString (char *cstr, Py_ssize_t len, std::string temp) {
// If the input Python object is a unicode object then we should convert it
// to UTF-8 before turning it into a bytestring
if (PyUnicode_Check($input)) {
// NB(toma): This may need a check for the unicode -> bytes conversion
// although it should never fail
if (
%#if PY_VERSION_HEX>=0x03000000
PyBytes_AsStringAndSize(
%#else
PyString_AsStringAndSize(
%#endif
PyUnicode_AsUTF8String($input),
&cstr,
&len
) == -1
) {
PyErr_SetString(PyExc_ValueError, "Expected a Unicode object");
SWIG_fail;
}
} else {
// Not unicode, we can just convert
if (
%#if PY_VERSION_HEX>=0x03000000
PyBytes_AsStringAndSize(
%#else
PyString_AsStringAndSize(
%#endif
$input,
&cstr,
&len
) == -1
) {
PyErr_SetString(PyExc_ValueError, "Expected a Bytes object");
SWIG_fail;
}
}
$1 = std::string(cstr);
}
%typemap(in) eos::ByteString& (char *cstr, Py_ssize_t len, std::string temp, int res) {
// If the input Python object is a unicode object then we should convert it
// to UTF-8 before turning it into a bytestring
res = 0;
if (PyUnicode_Check($input)) {
// NB(toma): This may need a check for the unicode -> bytes conversion
// although it should never fail
if (
%#if PY_VERSION_HEX>=0x03000000
PyBytes_AsStringAndSize(
%#else
PyString_AsStringAndSize(
%#endif
PyUnicode_AsUTF8String($input),
&cstr,
&len
) == -1
) {
PyErr_SetString(PyExc_ValueError, "Expected a Unicode object");
SWIG_fail;
}
} else {
// Not unicode, we can just convert
if (
%#if PY_VERSION_HEX>=0x03000000
PyBytes_AsStringAndSize(
%#else
PyString_AsStringAndSize(
%#endif
$input,
&cstr,
&len
) == -1
) {
PyErr_SetString(PyExc_ValueError, "Expected a Bytes value");
SWIG_fail;
}
}
temp = std::string(cstr);
$1 = &temp;
}
%typemap(typecheck) eos::ByteString, eos::ByteString& {
$1 = (PyString_Check($input) || PyUnicode_Check($input)) ? 1 : 0;
}
%typemap(out) eos::ByteString {
// This should only return bytes
%#if PY_VERSION_HEX>=0x03000000
$result = PyBytes_FromStringAndSize($1.c_str(), %numeric_cast($1.size(), Py_ssize_t));
%#else
$result = PyString_FromStringAndSize($1.c_str(), %numeric_cast($1.size(), Py_ssize_t));
%#endif
}
// To make sure instance of EosSdk types are hashable in python, this is important
// when the objects are used in sets, dicts, etc (as key).
// Three test cases added for this, one is in EthLagIntfTest.py, one in IntfTest.py
// and the third in IpAddrTest.py
%rename(__hash__) *::hash;
// generate directors for all classes that have virtual methods, except the managers
%feature("director");
%feature("nodirector") eos::acl_mgr;
%feature("nodirector") eos::agent_mgr;
%feature("nodirector") eos::aresolve_mgr;
%feature("nodirector") eos::bfd_session_mgr;
%feature("nodirector") eos::bgp_mgr;
%feature("nodirector") eos::bgp_path_mgr;
%feature("nodirector") eos::class_map_mgr;
%feature("nodirector") eos::decap_group_mgr;
%feature("nodirector") eos::directflow_mgr;
%feature("nodirector") eos::eapi_mgr;
%feature("nodirector") eos::eth_intf_mgr;
%feature("nodirector") eos::eth_lag_intf_mgr;
%feature("nodirector") eos::eth_phy_intf_mgr;
%feature("nodirector") eos::eth_phy_intf_counter_mgr;
%feature("nodirector") eos::fib_mgr;
%feature("nodirector") eos::hardware_table_mgr;
%feature("nodirector") eos::intf_mgr;
%feature("nodirector") eos::intf_counter_mgr;
%feature("nodirector") eos::ip_intf_mgr;
%feature("nodirector") eos::ip_route_mgr;
%feature("nodirector") eos::lldp_mgr;
%feature("nodirector") eos::macsec_mgr;
%feature("nodirector") eos::mac_table_mgr;
%feature("nodirector") eos::mlag_mgr;
%feature("nodirector") eos::mpls_route_mgr;
%feature("nodirector") eos::mpls_vrf_label_mgr;
%feature("nodirector") eos::neighbor_table_mgr;
%feature("nodirector") eos::nexthop_group_mgr;
%feature("nodirector") eos::nexthop_group_tunnel_mgr;
%feature("nodirector") eos::policy_map_mgr;
%feature("nodirector") eos::route_map_mgr;
%feature("nodirector") eos::subintf_mgr;
%feature("nodirector") eos::system_mgr;
%feature("nodirector") eos::timeout_mgr;
%feature("nodirector") eos::vrf_mgr;
%feature("nodirector") eos::xcvr_mgr;
// Do not generate Python bindings for the old programmed callbacks in the extended
// nexthop group handlers
%feature("nodirector") eos::nexthop_group_handler_v2::on_nexthop_group_programmed(
std::string const &);
%feature("nodirector") eos::nexthop_group_handler_v3::on_nexthop_group_programmed(
std::string const &);
%{
#include "eos/acl.h"
#include "eos/agent.h"
#include "eos/aresolve.h"
#include "eos/bfd.h"
#include "eos/bgp.h"
#include "eos/bgp_path.h"
#include "eos/event_loop.h"
#include "eos/class_map.h"
#include "eos/decap_group.h"
#include "eos/directflow.h"
#include "eos/eapi.h"
#include "eos/eth.h"
#include "eos/eth_intf.h"
#include "eos/eth_lag_intf.h"
#include "eos/eth_phy_intf.h"
#include "eos/exception.h"
#include "eos/fd.h"
#include "eos/fib.h"
#include "eos/hardware_table.h"
#include "eos/hash_mix.h"
#include "eos/intf.h"
#include "eos/ip.h"
#include "eos/ip_intf.h"
#include "eos/ip_route.h"
#include "eos/iterator.h"
#include "eos/lldp.h"
#include "eos/macsec.h"
#include "eos/mac_table.h"
#include "eos/mlag.h"
#include "eos/mpls.h"
#include "eos/mpls_route.h"
#include "eos/mpls_vrf_label.h"
#include "eos/neighbor_table.h"
#include "eos/nexthop_group.h"
#include "eos/nexthop_group_tunnel.h"
#include "eos/policy_map.h"
#include "eos/route_map.h"
#include "eos/sdk.h"
#include "eos/subintf.h"
#include "eos/system.h"
#include "eos/timer.h"
#include "eos/tracing.h"
#include "eos/utility.h"
#include "eos/version.h"
#include "eos/vrf.h"
#include "eos/xcvr.h"
using namespace eos;
/// Used to convert uncaught exceptions into Python exceptions.
class misc_error : public error {
public:
virtual ~misc_error() noexcept {
}
explicit misc_error(std::string const & msg) noexcept : error(msg) {
}
virtual void raise() const EOS_SDK_NORETURN {
throw *this;
}
};
struct stop_iteration {};
#define TRANSLATE_EXCEPTION(ExceptionClass)\
catch(ExceptionClass const & e) {\
ExceptionClass * exc = new ExceptionClass(e);\
PyObject * obj = SWIG_NewPointerObj(exc, SWIGTYPE_p_eos__##ExceptionClass,\
SWIG_POINTER_OWN);\
PyErr_SetObject(SWIG_Python_ExceptionType(SWIGTYPE_p_eos__##ExceptionClass), obj);\
}
void throw_py_error(error const& err) {
try {
err.raise();
}
TRANSLATE_EXCEPTION(invalid_range_error)
TRANSLATE_EXCEPTION(invalid_argument_error)
TRANSLATE_EXCEPTION(no_such_interface_error)
TRANSLATE_EXCEPTION(not_switchport_eligible_error)
TRANSLATE_EXCEPTION(invalid_vlan_error)
TRANSLATE_EXCEPTION(internal_vlan_error)
TRANSLATE_EXCEPTION(unsupported_policy_feature_error)
TRANSLATE_EXCEPTION(address_overlap_error)
catch(error const & e) {
misc_error * exc = new misc_error(e.msg());
PyObject * obj = SWIG_NewPointerObj(exc, SWIGTYPE_p_eos__error,
SWIG_POINTER_OWN);
PyErr_SetObject(SWIG_Python_ExceptionType(SWIGTYPE_p_eos__error), obj);
}
}
%}
%exceptionclass error;
%exception {
try {
$action
} catch (error const & e) {
throw_py_error(e);
SWIG_fail;
}
}
%typemap(throws) stop_iteration {
(void)$1;
SWIG_SetErrorObj(PyExc_StopIteration, SWIG_Py_Void());
SWIG_fail;
}
%include "eos/iterator.h"
%define wrap_iterator(IteratorClass, IteratorImplClass, ReturnType)
%template() eos::iter_base<ReturnType, IteratorImplClass>;
%extend IteratorClass {
// Note: __next__() is needed for Python 3.x, so we alias it to next().
%pythoncode {
def __iter__(self): return self
def __next__(self): return self.next()
}
ReturnType next() throw(stop_iteration) {
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
if(!*$self) {
throw stop_iteration();
}
ReturnType result = $self->operator*();
$self->operator++();
SWIG_PYTHON_THREAD_END_BLOCK;
return result;
}
}
%enddef
%define default_iterator(ManagerClass, IteratorMethod)
%extend ManagerClass {
%pythoncode {
def __iter__(self): return self.IteratorMethod()
}
}
%enddef
// Different stl containers used directly in our exposed headers
%template() std::map<uint32_t, eos::class_map_rule_t>;
%template() std::map<uint32_t, eos::policy_map_rule_t>;
%template() std::map<uint16_t, eos::ip_addr_t>;
%template() std::map<uint32_t, eos::route_map_entry_t>;
%template() std::set<eos::intf_id_t>;
%template() std::set<eos::ip_addr_mask_t>;
%template() std::list<uint16_t>;
%template() std::list<eos::mpls_label_t>;
%template() std::map<uint16_t, eos::nexthop_group_mpls_action_t>;
%template() std::map<uint16_t, eos::nexthop_group_entry_t>;
%template() std::pair<uint32_t, eos::acl_rule_ip_t>;
%template() std::pair<uint32_t, eos::acl_rule_eth_t>;
%template() std::unordered_set<uint32_t>;
%template() std::unordered_set<eos::ip_addr_t>;
%template() std::unordered_set<std::string>;
%template() std::map<eos::lldp_tlv_type_t, std::string>;
%template() std::list<eos::lldp_management_address_t>;
%template() std::vector<std::string>;
%template() std::vector<eos::mpls_label_t>;
%template() std::set<eos::policy_map_action_t>;
STD_FORWARD_LIST(fib_via_t)
STD_FORWARD_LIST(mpls_label_t)
// For vlan_set_t
%template(_BitSet4096) std::bitset<4096>;
// Ignore the `raise' method of all exceptions. `raise' is a Python keyword
// and also this method is used to throw the exception from C++ and is useless
// in Python.
%ignore raise;
// Ignore the `what' method of all exceptions. This method is redundant with
// the `msg' method (it's only provided for "compatibility" with standard
// exceptions).
%ignore what;
// Python code should use Python exceptions, and not call panic().
%ignore panic;
%ignore vpanic;
%ignore modbit;
%ignore invalid_mpls_label;
%include "Includes.i"
%extend eos::error {
std::string const & __str__() {
return $self->msg();
}
};
// This is required to get the version ID when calling nexthop_group_set as it is
// returned in a uint16_t pointer.
%pointer_functions(uint16_t, uint16_t_p);
// Pythonify our iterators.
wrap_iterator(eos::acl_iter_t, eos::acl_iter_impl, eos::acl_key_t);
wrap_iterator(eos::acl_rule_ip_iter_t, eos::acl_rule_ip_iter_impl, eos::acl_rule_ip_entry_t);
wrap_iterator(eos::acl_rule_eth_iter_t, eos::acl_rule_eth_iter_impl, eos::acl_rule_eth_entry_t);
wrap_iterator(eos::agent_option_iter_t, eos::agent_option_iter_impl, std::string);
wrap_iterator(eos::agent_status_iter_t, eos::agent_status_iter_impl, std::string);
wrap_iterator(eos::bfd_session_iter_t, eos::bfd_session_iter_impl, eos::bfd_session_key_t);
wrap_iterator(eos::bgp_path_iter_t, eos::bgp_path_iter_impl, eos::bgp_path_t);
wrap_iterator(eos::class_map_iter_t, eos::class_map_iter_impl, eos::class_map_key_t);
wrap_iterator(eos::fib_fec_iter_t, eos::fib_fec_iter_impl, eos::fib_fec_t);
wrap_iterator(eos::fib_route_iter_t, eos::fib_route_iter_impl, eos::fib_route_t);
wrap_iterator(eos::flow_entry_iter_t, eos::flow_entry_iter_impl, eos::flow_entry_t);
wrap_iterator(eos::eth_intf_iter_t, eos::eth_intf_iter_impl, eos::intf_id_t);
wrap_iterator(eos::eth_lag_intf_iter_t, eos::eth_lag_intf_iter_impl, eos::intf_id_t);
wrap_iterator(eos::eth_lag_intf_member_iter_t, eos::eth_lag_intf_member_iter_impl, eos::intf_id_t);
wrap_iterator(eos::eth_phy_intf_iter_t, eos::eth_phy_intf_iter_impl, eos::intf_id_t);
wrap_iterator(eos::hardware_table_iter_t, eos::hardware_table_iter_impl, eos::hardware_table_key_t);
wrap_iterator(eos::intf_iter_t, eos::intf_iter_impl, eos::intf_id_t);
wrap_iterator(eos::ip_route_iter_t, eos::ip_route_iter_impl, eos::ip_route_t);
wrap_iterator(eos::ip_route_via_iter_t, eos::ip_route_via_iter_impl, eos::ip_route_via_t);
wrap_iterator(eos::decap_group_iter_t, eos::decap_group_iter_impl, eos::decap_group_t);
wrap_iterator(eos::lldp_remote_system_iter_t, eos::lldp_remote_system_iter_impl, lldp_remote_system_t);
wrap_iterator(eos::lldp_intf_iter_t, eos::lldp_intf_iter_impl, intf_id_t);
wrap_iterator(eos::lldp_neighbor_iter_t, eos::lldp_neighbor_iter_impl, lldp_neighbor_t);
wrap_iterator(eos::mac_table_iter_t, eos::mac_table_iter_impl, eos::mac_key_t);
wrap_iterator(eos::mac_table_status_iter_t, eos::mac_table_status_iter_impl, eos::mac_key_t);
wrap_iterator(eos::mpls_route_iter_t, eos::mpls_route_iter_impl, eos::mpls_route_t);
wrap_iterator(eos::mpls_route_status_iter_t, eos::mpls_route_status_iter_impl, eos::mpls_route_key_t);
wrap_iterator(eos::mpls_route_via_iter_t, eos::mpls_route_via_iter_impl, eos::mpls_route_via_t);
wrap_iterator(eos::mpls_route_via_status_iter_t, eos::mpls_route_via_status_iter_impl, eos::mpls_route_via_t);
wrap_iterator(eos::mpls_vrf_label_iter_t, eos::mpls_vrf_label_iter_impl, eos::mpls_vrf_label_t);
wrap_iterator(eos::neighbor_table_iter_t, eos::neighbor_table_iter_impl, eos::neighbor_key_t);
wrap_iterator(eos::neighbor_table_status_iter_t, eos::neighbor_table_status_iter_impl, eos::neighbor_key_t);
wrap_iterator(eos::nexthop_group_iter_t, eos::nexthop_group_iter_impl, eos::nexthop_group_t);
wrap_iterator(eos::nexthop_group_tunnel_iter_t, eos::nexthop_group_tunnel_iter_impl, eos::nexthop_group_tunnel_t);
wrap_iterator(eos::policy_map_iter_t, eos::policy_map_iter_impl, eos::policy_map_key_t);
wrap_iterator(eos::policy_map_hw_status_iter_t, eos::policy_map_hw_status_iter_impl, eos::policy_map_key_t);
wrap_iterator(eos::subintf_iter_t, eos::subintf_iter_impl, eos::intf_id_t);
wrap_iterator(eos::vrf_iter_t, eos::vrf_iter_impl, std::string);
wrap_iterator(eos::programmed_nexthop_group_iter_t, eos::programmed_nexthop_group_iter_impl, std::string);
wrap_iterator(eos::macsec_profile_iter_t, eos::macsec_profile_iter_impl, eos::macsec_profile_name_t);
wrap_iterator(eos::macsec_intf_status_iter_t, eos::macsec_intf_status_iter_impl, eos::intf_id_t);
wrap_iterator(eos::sbfd_echo_session_iter_t, eos::sbfd_echo_session_iter_impl, eos::sbfd_echo_session_key_t);
// Make managers themselves iterable, when it makes sense:
default_iterator(eos::directflow_mgr, flow_entry_iter);
default_iterator(eos::intf_mgr, intf_iter);
default_iterator(eos::ip_route_mgr, ip_route_iter);
default_iterator(eos::mpls_route_mgr, mpls_route_iter);
default_iterator(eos::subintf_mgr, subintf_iter);
%{
/*
SWIG generates code for Swig::Director, that includes a static field
containing a heap-allocated python lock. If the code using that director
lock is not used here, then the lock is created, but never used, and the
compiler can eliminate the store of the constructed lock to the static
variable that holds it. Make sure there's a public path to use the lock,
so that elision doesn't happen, and we don't see the allocation as a
memory leak.
*/
[[gnu::visibility("default")]]
void _EOSSDK_SWIG_NO_ELIDE_STATICS(PyObject *pyo, void *vptr) {
Swig::Director director(pyo);
director.swig_acquire_ownership_obj(vptr, true);
}
%}