[SDFAB-486][SDFAB-487] Add simple INT flow report test and DeepInsight driver

Change-Id: Iebfe514e3a811d157808973cf6d3d59beb48423f
diff --git a/TestON/drivers/common/api/deepinsightapidriver.py b/TestON/drivers/common/api/deepinsightapidriver.py
new file mode 100644
index 0000000..af02256
--- /dev/null
+++ b/TestON/drivers/common/api/deepinsightapidriver.py
@@ -0,0 +1,93 @@
+from deepinsight.client import DeepInsightClient
+from drivers.common.apidriver import API
+
+class DeepInsightApiDriver( API ):
+    def __init__( self ):
+        self.name = None
+        self.serverUrl = None
+        self.accessToken = None
+        self.refreshToken = None
+        self.requestAuthHeader = None
+        self.verifySsl = False
+        self.client = None
+        super( DeepInsightApiDriver, self ).__init__()
+
+    def connect(
+        self,
+        **connectargs
+    ):
+        for key in connectargs:
+            vars(self)[key] = connectargs[key]
+        self.name = self.options["name"]
+        self.client = DeepInsightClient(
+            server_url = self.options["server_url"],
+            username = self.options["username"],
+            password = self.options["password"],
+            verify_ssl = self.options["verify_ssl"] == "True",
+        )
+        self.handle = super( DeepInsightApiDriver, self ).connect()
+        return self.handle
+
+    def disconnect( self, **connectargs ):
+        self.client.logout()
+
+    def getFlows(
+        self,
+        startTimeMs = None,
+        endTimeMs = None,
+        maxResults = 100,
+        srcIp = None,
+        dstIp = None,
+        srcPort = None,
+        dstPort = None,
+        ipProto = None,
+    ):
+        return self.client.get_flows(
+            startTimeMs,
+            endTimeMs,
+            maxResults,
+            srcIp,
+            dstIp,
+            srcPort,
+            dstPort,
+            ipProto,
+        )
+
+    def getSwitchPacketDrop(
+        self,
+        switchId,
+        egressPort = 0,
+        queueId = 0,
+        startTime = None,
+        endTime = None,
+        numBuckets = 100,
+    ):
+        return self.client.get_switch_packet_drop(
+            switchId,
+            egressPort,
+            queueId,
+            startTime,
+            endTime,
+            numBuckets,
+        )
+
+    def getSwitchAnomalies(
+        self, switchId, startTime = None, endTime = None
+    ):
+        return self.client.get_switch_anomalies(
+            switchId, startTime, endTime
+        )
+
+    def getSwitchLatencies(
+        self,
+        switchId,
+        startTime = None,
+        endTime = None,
+        granularity = 1000,
+    ):
+        return self.client.get_switch_latencies(
+            switchId,
+            startTime,
+            endTime,
+            granularity,
+        )
diff --git a/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.params b/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.params
index 2cb31b3..a534c7c 100644
--- a/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.params
+++ b/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.params
@@ -1,5 +1,5 @@
 <PARAMS>
-    <testcases>1,2,101,102,103,104,201,202,203,204,205,206,207,208,301</testcases>
+    <testcases>1,2,101,102,103,104,201,202,203,204,205,206,207,208,301,302</testcases>
 
     <GRAPH>
         <nodeCluster>pairedleaves</nodeCluster>
diff --git a/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.py b/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.py
index 96618cf..077df04 100644
--- a/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.py
+++ b/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.py
@@ -670,3 +670,61 @@
         # Cleanup
         main.log.warn( json.dumps( main.downtimeResults, indent=4, sort_keys=True ) )
         main.funcs.cleanup( main )
+
+    def CASE302 ( self, main ):
+        """
+        Send ping packets from one host to another host and check flows from DeepInsight.
+        """
+
+        try:
+            from tests.USECASE.SegmentRouting.SRStaging.dependencies.SRStagingTest import SRStagingTest
+            from tests.USECASE.SegmentRouting.dependencies.Testcaselib import Testcaselib as run
+            from core import utilities
+            import time
+            import socket
+        except ImportError as e:
+            main.log.exception( "SRStagingTest not found. Exiting the test" )
+            main.cleanAndExit()
+        try:
+            main.funcs
+        except ( NameError, AttributeError ):
+            main.funcs = SRStagingTest()
+
+        pod = main.params['GRAPH'].get( 'nodeCluster', "hardware" )
+        main.cfgName = 'CASE302'
+        main.funcs.setupTest( main,
+                              topology='0x2',
+                              onosNodes=3,
+                              description="INT flow report tests on %s POD" % ( pod ) )
+        startTimeMs = ( time.time() - 5 ) * 1000
+        run.verifyPing( main, ['h1'], ['h2'] )
+        endTimeMs = ( time.time() + 5 ) * 1000
+        main.step( "Checking flow report from DeepInsight" )
+
+        def getFiveTupleCount(*args, **kwargs):
+            flows = main.DeepInsight.getFlows(
+                startTimeMs=startTimeMs,
+                endTimeMs=endTimeMs,
+                srcIp=main.h1.interfaces[0]['ips'][0],
+                dstIp=main.h2.interfaces[0]['ips'][0],
+                ipProto=socket.IPPROTO_ICMP
+            )
+            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.funcs.cleanup( main )
diff --git a/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.topo b/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.topo
index 40bf030..e529c6f 100644
--- a/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.topo
+++ b/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.topo
@@ -184,5 +184,19 @@
             </COMPONENTS>
         </NetworkBench>
 
+        <DeepInsight>
+            <host>10.76.28.74</host>
+            <user>jenkins</user>
+            <password></password>
+            <type>DeepInsightApiDriver</type>
+            <connect_order>1</connect_order>
+            <COMPONENTS>
+                <server_url>https://10.76.28.74:30000</server_url>
+                <username>diadmin</username>
+                <password>diadmin</password>
+                <verify_ssl>False</verify_ssl>
+            </COMPONENTS>
+        </DeepInsight>
+
     </COMPONENT>
 </TOPOLOGY>