#!/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 TestL2LearnLimitEnable(VppTestCase):
    """L2 Global Learn limit Test Case"""

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

    @classmethod
    def tearDownClass(cls):
        super(TestL2LearnLimitEnable, 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_learnlimit01(self):
        """L2BD test with learn Limit"""
        self.vapi.want_l2_macs_events(enable_disable=1, learn_limit=10)
        hosts = self.create_hosts(self.pg_interfaces[0], 20, 1)
        fhosts = self.create_hosts(self.pg_interfaces[1], 1, 2)

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

        # inject 1 mac address on bd2
        self.learn_hosts(self.pg_interfaces[1], 2, fhosts)

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

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

        # check that bd2 was not able to learn
        self.assertEqual(len(lfs2), 0)

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

        self.vapi.bridge_domain_add_del_v2(
            bd_id=1, is_add=1, flood=1, forward=1, uu_flood=1, learn=1
        )
        self.vapi.bridge_domain_add_del_v2(
            bd_id=2, is_add=1, flood=1, forward=1, uu_flood=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(TestL2LearnLimitEnable, 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)
