vpp_papi_provider.py revision 04338e85
1# NB NB NB NB NB NB NB NB NB NB NB
2#
3# NOTE: The API binary wrappers in this file are in the process of being
4# deprecated. DO NOT ADD NEW WRAPPERS HERE. Call the functions using
5# named arguments directly instead.
6#
7
8import os
9import time
10from collections import deque
11
12from six import moves, iteritems
13from vpp_papi import VPPApiClient, mac_pton
14from hook import Hook
15from vpp_ip_route import MPLS_IETF_MAX_LABEL, MPLS_LABEL_INVALID
16
17
18#
19# Dictionary keyed on message name to override default values for
20# named parameters
21#
22defaultmapping = {
23    'map_add_domain': {'mtu': 1280},
24    'syslog_set_sender': {'collector_port': 514,
25                          'max_msg_size': 480},
26    'acl_interface_add_del': {'is_add': 1, 'is_input': 1},
27    'acl_interface_list_dump': {'sw_if_index': 4294967295, },
28    'app_namespace_add_del': {'sw_if_index': 4294967295, },
29    'bd_ip_mac_add_del': {'is_add': 1, },
30    'bfd_udp_add': {'is_authenticated': False, 'bfd_key_id': None,
31                    'conf_key_id': None},
32    'bfd_udp_auth_activate': {'bfd_key_id': None, 'conf_key_id': None,
33                              'is_delayed': False},
34    'bier_disp_entry_add_del': {'next_hop_rpf_id': -1, 'next_hop_is_ip4': 1,
35                                'is_add': 1, },
36    'bier_disp_table_add_del': {'is_add': 1, },
37    'bier_imp_add': {'is_add': 1, },
38    'bier_route_add_del': {'is_add': 1, },
39    'bier_table_add_del': {'is_add': 1, },
40    'bond_create': {'mac_address': '', 'id': 0xFFFFFFFF},
41    'bridge_domain_add_del': {'flood': 1, 'uu_flood': 1, 'forward': 1,
42                              'learn': 1, 'is_add': 1, },
43    'bvi_create': {'user_instance': 4294967295, },
44    'bvi_delete': {},
45    'gbp_subnet_add_del': {'sw_if_index': 4294967295, 'epg_id': 65535, },
46    'geneve_add_del_tunnel': {'mcast_sw_if_index': 4294967295, 'is_add': 1,
47                              'decap_next_index': 4294967295, },
48    'gre_tunnel_add_del': {'instance': 4294967295, 'is_add': 1, },
49    'gtpu_add_del_tunnel': {'is_add': 1, 'mcast_sw_if_index': 4294967295,
50                            'decap_next_index': 4294967295, },
51    'input_acl_set_interface': {'ip4_table_index': 4294967295,
52                                'ip6_table_index': 4294967295,
53                                'l2_table_index': 4294967295, },
54    'ip6_add_del_address_using_prefix': {'is_add': 1, },
55    'ip6nd_send_router_solicitation': {'irt': 1, 'mrt': 120, },
56    'ip_add_del_route': {'next_hop_sw_if_index': 4294967295,
57                         'next_hop_weight': 1, 'next_hop_via_label': 1048576,
58                         'classify_table_index': 4294967295, 'is_add': 1, },
59    'ip_mroute_add_del': {'is_add': 1, },
60    'ip_neighbor_add_del': {'is_add': 1, },
61    'ip_punt_police': {'is_add': 1, },
62    'ip_punt_redirect': {'is_add': 1, },
63    'ip_route_add_del': {'is_add': 1, },
64    'ip_unnumbered_dump': {'sw_if_index': 4294967295, },
65    'ipsec_interface_add_del_spd': {'is_add': 1, },
66    'ipsec_sad_entry_add_del': {'is_add': 1, },
67    'ipsec_spd_add_del': {'is_add': 1, },
68    'ipsec_spd_dump': {'sa_id': 4294967295, },
69    'ipsec_spd_entry_add_del': {'local_port_stop': 65535,
70                                'remote_port_stop': 65535, 'priority': 100,
71                                'is_outbound': 1,
72                                'is_add': 1, },
73    'ipsec_tunnel_if_add_del': {'is_add': 1, 'anti_replay': 1, },
74    'l2_emulation': {'enable': 1, },
75    'l2fib_add_del': {'is_add': 1, },
76    'lb_conf': {'sticky_buckets_per_core': 4294967295,
77                'flow_timeout': 4294967295},
78    'lisp_add_del_adjacency': {'is_add': 1, },
79    'lisp_add_del_local_eid': {'is_add': 1, },
80    'lisp_add_del_locator': {'priority': 1, 'weight': 1, 'is_add': 1, },
81    'lisp_add_del_locator_set': {'is_add': 1, },
82    'lisp_add_del_remote_mapping': {'is_add': 1, },
83    'macip_acl_add_replace': {'acl_index': 4294967295, },
84    'macip_acl_dump': {'acl_index': 4294967295, },
85    'macip_acl_interface_add_del': {'is_add': 1, },
86    'mpls_ip_bind_unbind': {'is_ip4': 1, 'is_bind': 1, },
87    'mpls_route_add_del': {'mr_next_hop_sw_if_index': 4294967295,
88                           'mr_next_hop_weight': 1,
89                           'mr_next_hop_via_label': 1048576,
90                           'mr_is_add': 1,
91                           'mr_classify_table_index': 4294967295, },
92    'mpls_table_add_del': {'is_add': 1, },
93    'mpls_tunnel_add_del': {'next_hop_sw_if_index': 4294967295,
94                            'next_hop_weight': 1,
95                            'next_hop_via_label': 1048576,
96                            'is_add': 1, },
97    'mpls_tunnel_dump': {'sw_if_index': 4294967295, },
98    'output_acl_set_interface': {'ip4_table_index': 4294967295,
99                                 'ip6_table_index': 4294967295,
100                                 'l2_table_index': 4294967295, },
101    'pppoe_add_del_session': {'is_add': 1, },
102    'policer_add_del': {'is_add': 1, 'conform_action_type': 1, },
103    'proxy_arp_add_del': {'is_add': 1, },
104    'proxy_arp_intfc_enable_disable': {'is_enable': 1, },
105    'set_ip_flow_hash': {'src': 1, 'dst': 1, 'sport': 1, 'dport': 1,
106                         'proto': 1, },
107    'set_ipfix_exporter': {'collector_port': 4739, },
108    'sr_localsid_add_del': {'sw_if_index': 4294967295, },
109    'sr_policy_add': {'weight': 1, 'is_encap': 1, },
110    'svs_enable_disable': {'is_enable': 1, },
111    'svs_route_add_del': {'is_add': 1, },
112    'svs_table_add_del': {'is_add': 1, },
113    'sw_interface_add_del_address': {'is_add': 1, },
114    'sw_interface_dump': {'sw_if_index': 4294967295, },
115    'sw_interface_ip6nd_ra_prefix': {'val_lifetime': 4294967295,
116                                     'pref_lifetime': 4294967295, },
117    'sw_interface_set_ip_directed_broadcast': {'enable': 1, },
118    'sw_interface_set_l2_bridge': {'enable': 1, },
119    'sw_interface_set_mpls_enable': {'enable': 1, },
120    'sw_interface_set_mtu': {'mtu': [0, 0, 0, 0], },
121    'sw_interface_set_unnumbered': {'is_add': 1, },
122    'sw_interface_span_enable_disable': {'state': 1, },
123    'vxlan_add_del_tunnel': {'mcast_sw_if_index': 4294967295, 'is_add': 1,
124                             'decap_next_index': 4294967295,
125                             'instance': 4294967295, },
126    'vxlan_gbp_tunnel_dump': {'sw_if_index': 4294967295, },
127    'vxlan_gpe_add_del_tunnel': {'mcast_sw_if_index': 4294967295, 'is_add': 1,
128                                 'protocol': 3, },
129    'want_bfd_events': {'enable_disable': 1, },
130    'want_igmp_events': {'enable': 1, },
131    'want_interface_events': {'enable_disable': 1, },
132    'want_ip4_arp_events': {'enable_disable': 1, 'ip': '0.0.0.0', },
133    'want_ip6_nd_events': {'enable_disable': 1, 'ip': '::', },
134    'want_ip6_ra_events': {'enable_disable': 1, },
135    'want_l2_macs_events': {'enable_disable': 1, },
136}
137
138
139class CliFailedCommandError(Exception):
140    """ cli command failed."""
141
142
143class CliSyntaxError(Exception):
144    """ cli command had a syntax error."""
145
146
147class UnexpectedApiReturnValueError(Exception):
148    """ exception raised when the API return value is unexpected """
149    pass
150
151
152class VppPapiProvider(object):
153    """VPP-api provider using vpp-papi
154
155    @property hook: hook object providing before and after api/cli hooks
156    """
157
158    _zero, _negative = range(2)
159
160    def __init__(self, name, shm_prefix, test_class, read_timeout):
161        self.hook = Hook(test_class)
162        self.name = name
163        self.shm_prefix = shm_prefix
164        self.test_class = test_class
165        self._expect_api_retval = self._zero
166        self._expect_stack = []
167
168        # install_dir is a class attribute. We need to set it before
169        # calling the constructor.
170        VPPApiClient.apidir = os.getenv('VPP_INSTALL_PATH')
171
172        use_socket = False
173        try:
174            if os.environ['SOCKET'] == '1':
175                use_socket = True
176        except KeyError:
177            pass
178
179        self.vpp = VPPApiClient(logger=test_class.logger,
180                                read_timeout=read_timeout,
181                                use_socket=use_socket,
182                                server_address=test_class.api_sock)
183        self._events = deque()
184
185    def __enter__(self):
186        return self
187
188    def assert_negative_api_retval(self):
189        """ Expect API failure - used with with, e.g.:
190            with self.vapi.assert_negative_api_retval():
191                self.vapi.<api call expected to fail>
192        """
193        self._expect_stack.append(self._expect_api_retval)
194        self._expect_api_retval = self._negative
195        return self
196
197    def assert_zero_api_retval(self):
198        """ Expect API success - used with with, e.g.:
199            with self.vapi.assert_negative_api_retval():
200                self.vapi.<api call expected to succeed>
201
202            note: this is useful only inside another with block
203                  as success is the default expected value
204        """
205        self._expect_stack.append(self._expect_api_retval)
206        self._expect_api_retval = self._zero
207        return self
208
209    def __exit__(self, exc_type, exc_value, traceback):
210        self._expect_api_retval = self._expect_stack.pop()
211
212    def register_hook(self, hook):
213        """Replace hook registration with new hook
214
215        :param hook:
216
217        """
218        self.hook = hook
219
220    def collect_events(self):
221        """ Collect all events from the internal queue and clear the queue. """
222        e = self._events
223        self._events = deque()
224        return e
225
226    def wait_for_event(self, timeout, name=None):
227        """ Wait for and return next event. """
228        if name:
229            self.test_class.logger.debug("Expecting event '%s' within %ss",
230                                         name, timeout)
231        else:
232            self.test_class.logger.debug("Expecting event within %ss",
233                                         timeout)
234        if self._events:
235            self.test_class.logger.debug("Not waiting, event already queued")
236        limit = time.time() + timeout
237        while time.time() < limit:
238            if self._events:
239                e = self._events.popleft()
240                if name and type(e).__name__ != name:
241                    raise Exception(
242                        "Unexpected event received: %s, expected: %s" %
243                        (type(e).__name__, name))
244                self.test_class.logger.debug("Returning event %s:%s" %
245                                             (name, e))
246                return e
247            self.test_class.sleep(0)  # yield
248        raise Exception("Event did not occur within timeout")
249
250    def __call__(self, name, event):
251        """ Enqueue event in the internal event queue. """
252        # FIXME use the name instead of relying on type(e).__name__ ?
253        # FIXME #2 if this throws, it is eaten silently, Ole?
254        self.test_class.logger.debug("New event: %s: %s" % (name, event))
255        self._events.append(event)
256
257    def factory(self, name, apifn):
258        def f(*a, **ka):
259            fields = apifn._func.msg.fields
260
261            # add positional and kw arguments
262            d = ka
263            for i, o in enumerate(fields[3:]):
264                try:
265                    d[o] = a[i]
266                except BaseException:
267                    break
268
269            # Default override
270            if name in defaultmapping:
271                for k, v in iteritems(defaultmapping[name]):
272                    if k in d:
273                        continue
274                    d[k] = v
275            return self.api(apifn, d)
276
277        return f
278
279    def __getattribute__(self, name):
280        try:
281            method = super(VppPapiProvider, self).__getattribute__(name)
282        except AttributeError:
283            method = self.factory(name, getattr(self.papi, name))
284            # lazily load the method so we don't need to call factory
285            # again for this name.
286            setattr(self, name, method)
287        return method
288
289    def connect(self):
290        """Connect the API to VPP"""
291        self.vpp.connect(self.name, self.shm_prefix)
292        self.papi = self.vpp.api
293        self.vpp.register_event_callback(self)
294
295    def disconnect(self):
296        """Disconnect the API from VPP"""
297        self.vpp.disconnect()
298
299    def api(self, api_fn, api_args, expected_retval=0):
300        """ Call API function and check it's return value.
301        Call the appropriate hooks before and after the API call
302
303        :param api_fn: API function to call
304        :param api_args: tuple of API function arguments
305        :param expected_retval: Expected return value (Default value = 0)
306        :returns: reply from the API
307
308        """
309        self.hook.before_api(api_fn.__name__, api_args)
310        reply = api_fn(**api_args)
311        if self._expect_api_retval == self._negative:
312            if hasattr(reply, 'retval') and reply.retval >= 0:
313                msg = "API call passed unexpectedly: expected negative " \
314                      "return value instead of %d in %s" % \
315                      (reply.retval, moves.reprlib.repr(reply))
316                self.test_class.logger.info(msg)
317                raise UnexpectedApiReturnValueError(msg)
318        elif self._expect_api_retval == self._zero:
319            if hasattr(reply, 'retval') and reply.retval != expected_retval:
320                msg = "API call failed, expected %d return value instead " \
321                      "of %d in %s" % (expected_retval, reply.retval,
322                                       repr(reply))
323                self.test_class.logger.info(msg)
324                raise UnexpectedApiReturnValueError(msg)
325        else:
326            raise Exception("Internal error, unexpected value for "
327                            "self._expect_api_retval %s" %
328                            self._expect_api_retval)
329        self.hook.after_api(api_fn.__name__, api_args)
330        return reply
331
332    def cli_return_response(self, cli):
333        """ Execute a CLI, calling the before/after hooks appropriately.
334        Return the reply without examining it
335
336        :param cli: CLI to execute
337        :returns: response object
338
339        """
340        self.hook.before_cli(cli)
341        cli += '\n'
342        r = self.papi.cli_inband(cmd=cli)
343        self.hook.after_cli(cli)
344        return r
345
346    def cli(self, cli):
347        """ Execute a CLI, calling the before/after hooks appropriately.
348
349        :param cli: CLI to execute
350        :returns: CLI output
351
352        """
353        r = self.cli_return_response(cli)
354        if r.retval == -156:
355            raise CliSyntaxError(r.reply)
356        if r.retval != 0:
357            raise CliFailedCommandError(r.reply)
358        if hasattr(r, 'reply'):
359            return r.reply
360
361    def ppcli(self, cli):
362        """ Helper method to print CLI command in case of info logging level.
363
364        :param cli: CLI to execute
365        :returns: CLI output
366        """
367        return cli + "\n" + self.cli(cli)
368
369    def want_ip4_arp_events(self, enable_disable=1, ip="0.0.0.0"):
370        return self.api(self.papi.want_ip4_arp_events,
371                        {'enable_disable': enable_disable,
372                         'ip': ip,
373                         'pid': os.getpid(), })
374
375    def want_ip6_nd_events(self, enable_disable=1, ip="::"):
376        return self.api(self.papi.want_ip6_nd_events,
377                        {'enable_disable': enable_disable,
378                         'ip': ip,
379                         'pid': os.getpid(), })
380
381    def want_ip6_ra_events(self, enable_disable=1):
382        return self.api(self.papi.want_ip6_ra_events,
383                        {'enable_disable': enable_disable,
384                         'pid': os.getpid(), })
385
386    def ip6nd_send_router_solicitation(self, sw_if_index, irt=1, mrt=120,
387                                       mrc=0, mrd=0):
388        return self.api(self.papi.ip6nd_send_router_solicitation,
389                        {'irt': irt,
390                         'mrt': mrt,
391                         'mrc': mrc,
392                         'mrd': mrd,
393                         'sw_if_index': sw_if_index})
394
395    def want_interface_events(self, enable_disable=1):
396        return self.api(self.papi.want_interface_events,
397                        {'enable_disable': enable_disable,
398                         'pid': os.getpid(), })
399
400    def want_l2_macs_events(self, enable_disable=1, scan_delay=0,
401                            max_macs_in_event=0, learn_limit=0):
402        return self.api(self.papi.want_l2_macs_events,
403                        {'enable_disable': enable_disable,
404                         'scan_delay': scan_delay,
405                         'max_macs_in_event': max_macs_in_event,
406                         'learn_limit': learn_limit,
407                         'pid': os.getpid(), })
408
409    def sw_interface_set_mac_address(self, sw_if_index, mac):
410        return self.api(self.papi.sw_interface_set_mac_address,
411                        {'sw_if_index': sw_if_index,
412                         'mac_address': mac})
413
414    def p2p_ethernet_add(self, sw_if_index, remote_mac, subif_id):
415        """Create p2p ethernet subinterface
416
417        :param sw_if_index: main (parent) interface
418        :param remote_mac: client (remote) mac address
419
420        """
421        return self.api(
422            self.papi.p2p_ethernet_add,
423            {'parent_if_index': sw_if_index,
424             'remote_mac': remote_mac,
425             'subif_id': subif_id})
426
427    def p2p_ethernet_del(self, sw_if_index, remote_mac):
428        """Delete p2p ethernet subinterface
429
430        :param sw_if_index: main (parent) interface
431        :param remote_mac: client (remote) mac address
432
433        """
434        return self.api(
435            self.papi.p2p_ethernet_del,
436            {'parent_if_index': sw_if_index,
437             'remote_mac': remote_mac})
438
439    def create_vlan_subif(self, sw_if_index, vlan):
440        """
441
442        :param vlan:
443        :param sw_if_index:
444
445        """
446        return self.api(self.papi.create_vlan_subif,
447                        {'sw_if_index': sw_if_index,
448                         'vlan_id': vlan})
449
450    def create_loopback(self, mac=''):
451        """
452
453        :param mac: (Optional)
454        """
455        return self.api(self.papi.create_loopback,
456                        {'mac_address': mac})
457
458    def ip_route_dump(self, table_id, is_ip6=False):
459        return self.api(self.papi.ip_route_dump,
460                        {'table': {
461                            'table_id': table_id,
462                            'is_ip6': is_ip6
463                        }})
464
465    def ip_neighbor_add_del(self,
466                            sw_if_index,
467                            mac_address,
468                            ip_address,
469                            is_add=1,
470                            flags=0):
471        """ Add neighbor MAC to IPv4 or IPv6 address.
472
473        :param sw_if_index:
474        :param mac_address:
475        :param dst_address:
476        :param is_add:  (Default value = 1)
477        :param flags:  (Default value = 0/NONE)
478        """
479        return self.api(
480            self.papi.ip_neighbor_add_del,
481            {
482                'is_add': is_add,
483                'neighbor': {
484                    'sw_if_index': sw_if_index,
485                    'flags': flags,
486                    'mac_address': mac_address,
487                    'ip_address': ip_address
488                }
489            }
490        )
491
492    def proxy_arp_add_del(self,
493                          low,
494                          hi,
495                          table_id=0,
496                          is_add=1):
497        """ Config Proxy Arp Range.
498
499        :param low_address: Start address in the rnage to Proxy for
500        :param hi_address: End address in the rnage to Proxy for
501        :param vrf_id: The VRF/table in which to proxy
502        """
503
504        return self.api(
505            self.papi.proxy_arp_add_del,
506            {'proxy':
507                {
508                    'table_id': table_id,
509                    'low': low,
510                    'hi': hi,
511                },
512                'is_add': is_add})
513
514    def proxy_arp_intfc_enable_disable(self,
515                                       sw_if_index,
516                                       is_enable=1):
517        """ Enable/Disable an interface for proxy ARP requests
518
519        :param sw_if_index: Interface
520        :param enable_disable: Enable/Disable
521        """
522
523        return self.api(
524            self.papi.proxy_arp_intfc_enable_disable,
525            {'sw_if_index': sw_if_index,
526             'enable_disable': is_enable
527             }
528        )
529
530    def gre_tunnel_add_del(self,
531                           src,
532                           dst,
533                           outer_table_id=0,
534                           type=0,
535                           mode=0,
536                           instance=0xFFFFFFFF,
537                           session_id=0,
538                           is_add=1):
539        """ Add a GRE tunnel
540
541        :param src_address:
542        :param dst_address:
543        :param outer_fib_id:  (Default value = 0)
544        :param tunnel_type:  (Default value = 0)
545        :param instance:  (Default value = 0xFFFFFFFF)
546        :param session_id: (Default value = 0)
547        :param is_add:  (Default value = 1)
548        :param is_ipv6:  (Default value = 0)
549        """
550
551        return self.api(
552            self.papi.gre_tunnel_add_del,
553            {'is_add': is_add,
554             'tunnel':
555             {
556                 'type': type,
557                 'mode': mode,
558                 'instance': instance,
559                 'src': src,
560                 'dst': dst,
561                 'outer_table_id': outer_table_id,
562                 'session_id': session_id}
563             }
564        )
565
566    def udp_encap_add(self,
567                      src_ip,
568                      dst_ip,
569                      src_port,
570                      dst_port,
571                      table_id=0):
572        """ Add a GRE tunnel
573        :param src_ip:
574        :param dst_ip:
575        :param src_port:
576        :param dst_port:
577        :param outer_fib_id:  (Default value = 0)
578        """
579
580        return self.api(
581            self.papi.udp_encap_add,
582            {
583                'udp_encap': {
584                    'src_ip': src_ip,
585                    'dst_ip': dst_ip,
586                    'src_port': src_port,
587                    'dst_port': dst_port,
588                    'table_id': table_id
589                }
590            })
591
592    def udp_encap_del(self, id):
593        return self.api(self.papi.udp_encap_del, {'id': id})
594
595    def udp_encap_dump(self):
596        return self.api(self.papi.udp_encap_dump, {})
597
598    def want_udp_encap_stats(self, enable=1):
599        return self.api(self.papi.want_udp_encap_stats,
600                        {'enable': enable,
601                         'pid': os.getpid()})
602
603    def mpls_route_dump(self, table_id):
604        return self.api(self.papi.mpls_route_dump,
605                        {'table': {
606                            'mt_table_id': table_id
607                        }})
608
609    def mpls_table_dump(self):
610        return self.api(self.papi.mpls_table_dump, {})
611
612    def mpls_table_add_del(
613            self,
614            table_id,
615            is_add=1):
616        """
617
618        :param table_id
619        :param is_add:  (Default value = 1)
620
621        """
622
623        return self.api(
624            self.papi.mpls_table_add_del,
625            {'mt_table':
626             {
627                 'mt_table_id': table_id,
628             },
629             'mt_is_add': is_add})
630
631    def mpls_route_add_del(self,
632                           table_id,
633                           label,
634                           eos,
635                           eos_proto,
636                           is_multicast,
637                           paths,
638                           is_add,
639                           is_multipath):
640        """ MPLS Route add/del """
641        return self.api(
642            self.papi.mpls_route_add_del,
643            {'mr_route':
644             {
645                 'mr_table_id': table_id,
646                 'mr_label': label,
647                 'mr_eos': eos,
648                 'mr_eos_proto': eos_proto,
649                 'mr_is_multicast': is_multicast,
650                 'mr_n_paths': len(paths),
651                 'mr_paths': paths,
652             },
653             'mr_is_add': is_add,
654             'mr_is_multipath': is_multipath})
655
656    def mpls_ip_bind_unbind(
657            self,
658            label,
659            prefix,
660            table_id=0,
661            ip_table_id=0,
662            is_bind=1):
663        """
664        """
665        return self.api(
666            self.papi.mpls_ip_bind_unbind,
667            {'mb_mpls_table_id': table_id,
668             'mb_label': label,
669             'mb_ip_table_id': ip_table_id,
670             'mb_is_bind': is_bind,
671             'mb_prefix': prefix})
672
673    def mpls_tunnel_add_del(
674            self,
675            tun_sw_if_index,
676            paths,
677            is_add=1,
678            l2_only=0,
679            is_multicast=0):
680        """
681        """
682        return self.api(
683            self.papi.mpls_tunnel_add_del,
684            {'mt_is_add': is_add,
685             'mt_tunnel':
686             {
687                 'mt_sw_if_index': tun_sw_if_index,
688                 'mt_l2_only': l2_only,
689                 'mt_is_multicast': is_multicast,
690                 'mt_n_paths': len(paths),
691                 'mt_paths': paths,
692             }})
693
694    def input_acl_set_interface(
695            self,
696            is_add,
697            sw_if_index,
698            ip4_table_index=0xFFFFFFFF,
699            ip6_table_index=0xFFFFFFFF,
700            l2_table_index=0xFFFFFFFF):
701        """
702        :param is_add:
703        :param sw_if_index:
704        :param ip4_table_index:  (Default value = 0xFFFFFFFF)
705        :param ip6_table_index:  (Default value = 0xFFFFFFFF)
706        :param l2_table_index:  (Default value = 0xFFFFFFFF)
707        """
708
709        return self.api(
710            self.papi.input_acl_set_interface,
711            {'sw_if_index': sw_if_index,
712             'ip4_table_index': ip4_table_index,
713             'ip6_table_index': ip6_table_index,
714             'l2_table_index': l2_table_index,
715             'is_add': is_add})
716
717    def output_acl_set_interface(
718            self,
719            is_add,
720            sw_if_index,
721            ip4_table_index=0xFFFFFFFF,
722            ip6_table_index=0xFFFFFFFF,
723            l2_table_index=0xFFFFFFFF):
724        """
725        :param is_add:
726        :param sw_if_index:
727        :param ip4_table_index:  (Default value = 0xFFFFFFFF)
728        :param ip6_table_index:  (Default value = 0xFFFFFFFF)
729        :param l2_table_index:  (Default value = 0xFFFFFFFF)
730        """
731
732        return self.api(
733            self.papi.output_acl_set_interface,
734            {'sw_if_index': sw_if_index,
735             'ip4_table_index': ip4_table_index,
736             'ip6_table_index': ip6_table_index,
737             'l2_table_index': l2_table_index,
738             'is_add': is_add})
739
740    def set_ipfix_exporter(
741            self,
742            collector_address,
743            src_address,
744            path_mtu,
745            template_interval,
746            vrf_id=0,
747            collector_port=4739,
748            udp_checksum=0):
749        return self.api(
750            self.papi.set_ipfix_exporter,
751            {
752                'collector_address': collector_address,
753                'collector_port': collector_port,
754                'src_address': src_address,
755                'vrf_id': vrf_id,
756                'path_mtu': path_mtu,
757                'template_interval': template_interval,
758                'udp_checksum': udp_checksum,
759            })
760
761    def ip_mroute_add_del(self,
762                          table_id,
763                          prefix,
764                          e_flags,
765                          rpf_id,
766                          paths,
767                          is_add=1,
768                          is_multipath=1):
769        """
770        IP Multicast Route add/del
771        """
772        return self.api(
773            self.papi.ip_mroute_add_del,
774            {
775                'is_add': is_add,
776                'is_multipath': is_multipath,
777                'route': {
778                    'table_id': table_id,
779                    'entry_flags': e_flags,
780                    'rpf_id': rpf_id,
781                    'prefix': prefix,
782                    'n_paths': len(paths),
783                    'paths': paths,
784                }
785            })
786
787    def mfib_signal_dump(self):
788        return self.api(self.papi.mfib_signal_dump, {})
789
790    def ip_mroute_dump(self, table_id, is_ip6=False):
791        return self.api(self.papi.ip_mroute_dump,
792                        {'table': {
793                            'table_id': table_id,
794                            'is_ip6': is_ip6
795                        }})
796
797    def lisp_enable_disable(self, is_enabled):
798        return self.api(
799            self.papi.lisp_enable_disable,
800            {
801                'is_en': is_enabled,
802            })
803
804    def lisp_add_del_locator_set(self,
805                                 ls_name,
806                                 is_add=1):
807        return self.api(
808            self.papi.lisp_add_del_locator_set,
809            {
810                'is_add': is_add,
811                'locator_set_name': ls_name
812            })
813
814    def lisp_add_del_locator(self,
815                             ls_name,
816                             sw_if_index,
817                             priority=1,
818                             weight=1,
819                             is_add=1):
820        return self.api(
821            self.papi.lisp_add_del_locator,
822            {
823                'is_add': is_add,
824                'locator_set_name': ls_name,
825                'sw_if_index': sw_if_index,
826                'priority': priority,
827                'weight': weight
828            })
829
830    def lisp_locator_dump(self, is_index_set, ls_name=None, ls_index=0):
831        return self.api(
832            self.papi.lisp_locator_dump,
833            {
834                'is_index_set': is_index_set,
835                'ls_name': ls_name,
836                'ls_index': ls_index,
837            })
838
839    def lisp_add_del_local_eid(self,
840                               ls_name,
841                               eid_type,
842                               eid,
843                               prefix_len,
844                               vni=0,
845                               key_id=0,
846                               key="",
847                               is_add=1):
848        return self.api(
849            self.papi.lisp_add_del_local_eid,
850            {
851                'locator_set_name': ls_name,
852                'is_add': is_add,
853                'eid_type': eid_type,
854                'eid': eid,
855                'prefix_len': prefix_len,
856                'vni': vni,
857                'key_id': key_id,
858                'key': key
859            })
860
861    def lisp_eid_table_dump(self,
862                            eid_set=0,
863                            prefix_length=0,
864                            vni=0,
865                            eid_type=0,
866                            eid=None,
867                            filter_opt=0):
868        return self.api(
869            self.papi.lisp_eid_table_dump,
870            {
871                'eid_set': eid_set,
872                'prefix_length': prefix_length,
873                'vni': vni,
874                'eid_type': eid_type,
875                'eid': eid,
876                'filter': filter_opt,
877            })
878
879    def lisp_add_del_remote_mapping(self,
880                                    eid_type,
881                                    eid,
882                                    eid_prefix_len=0,
883                                    vni=0,
884                                    rlocs=[],
885                                    rlocs_num=0,
886                                    is_src_dst=0,
887                                    is_add=1):
888        return self.api(
889            self.papi.lisp_add_del_remote_mapping,
890            {
891                'is_add': is_add,
892                'eid_type': eid_type,
893                'eid': eid,
894                'eid_len': eid_prefix_len,
895                'rloc_num': rlocs_num,
896                'rlocs': rlocs,
897                'vni': vni,
898                'is_src_dst': is_src_dst,
899            })
900
901    def lisp_add_del_adjacency(self,
902                               leid,
903                               reid,
904                               leid_len,
905                               reid_len,
906                               eid_type,
907                               is_add=1,
908                               vni=0):
909        return self.api(
910            self.papi.lisp_add_del_adjacency,
911            {
912                'is_add': is_add,
913                'vni': vni,
914                'eid_type': eid_type,
915                'leid': leid,
916                'reid': reid,
917                'leid_len': leid_len,
918                'reid_len': reid_len,
919            })
920
921    def gtpu_add_del_tunnel(
922            self,
923            src_addr,
924            dst_addr,
925            is_add=1,
926            is_ipv6=0,
927            mcast_sw_if_index=0xFFFFFFFF,
928            encap_vrf_id=0,
929            decap_next_index=0xFFFFFFFF,
930            teid=0):
931        """
932
933        :param is_add:  (Default value = 1)
934        :param is_ipv6:  (Default value = 0)
935        :param src_addr:
936        :param dst_addr:
937        :param mcast_sw_if_index:  (Default value = 0xFFFFFFFF)
938        :param encap_vrf_id:  (Default value = 0)
939        :param decap_next_index:  (Default value = 0xFFFFFFFF)
940        :param teid:  (Default value = 0)
941
942        """
943        return self.api(self.papi.gtpu_add_del_tunnel,
944                        {'is_add': is_add,
945                         'is_ipv6': is_ipv6,
946                         'src_address': src_addr,
947                         'dst_address': dst_addr,
948                         'mcast_sw_if_index': mcast_sw_if_index,
949                         'encap_vrf_id': encap_vrf_id,
950                         'decap_next_index': decap_next_index,
951                         'teid': teid})
952
953    def vxlan_gpe_add_del_tunnel(
954            self,
955            src_addr,
956            dst_addr,
957            mcast_sw_if_index=0xFFFFFFFF,
958            is_add=1,
959            is_ipv6=0,
960            encap_vrf_id=0,
961            decap_vrf_id=0,
962            protocol=3,
963            vni=0):
964        """
965
966        :param local:
967        :param remote:
968        :param is_add:  (Default value = 1)
969        :param is_ipv6:  (Default value = 0)
970        :param encap_vrf_id:  (Default value = 0)
971        :param decap_vrf_id:  (Default value = 0)
972        :param mcast_sw_if_index:  (Default value = 0xFFFFFFFF)
973        :param protocol:  (Default value = 3)
974        :param vni:  (Default value = 0)
975
976        """
977        return self.api(self.papi.vxlan_gpe_add_del_tunnel,
978                        {'is_add': is_add,
979                         'is_ipv6': is_ipv6,
980                         'local': src_addr,
981                         'remote': dst_addr,
982                         'mcast_sw_if_index': mcast_sw_if_index,
983                         'encap_vrf_id': encap_vrf_id,
984                         'decap_vrf_id': decap_vrf_id,
985                         'protocol': protocol,
986                         'vni': vni})
987
988    def vxlan_gbp_tunnel_dump(self, sw_if_index=0xffffffff):
989        return self.api(self.papi.vxlan_gbp_tunnel_dump,
990                        {'sw_if_index': sw_if_index})
991
992    def pppoe_add_del_session(
993            self,
994            client_ip,
995            client_mac,
996            session_id=0,
997            is_add=1,
998            decap_vrf_id=0):
999        """
1000
1001        :param is_add:  (Default value = 1)
1002        :param is_ipv6:  (Default value = 0)
1003        :param client_ip:
1004        :param session_id:  (Default value = 0)
1005        :param client_mac:
1006        :param decap_vrf_id:  (Default value = 0)
1007
1008        """
1009        return self.api(self.papi.pppoe_add_del_session,
1010                        {'is_add': is_add,
1011                         'session_id': session_id,
1012                         'client_ip': client_ip,
1013                         'decap_vrf_id': decap_vrf_id,
1014                         'client_mac': client_mac})
1015
1016    def sr_mpls_policy_add(self, bsid, weight, type, segments):
1017        return self.api(self.papi.sr_mpls_policy_add,
1018                        {'bsid': bsid,
1019                         'weight': weight,
1020                         'type': type,
1021                         'n_segments': len(segments),
1022                         'segments': segments})
1023
1024    def sr_mpls_policy_del(self, bsid):
1025        return self.api(self.papi.sr_mpls_policy_del,
1026                        {'bsid': bsid})
1027
1028    def sr_localsid_add_del(self,
1029                            localsid,
1030                            behavior,
1031                            nh_addr4,
1032                            nh_addr6,
1033                            is_del=0,
1034                            end_psp=0,
1035                            sw_if_index=0xFFFFFFFF,
1036                            vlan_index=0,
1037                            fib_table=0,
1038                            ):
1039        """ Add/del IPv6 SR local-SID.
1040
1041        :param localsid:
1042        :param behavior: END=1; END.X=2; END.DX2=4; END.DX6=5;
1043        :param behavior: END.DX4=6; END.DT6=7; END.DT4=8
1044        :param nh_addr4:
1045        :param nh_addr6:
1046        :param is_del:  (Default value = 0)
1047        :param end_psp: (Default value = 0)
1048        :param sw_if_index: (Default value = 0xFFFFFFFF)
1049        :param vlan_index:  (Default value = 0)
1050        :param fib_table:   (Default value = 0)
1051        """
1052        return self.api(
1053            self.papi.sr_localsid_add_del,
1054            {'is_del': is_del,
1055             'localsid': localsid,
1056             'end_psp': end_psp,
1057             'behavior': behavior,
1058             'sw_if_index': sw_if_index,
1059             'vlan_index': vlan_index,
1060             'fib_table': fib_table,
1061             'nh_addr4': nh_addr4,
1062             'nh_addr6': nh_addr6
1063             }
1064        )
1065
1066    def sr_policy_add(
1067            self,
1068            bsid_addr,
1069            weight=1,
1070            is_encap=1,
1071            type=0,
1072            fib_table=0,
1073            n_segments=0,
1074            segments=[]):
1075        """
1076        :param bsid_addr: bindingSID of the SR Policy
1077        :param weight: weight of the sid list. optional. (default: 1)
1078        :param is_encap: (bool) whether SR policy should Encap or SRH insert \
1079            (default: Encap)
1080        :param type: type/behavior of the SR policy. (default or spray) \
1081            (default: default)
1082        :param fib_table: VRF where to install the FIB entry for the BSID \
1083            (default: 0)
1084        :param n_segments: number of segments \
1085            (default: 0)
1086        :param segments: a vector of IPv6 address composing the segment list \
1087            (default: [])
1088        """
1089        return self.api(
1090            self.papi.sr_policy_add,
1091            {'bsid_addr': bsid_addr,
1092             'weight': weight,
1093             'is_encap': is_encap,
1094             'type': type,
1095             'fib_table': fib_table,
1096             'n_segments': n_segments,
1097             'segments': segments
1098             }
1099        )
1100
1101    def sr_policy_del(
1102            self,
1103            bsid_addr,
1104            sr_policy_index=0):
1105        """
1106        :param bsid: bindingSID of the SR Policy
1107        :param sr_policy_index: index of the sr policy (default: 0)
1108        """
1109        return self.api(
1110            self.papi.sr_policy_del,
1111            {'bsid_addr': bsid_addr,
1112             'sr_policy_index': sr_policy_index
1113             })
1114
1115    def sr_steering_add_del(
1116            self,
1117            is_del,
1118            bsid_addr,
1119            sr_policy_index,
1120            table_id,
1121            prefix_addr,
1122            mask_width,
1123            sw_if_index,
1124            traffic_type):
1125        """
1126        Steer traffic L2 and L3 traffic through a given SR policy
1127
1128        :param is_del: delete or add
1129        :param bsid_addr: bindingSID of the SR Policy (alt to sr_policy_index)
1130        :param sr_policy: is the index of the SR Policy (alt to bsid)
1131        :param table_id: is the VRF where to install the FIB entry for the BSID
1132        :param prefix_addr: is the IPv4/v6 address for L3 traffic type
1133        :param mask_width: is the mask for L3 traffic type
1134        :param sw_if_index: is the incoming interface for L2 traffic
1135        :param traffic_type: type of traffic (IPv4: 4, IPv6: 6, L2: 2)
1136        """
1137        return self.api(
1138            self.papi.sr_steering_add_del,
1139            {'is_del': is_del,
1140             'bsid_addr': bsid_addr,
1141             'sr_policy_index': sr_policy_index,
1142             'table_id': table_id,
1143             'prefix_addr': prefix_addr,
1144             'mask_width': mask_width,
1145             'sw_if_index': sw_if_index,
1146             'traffic_type': traffic_type
1147             })
1148
1149    def acl_add_replace(self, acl_index, r, tag='',
1150                        expected_retval=0):
1151        """Add/replace an ACL
1152        :param int acl_index: ACL index to replace, 2^32-1 to create new ACL.
1153        :param acl_rule r: ACL rules array.
1154        :param str tag: symbolic tag (description) for this ACL.
1155        :param int count: number of rules.
1156        """
1157        return self.api(self.papi.acl_add_replace,
1158                        {'acl_index': acl_index,
1159                         'r': r,
1160                         'count': len(r),
1161                         'tag': tag},
1162                        expected_retval=expected_retval)
1163
1164    def acl_del(self, acl_index, expected_retval=0):
1165        """
1166
1167        :param acl_index:
1168        :return:
1169        """
1170        return self.api(self.papi.acl_del,
1171                        {'acl_index': acl_index},
1172                        expected_retval=expected_retval)
1173
1174    def acl_interface_set_acl_list(self, sw_if_index, n_input, acls,
1175                                   expected_retval=0):
1176        return self.api(self.papi.acl_interface_set_acl_list,
1177                        {'sw_if_index': sw_if_index,
1178                         'count': len(acls),
1179                         'n_input': n_input,
1180                         'acls': acls},
1181                        expected_retval=expected_retval)
1182
1183    def acl_interface_set_etype_whitelist(self, sw_if_index,
1184                                          n_input, whitelist,
1185                                          expected_retval=0):
1186        return self.api(self.papi.acl_interface_set_etype_whitelist,
1187                        {'sw_if_index': sw_if_index,
1188                         'count': len(whitelist),
1189                         'n_input': n_input,
1190                         'whitelist': whitelist},
1191                        expected_retval=expected_retval)
1192
1193    def acl_interface_add_del(self,
1194                              sw_if_index,
1195                              acl_index,
1196                              is_add=1):
1197        """ Add/Delete ACL to/from interface
1198
1199        :param sw_if_index:
1200        :param acl_index:
1201        :param is_add:  (Default value = 1)
1202        """
1203
1204        return self.api(self.papi.acl_interface_add_del,
1205                        {'is_add': is_add,
1206                         'is_input': 1,
1207                         'sw_if_index': sw_if_index,
1208                         'acl_index': acl_index})
1209
1210    def acl_dump(self, acl_index, expected_retval=0):
1211        return self.api(self.papi.acl_dump,
1212                        {'acl_index': acl_index},
1213                        expected_retval=expected_retval)
1214
1215    def acl_interface_list_dump(self, sw_if_index=0xFFFFFFFF,
1216                                expected_retval=0):
1217        return self.api(self.papi.acl_interface_list_dump,
1218                        {'sw_if_index': sw_if_index},
1219                        expected_retval=expected_retval)
1220
1221    def macip_acl_add(self, rules, tag=""):
1222        """ Add MACIP acl
1223
1224        :param rules: list of rules for given acl
1225        :param tag: acl tag
1226        """
1227
1228        return self.api(self.papi.macip_acl_add,
1229                        {'r': rules,
1230                         'count': len(rules),
1231                         'tag': tag})
1232
1233    def macip_acl_add_replace(self, rules, acl_index=0xFFFFFFFF, tag=""):
1234        """ Add MACIP acl
1235
1236        :param rules: list of rules for given acl
1237        :param tag: acl tag
1238        """
1239
1240        return self.api(self.papi.macip_acl_add_replace,
1241                        {'acl_index': acl_index,
1242                         'r': rules,
1243                         'count': len(rules),
1244                         'tag': tag})
1245
1246    def macip_acl_interface_add_del(self,
1247                                    sw_if_index,
1248                                    acl_index,
1249                                    is_add=1):
1250        """ Add MACIP acl to interface
1251
1252        :param sw_if_index:
1253        :param acl_index:
1254        :param is_add:  (Default value = 1)
1255        """
1256
1257        return self.api(self.papi.macip_acl_interface_add_del,
1258                        {'is_add': is_add,
1259                         'sw_if_index': sw_if_index,
1260                         'acl_index': acl_index})
1261
1262    def macip_acl_dump(self, acl_index=4294967295):
1263        """ Return MACIP acl dump
1264        """
1265
1266        return self.api(
1267            self.papi.macip_acl_dump, {'acl_index': acl_index})
1268
1269    def policer_add_del(self,
1270                        name,
1271                        cir,
1272                        eir,
1273                        cb,
1274                        eb,
1275                        is_add=1,
1276                        rate_type=0,
1277                        round_type=0,
1278                        ptype=0,
1279                        color_aware=0,
1280                        conform_action_type=1,
1281                        conform_dscp=0,
1282                        exceed_action_type=0,
1283                        exceed_dscp=0,
1284                        violate_action_type=0,
1285                        violate_dscp=0):
1286        return self.api(self.papi.policer_add_del,
1287                        {'name': name,
1288                         'cir': cir,
1289                         'eir': eir,
1290                         'cb': cb,
1291                         'eb': eb,
1292                         'is_add': is_add,
1293                         'rate_type': rate_type,
1294                         'round_type': round_type,
1295                         'type': ptype,
1296                         'color_aware': color_aware,
1297                         'conform_action_type': conform_action_type,
1298                         'conform_dscp': conform_dscp,
1299                         'exceed_action_type': exceed_action_type,
1300                         'exceed_dscp': exceed_dscp,
1301                         'violate_action_type': violate_action_type,
1302                         'violate_dscp': violate_dscp})
1303
1304    def ip_punt_police(self,
1305                       policer_index,
1306                       is_ip6=0,
1307                       is_add=1):
1308        return self.api(self.papi.ip_punt_police,
1309                        {'policer_index': policer_index,
1310                         'is_add': is_add,
1311                         'is_ip6': is_ip6})
1312
1313    def ip_punt_redirect(self,
1314                         rx_sw_if_index,
1315                         tx_sw_if_index,
1316                         address,
1317                         is_add=1):
1318        return self.api(self.papi.ip_punt_redirect,
1319                        {'punt': {'rx_sw_if_index': rx_sw_if_index,
1320                                  'tx_sw_if_index': tx_sw_if_index,
1321                                  'nh': address},
1322                         'is_add': is_add})
1323
1324    def ip_punt_redirect_dump(self, sw_if_index, is_ipv6=0):
1325        return self.api(self.papi.ip_punt_redirect_dump,
1326                        {'sw_if_index': sw_if_index,
1327                         'is_ipv6': is_ipv6})
1328
1329    def bier_table_add_del(self,
1330                           bti,
1331                           mpls_label,
1332                           is_add=1):
1333        """ BIER Table add/del """
1334        return self.api(
1335            self.papi.bier_table_add_del,
1336            {'bt_tbl_id': {"bt_set": bti.set_id,
1337                           "bt_sub_domain": bti.sub_domain_id,
1338                           "bt_hdr_len_id": bti.hdr_len_id},
1339             'bt_label': mpls_label,
1340             'bt_is_add': is_add})
1341
1342    def bier_table_dump(self):
1343        return self.api(self.papi.bier_table_dump, {})
1344
1345    def bier_route_add_del(self,
1346                           bti,
1347                           bp,
1348                           paths,
1349                           is_add=1,
1350                           is_replace=0):
1351        """ BIER Route add/del """
1352        return self.api(
1353            self.papi.bier_route_add_del,
1354            {
1355                'br_route': {
1356                    'br_tbl_id': {"bt_set": bti.set_id,
1357                                  "bt_sub_domain": bti.sub_domain_id,
1358                                  "bt_hdr_len_id": bti.hdr_len_id},
1359                    'br_bp': bp,
1360                    'br_n_paths': len(paths),
1361                    'br_paths': paths,
1362                },
1363                'br_is_add': is_add,
1364                'br_is_replace': is_replace
1365            })
1366
1367    def bier_route_dump(self, bti):
1368        return self.api(
1369            self.papi.bier_route_dump,
1370            {'br_tbl_id': {"bt_set": bti.set_id,
1371                           "bt_sub_domain": bti.sub_domain_id,
1372                           "bt_hdr_len_id": bti.hdr_len_id}})
1373
1374    def bier_imp_add(self,
1375                     bti,
1376                     src,
1377                     ibytes,
1378                     is_add=1):
1379        """ BIER Imposition Add """
1380        return self.api(
1381            self.papi.bier_imp_add,
1382            {'bi_tbl_id': {"bt_set": bti.set_id,
1383                           "bt_sub_domain": bti.sub_domain_id,
1384                           "bt_hdr_len_id": bti.hdr_len_id},
1385             'bi_src': src,
1386             'bi_n_bytes': len(ibytes),
1387             'bi_bytes': ibytes})
1388
1389    def bier_imp_del(self, bi_index):
1390        """ BIER Imposition del """
1391        return self.api(
1392            self.papi.bier_imp_del,
1393            {'bi_index': bi_index})
1394
1395    def bier_imp_dump(self):
1396        return self.api(self.papi.bier_imp_dump, {})
1397
1398    def bier_disp_table_add_del(self,
1399                                bdti,
1400                                is_add=1):
1401        """ BIER Disposition Table add/del """
1402        return self.api(
1403            self.papi.bier_disp_table_add_del,
1404            {'bdt_tbl_id': bdti,
1405             'bdt_is_add': is_add})
1406
1407    def bier_disp_table_dump(self):
1408        return self.api(self.papi.bier_disp_table_dump, {})
1409
1410    def bier_disp_entry_add_del(self,
1411                                bdti,
1412                                bp,
1413                                payload_proto,
1414                                next_hop_afi,
1415                                next_hop,
1416                                next_hop_tbl_id=0,
1417                                next_hop_rpf_id=~0,
1418                                next_hop_is_ip4=1,
1419                                is_add=1):
1420        """ BIER Route add/del """
1421        lstack = []
1422        while (len(lstack) < 16):
1423            lstack.append({})
1424        return self.api(
1425            self.papi.bier_disp_entry_add_del,
1426            {'bde_tbl_id': bdti,
1427             'bde_bp': bp,
1428             'bde_payload_proto': payload_proto,
1429             'bde_n_paths': 1,
1430             'bde_paths': [{'table_id': next_hop_tbl_id,
1431                            'rpf_id': next_hop_rpf_id,
1432                            'n_labels': 0,
1433                            'label_stack': lstack}],
1434             'bde_is_add': is_add})
1435
1436    def bier_disp_entry_dump(self, bdti):
1437        return self.api(
1438            self.papi.bier_disp_entry_dump,
1439            {'bde_tbl_id': bdti})
1440
1441    def session_enable_disable(self, is_enabled):
1442        return self.api(
1443            self.papi.session_enable_disable,
1444            {'is_enable': is_enabled})
1445
1446    def ipsec_spd_add_del(self, spd_id, is_add=1):
1447        """ SPD add/del - Wrapper to add or del ipsec SPD
1448        Sample CLI : 'ipsec spd add 1'
1449
1450        :param spd_id - SPD ID to be created in the vpp . mandatory
1451        :param is_add - create (1) or delete(0) SPD (Default 1 - add) .
1452              optional
1453        :returns: reply from the API
1454        """
1455        return self.api(
1456            self.papi.ipsec_spd_add_del, {
1457                'spd_id': spd_id, 'is_add': is_add})
1458
1459    def ipsec_spds_dump(self):
1460        return self.api(self.papi.ipsec_spds_dump, {})
1461
1462    def ipsec_interface_add_del_spd(self, spd_id, sw_if_index, is_add=1):
1463        """ IPSEC interface SPD add/del - \
1464             Wrapper to associate/disassociate SPD to interface in VPP
1465        Sample CLI : 'set interface ipsec spd GigabitEthernet0/6/0 1'
1466
1467        :param spd_id - SPD ID to associate with the interface . mandatory
1468        :param sw_if_index - Interface Index which needs to ipsec \
1469            association mandatory
1470        :param is_add - add(1) or del(0) association with interface \
1471                (Default 1 - add) . optional
1472        :returns: reply from the API
1473        """
1474        return self.api(
1475            self.papi.ipsec_interface_add_del_spd,
1476            {'spd_id': spd_id, 'sw_if_index': sw_if_index, 'is_add': is_add})
1477
1478    def ipsec_spd_interface_dump(self, spd_index=None):
1479        return self.api(self.papi.ipsec_spd_interface_dump,
1480                        {'spd_index': spd_index if spd_index else 0,
1481                         'spd_index_valid': 1 if spd_index else 0})
1482
1483    def ipsec_sad_entry_add_del(self,
1484                                sad_id,
1485                                spi,
1486                                integrity_algorithm,
1487                                integrity_key,
1488                                crypto_algorithm,
1489                                crypto_key,
1490                                protocol,
1491                                tunnel_src_address='',
1492                                tunnel_dst_address='',
1493                                flags=0,
1494                                salt=0,
1495                                is_add=1):
1496        """ IPSEC SA add/del
1497        :param sad_id: security association ID
1498        :param spi: security param index of the SA in decimal
1499        :param integrity_algorithm:
1500        :param integrity_key:
1501        :param crypto_algorithm:
1502        :param crypto_key:
1503        :param protocol: AH(0) or ESP(1) protocol
1504        :param tunnel_src_address: tunnel mode outer src address
1505        :param tunnel_dst_address: tunnel mode outer dst address
1506        :param is_add:
1507        :param is_tunnel:
1508        :** reference /vpp/src/vnet/ipsec/ipsec.h file for enum values of
1509             crypto and ipsec algorithms
1510        """
1511        return self.api(
1512            self.papi.ipsec_sad_entry_add_del,
1513            {
1514                'is_add': is_add,
1515                'entry':
1516                    {
1517                        'sad_id': sad_id,
1518                        'spi': spi,
1519                        'tunnel_src': tunnel_src_address,
1520                        'tunnel_dst': tunnel_dst_address,
1521                        'protocol': protocol,
1522                        'integrity_algorithm': integrity_algorithm,
1523                        'integrity_key': {
1524                            'length': len(integrity_key),
1525                            'data': integrity_key,
1526                        },
1527                        'crypto_algorithm': crypto_algorithm,
1528                        'crypto_key': {
1529                            'length': len(crypto_key),
1530                            'data': crypto_key,
1531                        },
1532                        'flags': flags,
1533                        'salt': salt,
1534                    }
1535            })
1536
1537    def ipsec_sa_dump(self, sa_id=None):
1538        return self.api(self.papi.ipsec_sa_dump,
1539                        {'sa_id': sa_id if sa_id else 0xffffffff})
1540
1541    def ipsec_spd_entry_add_del(self,
1542                                spd_id,
1543                                sa_id,
1544                                local_address_start,
1545                                local_address_stop,
1546                                remote_address_start,
1547                                remote_address_stop,
1548                                local_port_start=0,
1549                                local_port_stop=65535,
1550                                remote_port_start=0,
1551                                remote_port_stop=65535,
1552                                protocol=0,
1553                                policy=0,
1554                                priority=100,
1555                                is_outbound=1,
1556                                is_add=1,
1557                                is_ipv6=0,
1558                                is_ip_any=0):
1559        """ IPSEC policy SPD add/del   -
1560                    Wrapper to configure ipsec SPD policy entries in VPP
1561        :param spd_id: SPD ID for the policy
1562        :param local_address_start: local-ip-range start address
1563        :param local_address_stop : local-ip-range stop address
1564        :param remote_address_start: remote-ip-range start address
1565        :param remote_address_stop : remote-ip-range stop address
1566        :param local_port_start: (Default value = 0)
1567        :param local_port_stop: (Default value = 65535)
1568        :param remote_port_start: (Default value = 0)
1569        :param remote_port_stop: (Default value = 65535)
1570        :param protocol: Any(0), AH(51) & ESP(50) protocol (Default value = 0)
1571        :param sa_id: Security Association ID for mapping it to SPD
1572        :param policy: bypass(0), discard(1), resolve(2) or protect(3) action
1573               (Default value = 0)
1574        :param priority: value for the spd action (Default value = 100)
1575        :param is_outbound: flag for inbound(0) or outbound(1)
1576               (Default value = 1)
1577        :param is_add: (Default value = 1)
1578        """
1579        return self.api(
1580            self.papi.ipsec_spd_entry_add_del,
1581            {
1582                'is_add': is_add,
1583                'entry':
1584                    {
1585                        'spd_id': spd_id,
1586                        'sa_id': sa_id,
1587                        'local_address_start': local_address_start,
1588                        'local_address_stop': local_address_stop,
1589                        'remote_address_start': remote_address_start,
1590                        'remote_address_stop': remote_address_stop,
1591                        'local_port_start': local_port_start,
1592                        'local_port_stop': local_port_stop,
1593                        'remote_port_start': remote_port_start,
1594                        'remote_port_stop': remote_port_stop,
1595                        'protocol': protocol,
1596                        'policy': policy,
1597                        'priority': priority,
1598                        'is_outbound': is_outbound,
1599                    }
1600            })
1601
1602    def ipsec_spd_dump(self, spd_id, sa_id=0xffffffff):
1603        return self.api(self.papi.ipsec_spd_dump,
1604                        {'spd_id': spd_id,
1605                         'sa_id': sa_id})
1606
1607    def ipsec_tunnel_if_add_del(self, local_ip, remote_ip, local_spi,
1608                                remote_spi, crypto_alg, local_crypto_key,
1609                                remote_crypto_key, integ_alg, local_integ_key,
1610                                remote_integ_key, is_add=1, esn=0, salt=0,
1611                                anti_replay=1, renumber=0,
1612                                udp_encap=0, show_instance=0xffffffff):
1613        return self.api(
1614            self.papi.ipsec_tunnel_if_add_del,
1615            {
1616                'local_ip': local_ip,
1617                'remote_ip': remote_ip,
1618                'local_spi': local_spi,
1619                'remote_spi': remote_spi,
1620                'crypto_alg': crypto_alg,
1621                'local_crypto_key_len': len(local_crypto_key),
1622                'local_crypto_key': local_crypto_key,
1623                'remote_crypto_key_len': len(remote_crypto_key),
1624                'remote_crypto_key': remote_crypto_key,
1625                'integ_alg': integ_alg,
1626                'local_integ_key_len': len(local_integ_key),
1627                'local_integ_key': local_integ_key,
1628                'remote_integ_key_len': len(remote_integ_key),
1629                'remote_integ_key': remote_integ_key,
1630                'is_add': is_add,
1631                'esn': esn,
1632                'anti_replay': anti_replay,
1633                'renumber': renumber,
1634                'show_instance': show_instance,
1635                'udp_encap': udp_encap,
1636                'salt': salt
1637            })
1638
1639    def ipsec_select_backend(self, protocol, index):
1640        return self.api(self.papi.ipsec_select_backend,
1641                        {'protocol': protocol, 'index': index})
1642
1643    def ipsec_backend_dump(self):
1644        return self.api(self.papi.ipsec_backend_dump, {})
1645
1646    def app_namespace_add_del(self,
1647                              namespace_id,
1648                              ip4_fib_id=0,
1649                              ip6_fib_id=0,
1650                              sw_if_index=0xFFFFFFFF,
1651                              secret=0):
1652        return self.api(
1653            self.papi.app_namespace_add_del,
1654            {'secret': secret,
1655             'sw_if_index': sw_if_index,
1656             'ip4_fib_id': ip4_fib_id,
1657             'ip6_fib_id': ip6_fib_id,
1658             'namespace_id': namespace_id,
1659             'namespace_id_len': len(namespace_id)})
1660
1661    def punt_socket_register(self, reg, pathname,
1662                             header_version=1):
1663        """ Register punt socket """
1664        return self.api(self.papi.punt_socket_register,
1665                        {'header_version': header_version,
1666                         'punt': reg,
1667                         'pathname': pathname})
1668
1669    def punt_socket_deregister(self, reg):
1670        """ Unregister punt socket """
1671        return self.api(self.papi.punt_socket_deregister,
1672                        {'punt': reg})
1673
1674    def gbp_endpoint_add(self, sw_if_index, ips, mac, sclass, flags,
1675                         tun_src, tun_dst):
1676        """ GBP endpoint Add """
1677        return self.api(self.papi.gbp_endpoint_add,
1678                        {'endpoint': {
1679                            'sw_if_index': sw_if_index,
1680                            'ips': ips,
1681                            'n_ips': len(ips),
1682                            'mac': mac,
1683                            'sclass': sclass,
1684                            'flags': flags,
1685                            'tun': {
1686                                'src': tun_src,
1687                                'dst': tun_dst,
1688                            }}})
1689
1690    def gbp_endpoint_del(self, handle):
1691        """ GBP endpoint Del """
1692        return self.api(self.papi.gbp_endpoint_del,
1693                        {'handle': handle})
1694
1695    def gbp_endpoint_dump(self):
1696        """ GBP endpoint Dump """
1697        return self.api(self.papi.gbp_endpoint_dump, {})
1698
1699    def gbp_endpoint_group_add(self, vnid, sclass, bd,
1700                               rd, uplink_sw_if_index,
1701                               retention):
1702        """ GBP endpoint group Add """
1703        return self.api(self.papi.gbp_endpoint_group_add,
1704                        {'epg':
1705                            {
1706                                'uplink_sw_if_index': uplink_sw_if_index,
1707                                'bd_id': bd,
1708                                'rd_id': rd,
1709                                'vnid': vnid,
1710                                'sclass': sclass,
1711                                'retention': retention
1712                            }})
1713
1714    def gbp_endpoint_group_del(self, sclass):
1715        """ GBP endpoint group Del """
1716        return self.api(self.papi.gbp_endpoint_group_del,
1717                        {'sclass': sclass})
1718
1719    def gbp_bridge_domain_add(self, bd_id, rd_id, flags,
1720                              bvi_sw_if_index,
1721                              uu_fwd_sw_if_index,
1722                              bm_flood_sw_if_index):
1723        """ GBP bridge-domain Add """
1724        return self.api(self.papi.gbp_bridge_domain_add,
1725                        {'bd':
1726                            {
1727                                'flags': flags,
1728                                'bvi_sw_if_index': bvi_sw_if_index,
1729                                'uu_fwd_sw_if_index': uu_fwd_sw_if_index,
1730                                'bm_flood_sw_if_index': bm_flood_sw_if_index,
1731                                'bd_id': bd_id,
1732                                'rd_id': rd_id
1733                            }})
1734
1735    def gbp_bridge_domain_del(self, bd_id):
1736        """ GBP bridge-domain Del """
1737        return self.api(self.papi.gbp_bridge_domain_del,
1738                        {'bd_id': bd_id})
1739
1740    def gbp_route_domain_add(self, rd_id,
1741                             scope,
1742                             ip4_table_id,
1743                             ip6_table_id,
1744                             ip4_uu_sw_if_index,
1745                             ip6_uu_sw_if_index):
1746        """ GBP route-domain Add """
1747        return self.api(self.papi.gbp_route_domain_add,
1748                        {'rd':
1749                            {
1750                                'scope': scope,
1751                                'ip4_table_id': ip4_table_id,
1752                                'ip6_table_id': ip6_table_id,
1753                                'ip4_uu_sw_if_index': ip4_uu_sw_if_index,
1754                                'ip6_uu_sw_if_index': ip6_uu_sw_if_index,
1755                                'rd_id': rd_id
1756                            }})
1757
1758    def gbp_route_domain_del(self, rd_id):
1759        """ GBP route-domain Del """
1760        return self.api(self.papi.gbp_route_domain_del,
1761                        {'rd_id': rd_id})
1762
1763    def gbp_recirc_add_del(self, is_add, sw_if_index, sclass, is_ext):
1764        """ GBP recirc Add/Del """
1765        return self.api(self.papi.gbp_recirc_add_del,
1766                        {'is_add': is_add,
1767                         'recirc': {
1768                             'is_ext': is_ext,
1769                             'sw_if_index': sw_if_index,
1770                             'sclass': sclass}})
1771
1772    def gbp_recirc_dump(self):
1773        """ GBP recirc Dump """
1774        return self.api(self.papi.gbp_recirc_dump, {})
1775
1776    def gbp_ext_itf_add_del(self, is_add, sw_if_index, bd_id, rd_id, flags):
1777        """ GBP recirc Add/Del """
1778        return self.api(self.papi.gbp_ext_itf_add_del,
1779                        {'is_add': is_add,
1780                         'ext_itf': {
1781                             'sw_if_index': sw_if_index,
1782                             'bd_id': bd_id,
1783                             'rd_id': rd_id,
1784                             'flags': flags}})
1785
1786    def gbp_ext_itf_dump(self):
1787        """ GBP recirc Dump """
1788        return self.api(self.papi.gbp_ext_itf_dump, {})
1789
1790    def gbp_subnet_add_del(self, is_add, rd_id,
1791                           prefix, type,
1792                           sw_if_index=0xffffffff,
1793                           sclass=0xffff):
1794        """ GBP Subnet Add/Del """
1795        return self.api(self.papi.gbp_subnet_add_del,
1796                        {'is_add': is_add,
1797                         'subnet': {
1798                             'type': type,
1799                             'sw_if_index': sw_if_index,
1800                             'sclass': sclass,
1801                             'prefix': prefix,
1802                             'rd_id': rd_id}})
1803
1804    def gbp_subnet_dump(self):
1805        """ GBP Subnet Dump """
1806        return self.api(self.papi.gbp_subnet_dump, {})
1807
1808    def gbp_contract_dump(self):
1809        """ GBP contract Dump """
1810        return self.api(self.papi.gbp_contract_dump, {})
1811
1812    def gbp_vxlan_tunnel_add(self, vni, bd_rd_id, mode, src):
1813        """ GBP VXLAN tunnel add """
1814        return self.api(self.papi.gbp_vxlan_tunnel_add,
1815                        {
1816                            'tunnel': {
1817                                'vni': vni,
1818                                'mode': mode,
1819                                'bd_rd_id': bd_rd_id,
1820                                'src': src
1821                            }
1822                        })
1823
1824    def gbp_vxlan_tunnel_del(self, vni):
1825        """ GBP VXLAN tunnel del """
1826        return self.api(self.papi.gbp_vxlan_tunnel_del,
1827                        {
1828                            'vni': vni,
1829                        })
1830
1831    def gbp_vxlan_tunnel_dump(self):
1832        """ GBP VXLAN tunnel add/del """
1833        return self.api(self.papi.gbp_vxlan_tunnel_dump, {})
1834
1835    def igmp_enable_disable(self, sw_if_index, enable, host):
1836        """ Enable/disable IGMP on a given interface """
1837        return self.api(self.papi.igmp_enable_disable,
1838                        {'enable': enable,
1839                         'mode': host,
1840                         'sw_if_index': sw_if_index})
1841
1842    def igmp_proxy_device_add_del(self, vrf_id, sw_if_index, add):
1843        """ Add/del IGMP proxy device """
1844        return self.api(self.papi.igmp_proxy_device_add_del,
1845                        {'vrf_id': vrf_id, 'sw_if_index': sw_if_index,
1846                         'add': add})
1847
1848    def igmp_proxy_device_add_del_interface(self, vrf_id, sw_if_index, add):
1849        """ Add/del interface to/from IGMP proxy device """
1850        return self.api(self.papi.igmp_proxy_device_add_del_interface,
1851                        {'vrf_id': vrf_id, 'sw_if_index': sw_if_index,
1852                         'add': add})
1853
1854    def igmp_listen(self, filter, sw_if_index, saddrs, gaddr):
1855        """ Listen for new (S,G) on specified interface
1856
1857        :param enable: add/del
1858        :param sw_if_index: interface sw index
1859        :param saddr: source ip4 addr
1860        :param gaddr: group ip4 addr
1861        """
1862        return self.api(self.papi.igmp_listen,
1863                        {
1864                            'group':
1865                                {
1866                                    'filter': filter,
1867                                    'sw_if_index': sw_if_index,
1868                                    'n_srcs': len(saddrs),
1869                                    'saddrs': saddrs,
1870                                    'gaddr': gaddr
1871                                }
1872                        })
1873
1874    def igmp_clear_interface(self, sw_if_index):
1875        """ Remove all (S,G)s from specified interface
1876            doesn't send IGMP report!
1877        """
1878        return self.api(
1879            self.papi.igmp_clear_interface, {
1880                'sw_if_index': sw_if_index})
1881
1882    def want_igmp_events(self, enable=1):
1883        return self.api(self.papi.want_igmp_events, {'enable': enable,
1884                                                     'pid': os.getpid()})
1885
1886    def bond_create(
1887            self,
1888            mode,
1889            lb,
1890            numa_only,
1891            use_custom_mac,
1892            mac_address='',
1893            interface_id=0xFFFFFFFF):
1894        """
1895        :param mode: mode
1896        :param lb: load balance
1897        :param numa_only: tx on local numa node for lacp mode
1898        :param use_custom_mac: use custom mac
1899        :param mac_address: mac address
1900        :param interface_id: custom interface ID
1901        """
1902        return self.api(
1903            self.papi.bond_create,
1904            {'mode': mode,
1905             'lb': lb,
1906             'numa_only': numa_only,
1907             'use_custom_mac': use_custom_mac,
1908             'mac_address': mac_address,
1909             'id': interface_id
1910             })
1911
1912    def pipe_delete(self, parent_sw_if_index):
1913        return self.api(self.papi.pipe_delete,
1914                        {'parent_sw_if_index': parent_sw_if_index})
1915
1916    def svs_table_add_del(self, af, table_id, is_add=1):
1917        return self.api(self.papi.svs_table_add_del,
1918                        {
1919                            'table_id': table_id,
1920                            'is_add': is_add,
1921                            'af': af,
1922                        })
1923
1924    def svs_route_add_del(self, table_id, prefix, src_table_id, is_add=1):
1925        return self.api(self.papi.svs_route_add_del,
1926                        {
1927                            'table_id': table_id,
1928                            'source_table_id': src_table_id,
1929                            'prefix': prefix,
1930                            'is_add': is_add,
1931                        })
1932
1933    def svs_enable_disable(self, af, table_id, sw_if_index, is_enable=1):
1934        return self.api(self.papi.svs_enable_disable,
1935                        {
1936                            'af': af,
1937                            'table_id': table_id,
1938                            'sw_if_index': sw_if_index,
1939                            'is_enable': is_enable,
1940                        })
1941
1942    def feature_gso_enable_disable(self, sw_if_index, enable_disable=1):
1943        return self.api(self.papi.feature_gso_enable_disable,
1944                        {
1945                            'sw_if_index': sw_if_index,
1946                            'enable_disable': enable_disable,
1947                        })
1948