#!/usr/bin/env python3

import unittest
import random

from scapy.packet import Raw
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, UDP

from framework import VppTestCase, VppTestRunner
from util import Host, ppp


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)
