# SPDX-FileCopyrightText: Copyright 2021-present Open Networking Foundation.
# SPDX-License-Identifier: GPL-2.0-or-later

class INT:

    def __init__(self):
        self.default = ""

    def CASE1 (self, main):
        """
        Send ping packets from one host to another host and check flows from DeepInsight.
        """
        import time
        import socket
        from core import utilities
        from tests.USECASE.SegmentRouting.INT.dependencies.IntTest import IntTest
        main.cfgName = "CASE1"

        main.step("Setting up the test")
        intTest = IntTest(scapy=True)
        intTest.setUpTest(main)

        main.step("Setting up hosts and variables")
        srcIfaceName = main.h1.interfaces[0]["name"]
        dstIfaceName = main.h2.interfaces[0]["name"]
        srcMac = main.h1.getMac(srcIfaceName)
        dstMac = main.h2.getMac(dstIfaceName)
        srcIp = main.h1.getIp(srcIfaceName)
        dstIp = main.h2.getIp(dstIfaceName)
        srcPort = 2000
        dstPort = 8888

        main.step("Send ping packets from h1 to h2")
        startTimeMs = (time.time() - 5) * 1000
        pkt = """(
            Ether(src="{}", dst="{}") /
            IP(src="{}", dst="{}") /
            UDP(sport={}, dport={}) /
            ("A"*30)
        )""".format(srcMac, dstMac, srcIp, dstIp, srcPort, dstPort)
        main.h1.sendPacket(iface=srcIfaceName, packet=pkt)
        endTimeMs = (time.time() + 5) * 1000

        main.step("Checking total number of flow reports from DeepInsight")
        def getFiveTupleCount(*args, **kwargs):
            flows = main.DeepInsight.getFlows(
                startTimeMs=startTimeMs,
                endTimeMs=endTimeMs,
                srcIp=srcIp,
                dstIp=dstIp,
                ipProto=socket.IPPROTO_UDP
            )
            if "FiveTupleCount" in flows:
                return flows["FiveTupleCount"]
            else:
                return 0
        # Need to wait few seconds until DeepInsight database updated.
        fiveTupleCount = utilities.retry(
            f=getFiveTupleCount,
            retValue=0,
            attempts=60,
        )

        utilities.assert_equals(
            expect=1, actual=fiveTupleCount,
            onpass="Got 1 flow report from DeepInsight as expected.",
            onfail="Got %d flow reports from DeepInsight (expect 1)" % (fiveTupleCount)
        )

        main.step("Clean up the test")
        intTest.cleanUp(main)

    def CASE2 (self, main):
        """
        Send a packet with invalid VLAN from one host to another host and check
        if DeepInsight receives drop reports.
        """
        import time
        import socket
        from core import utilities
        from tests.USECASE.SegmentRouting.INT.dependencies.IntTest import IntTest
        main.cfgName = "CASE2"

        main.step("Setting up the test")
        intTest = IntTest(scapy=True)
        intTest.setUpTest(main)

        main.step("Setting up hosts and variables")
        srcIfaceName = main.h1.interfaces[0]["name"]
        dstIfaceName = main.h2.interfaces[0]["name"]
        srcMac = main.h1.getMac(srcIfaceName)
        dstMac = main.h2.getMac(dstIfaceName)
        srcIp = main.h1.getIp(srcIfaceName)
        dstIp = main.h2.getIp(dstIfaceName)
        srcPort = 2000
        dstPort = 8888

        main.step("Sending a packet with invalid VLAN ID from h1")
        startTimeMs = (time.time() - 5) * 1000
        pkt = """(
            Ether(src="{}", dst="{}") /
            Dot1Q(vlan=4093) /
            IP(src="{}", dst="{}") /
            UDP(sport={}, dport={}) /
            ("A"*30)
        )""".format(srcMac, dstMac, srcIp, dstIp, srcPort, dstPort)
        main.h1.sendPacket(iface=srcIfaceName, packet=pkt)
        endTimeMs = (time.time() + 5) * 1000

        main.step("Checking drop report from DeepInsight")
        def getDropAnomalies(*args, **kwargs):
            return main.DeepInsight.getAnomalyRecords(
                startTime=startTimeMs,
                endTime=endTimeMs,
                srcIp=srcIp,
                dstIp=dstIp,
                srcPort=srcPort,
                dstPort=dstPort,
                ipProto=socket.IPPROTO_UDP,
                anomalyType="packet_drop",
           )

        # Need to wait few seconds until DeepInsight database updated.
        dropAnomalies = utilities.retry(
            f=getDropAnomalies,
            retValue=[[]],
            attempts=60,
        )

        utilities.assert_equals(
            expect=1, actual=len(dropAnomalies),
            onpass="Got 1 drop anomaly from DeepInsight as expected.",
            onfail="Got %d drop anomaly from DeepInsight, expect 1" % (len(dropAnomalies))
        )

        main.step("Checking drop reason from the report")
        dropAnomaly = dropAnomalies[0]
        dropReason = dropAnomaly["DropReason"]

        # DROP_REASON_PORT_VLAN_MAPPING_MISS = 55
        utilities.assert_equals(
            expect=55, actual=dropReason,
            onpass="Got drop reason '55' as expected.",
            onfail="Got drop reason '%d', expect '55'." % (dropReason)
        )

        main.step("Clean up the test")
        intTest.cleanUp(main)

    def CASE3 (self, main):
        """
        Send a packet with IP TTL value 1 from one host to another host and check
        if DeepInsight receives drop reports.
        """
        import time
        import socket
        from core import utilities
        from tests.USECASE.SegmentRouting.INT.dependencies.IntTest import IntTest
        main.cfgName = "CASE3"

        main.step("Setting up the test")
        intTest = IntTest(scapy=True)
        intTest.setUpTest(main)

        main.step("Setting up hosts and variables")
        srcIfaceName = main.h1.interfaces[0]["name"]
        dstIfaceName = main.h3.interfaces[0]["name"]
        srcMac = main.h1.getMac(srcIfaceName)
        dstMac = main.params.get("routerMac", "00:00:00:00:00:00")
        srcIp = main.h1.getIp(srcIfaceName)
        dstIp = main.h3.getIp(dstIfaceName)
        srcPort = 3000
        dstPort = 8888

        main.step("Sending a packet with IP TTL value 1 from h1")
        startTimeMs = (time.time() - 5) * 1000
        pkt = """(
            Ether(src="{}", dst="{}") /
            IP(src="{}", dst="{}", ttl=1) /
            UDP(sport={}, dport={}) /
            ("A"*30)
        )""".format(srcMac, dstMac, srcIp, dstIp, srcPort, dstPort)
        main.h1.sendPacket(iface=srcIfaceName, packet=pkt)
        endTimeMs = (time.time() + 5) * 1000

        main.step("Checking drop report from DeepInsight")
        def getDropAnomalies(*args, **kwargs):
            return main.DeepInsight.getAnomalyRecords(
                startTime=startTimeMs,
                endTime=endTimeMs,
                srcIp=srcIp,
                dstIp=dstIp,
                srcPort=srcPort,
                dstPort=dstPort,
                ipProto=socket.IPPROTO_UDP,
                anomalyType="packet_drop",
            )

        # Need to wait few seconds until DeepInsight database updated.
        dropAnomalies = utilities.retry(
            f=getDropAnomalies,
            retValue=[[]],
            attempts=60,
        )

        utilities.assert_equals(
            expect=1, actual=len(dropAnomalies),
            onpass="Got 1 drop anomaly from DeepInsight as expected.",
            onfail="Got %d drop anomaly from DeepInsight, expect '1'." % (len(dropAnomalies))
        )

        main.step("Checking drop reason from report")
        dropAnomaly = dropAnomalies[0]
        dropReason = dropAnomaly["DropReason"]
        # DROP_REASON_IP_TTL_ZERO = 26
        utilities.assert_equals(
            expect=26, actual=dropReason,
            onpass="Got drop reason '26' as expected.",
            onfail="Got drop reason '%d', expect '26'." % (dropReason)
        )

        main.step("Clean up the test")
        intTest.cleanUp(main)
