#!/usr/bin/env python3

import unittest

from scapy.layers.l2 import Ether

from framework import VppTestCase
from asfframework import VppTestRunner
from util import Host


class TestL2LearnLimitBdEnable(VppTestCase):
    """L2 Bridge Domain Learn limit Test Case"""

    @classmethod
    def setUpClass(self):
        super(TestL2LearnLimitBdEnable, self).setUpClass()
        self.create_pg_interfaces(range(3))

    @classmethod
    def tearDownClass(cls):
        super(TestL2LearnLimitBdEnable, cls).tearDownClass()

    def create_hosts(self, pg_if, n_hosts_per_if, subnet):
        """
        Create required number of host MAC addresses and distribute them among
        interfaces. Create host IPv4 address for every host MAC address.

        :param int n_hosts_per_if: Number of per interface hosts to
            create MAC/IPv4 addresses for.
        """

        hosts = dict()
        swif = pg_if.sw_if_index

        def mac(j):
            return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j)

        def ip(j):
            return "172.%02u.1%02x.%u" % (subnet, swif, j)

        def h(j):
            return Host(mac(j), ip(j))

        hosts[swif] = [h(j) for j in range(n_hosts_per_if)]

        return hosts

    def learn_hosts(self, pg_if, bd_id, hosts):
        """
        Create and send per interface L2 MAC broadcast packet stream to
        let the bridge domain learn these MAC addresses.

        :param int bd_id: BD to teach
        :param dict hosts: dict of hosts per interface
        """

        self.vapi.bridge_flags(bd_id=bd_id, is_set=1, flags=1)

        swif = pg_if.sw_if_index
        packets = [Ether(dst="ff:ff:ff:ff:ff:ff", src=host.mac) for host in hosts[swif]]
        pg_if.add_stream(packets)
        self.logger.info("Sending broadcast eth frames for MAC learning")
        self.pg_start()

    def test_l2bd_learnlimit(self):
        """L2BD test with bridge domain limit"""
        self.vapi.want_l2_macs_events(enable_disable=1, learn_limit=1000)
        self.vapi.bridge_domain_set_default_learn_limit(4)
        self.vapi.bridge_domain_add_del_v2(
            bd_id=3, is_add=1, flood=1, uu_flood=1, forward=1, learn=1
        )
        self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[2].sw_if_index, bd_id=3)

        self.vapi.bridge_domain_set_learn_limit(2, 5)

        hosts = self.create_hosts(self.pg_interfaces[1], 20, 2)
        fhosts = self.create_hosts(self.pg_interfaces[2], 20, 3)

        # inject 20 mac addresses on bd2
        self.learn_hosts(self.pg_interfaces[1], 2, hosts)

        # inject 20 macs address on bd3
        self.learn_hosts(self.pg_interfaces[2], 3, fhosts)

        lfs1 = self.vapi.l2_fib_table_dump(2)
        lfs2 = self.vapi.l2_fib_table_dump(3)

        # check that only 5 macs are learned.
        self.assertEqual(len(lfs1), 5)

        # check that only 4 macs are learned.
        self.assertEqual(len(lfs2), 4)

        self.vapi.sw_interface_set_l2_bridge(
            rx_sw_if_index=self.pg_interfaces[2].sw_if_index, bd_id=3, enable=0
        )
        self.vapi.bridge_domain_add_del_v2(is_add=0, bd_id=3)

    def setUp(self):
        super(TestL2LearnLimitBdEnable, self).setUp()

        self.vapi.bridge_domain_add_del_v2(
            bd_id=1, is_add=1, flood=1, uu_flood=1, forward=1, learn=1
        )
        self.vapi.bridge_domain_add_del_v2(
            bd_id=2, is_add=1, flood=1, uu_flood=1, forward=1, learn=1
        )

        self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[0].sw_if_index, bd_id=1)
        self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[1].sw_if_index, bd_id=2)

    def tearDown(self):
        super(TestL2LearnLimitBdEnable, self).tearDown()
        self.vapi.sw_interface_set_l2_bridge(
            rx_sw_if_index=self.pg_interfaces[0].sw_if_index, bd_id=1, enable=0
        )
        self.vapi.sw_interface_set_l2_bridge(
            rx_sw_if_index=self.pg_interfaces[1].sw_if_index, bd_id=2, enable=0
        )
        self.vapi.bridge_domain_add_del_v2(bd_id=1, is_add=0)
        self.vapi.bridge_domain_add_del_v2(bd_id=2, is_add=0)


if __name__ == "__main__":
    unittest.main(testRunner=VppTestRunner)
