Refactor bandwidth allocation checks for FUNCintent

Change-Id: Iff583a972f2b1928454ad67ce33e4116a3523219
diff --git a/TestON/drivers/common/cli/onosclidriver.py b/TestON/drivers/common/cli/onosclidriver.py
index a348ee8..6289925 100755
--- a/TestON/drivers/common/cli/onosclidriver.py
+++ b/TestON/drivers/common/cli/onosclidriver.py
@@ -2206,15 +2206,13 @@
             main.cleanup()
             main.exit()
 
-    def checkIntentState( self, intentsId, bandwidthFlag=False, expectedState='INSTALLED' ):
+    def checkIntentState( self, intentsId, expectedState='INSTALLED' ):
         """
         Description:
             Check intents state
         Required:
             intentsId - List of intents ID to be checked
         Optional:
-            bandwidthFlag - Check the bandwidth allocations. If bandwidth is
-                                specified, then check for bandwidth allocations
             expectedState - Check the expected state(s) of each intents
                             state in the list.
                             *NOTE: You can pass in a list of expected state,
@@ -2224,43 +2222,14 @@
             otherwise returns main.FALSE.
         """
         try:
-            # Generating a dictionary: intent id as a key and state as value
             returnValue = main.TRUE
+            # Generating a dictionary: intent id as a key and state as value
             intentsDict = self.getIntentState( intentsId )
             if len( intentsId ) != len( intentsDict ):
                 main.log.info( self.name + ": There is something wrong " +
                                "getting intents state" )
                 return main.FALSE
 
-            bandwidthFailed = False
-            if bandwidthFlag:
-                rawAlloc = self.allocations()
-                expectedFormat = open( os.path.dirname( main.testFile ) + main.params[ 'DEPENDENCY' ][ 'filePath' ], 'r' )
-                ONOSOutput = StringIO(rawAlloc)
-                main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str(ONOSOutput), str( expectedFormat ) ) )
-
-                for actual, expected in izip( ONOSOutput, expectedFormat ):
-                    actual = actual.rstrip()
-                    expected = expected.rstrip()
-                    main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) )
-                    if actual != expected and 'allocated' in actual and 'allocated' in expected:
-                        marker1 = actual.find('allocated')
-                        m1 = actual[:marker1]
-                        marker2 = expected.find('allocated')
-                        m2 = expected[:marker2]
-                        if m1 != m2:
-                            bandwidthFailed = True
-                    elif actual != expected and 'allocated' not in actual and 'allocated' not in expected:
-                        bandwidthFailed = True
-
-                expectedFormat.close()
-                ONOSOutput.close()
-
-            if bandwidthFailed:
-                main.log.error("Bandwidth not allocated correctly using Intents!!")
-                returnValue = main.FALSE
-                return returnValue
-
             if isinstance( expectedState, types.StringType ):
                 for intents in intentsDict:
                     if intents.get( 'state' ) != expectedState:
@@ -2303,6 +2272,59 @@
             main.cleanup()
             main.exit()
 
+    def compareBandwidthAllocations( self, expectedAllocations ):
+        """
+        Description:
+            Compare the allocated bandwidth with the given allocations
+        Required:
+            expectedAllocations - The expected ONOS output of the allocations command
+        Return:
+            Returns main.TRUE only if all intent are the same as expected states,
+            otherwise returns main.FALSE.
+        """
+        # FIXME: Convert these string comparisons to object comparisons
+        try:
+            returnValue = main.TRUE
+            bandwidthFailed = False
+            rawAlloc = self.allocations()
+            expectedFormat = StringIO( expectedAllocations )
+            ONOSOutput = StringIO( rawAlloc )
+            main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ),
+                                                                          str( expectedFormat ) ) )
+
+            for actual, expected in izip( ONOSOutput, expectedFormat ):
+                actual = actual.rstrip()
+                expected = expected.rstrip()
+                main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) )
+                if actual != expected and 'allocated' in actual and 'allocated' in expected:
+                    marker1 = actual.find('allocated')
+                    m1 = actual[:marker1]
+                    marker2 = expected.find('allocated')
+                    m2 = expected[:marker2]
+                    if m1 != m2:
+                        bandwidthFailed = True
+                elif actual != expected and 'allocated' not in actual and 'allocated' not in expected:
+                    bandwidthFailed = True
+            expectedFormat.close()
+            ONOSOutput.close()
+
+            if bandwidthFailed:
+                main.log.error("Bandwidth not allocated correctly using Intents!!")
+                returnValue = main.FALSE
+            return returnValue
+        except TypeError:
+            main.log.exception( self.name + ": Object not as expected" )
+            return None
+        except pexpect.EOF:
+            main.log.error( self.name + ": EOF exception found" )
+            main.log.error( self.name + ":    " + self.handle.before )
+            main.cleanup()
+            main.exit()
+        except Exception:
+            main.log.exception( self.name + ": Uncaught exception!" )
+            main.cleanup()
+            main.exit()
+
     def compareIntent( self, intentDict ):
         """
         Description:
diff --git a/TestON/tests/FUNC/FUNCintent/FUNCintent.params b/TestON/tests/FUNC/FUNCintent/FUNCintent.params
index c55cd83..1215e16 100644
--- a/TestON/tests/FUNC/FUNCintent/FUNCintent.params
+++ b/TestON/tests/FUNC/FUNCintent/FUNCintent.params
@@ -48,14 +48,14 @@
         <startup>15</startup>
         <reroute>5</reroute>
         <removeintent>10</removeintent>
-        <checkintent>10</checkintent>
+        <checkintent>5</checkintent>
         <fwd>10</fwd>
         <topoAttempts>3</topoAttempts>
         <flowDuration>10</flowDuration>
-        <checkConnection>50</checkConnection>
+        <checkConnection>30</checkConnection>
         <checkFlowCount>20</checkFlowCount>
-        <checkIntentHost>10</checkIntentHost>
-        <checkIntentPoint>20</checkIntentPoint>
+        <checkIntentHost>5</checkIntentHost>
+        <checkIntentPoint>5</checkIntentPoint>
     </SLEEP>
 
     <MININET>
diff --git a/TestON/tests/FUNC/FUNCintent/FUNCintent.py b/TestON/tests/FUNC/FUNCintent/FUNCintent.py
index 074b472..5b88b25 100644
--- a/TestON/tests/FUNC/FUNCintent/FUNCintent.py
+++ b/TestON/tests/FUNC/FUNCintent/FUNCintent.py
@@ -153,7 +153,6 @@
         main.log.info( "Safety check, killing all ONOS processes" +
                        " before initiating environment setup" )
 
-        time.sleep( main.startUpSleep )
         main.step( "Uninstalling ONOS package" )
         onosUninstallResult = main.TRUE
         for ip in main.ONOSip:
@@ -164,6 +163,7 @@
                                  actual=stepResult,
                                  onpass="Successfully uninstalled ONOS package",
                                  onfail="Failed to uninstall ONOS package" )
+        main.log.info( "Sleeping {} seconds".format( main.startUpSleep ) )
         time.sleep( main.startUpSleep )
 
         for i in range( main.maxNodes ):
@@ -197,7 +197,6 @@
                                  onpass="Successfully created ONOS package",
                                  onfail="Failed to create ONOS package" )
 
-        time.sleep( main.startUpSleep )
         main.step( "Installing ONOS package" )
         onosInstallResult = main.TRUE
         for i in range( main.numCtrls ):
@@ -217,6 +216,7 @@
                                  onpass="Test step PASS",
                                  onfail="Test step FAIL" )
 
+        main.log.info( "Sleeping {} seconds".format( main.startUpSleep ) )
         time.sleep( main.startUpSleep )
         main.step( "Starting ONOS service" )
         stopResult = main.TRUE
@@ -286,6 +286,7 @@
 
         while ( attempts >= 0 ) and\
             ( not devicesResults or not linksResults or not hostsResults ):
+            main.log.info( "Sleeping {} seconds".format( 2 ) )
             time.sleep( 2 )
             if not devicesResults:
                 devices = main.topo.getAllDevices( main )
@@ -1450,8 +1451,7 @@
                                       name="NOOPTION",
                                       senders=senders,
                                       recipients=recipients,
-                                      bandwidth=100,
-                                      bandwidthFlag=True )
+                                      bandwidth=100 )
 
         if installResult:
             testResult = main.intentFunction.testPointIntent(
diff --git a/TestON/tests/FUNC/FUNCintent/dependencies/FuncIntentFunction.py b/TestON/tests/FUNC/FUNCintent/dependencies/FuncIntentFunction.py
index d4670b8..2607399 100755
--- a/TestON/tests/FUNC/FUNCintent/dependencies/FuncIntentFunction.py
+++ b/TestON/tests/FUNC/FUNCintent/dependencies/FuncIntentFunction.py
@@ -6,6 +6,7 @@
 import time
 import copy
 import json
+import os
 
 def __init__( self ):
     self.default = ''
@@ -29,8 +30,7 @@
                        sw1="",
                        sw2="",
                        setVlan="",
-                       encap="",
-                       bandwidthFlag=False ):
+                       encap="" ):
     """
     Installs a Host Intent
 
@@ -101,7 +101,7 @@
     # Check intents state
     if utilities.retry( f=checkIntentState,
                         retValue=main.FALSE,
-                        args=( main, [ intentId ], bandwidthFlag ),
+                        args=( main, [ intentId ] ),
                         sleep=main.checkIntentSleep,
                         attempts=50 ):
         main.assertReturnString += 'Install Intent State Passed\n'
@@ -112,12 +112,22 @@
                 main.assertReturnString += 'Encapsulation intents check Passed\n'
             else:
                 main.assertReturnString += 'Encapsulation intents check failed\n'
+        if bandwidth != "":
+            allocationsFile = open( os.path.dirname( main.testFile ) + main.params[ 'DEPENDENCY' ][ 'filePath' ], 'r' )
+            expectedFormat = allocationsFile.read()
+            bandwidthCheck = checkBandwidthAllocations( main, expectedFormat )
+            allocationsFile.close()
+            if bandwidthCheck:
+                main.assertReturnString += 'Bandwidth Allocation check Passed\n'
+            else:
+                main.assertReturnString += 'Bandwidth Allocation check Failed\n'
+
 
         if flowDuration( main ):
             main.assertReturnString += 'Flow duration check Passed\n'
             return intentId
         else:
-            main.assertReturnString += 'Flow duration check failed\n'
+            main.assertReturnString += 'Flow duration check Failed\n'
             return main.FALSE
 
     else:
@@ -231,11 +241,11 @@
     if utilities.retry( f=checkFlowsCount,
                         retValue=main.FALSE,
                         args=[ main ],
-                        sleep=20,
+                        sleep=main.checkFlowCountSleep,
                         attempts=3 ) and utilities.retry( f=checkFlowsState,
                                                           retValue=main.FALSE,
                                                           args=[ main ],
-                                                          sleep=20,
+                                                          sleep=main.checkFlowCountSleep,
                                                           attempts=3 ):
         main.assertReturnString += 'Initial Flow State Passed\n'
     else:
@@ -320,6 +330,7 @@
             testResult = main.FALSE
 
         # Wait for reroute
+        main.log.info( "Sleeping {} seconds".format( main.rerouteSleep ) )
         time.sleep( main.rerouteSleep )
 
         # Check Intents
@@ -337,11 +348,11 @@
         if utilities.retry( f=checkFlowsCount,
                             retValue=main.FALSE,
                             args=[ main ],
-                            sleep=20,
+                            sleep=main.checkFlowCountSleep,
                             attempts=3 ) and utilities.retry( f=checkFlowsState,
                                                               retValue=main.FALSE,
                                                               args=[ main ],
-                                                              sleep=20,
+                                                              sleep=main.checkFlowCountSleep,
                                                               attempts=3 ):
             main.assertReturnString += 'Link Up Flow State Passed\n'
         else:
@@ -393,8 +404,7 @@
                         tcpSrc="",
                         tcpDst="",
                         setVlan="",
-                        encap="",
-                        bandwidthFlag=False ):
+                        encap="" ):
     """
     Installs a Single to Single Point Intent
 
@@ -498,13 +508,20 @@
     # Check intents state
     if utilities.retry( f=checkIntentState,
                         retValue=main.FALSE,
-                        args=( main, [ intentId ], bandwidthFlag ),
-                        sleep=main.checkIntentSleep * 2,
+                        args=( main, [ intentId ] ),
+                        sleep=main.checkIntentSleep,
                         attempts=50 ):
         main.assertReturnString += 'Install Intent State Passed\n'
 
         if bandwidth != "":
-            main.assertReturnString += 'Bandwidth Allocation Passed\n'
+            allocationsFile = open( os.path.dirname( main.testFile ) + main.params[ 'DEPENDENCY' ][ 'filePath' ], 'r' )
+            expectedFormat = allocationsFile.read()
+            bandwidthCheck = checkBandwidthAllocations( main, expectedFormat )
+            allocationsFile.close()
+            if bandwidthCheck:
+                main.assertReturnString += 'Bandwidth Allocation check Passed\n'
+            else:
+                main.assertReturnString += 'Bandwidth Allocation check Failed\n'
 
         # Check VLAN if test encapsulation
         if encap != "":
@@ -520,8 +537,6 @@
             main.assertReturnString += 'Flow duration check failed\n'
             return main.FALSE
     else:
-        if bandwidth != "":
-            main.assertReturnString += 'Bandwidth Allocation Failed\n'
         main.log.error( "Point Intent did not install correctly" )
         pointIntentFailFlag = True
         return main.FALSE
@@ -680,6 +695,7 @@
     intentsId.append( intent4 )
 
     # Check intents state
+    main.log.info( "Sleeping {} seconds".format( main.checkIntentSleep ) )
     time.sleep( main.checkIntentSleep )
     intentResult = utilities.retry( f=checkIntentState,
                                     retValue=main.FALSE,
@@ -766,6 +782,7 @@
         else:
             main.assertReturnString += 'Link Up Failed\n'
 
+        main.log.info( "Sleeping {} seconds".format( main.rerouteSleep ) )
         time.sleep( main.rerouteSleep )
 
         # Check flows count in each node
@@ -834,8 +851,7 @@
                                 sw2="",
                                 setVlan="",
                                 partial=False,
-                                encap="",
-                                bandwidthFlag=False ):
+                                encap="" ):
     """
     Installs a Single to Multi Point Intent
 
@@ -944,10 +960,19 @@
 
     if utilities.retry( f=checkIntentState,
                         retValue=main.FALSE,
-                        args=( main, [ intentId ], bandwidthFlag ),
+                        args=( main, [ intentId ] ),
                         sleep=main.checkIntentSleep,
                         attempts=attempts ):
         main.assertReturnString += 'Install Intent State Passed\n'
+        if bandwidth != "":
+            allocationsFile = open( os.path.dirname( main.testFile ) + main.params[ 'DEPENDENCY' ][ 'filePath' ], 'r' )
+            expectedFormat = allocationsFile.read()
+            bandwidthCheck = checkBandwidthAllocations( main, expectedFormat )
+            allocationsFile.close()
+            if bandwidthCheck:
+                main.assertReturnString += 'Bandwidth Allocation check Passed\n'
+            else:
+                main.assertReturnString += 'Bandwidth Allocation check Failed\n'
         if flowDuration( main ):
             main.assertReturnString += 'Flow duration check Passed\n'
             return intentId
@@ -974,8 +999,7 @@
                                 sw2="",
                                 setVlan="",
                                 partial=False,
-                                encap="",
-                                bandwidthFlag=False ):
+                                encap="" ):
     """
     Installs a Multi to Single Point Intent
 
@@ -1083,10 +1107,19 @@
 
     if utilities.retry( f=checkIntentState,
                         retValue=main.FALSE,
-                        args=( main, [ intentId ], bandwidthFlag ),
+                        args=( main, [ intentId ] ),
                         sleep=main.checkIntentSleep,
                         attempts=attempts ):
         main.assertReturnString += 'Install Intent State Passed\n'
+        if bandwidth != "":
+            allocationsFile = open( os.path.dirname( main.testFile ) + main.params[ 'DEPENDENCY' ][ 'filePath' ], 'r' )
+            expectedFormat = allocationsFile.read()
+            bandwidthCheck = checkBandwidthAllocations( main, expectedFormat )
+            allocationsFile.close()
+            if bandwidthCheck:
+                main.assertReturnString += 'Bandwidth Allocation check Passed\n'
+            else:
+                main.assertReturnString += 'Bandwidth Allocation check Failed\n'
         if flowDuration( main ):
             main.assertReturnString += 'Flow duration check Passed\n'
             return intentId
@@ -1225,11 +1258,11 @@
     if utilities.retry( f=checkFlowsCount,
                         retValue=main.FALSE,
                         args=[ main ],
-                        sleep=20,
+                        sleep=main.checkFlowCountSleep,
                         attempts=3 ) and utilities.retry( f=checkFlowsState,
                                                           retValue=main.FALSE,
                                                           args=[ main ],
-                                                          sleep=20,
+                                                          sleep=main.checkFlowCountSleep,
                                                           attempts=3 ):
         main.assertReturnString += 'Initial Flow State Passed\n'
     else:
@@ -1353,6 +1386,7 @@
             testResult = main.FALSE
 
         # Wait for reroute
+        main.log.info( "Sleeping {} seconds".format( main.rerouteSleep ) )
         time.sleep( main.rerouteSleep )
 
         # Check Intents
@@ -1370,11 +1404,11 @@
         if utilities.retry( f=checkFlowsCount,
                             retValue=main.FALSE,
                             args=[ main ],
-                            sleep=20,
+                            sleep=main.checkFlowCountSleep,
                             attempts=3 ) and utilities.retry( f=checkFlowsState,
                                                               retValue=main.FALSE,
                                                               args=[ main ],
-                                                              sleep=20,
+                                                              sleep=main.checkFlowCountSleep,
                                                               attempts=3 ):
             main.assertReturnString += 'Link Up Flow State Passed\n'
         else:
@@ -1766,6 +1800,7 @@
         testResult = main.FALSE
 
     # Wait for reroute
+    main.log.info( "Sleeping {} seconds".format( main.rerouteSleep ) )
     time.sleep( main.rerouteSleep )
 
     # Check Intents
@@ -1783,11 +1818,11 @@
     if utilities.retry( f=checkFlowsCount,
                         retValue=main.FALSE,
                         args=[ main ],
-                        sleep=5,
-                        attempts=5*20 ) and utilities.retry( f=checkFlowsState,
+                        sleep=main.checkFlowCountSleep,
+                        attempts=attempts ) and utilities.retry( f=checkFlowsState,
                                                              retValue=main.FALSE,
                                                              args=[ main ],
-                                                             sleep=5,
+                                                             sleep=main.checkFlowCountSleep,
                                                              attempts=attempts ):
         main.assertReturnString += 'Link Up Flow State Passed\n'
     else:
@@ -1843,6 +1878,7 @@
     activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" )
 
     # Wait for forward app activation to propagate
+    main.log.info( "Sleeping {} seconds".format( main.fwdSleep ) )
     time.sleep( main.fwdSleep )
 
     # Check that forwarding is enabled on all nodes
@@ -1980,23 +2016,41 @@
         main.log.info( itemName + ": Topology match" )
     return statusResult
 
-def checkIntentState( main, intentsId, bandwidthFlag=False ):
+def checkIntentState( main, intentsId ):
     """
         This function will check intent state to make sure all the intents
         are in INSTALLED state
+        Returns main.TRUE or main.FALSE
     """
     intentResult = main.TRUE
-    results = []
+    stateCheckResults = []
     for i in range( main.numCtrls ):
-        output = main.CLIs[ i ].checkIntentState( intentsId=intentsId, bandwidthFlag=bandwidthFlag )
-        results.append( output )
-    if all( result == main.TRUE for result in results ):
-        main.log.info( itemName + ": Intents are installed correctly" )
+        output = main.CLIs[ i ].checkIntentState( intentsId=intentsId )
+        stateCheckResults.append( output )
+    if all( result == main.TRUE for result in stateCheckResults ):
+        main.log.info( itemName + ": Intents state check passed" )
     else:
-        main.log.warn( "Intents are not installed correctly" )
+        main.log.warn( "Intents state check failed" )
         intentResult = main.FALSE
     return intentResult
 
+def checkBandwidthAllocations( main, bandwidth ):
+    """
+        Compare the given bandwith allocation output to the cli output on each node
+        Returns main.TRUE or main.FALSE
+    """
+    bandwidthResults = []
+    for i in range( main.numCtrls ):
+        output = main.CLIs[ i ].compareBandwidthAllocations( bandwidth )
+        bandwidthResults.append( output )
+    if all( result == main.TRUE for result in bandwidthResults ):
+        main.log.info( itemName + ": bandwidth check passed" )
+        bandwidthResult = main.TRUE
+    else:
+        main.log.warn( itemName + ": bandwidth check failed" )
+        bandwidthResult = main.FALSE
+    return bandwidthResult
+
 def checkFlowsState( main ):
 
     main.log.info( itemName + ": Check flows state" )
@@ -2113,6 +2167,7 @@
     for intent in intentsId:
         main.CLIs[ 0 ].removeIntent( intentId=intent, purge=True )
 
+    main.log.info( "Sleeping {} seconds".format( main.removeIntentSleep ) )
     time.sleep( main.removeIntentSleep )
 
     # If there is remianing intents then remove intents should fail