# NB NB NB NB NB NB NB NB NB NB NB
#
# NOTE: The API binary wrappers in this file are in the process of being
# deprecated. DO NOT ADD NEW WRAPPERS HERE. Call the functions using
# named arguments directly instead.
#

import os
import time
import queue
from six import moves, iteritems
from config import config
from vpp_papi import VPPApiClient
from hook import Hook
from vpp_papi_exceptions import (
    CliFailedCommandError,
    CliSyntaxError,
    UnexpectedApiReturnValueError,
)

#
# Dictionary keyed on message name to override default values for
# named parameters
#
defaultmapping = {
    "acl_interface_add_del": {"is_add": 1, "is_input": 1},
    "bd_ip_mac_add_del": {
        "is_add": 1,
    },
    "bfd_udp_add": {"is_authenticated": False, "bfd_key_id": None, "conf_key_id": None},
    "bfd_udp_auth_activate": {
        "bfd_key_id": None,
        "conf_key_id": None,
        "is_delayed": False,
    },
    "bier_disp_entry_add_del": {
        "next_hop_rpf_id": -1,
        "next_hop_is_ip4": 1,
        "is_add": 1,
    },
    "bier_disp_table_add_del": {
        "is_add": 1,
    },
    "bier_imp_add": {
        "is_add": 1,
    },
    "bier_route_add_del": {
        "is_add": 1,
    },
    "bier_table_add_del": {
        "is_add": 1,
    },
    "bvi_delete": {},
    "geneve_add_del_tunnel": {
        "mcast_sw_if_index": 4294967295,
        "is_add": 1,
        "decap_next_index": 4294967295,
    },
    "input_acl_set_interface": {
        "ip4_table_index": 4294967295,
        "ip6_table_index": 4294967295,
        "l2_table_index": 4294967295,
    },
    "ip6_add_del_address_using_prefix": {
        "is_add": 1,
    },
    "ip6nd_send_router_solicitation": {
        "irt": 1,
        "mrt": 120,
    },
    "ip_add_del_route": {
        "next_hop_sw_if_index": 4294967295,
        "next_hop_weight": 1,
        "next_hop_via_label": 1048576,
        "classify_table_index": 4294967295,
        "is_add": 1,
    },
    "ip_mroute_add_del": {
        "is_add": 1,
    },
    "ip_neighbor_add_del": {
        "is_add": 1,
    },
    "ipsec_interface_add_del_spd": {
        "is_add": 1,
    },
    "ipsec_spd_add_del": {
        "is_add": 1,
    },
    "ipsec_spd_dump": {
        "sa_id": 4294967295,
    },
    "ipsec_spd_entry_add_del": {
        "local_port_stop": 65535,
        "remote_port_stop": 65535,
        "priority": 100,
        "is_outbound": 1,
        "is_add": 1,
    },
    "ipsec_tunnel_if_add_del": {
        "is_add": 1,
        "anti_replay": 1,
    },
    "l2_emulation": {
        "enable": 1,
    },
    "l2fib_add_del": {
        "is_add": 1,
    },
    "lisp_add_del_adjacency": {
        "is_add": 1,
    },
    "lisp_add_del_local_eid": {
        "is_add": 1,
    },
    "lisp_add_del_locator": {
        "priority": 1,
        "weight": 1,
        "is_add": 1,
    },
    "lisp_add_del_locator_set": {
        "is_add": 1,
    },
    "lisp_add_del_remote_mapping": {
        "is_add": 1,
    },
    "macip_acl_interface_add_del": {
        "is_add": 1,
    },
    "mpls_ip_bind_unbind": {
        "is_ip4": 1,
        "is_bind": 1,
    },
    "mpls_route_add_del": {
        "mr_next_hop_sw_if_index": 4294967295,
        "mr_next_hop_weight": 1,
        "mr_next_hop_via_label": 1048576,
        "mr_is_add": 1,
        "mr_classify_table_index": 4294967295,
    },
    "mpls_table_add_del": {
        "is_add": 1,
    },
    "mpls_tunnel_add_del": {
        "next_hop_sw_if_index": 4294967295,
        "next_hop_weight": 1,
        "next_hop_via_label": 1048576,
        "is_add": 1,
    },
    "output_acl_set_interface": {
        "ip4_table_index": 4294967295,
        "ip6_table_index": 4294967295,
        "l2_table_index": 4294967295,
    },
    "pppoe_add_del_session": {
        "is_add": 1,
    },
    "policer_add_del": {
        "is_add": 1,
        "conform_action": {"type": 1},
    },
    "set_ipfix_exporter": {
        "collector_port": 4739,
    },
    "sr_policy_add": {
        "weight": 1,
        "is_encap": 1,
    },
    "sw_interface_add_del_address": {
        "is_add": 1,
    },
    "sw_interface_ip6nd_ra_prefix": {
        "val_lifetime": 4294967295,
        "pref_lifetime": 4294967295,
    },
    "sw_interface_set_ip_directed_broadcast": {
        "enable": 1,
    },
    "sw_interface_set_l2_bridge": {
        "enable": 1,
    },
    "sw_interface_set_mpls_enable": {
        "enable": 1,
    },
    "sw_interface_set_mtu": {
        "mtu": [0, 0, 0, 0],
    },
    "sw_interface_set_unnumbered": {
        "is_add": 1,
    },
    "sw_interface_span_enable_disable": {
        "state": 1,
    },
    "vxlan_add_del_tunnel": {
        "mcast_sw_if_index": 4294967295,
        "is_add": 1,
        "decap_next_index": 4294967295,
        "instance": 4294967295,
    },
    "want_bfd_events": {
        "enable_disable": 1,
    },
    "want_igmp_events": {
        "enable": 1,
    },
    "want_interface_events": {
        "enable_disable": 1,
    },
    "want_l2_macs_events": {
        "enable_disable": 1,
        "pid": os.getpid(),
    },
    "want_l2_macs_events2": {
        "enable_disable": 1,
        "pid": os.getpid(),
    },
}


def as_fn_signature(d):
    return ", ".join(f"{k}={v}" for k, v in d.items())


class VppPapiProvider(object):
    """VPP-api provider using vpp-papi

    @property hook: hook object providing before and after api/cli hooks
    """

    _zero, _negative = range(2)

    def __init__(self, name, test_class, read_timeout):
        self.hook = Hook(test_class)
        self.name = name
        self.test_class = test_class
        self._expect_api_retval = self._zero
        self._expect_stack = []

        self.vpp = VPPApiClient(
            apidir=config.extern_apidir + [config.vpp_install_dir],
            logger=test_class.logger,
            read_timeout=read_timeout,
            use_socket=True,
            server_address=test_class.get_api_sock_path(),
        )
        self._events = queue.Queue()

    def __enter__(self):
        return self

    def assert_negative_api_retval(self):
        """Expect API failure - used with with, e.g.::

            with self.vapi.assert_negative_api_retval():
                self.vapi.<api call expected to fail>

        ..
        """
        self._expect_stack.append(self._expect_api_retval)
        self._expect_api_retval = self._negative
        return self

    def assert_zero_api_retval(self):
        """Expect API success - used with with, e.g.::

            with self.vapi.assert_negative_api_retval():
                self.vapi.<api call expected to succeed>

        :note: this is useful only inside another with block
             as success is the default expected value
        """
        self._expect_stack.append(self._expect_api_retval)
        self._expect_api_retval = self._zero
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self._expect_api_retval = self._expect_stack.pop()

    def register_hook(self, hook):
        """Replace hook registration with new hook

        :param hook:

        """
        self.hook = hook

    def collect_events(self):
        """Collect all events from the internal queue and clear the queue."""
        result = []
        while True:
            try:
                e = self._events.get(block=False)
                result.append(e)
            except queue.Empty:
                return result
        return result

    def wait_for_event(self, timeout, name=None):
        """Wait for and return next event."""
        if name:
            self.test_class.logger.debug(
                "Expecting event '%s' within %ss", name, timeout
            )
        else:
            self.test_class.logger.debug("Expecting event within %ss", timeout)
        try:
            e = self._events.get(timeout=timeout)
        except queue.Empty:
            raise Exception("Event did not occur within timeout")
        msgname = type(e).__name__
        if name and msgname != name:
            raise Exception("Unexpected event received: %s, expected: %s" % msgname)
        self.test_class.logger.debug("Returning event %s:%s" % (name, e))
        return e

    def __call__(self, name, event):
        """Enqueue event in the internal event queue."""
        self.test_class.logger.debug("New event: %s: %s" % (name, event))
        self._events.put(event)

    def factory(self, name, apifn):
        def f(*a, **ka):
            fields = apifn._func.msg.fields

            # add positional and kw arguments
            d = ka
            for i, o in enumerate(fields[3:]):
                try:
                    d[o] = a[i]
                except BaseException:
                    break

            # Default override
            if name in defaultmapping:
                for k, v in iteritems(defaultmapping[name]):
                    if k in d:
                        continue
                    d[k] = v
            return self.api(apifn, d)

        return f

    def __getattribute__(self, name):
        try:
            method = super(VppPapiProvider, self).__getattribute__(name)
        except AttributeError:
            method = self.factory(name, getattr(self.papi, name))
            # lazily load the method so we don't need to call factory
            # again for this name.
            setattr(self, name, method)
        return method

    def connect(self):
        """Connect the API to VPP"""
        # This might be called before VPP is prepared to listen to the socket
        retries = 0
        while not os.path.exists(self.test_class.get_api_sock_path()):
            time.sleep(0.5)
            retries += 1
            if retries > 120:
                break
        self.vpp.connect(self.name[:63])
        self.papi = self.vpp.api
        self.vpp.register_event_callback(self)

    def disconnect(self):
        """Disconnect the API from VPP"""
        self.vpp.disconnect()

    def api(self, api_fn, api_args, expected_retval=0):
        """Call API function and check it's return value.
        Call the appropriate hooks before and after the API call

        :param api_fn: API function to call
        :param api_args: tuple of API function arguments
        :param expected_retval: Expected return value (Default value = 0)
        :returns: reply from the API

        """
        self.hook.before_api(api_fn.__name__, api_args)
        reply = api_fn(**api_args)
        if self._expect_api_retval == self._negative:
            if hasattr(reply, "retval") and reply.retval >= 0:
                msg = (
                    "%s(%s) passed unexpectedly: expected negative "
                    "return value instead of %d in %s"
                    % (
                        api_fn.__name__,
                        as_fn_signature(api_args),
                        reply.retval,
                        moves.reprlib.repr(reply),
                    )
                )
                self.test_class.logger.info(msg)
                raise UnexpectedApiReturnValueError(reply.retval, msg)
        elif self._expect_api_retval == self._zero:
            if hasattr(reply, "retval") and reply.retval != expected_retval:
                msg = (
                    "%s(%s) failed, expected %d return value instead "
                    "of %d in %s"
                    % (
                        api_fn.__name__,
                        as_fn_signature(api_args),
                        expected_retval,
                        reply.retval,
                        repr(reply),
                    )
                )
                self.test_class.logger.info(msg)
                raise UnexpectedApiReturnValueError(reply.retval, msg)
        else:
            raise Exception(
                "Internal error, unexpected value for "
                "self._expect_api_retval %s" % self._expect_api_retval
            )
        self.hook.after_api(api_fn.__name__, api_args)
        return reply

    def cli_return_response(self, cli):
        """Execute a CLI, calling the before/after hooks appropriately.
        Return the reply without examining it

        :param cli: CLI to execute
        :returns: response object

        """
        self.hook.before_cli(cli)
        cli += "\n"
        r = self.papi.cli_inband(cmd=cli)
        self.hook.after_cli(cli)
        return r

    def cli(self, cli):
        """Execute a CLI, calling the before/after hooks appropriately.

        :param cli: CLI to execute
        :returns: CLI output

        """
        r = self.cli_return_response(cli)
        if r.retval == -156:
            raise CliSyntaxError(r.reply)
        if r.retval != 0:
            raise CliFailedCommandError(r.reply)
        if hasattr(r, "reply"):
            return r.reply

    def ppcli(self, cli):
        """Helper method to print CLI command in case of info logging level.

        :param cli: CLI to execute
        :returns: CLI output
        """
        return cli + "\n" + self.cli(cli)

    def ip6nd_send_router_solicitation(self, sw_if_index, irt=1, mrt=120, mrc=0, mrd=0):
        return self.api(
            self.papi.ip6nd_send_router_solicitation,
            {
                "irt": irt,
                "mrt": mrt,
                "mrc": mrc,
                "mrd": mrd,
                "sw_if_index": sw_if_index,
            },
        )

    def want_interface_events(self, enable_disable=1):
        return self.api(
            self.papi.want_interface_events,
            {
                "enable_disable": enable_disable,
                "pid": os.getpid(),
            },
        )

    def sw_interface_set_mac_address(self, sw_if_index, mac):
        return self.api(
            self.papi.sw_interface_set_mac_address,
            {"sw_if_index": sw_if_index, "mac_address": mac},
        )

    def p2p_ethernet_add(self, sw_if_index, remote_mac, subif_id):
        """Create p2p ethernet subinterface

        :param sw_if_index: main (parent) interface
        :param remote_mac: client (remote) mac address

        """
        return self.api(
            self.papi.p2p_ethernet_add,
            {
                "parent_if_index": sw_if_index,
                "remote_mac": remote_mac,
                "subif_id": subif_id,
            },
        )

    def p2p_ethernet_del(self, sw_if_index, remote_mac):
        """Delete p2p ethernet subinterface

        :param sw_if_index: main (parent) interface
        :param remote_mac: client (remote) mac address

        """
        return self.api(
            self.papi.p2p_ethernet_del,
            {"parent_if_index": sw_if_index, "remote_mac": remote_mac},
        )

    def create_vlan_subif(self, sw_if_index, vlan):
        """

        :param vlan:
        :param sw_if_index:

        """
        return self.api(
            self.papi.create_vlan_subif, {"sw_if_index": sw_if_index, "vlan_id": vlan}
        )

    def create_loopback(self, mac=""):
        """

        :param mac: (Optional)
        """
        return self.api(self.papi.create_loopback, {"mac_address": mac})

    def ip_route_dump(self, table_id, is_ip6=False):
        return self.api(
            self.papi.ip_route_dump, {"table": {"table_id": table_id, "is_ip6": is_ip6}}
        )

    def ip_route_v2_dump(self, table_id, is_ip6=False, src=0):
        return self.api(
            self.papi.ip_route_v2_dump,
            {"src": src, "table": {"table_id": table_id, "is_ip6": is_ip6}},
        )

    def ip_neighbor_add_del(
        self, sw_if_index, mac_address, ip_address, is_add=1, flags=0
    ):
        """Add neighbor MAC to IPv4 or IPv6 address.

        :param sw_if_index:
        :param mac_address:
        :param dst_address:
        :param is_add:  (Default value = 1)
        :param flags:  (Default value = 0/NONE)
        """
        return self.api(
            self.papi.ip_neighbor_add_del,
            {
                "is_add": is_add,
                "neighbor": {
                    "sw_if_index": sw_if_index,
                    "flags": flags,
                    "mac_address": mac_address,
                    "ip_address": ip_address,
                },
            },
        )

    def udp_encap_add(self, src_ip, dst_ip, src_port, dst_port, table_id=0):
        """Add a GRE tunnel
        :param src_ip:
        :param dst_ip:
        :param src_port:
        :param dst_port:
        :param outer_fib_id:  (Default value = 0)
        """

        return self.api(
            self.papi.udp_encap_add,
            {
                "udp_encap": {
                    "src_ip": src_ip,
                    "dst_ip": dst_ip,
                    "src_port": src_port,
                    "dst_port": dst_port,
                    "table_id": table_id,
                }
            },
        )

    def udp_encap_del(self, id):
        return self.api(self.papi.udp_encap_del, {"id": id})

    def udp_encap_dump(self):
        return self.api(self.papi.udp_encap_dump, {})

    def want_udp_encap_stats(self, enable=1):
        return self.api(
            self.papi.want_udp_encap_stats, {"enable": enable, "pid": os.getpid()}
        )

    def mpls_route_dump(self, table_id):
        return self.api(self.papi.mpls_route_dump, {"table": {"mt_table_id": table_id}})

    def mpls_table_dump(self):
        return self.api(self.papi.mpls_table_dump, {})

    def mpls_table_add_del(self, table_id, is_add=1):
        """

        :param table_id
        :param is_add:  (Default value = 1)

        """

        return self.api(
            self.papi.mpls_table_add_del,
            {
                "mt_table": {
                    "mt_table_id": table_id,
                },
                "mt_is_add": is_add,
            },
        )

    def mpls_route_add_del(
        self, table_id, label, eos, eos_proto, is_multicast, paths, is_add, is_multipath
    ):
        """MPLS Route add/del"""
        return self.api(
            self.papi.mpls_route_add_del,
            {
                "mr_route": {
                    "mr_table_id": table_id,
                    "mr_label": label,
                    "mr_eos": eos,
                    "mr_eos_proto": eos_proto,
                    "mr_is_multicast": is_multicast,
                    "mr_n_paths": len(paths),
                    "mr_paths": paths,
                },
                "mr_is_add": is_add,
                "mr_is_multipath": is_multipath,
            },
        )

    def mpls_ip_bind_unbind(self, label, prefix, table_id=0, ip_table_id=0, is_bind=1):
        """ """
        return self.api(
            self.papi.mpls_ip_bind_unbind,
            {
                "mb_mpls_table_id": table_id,
                "mb_label": label,
                "mb_ip_table_id": ip_table_id,
                "mb_is_bind": is_bind,
                "mb_prefix": prefix,
            },
        )

    def mpls_tunnel_add_del(
        self, tun_sw_if_index, paths, is_add=1, l2_only=0, is_multicast=0
    ):
        """ """
        return self.api(
            self.papi.mpls_tunnel_add_del,
            {
                "mt_is_add": is_add,
                "mt_tunnel": {
                    "mt_sw_if_index": tun_sw_if_index,
                    "mt_l2_only": l2_only,
                    "mt_is_multicast": is_multicast,
                    "mt_n_paths": len(paths),
                    "mt_paths": paths,
                },
            },
        )

    def input_acl_set_interface(
        self,
        is_add,
        sw_if_index,
        ip4_table_index=0xFFFFFFFF,
        ip6_table_index=0xFFFFFFFF,
        l2_table_index=0xFFFFFFFF,
    ):
        """
        :param is_add:
        :param sw_if_index:
        :param ip4_table_index:  (Default value = 0xFFFFFFFF)
        :param ip6_table_index:  (Default value = 0xFFFFFFFF)
        :param l2_table_index:  (Default value = 0xFFFFFFFF)
        """

        return self.api(
            self.papi.input_acl_set_interface,
            {
                "sw_if_index": sw_if_index,
                "ip4_table_index": ip4_table_index,
                "ip6_table_index": ip6_table_index,
                "l2_table_index": l2_table_index,
                "is_add": is_add,
            },
        )

    def output_acl_set_interface(
        self,
        is_add,
        sw_if_index,
        ip4_table_index=0xFFFFFFFF,
        ip6_table_index=0xFFFFFFFF,
        l2_table_index=0xFFFFFFFF,
    ):
        """
        :param is_add:
        :param sw_if_index:
        :param ip4_table_index:  (Default value = 0xFFFFFFFF)
        :param ip6_table_index:  (Default value = 0xFFFFFFFF)
        :param l2_table_index:  (Default value = 0xFFFFFFFF)
        """

        return self.api(
            self.papi.output_acl_set_interface,
            {
                "sw_if_index": sw_if_index,
                "ip4_table_index": ip4_table_index,
                "ip6_table_index": ip6_table_index,
                "l2_table_index": l2_table_index,
                "is_add": is_add,
            },
        )

    def set_ipfix_exporter(
        self,
        collector_address,
        src_address,
        path_mtu,
        template_interval,
        vrf_id=0,
        collector_port=4739,
        udp_checksum=0,
    ):
        return self.api(
            self.papi.set_ipfix_exporter,
            {
                "collector_address": collector_address,
                "collector_port": collector_port,
                "src_address": src_address,
                "vrf_id": vrf_id,
                "path_mtu": path_mtu,
                "template_interval": template_interval,
                "udp_checksum": udp_checksum,
            },
        )

    def mfib_signal_dump(self):
        return self.api(self.papi.mfib_signal_dump, {})

    def ip_mroute_dump(self, table_id, is_ip6=False):
        return self.api(
            self.papi.ip_mroute_dump,
            {"table": {"table_id": table_id, "is_ip6": is_ip6}},
        )

    def pppoe_add_del_session(
        self, client_ip, client_mac, session_id=0, is_add=1, decap_vrf_id=0
    ):
        """

        :param is_add:  (Default value = 1)
        :param is_ipv6:  (Default value = 0)
        :param client_ip:
        :param session_id:  (Default value = 0)
        :param client_mac:
        :param decap_vrf_id:  (Default value = 0)

        """
        return self.api(
            self.papi.pppoe_add_del_session,
            {
                "is_add": is_add,
                "session_id": session_id,
                "client_ip": client_ip,
                "decap_vrf_id": decap_vrf_id,
                "client_mac": client_mac,
            },
        )

    def sr_mpls_policy_add(self, bsid, weight, type, segments):
        return self.api(
            self.papi.sr_mpls_policy_add,
            {
                "bsid": bsid,
                "weight": weight,
                "is_spray": type,
                "n_segments": len(segments),
                "segments": segments,
            },
        )

    def sr_mpls_policy_del(self, bsid):
        return self.api(self.papi.sr_mpls_policy_del, {"bsid": bsid})

    def bier_table_add_del(self, bti, mpls_label, is_add=1):
        """BIER Table add/del"""
        return self.api(
            self.papi.bier_table_add_del,
            {
                "bt_tbl_id": {
                    "bt_set": bti.set_id,
                    "bt_sub_domain": bti.sub_domain_id,
                    "bt_hdr_len_id": bti.hdr_len_id,
                },
                "bt_label": mpls_label,
                "bt_is_add": is_add,
            },
        )

    def bier_table_dump(self):
        return self.api(self.papi.bier_table_dump, {})

    def bier_route_add_del(self, bti, bp, paths, is_add=1, is_replace=0):
        """BIER Route add/del"""
        return self.api(
            self.papi.bier_route_add_del,
            {
                "br_route": {
                    "br_tbl_id": {
                        "bt_set": bti.set_id,
                        "bt_sub_domain": bti.sub_domain_id,
                        "bt_hdr_len_id": bti.hdr_len_id,
                    },
                    "br_bp": bp,
                    "br_n_paths": len(paths),
                    "br_paths": paths,
                },
                "br_is_add": is_add,
                "br_is_replace": is_replace,
            },
        )

    def bier_route_dump(self, bti):
        return self.api(
            self.papi.bier_route_dump,
            {
                "br_tbl_id": {
                    "bt_set": bti.set_id,
                    "bt_sub_domain": bti.sub_domain_id,
                    "bt_hdr_len_id": bti.hdr_len_id,
                }
            },
        )

    def bier_imp_add(self, bti, src, ibytes, is_add=1):
        """BIER Imposition Add"""
        return self.api(
            self.papi.bier_imp_add,
            {
                "bi_tbl_id": {
                    "bt_set": bti.set_id,
                    "bt_sub_domain": bti.sub_domain_id,
                    "bt_hdr_len_id": bti.hdr_len_id,
                },
                "bi_src": src,
                "bi_n_bytes": len(ibytes),
                "bi_bytes": ibytes,
            },
        )

    def bier_imp_del(self, bi_index):
        """BIER Imposition del"""
        return self.api(self.papi.bier_imp_del, {"bi_index": bi_index})

    def bier_imp_dump(self):
        return self.api(self.papi.bier_imp_dump, {})

    def bier_disp_table_add_del(self, bdti, is_add=1):
        """BIER Disposition Table add/del"""
        return self.api(
            self.papi.bier_disp_table_add_del,
            {"bdt_tbl_id": bdti, "bdt_is_add": is_add},
        )

    def bier_disp_table_dump(self):
        return self.api(self.papi.bier_disp_table_dump, {})

    def bier_disp_entry_add_del(
        self,
        bdti,
        bp,
        payload_proto,
        next_hop_afi,
        next_hop,
        next_hop_tbl_id=0,
        next_hop_rpf_id=~0,
        next_hop_is_ip4=1,
        is_add=1,
    ):
        """BIER Route add/del"""
        lstack = []
        while len(lstack) < 16:
            lstack.append({})
        return self.api(
            self.papi.bier_disp_entry_add_del,
            {
                "bde_tbl_id": bdti,
                "bde_bp": bp,
                "bde_payload_proto": payload_proto,
                "bde_n_paths": 1,
                "bde_paths": [
                    {
                        "table_id": next_hop_tbl_id,
                        "rpf_id": next_hop_rpf_id,
                        "n_labels": 0,
                        "label_stack": lstack,
                    }
                ],
                "bde_is_add": is_add,
            },
        )

    def bier_disp_entry_dump(self, bdti):
        return self.api(self.papi.bier_disp_entry_dump, {"bde_tbl_id": bdti})

    def ipsec_spd_add_del(self, spd_id, is_add=1):
        """SPD add/del - Wrapper to add or del ipsec SPD
        Sample CLI : 'ipsec spd add 1'

        :param spd_id - SPD ID to be created in the vpp . mandatory
        :param is_add - create (1) or delete(0) SPD (Default 1 - add) .
        optional
        :returns: reply from the API
        """
        return self.api(
            self.papi.ipsec_spd_add_del, {"spd_id": spd_id, "is_add": is_add}
        )

    def ipsec_spds_dump(self):
        return self.api(self.papi.ipsec_spds_dump, {})

    def ipsec_interface_add_del_spd(self, spd_id, sw_if_index, is_add=1):
        """ IPSEC interface SPD add/del - \
             Wrapper to associate/disassociate SPD to interface in VPP
        Sample CLI : 'set interface ipsec spd GigabitEthernet0/6/0 1'

        :param spd_id - SPD ID to associate with the interface . mandatory
        :param sw_if_index - Interface Index which needs to ipsec \
            association mandatory
        :param is_add - add(1) or del(0) association with interface \
                (Default 1 - add) . optional
        :returns: reply from the API
        """
        return self.api(
            self.papi.ipsec_interface_add_del_spd,
            {"spd_id": spd_id, "sw_if_index": sw_if_index, "is_add": is_add},
        )

    def ipsec_spd_interface_dump(self, spd_index=None):
        return self.api(
            self.papi.ipsec_spd_interface_dump,
            {
                "spd_index": spd_index if spd_index else 0,
                "spd_index_valid": 1 if spd_index else 0,
            },
        )

    def ipsec_spd_entry_add_del(
        self,
        spd_id,
        sa_id,
        local_address_start,
        local_address_stop,
        remote_address_start,
        remote_address_stop,
        local_port_start=0,
        local_port_stop=65535,
        remote_port_start=0,
        remote_port_stop=65535,
        protocol=socket.IPPROTO_RAW,
        policy=0,
        priority=100,
        is_outbound=1,
        is_add=1,
        is_ipv6=0,
        is_ip_any=0,
    ):
        """IPSEC policy SPD add/del   -
                    Wrapper to configure ipsec SPD policy entries in VPP

        :param spd_id: SPD ID for the policy
        :param local_address_start: local-ip-range start address
        :param local_address_stop: local-ip-range stop address
        :param remote_address_start: remote-ip-range start address
        :param remote_address_stop: remote-ip-range stop address
        :param local_port_start: (Default value = 0)
        :param local_port_stop: (Default value = 65535)
        :param remote_port_start: (Default value = 0)
        :param remote_port_stop: (Default value = 65535)
        :param protocol: Any(0), AH(51) & ESP(50) protocol (Default value = 0)
        :param sa_id: Security Association ID for mapping it to SPD
        :param policy: bypass(0), discard(1), resolve(2) or protect(3) action
            (Default value = 0)
        :param priority: value for the spd action (Default value = 100)
        :param is_outbound: flag for inbound(0) or outbound(1)
            (Default value = 1)
        :param is_add: (Default value = 1)
        """
        return self.api(
            self.papi.ipsec_spd_entry_add_del_v2,
            {
                "is_add": is_add,
                "entry": {
                    "spd_id": spd_id,
                    "sa_id": sa_id,
                    "local_address_start": local_address_start,
                    "local_address_stop": local_address_stop,
                    "remote_address_start": remote_address_start,
                    "remote_address_stop": remote_address_stop,
                    "local_port_start": local_port_start,
                    "local_port_stop": local_port_stop,
                    "remote_port_start": remote_port_start,
                    "remote_port_stop": remote_port_stop,
                    "protocol": protocol,
                    "policy": policy,
                    "priority": priority,
                    "is_outbound": is_outbound,
                },
            },
        )

    def ipsec_spd_dump(self, spd_id, sa_id=0xFFFFFFFF):
        return self.api(self.papi.ipsec_spd_dump, {"spd_id": spd_id, "sa_id": sa_id})

    def ipsec_tunnel_if_add_del(
        self,
        local_ip,
        remote_ip,
        local_spi,
        remote_spi,
        crypto_alg,
        local_crypto_key,
        remote_crypto_key,
        integ_alg,
        local_integ_key,
        remote_integ_key,
        is_add=1,
        esn=0,
        salt=0,
        anti_replay=1,
        renumber=0,
        udp_encap=0,
        show_instance=0xFFFFFFFF,
    ):
        return self.api(
            self.papi.ipsec_tunnel_if_add_del,
            {
                "local_ip": local_ip,
                "remote_ip": remote_ip,
                "local_spi": local_spi,
                "remote_spi": remote_spi,
                "crypto_alg": crypto_alg,
                "local_crypto_key_len": len(local_crypto_key),
                "local_crypto_key": local_crypto_key,
                "remote_crypto_key_len": len(remote_crypto_key),
                "remote_crypto_key": remote_crypto_key,
                "integ_alg": integ_alg,
                "local_integ_key_len": len(local_integ_key),
                "local_integ_key": local_integ_key,
                "remote_integ_key_len": len(remote_integ_key),
                "remote_integ_key": remote_integ_key,
                "is_add": is_add,
                "esn": esn,
                "anti_replay": anti_replay,
                "renumber": renumber,
                "show_instance": show_instance,
                "udp_encap": udp_encap,
                "salt": salt,
            },
        )

    def ipsec_select_backend(self, protocol, index):
        return self.api(
            self.papi.ipsec_select_backend, {"protocol": protocol, "index": index}
        )

    def ipsec_backend_dump(self):
        return self.api(self.papi.ipsec_backend_dump, {})

    def punt_socket_register(self, reg, pathname, header_version=1):
        """Register punt socket"""
        return self.api(
            self.papi.punt_socket_register,
            {"header_version": header_version, "punt": reg, "pathname": pathname},
        )

    def punt_socket_deregister(self, reg):
        """Unregister punt socket"""
        return self.api(self.papi.punt_socket_deregister, {"punt": reg})

    def igmp_enable_disable(self, sw_if_index, enable, host):
        """Enable/disable IGMP on a given interface"""
        return self.api(
            self.papi.igmp_enable_disable,
            {"enable": enable, "mode": host, "sw_if_index": sw_if_index},
        )

    def igmp_proxy_device_add_del(self, vrf_id, sw_if_index, add):
        """Add/del IGMP proxy device"""
        return self.api(
            self.papi.igmp_proxy_device_add_del,
            {"vrf_id": vrf_id, "sw_if_index": sw_if_index, "add": add},
        )

    def igmp_proxy_device_add_del_interface(self, vrf_id, sw_if_index, add):
        """Add/del interface to/from IGMP proxy device"""
        return self.api(
            self.papi.igmp_proxy_device_add_del_interface,
            {"vrf_id": vrf_id, "sw_if_index": sw_if_index, "add": add},
        )

    def igmp_listen(self, filter, sw_if_index, saddrs, gaddr):
        """Listen for new (S,G) on specified interface

        :param enable: add/delas
        :param sw_if_index: interface sw index
        :param saddr: source ip4 addr
        :param gaddr: group ip4 addr
        """
        return self.api(
            self.papi.igmp_listen,
            {
                "group": {
                    "filter": filter,
                    "sw_if_index": sw_if_index,
                    "n_srcs": len(saddrs),
                    "saddrs": saddrs,
                    "gaddr": gaddr,
                }
            },
        )

    def igmp_clear_interface(self, sw_if_index):
        """Remove all (S,G)s from specified interface
        doesn't send IGMP report!
        """
        return self.api(self.papi.igmp_clear_interface, {"sw_if_index": sw_if_index})

    def want_igmp_events(self, enable=1):
        return self.api(
            self.papi.want_igmp_events, {"enable": enable, "pid": os.getpid()}
        )
