c45518bd2c1c6cf67a222d9209e88b7631ca8974
[vpp.git] / test / vpp_papi_provider.py
1 import os
2 import fnmatch
3 import time
4 from hook import Hook
5 from collections import deque
6
7 # Sphinx creates auto-generated documentation by importing the python source
8 # files and collecting the docstrings from them. The NO_VPP_PAPI flag allows the
9 # vpp_papi_provider.py file to be importable without having to build the whole
10 # vpp api if the user only wishes to generate the test documentation.
11 do_import = True
12 try:
13     no_vpp_papi = os.getenv("NO_VPP_PAPI")
14     if no_vpp_papi == "1":
15         do_import = False
16 except:
17     pass
18
19 if do_import:
20     from vpp_papi import VPP
21
22 # from vnet/vnet/mpls/mpls_types.h
23 MPLS_IETF_MAX_LABEL = 0xfffff
24 MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
25
26
27 class L2_VTR_OP:
28     L2_POP_1 = 3
29
30
31 class VppPapiProvider(object):
32     """VPP-api provider using vpp-papi
33
34     @property hook: hook object providing before and after api/cli hooks
35
36
37     """
38
39     def __init__(self, name, shm_prefix, test_class):
40         self.hook = Hook("vpp-papi-provider")
41         self.name = name
42         self.shm_prefix = shm_prefix
43         self.test_class = test_class
44         jsonfiles = []
45
46         install_dir = os.getenv('VPP_TEST_INSTALL_PATH')
47         for root, dirnames, filenames in os.walk(install_dir):
48             for filename in fnmatch.filter(filenames, '*.api.json'):
49                 jsonfiles.append(os.path.join(root, filename))
50
51         self.papi = VPP(jsonfiles)
52         self._events = deque()
53
54     def register_hook(self, hook):
55         """Replace hook registration with new hook
56
57         :param hook:
58
59         """
60         self.hook = hook
61
62     def collect_events(self):
63         """ Collect all events from the internal queue and clear the queue. """
64         e = self._events
65         self._events = deque()
66         return e
67
68     def wait_for_event(self, timeout, name=None):
69         """ Wait for and return next event. """
70         if self._events:
71             self.test_class.logger.debug("Not waiting, event already queued")
72         limit = time.time() + timeout
73         while time.time() < limit:
74             if self._events:
75                 e = self._events.popleft()
76                 if name and type(e).__name__ != name:
77                     raise Exception(
78                         "Unexpected event received: %s, expected: %s" %
79                         (type(e).__name__, name))
80                 self.test_class.logger.debug("Returning event %s:%s" %
81                                              (name, e))
82                 return e
83             time.sleep(0)  # yield
84         if name is not None:
85             raise Exception("Event %s did not occur within timeout" % name)
86         raise Exception("Event did not occur within timeout")
87
88     def __call__(self, name, event):
89         """ Enqueue event in the internal event queue. """
90         # FIXME use the name instead of relying on type(e).__name__ ?
91         # FIXME #2 if this throws, it is eaten silently, Ole?
92         self.test_class.logger.debug("New event: %s: %s" % (name, event))
93         self._events.append(event)
94
95     def connect(self):
96         """Connect the API to VPP"""
97         self.papi.connect(self.name, self.shm_prefix)
98         self.papi.register_event_callback(self)
99
100     def disconnect(self):
101         """Disconnect the API from VPP"""
102         self.papi.disconnect()
103
104     def api(self, api_fn, api_args, expected_retval=0):
105         """ Call API function and check it's return value.
106         Call the appropriate hooks before and after the API call
107
108         :param api_fn: API function to call
109         :param api_args: tuple of API function arguments
110         :param expected_retval: Expected return value (Default value = 0)
111         :returns: reply from the API
112
113         """
114         self.hook.before_api(api_fn.__name__, api_args)
115         reply = api_fn(**api_args)
116         if hasattr(reply, 'retval') and reply.retval != expected_retval:
117             msg = "API call failed, expected retval == %d, got %s" % (
118                 expected_retval, repr(reply))
119             self.test_class.logger.info(msg)
120             raise Exception(msg)
121         self.hook.after_api(api_fn.__name__, api_args)
122         return reply
123
124     def cli(self, cli):
125         """ Execute a CLI, calling the before/after hooks appropriately.
126
127         :param cli: CLI to execute
128         :returns: CLI output
129
130         """
131         self.hook.before_cli(cli)
132         cli += '\n'
133         r = self.papi.cli_inband(length=len(cli), cmd=cli)
134         self.hook.after_cli(cli)
135         if hasattr(r, 'reply'):
136             return r.reply.decode().rstrip('\x00')
137
138     def ppcli(self, cli):
139         """ Helper method to print CLI command in case of info logging level.
140
141         :param cli: CLI to execute
142         :returns: CLI output
143         """
144         return cli + "\n" + str(self.cli(cli))
145
146     def _convert_mac(self, mac):
147         return int(mac.replace(":", ""), 16) << 16
148
149     def show_version(self):
150         """ """
151         return self.papi.show_version()
152
153     def pg_create_interface(self, pg_index):
154         """
155
156         :param pg_index:
157
158         """
159         return self.api(self.papi.pg_create_interface,
160                         {"interface_id": pg_index})
161
162     def sw_interface_dump(self, filter=None):
163         """
164
165         :param filter:  (Default value = None)
166
167         """
168         if filter is not None:
169             args = {"name_filter_valid": 1, "name_filter": filter}
170         else:
171             args = {}
172         return self.api(self.papi.sw_interface_dump, args)
173
174     def sw_interface_set_table(self, sw_if_index, is_ipv6, table_id):
175         """ Set the IPvX Table-id for the Interface
176
177         :param sw_if_index:
178         :param is_ipv6:
179         :param table_id:
180
181         """
182         return self.api(self.papi.sw_interface_set_table,
183                         {'sw_if_index': sw_if_index, 'is_ipv6': is_ipv6,
184                          'vrf_id': table_id})
185
186     def sw_interface_add_del_address(self, sw_if_index, addr, addr_len,
187                                      is_ipv6=0, is_add=1, del_all=0):
188         """
189
190         :param addr: param is_ipv6:  (Default value = 0)
191         :param sw_if_index:
192         :param addr_len:
193         :param is_ipv6:  (Default value = 0)
194         :param is_add:  (Default value = 1)
195         :param del_all:  (Default value = 0)
196
197         """
198         return self.api(self.papi.sw_interface_add_del_address,
199                         {'sw_if_index': sw_if_index,
200                          'is_add': is_add,
201                          'is_ipv6': is_ipv6,
202                          'del_all': del_all,
203                          'address_length': addr_len,
204                          'address': addr})
205
206     def sw_interface_enable_disable_mpls(self, sw_if_index,
207                                          is_enable=1):
208         """
209         Enable/Disable MPLS on the interface
210         :param sw_if_index:
211         :param is_enable:  (Default value = 1)
212
213         """
214         return self.api(self.papi.sw_interface_set_mpls_enable,
215                         {'sw_if_index': sw_if_index,
216                          'enable': is_enable})
217
218     def sw_interface_ra_suppress(self, sw_if_index):
219         return self.api(self.papi.sw_interface_ip6nd_ra_config,
220                         {'sw_if_index': sw_if_index})
221
222     def vxlan_add_del_tunnel(
223             self,
224             src_addr,
225             dst_addr,
226             mcast_sw_if_index=0xFFFFFFFF,
227             is_add=1,
228             is_ipv6=0,
229             encap_vrf_id=0,
230             decap_next_index=0xFFFFFFFF,
231             vni=0):
232         """
233
234         :param dst_addr:
235         :param src_addr:
236         :param is_add:  (Default value = 1)
237         :param is_ipv6:  (Default value = 0)
238         :param encap_vrf_id:  (Default value = 0)
239         :param decap_next_index:  (Default value = 0xFFFFFFFF)
240         :param mcast_sw_if_index:  (Default value = 0xFFFFFFFF)
241         :param vni:  (Default value = 0)
242
243         """
244         return self.api(self.papi.vxlan_add_del_tunnel,
245                         {'is_add': is_add,
246                          'is_ipv6': is_ipv6,
247                          'src_address': src_addr,
248                          'dst_address': dst_addr,
249                          'mcast_sw_if_index': mcast_sw_if_index,
250                          'encap_vrf_id': encap_vrf_id,
251                          'decap_next_index': decap_next_index,
252                          'vni': vni})
253
254     def bridge_domain_add_del(self, bd_id, flood=1, uu_flood=1, forward=1,
255                               learn=1, arp_term=0, is_add=1):
256         """Create/delete bridge domain.
257
258         :param int bd_id: Bridge domain index.
259         :param int flood: Enable/disable bcast/mcast flooding in the BD.
260             (Default value = 1)
261         :param int uu_flood: Enable/disable unknown unicast flood in the BD.
262             (Default value = 1)
263         :param int forward: Enable/disable forwarding on all interfaces in
264             the BD. (Default value = 1)
265         :param int learn: Enable/disable learning on all interfaces in the BD.
266             (Default value = 1)
267         :param int arp_term: Enable/disable arp termination in the BD.
268             (Default value = 1)
269         :param int is_add: Add or delete flag. (Default value = 1)
270         """
271         return self.api(self.papi.bridge_domain_add_del,
272                         {'bd_id': bd_id,
273                          'flood': flood,
274                          'uu_flood': uu_flood,
275                          'forward': forward,
276                          'learn': learn,
277                          'arp_term': arp_term,
278                          'is_add': is_add})
279
280     def l2fib_add_del(self, mac, bd_id, sw_if_index, is_add=1, static_mac=0,
281                       filter_mac=0, bvi_mac=0):
282         """Create/delete L2 FIB entry.
283
284         :param str mac: MAC address to create FIB entry for.
285         :param int bd_id: Bridge domain index.
286         :param int sw_if_index: Software interface index of the interface.
287         :param int is_add: Add or delete flag. (Default value = 1)
288         :param int static_mac: Set to 1 to create static MAC entry.
289             (Default value = 0)
290         :param int filter_mac: Set to 1 to drop packet that's source or
291             destination MAC address contains defined MAC address.
292             (Default value = 0)
293         :param int bvi_mac: Set to 1 to create entry that points to BVI
294             interface. (Default value = 0)
295         """
296         return self.api(self.papi.l2fib_add_del,
297                         {'mac': self._convert_mac(mac),
298                          'bd_id': bd_id,
299                          'sw_if_index': sw_if_index,
300                          'is_add': is_add,
301                          'static_mac': static_mac,
302                          'filter_mac': filter_mac,
303                          'bvi_mac': bvi_mac})
304
305     def sw_interface_set_l2_bridge(self, sw_if_index, bd_id,
306                                    shg=0, bvi=0, enable=1):
307         """Add/remove interface to/from bridge domain.
308
309         :param int sw_if_index: Software interface index of the interface.
310         :param int bd_id: Bridge domain index.
311         :param int shg: Split-horizon group index. (Default value = 0)
312         :param int bvi: Set interface as a bridge group virtual interface.
313             (Default value = 0)
314         :param int enable: Add or remove interface. (Default value = 1)
315         """
316         return self.api(self.papi.sw_interface_set_l2_bridge,
317                         {'rx_sw_if_index': sw_if_index,
318                          'bd_id': bd_id,
319                          'shg': shg,
320                          'bvi': bvi,
321                          'enable': enable})
322
323     def bridge_flags(self, bd_id, is_set, feature_bitmap):
324         """Enable/disable required feature of the bridge domain with defined ID.
325
326         :param int bd_id: Bridge domain ID.
327         :param int is_set: Set to 1 to enable, set to 0 to disable the feature.
328         :param int feature_bitmap: Bitmap value of the feature to be set:
329             - learn (1 << 0),
330             - forward (1 << 1),
331             - flood (1 << 2),
332             - uu-flood (1 << 3) or
333             - arp-term (1 << 4).
334         """
335         return self.api(self.papi.bridge_flags,
336                         {'bd_id': bd_id,
337                          'is_set': is_set,
338                          'feature_bitmap': feature_bitmap})
339
340     def bridge_domain_dump(self, bd_id=0):
341         """
342
343         :param int bd_id: Bridge domain ID. (Default value = 0 => dump of all
344             existing bridge domains returned)
345         :return: Dictionary of bridge domain(s) data.
346         """
347         return self.api(self.papi.bridge_domain_dump,
348                         {'bd_id': bd_id})
349
350     def sw_interface_set_l2_xconnect(self, rx_sw_if_index, tx_sw_if_index,
351                                      enable):
352         """Create or delete unidirectional cross-connect from Tx interface to
353         Rx interface.
354
355         :param int rx_sw_if_index: Software interface index of Rx interface.
356         :param int tx_sw_if_index: Software interface index of Tx interface.
357         :param int enable: Create cross-connect if equal to 1, delete
358             cross-connect if equal to 0.
359
360         """
361         return self.api(self.papi.sw_interface_set_l2_xconnect,
362                         {'rx_sw_if_index': rx_sw_if_index,
363                          'tx_sw_if_index': tx_sw_if_index,
364                          'enable': enable})
365
366     def sw_interface_set_l2_tag_rewrite(
367             self,
368             sw_if_index,
369             vtr_oper,
370             push=0,
371             tag1=0,
372             tag2=0):
373         """L2 interface vlan tag rewrite configure request
374         :param client_index - opaque cookie to identify the sender
375         :param context - sender context, to match reply w/ request
376         :param sw_if_index - interface the operation is applied to
377         :param vtr_op - Choose from l2_vtr_op_t enum values
378         :param push_dot1q - first pushed flag dot1q id set, else dot1ad
379         :param tag1 - Needed for any push or translate vtr op
380         :param tag2 - Needed for any push 2 or translate x-2 vtr ops
381
382         """
383         return self.api(self.papi.l2_interface_vlan_tag_rewrite,
384                         {'sw_if_index': sw_if_index,
385                          'vtr_op': vtr_oper,
386                          'push_dot1q': push,
387                          'tag1': tag1,
388                          'tag2': tag2})
389
390     def sw_interface_set_flags(self, sw_if_index, admin_up_down,
391                                link_up_down=0, deleted=0):
392         """
393
394         :param admin_up_down:
395         :param sw_if_index:
396         :param link_up_down:  (Default value = 0)
397         :param deleted:  (Default value = 0)
398
399         """
400         return self.api(self.papi.sw_interface_set_flags,
401                         {'sw_if_index': sw_if_index,
402                          'admin_up_down': admin_up_down,
403                          'link_up_down': link_up_down,
404                          'deleted': deleted})
405
406     def create_subif(self, sw_if_index, sub_id, outer_vlan, inner_vlan,
407                      no_tags=0, one_tag=0, two_tags=0, dot1ad=0, exact_match=0,
408                      default_sub=0, outer_vlan_id_any=0, inner_vlan_id_any=0):
409         """Create subinterface
410         from vpe.api: set dot1ad = 0 for dot1q, set dot1ad = 1 for dot1ad
411
412         :param sub_id: param inner_vlan:
413         :param sw_if_index:
414         :param outer_vlan:
415         :param inner_vlan:
416         :param no_tags:  (Default value = 0)
417         :param one_tag:  (Default value = 0)
418         :param two_tags:  (Default value = 0)
419         :param dot1ad:  (Default value = 0)
420         :param exact_match:  (Default value = 0)
421         :param default_sub:  (Default value = 0)
422         :param outer_vlan_id_any:  (Default value = 0)
423         :param inner_vlan_id_any:  (Default value = 0)
424
425         """
426         return self.api(
427             self.papi.create_subif,
428             {'sw_if_index': sw_if_index,
429              'sub_id': sub_id,
430              'no_tags': no_tags,
431              'one_tag': one_tag,
432              'two_tags': two_tags,
433              'dot1ad': dot1ad,
434              'exact_match': exact_match,
435              'default_sub': default_sub,
436              'outer_vlan_id_any': outer_vlan_id_any,
437              'inner_vlan_id_any': inner_vlan_id_any,
438              'outer_vlan_id': outer_vlan,
439              'inner_vlan_id': inner_vlan})
440
441     def delete_subif(self, sw_if_index):
442         """Delete subinterface
443
444         :param sw_if_index:
445         """
446         return self.api(self.papi.delete_subif,
447                         {'sw_if_index': sw_if_index})
448
449     def create_vlan_subif(self, sw_if_index, vlan):
450         """
451
452         :param vlan:
453         :param sw_if_index:
454
455         """
456         return self.api(self.papi.create_vlan_subif,
457                         {'sw_if_index': sw_if_index,
458                          'vlan_id': vlan})
459
460     def create_loopback(self, mac=''):
461         """
462
463         :param mac: (Optional)
464         """
465         return self.api(self.papi.create_loopback,
466                         {'mac_address': mac})
467
468     def ip_add_del_route(
469             self,
470             dst_address,
471             dst_address_length,
472             next_hop_address,
473             next_hop_sw_if_index=0xFFFFFFFF,
474             table_id=0,
475             next_hop_table_id=0,
476             next_hop_weight=1,
477             next_hop_n_out_labels=0,
478             next_hop_out_label_stack=[],
479             next_hop_via_label=MPLS_LABEL_INVALID,
480             create_vrf_if_needed=0,
481             is_resolve_host=0,
482             is_resolve_attached=0,
483             classify_table_index=0xFFFFFFFF,
484             is_add=1,
485             is_drop=0,
486             is_unreach=0,
487             is_prohibit=0,
488             is_ipv6=0,
489             is_local=0,
490             is_classify=0,
491             is_multipath=0,
492             not_last=0):
493         """
494
495         :param dst_address_length:
496         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
497         :param dst_address:
498         :param next_hop_address:
499         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
500         :param vrf_id:  (Default value = 0)
501         :param lookup_in_vrf:  (Default value = 0)
502         :param classify_table_index:  (Default value = 0xFFFFFFFF)
503         :param create_vrf_if_needed:  (Default value = 0)
504         :param is_add:  (Default value = 1)
505         :param is_drop:  (Default value = 0)
506         :param is_ipv6:  (Default value = 0)
507         :param is_local:  (Default value = 0)
508         :param is_classify:  (Default value = 0)
509         :param is_multipath:  (Default value = 0)
510         :param is_resolve_host:  (Default value = 0)
511         :param is_resolve_attached:  (Default value = 0)
512         :param not_last:  (Default value = 0)
513         :param next_hop_weight:  (Default value = 1)
514
515         """
516
517         return self.api(
518             self.papi.ip_add_del_route,
519             {'next_hop_sw_if_index': next_hop_sw_if_index,
520              'table_id': table_id,
521              'classify_table_index': classify_table_index,
522              'next_hop_table_id': next_hop_table_id,
523              'create_vrf_if_needed': create_vrf_if_needed,
524              'is_add': is_add,
525              'is_drop': is_drop,
526              'is_unreach': is_unreach,
527              'is_prohibit': is_prohibit,
528              'is_ipv6': is_ipv6,
529              'is_local': is_local,
530              'is_classify': is_classify,
531              'is_multipath': is_multipath,
532              'is_resolve_host': is_resolve_host,
533              'is_resolve_attached': is_resolve_attached,
534              'not_last': not_last,
535              'next_hop_weight': next_hop_weight,
536              'dst_address_length': dst_address_length,
537              'dst_address': dst_address,
538              'next_hop_address': next_hop_address,
539              'next_hop_n_out_labels': next_hop_n_out_labels,
540              'next_hop_via_label': next_hop_via_label,
541              'next_hop_out_label_stack': next_hop_out_label_stack})
542
543     def ip_neighbor_add_del(self,
544                             sw_if_index,
545                             mac_address,
546                             dst_address,
547                             vrf_id=0,
548                             is_add=1,
549                             is_ipv6=0,
550                             is_static=0,
551                             ):
552         """ Add neighbor MAC to IPv4 or IPv6 address.
553
554         :param sw_if_index:
555         :param mac_address:
556         :param dst_address:
557         :param vrf_id:  (Default value = 0)
558         :param is_add:  (Default value = 1)
559         :param is_ipv6:  (Default value = 0)
560         :param is_static:  (Default value = 0)
561         """
562
563         return self.api(
564             self.papi.ip_neighbor_add_del,
565             {'vrf_id': vrf_id,
566              'sw_if_index': sw_if_index,
567              'is_add': is_add,
568              'is_ipv6': is_ipv6,
569              'is_static': is_static,
570              'mac_address': mac_address,
571              'dst_address': dst_address
572              }
573         )
574
575     def sw_interface_span_enable_disable(
576             self, sw_if_index_from, sw_if_index_to, state=1):
577         """
578
579         :param sw_if_index_from:
580         :param sw_if_index_to:
581         :param enable
582
583         """
584         return self.api(self.papi.sw_interface_span_enable_disable,
585                         {'sw_if_index_from': sw_if_index_from,
586                          'sw_if_index_to': sw_if_index_to,
587                          'state': state})
588
589     def gre_tunnel_add_del(self,
590                            src_address,
591                            dst_address,
592                            outer_fib_id=0,
593                            is_teb=0,
594                            is_add=1,
595                            is_ip6=0):
596         """ Add a GRE tunnel
597
598         :param src_address:
599         :param dst_address:
600         :param outer_fib_id:  (Default value = 0)
601         :param is_add:  (Default value = 1)
602         :param is_ipv6:  (Default value = 0)
603         :param is_teb:  (Default value = 0)
604         """
605
606         return self.api(
607             self.papi.gre_add_del_tunnel,
608             {'is_add': is_add,
609              'is_ipv6': is_ip6,
610              'teb': is_teb,
611              'src_address': src_address,
612              'dst_address': dst_address,
613              'outer_fib_id': outer_fib_id}
614         )
615
616     def mpls_route_add_del(
617             self,
618             label,
619             eos,
620             next_hop_proto_is_ip4,
621             next_hop_address,
622             next_hop_sw_if_index=0xFFFFFFFF,
623             table_id=0,
624             next_hop_table_id=0,
625             next_hop_weight=1,
626             next_hop_n_out_labels=0,
627             next_hop_out_label_stack=[],
628             next_hop_via_label=MPLS_LABEL_INVALID,
629             create_vrf_if_needed=0,
630             is_resolve_host=0,
631             is_resolve_attached=0,
632             is_add=1,
633             is_drop=0,
634             is_multipath=0,
635             classify_table_index=0xFFFFFFFF,
636             is_classify=0,
637             not_last=0):
638         """
639
640         :param dst_address_length:
641         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
642         :param dst_address:
643         :param next_hop_address:
644         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
645         :param vrf_id:  (Default value = 0)
646         :param lookup_in_vrf:  (Default value = 0)
647         :param classify_table_index:  (Default value = 0xFFFFFFFF)
648         :param create_vrf_if_needed:  (Default value = 0)
649         :param is_add:  (Default value = 1)
650         :param is_drop:  (Default value = 0)
651         :param is_ipv6:  (Default value = 0)
652         :param is_local:  (Default value = 0)
653         :param is_classify:  (Default value = 0)
654         :param is_multipath:  (Default value = 0)
655         :param is_resolve_host:  (Default value = 0)
656         :param is_resolve_attached:  (Default value = 0)
657         :param not_last:  (Default value = 0)
658         :param next_hop_weight:  (Default value = 1)
659
660         """
661
662         return self.api(
663             self.papi.mpls_route_add_del,
664             {'mr_label': label,
665              'mr_eos': eos,
666              'mr_table_id': table_id,
667              'mr_classify_table_index': classify_table_index,
668              'mr_create_table_if_needed': create_vrf_if_needed,
669              'mr_is_add': is_add,
670              'mr_is_classify': is_classify,
671              'mr_is_multipath': is_multipath,
672              'mr_is_resolve_host': is_resolve_host,
673              'mr_is_resolve_attached': is_resolve_attached,
674              'mr_next_hop_proto_is_ip4': next_hop_proto_is_ip4,
675              'mr_next_hop_weight': next_hop_weight,
676              'mr_next_hop': next_hop_address,
677              'mr_next_hop_n_out_labels': next_hop_n_out_labels,
678              'mr_next_hop_sw_if_index': next_hop_sw_if_index,
679              'mr_next_hop_table_id': next_hop_table_id,
680              'mr_next_hop_via_label': next_hop_via_label,
681              'mr_next_hop_out_label_stack': next_hop_out_label_stack})
682
683     def mpls_ip_bind_unbind(
684             self,
685             label,
686             dst_address,
687             dst_address_length,
688             table_id=0,
689             ip_table_id=0,
690             is_ip4=1,
691             create_vrf_if_needed=0,
692             is_bind=1):
693         """
694         """
695         return self.api(
696             self.papi.mpls_ip_bind_unbind,
697             {'mb_mpls_table_id': table_id,
698              'mb_label': label,
699              'mb_ip_table_id': ip_table_id,
700              'mb_create_table_if_needed': create_vrf_if_needed,
701              'mb_is_bind': is_bind,
702              'mb_is_ip4': is_ip4,
703              'mb_address_length': dst_address_length,
704              'mb_address': dst_address})
705
706     def mpls_tunnel_add_del(
707             self,
708             tun_sw_if_index,
709             next_hop_proto_is_ip4,
710             next_hop_address,
711             next_hop_sw_if_index=0xFFFFFFFF,
712             next_hop_table_id=0,
713             next_hop_weight=1,
714             next_hop_n_out_labels=0,
715             next_hop_out_label_stack=[],
716             next_hop_via_label=MPLS_LABEL_INVALID,
717             create_vrf_if_needed=0,
718             is_add=1,
719             l2_only=0):
720         """
721
722         :param dst_address_length:
723         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
724         :param dst_address:
725         :param next_hop_address:
726         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
727         :param vrf_id:  (Default value = 0)
728         :param lookup_in_vrf:  (Default value = 0)
729         :param classify_table_index:  (Default value = 0xFFFFFFFF)
730         :param create_vrf_if_needed:  (Default value = 0)
731         :param is_add:  (Default value = 1)
732         :param is_drop:  (Default value = 0)
733         :param is_ipv6:  (Default value = 0)
734         :param is_local:  (Default value = 0)
735         :param is_classify:  (Default value = 0)
736         :param is_multipath:  (Default value = 0)
737         :param is_resolve_host:  (Default value = 0)
738         :param is_resolve_attached:  (Default value = 0)
739         :param not_last:  (Default value = 0)
740         :param next_hop_weight:  (Default value = 1)
741
742         """
743         return self.api(
744             self.papi.mpls_tunnel_add_del,
745             {'mt_sw_if_index': tun_sw_if_index,
746              'mt_is_add': is_add,
747              'mt_l2_only': l2_only,
748              'mt_next_hop_proto_is_ip4': next_hop_proto_is_ip4,
749              'mt_next_hop_weight': next_hop_weight,
750              'mt_next_hop': next_hop_address,
751              'mt_next_hop_n_out_labels': next_hop_n_out_labels,
752              'mt_next_hop_sw_if_index': next_hop_sw_if_index,
753              'mt_next_hop_table_id': next_hop_table_id,
754              'mt_next_hop_out_label_stack': next_hop_out_label_stack})
755
756     def snat_interface_add_del_feature(
757             self,
758             sw_if_index,
759             is_inside=1,
760             is_add=1):
761         """Enable/disable S-NAT feature on the interface
762
763         :param sw_if_index: Software index of the interface
764         :param is_inside: 1 if inside, 0 if outside (Default value = 1)
765         :param is_add: 1 if add, 0 if delete (Default value = 1)
766         """
767         return self.api(
768             self.papi.snat_interface_add_del_feature,
769             {'is_add': is_add,
770              'is_inside': is_inside,
771              'sw_if_index': sw_if_index})
772
773     def snat_add_static_mapping(
774             self,
775             local_ip,
776             external_ip,
777             local_port=0,
778             external_port=0,
779             addr_only=1,
780             vrf_id=0,
781             is_add=1,
782             is_ip4=1):
783         """Add/delete S-NAT static mapping
784
785         :param local_ip: Local IP address
786         :param external_ip: External IP address
787         :param local_port: Local port number (Default value = 0)
788         :param external_port: External port number (Default value = 0)
789         :param addr_only: 1 if address only mapping, 0 if address and port
790         :param vrf_id: VRF ID
791         :param is_add: 1 if add, 0 if delete (Default value = 1)
792         :param is_ip4: 1 if address type is IPv4 (Default value = 1)
793         """
794         return self.api(
795             self.papi.snat_add_static_mapping,
796             {'is_add': is_add,
797              'is_ip4': is_ip4,
798              'addr_only': addr_only,
799              'local_ip_address': local_ip,
800              'external_ip_address': external_ip,
801              'local_port': local_port,
802              'external_port': external_port,
803              'vrf_id': vrf_id})
804
805     def snat_add_address_range(
806             self,
807             first_ip_address,
808             last_ip_address,
809             is_add=1,
810             is_ip4=1):
811         """Add/del S-NAT address range
812
813         :param first_ip_address: First IP address
814         :param last_ip_address: Last IP address
815         :param is_add: 1 if add, 0 if delete (Default value = 1)
816         :param is_ip4: 1 if address type is IPv4 (Default value = 1)
817         """
818         return self.api(
819             self.papi.snat_add_address_range,
820             {'is_ip4': is_ip4,
821              'first_ip_address': first_ip_address,
822              'last_ip_address': last_ip_address,
823              'is_add': is_add})
824
825     def snat_address_dump(self):
826         """Dump S-NAT addresses
827         :return: Dictionary of S-NAT addresses
828         """
829         return self.api(self.papi.snat_address_dump, {})
830
831     def snat_interface_dump(self):
832         """Dump interfaces with S-NAT feature
833         :return: Dictionary of interfaces with S-NAT feature
834         """
835         return self.api(self.papi.snat_interface_dump, {})
836
837     def snat_static_mapping_dump(self):
838         """Dump S-NAT static mappings
839         :return: Dictionary of S-NAT static mappings
840         """
841         return self.api(self.papi.snat_static_mapping_dump, {})
842
843     def control_ping(self):
844         self.api(self.papi.control_ping)
845
846     def bfd_udp_add(self, sw_if_index, desired_min_tx, required_min_rx,
847                     detect_mult, local_addr, peer_addr, is_ipv6=0):
848         return self.api(self.papi.bfd_udp_add,
849                         {
850                             'sw_if_index': sw_if_index,
851                             'desired_min_tx': desired_min_tx,
852                             'required_min_rx': required_min_rx,
853                             'local_addr': local_addr,
854                             'peer_addr': peer_addr,
855                             'is_ipv6': is_ipv6,
856                             'detect_mult': detect_mult,
857                         })
858
859     def bfd_udp_del(self, sw_if_index, local_addr, peer_addr, is_ipv6=0):
860         return self.api(self.papi.bfd_udp_del,
861                         {
862                             'sw_if_index': sw_if_index,
863                             'local_addr': local_addr,
864                             'peer_addr': peer_addr,
865                             'is_ipv6': is_ipv6,
866                         })
867
868     def bfd_udp_session_dump(self):
869         return self.api(self.papi.bfd_udp_session_dump, {})
870
871     def bfd_session_set_flags(self, bs_idx, admin_up_down):
872         return self.api(self.papi.bfd_session_set_flags, {
873             'bs_index': bs_idx,
874             'admin_up_down': admin_up_down,
875         })
876
877     def want_bfd_events(self, enable_disable=1):
878         return self.api(self.papi.want_bfd_events, {
879             'enable_disable': enable_disable,
880             'pid': os.getpid(),
881         })