Added cli to FUNCintentRest to allow checking of leaders during case 1000

Change-Id: I7935ad6bfdc02c39f2c3cc6dc0504c9d842bd681
diff --git a/TestON/tests/FUNCintentRest/Dependency/FuncIntentFunction.py b/TestON/tests/FUNCintentRest/Dependency/FuncIntentFunction.py
index af02e45..900abd5 100644
--- a/TestON/tests/FUNCintentRest/Dependency/FuncIntentFunction.py
+++ b/TestON/tests/FUNCintentRest/Dependency/FuncIntentFunction.py
@@ -1650,6 +1650,44 @@
 
         return connectionsFunctional
 
+def checkLeaderChange( leaders1, leaders2 ):
+    """
+        Checks for a change in intent partition leadership.
+
+        Takes the output of leaders -c in json string format before and after
+        a potential change as input
+
+        Returns main.TRUE if no mismatches are detected
+        Returns main.FALSE if there is a mismatch or on error loading the input
+    """
+    try:
+        leaders1 = json.loads( leaders1 )
+        leaders2 = json.loads( leaders2 )
+    except ( AttributeError, TypeError):
+        main.log.exception( self.name + ": Object not as expected" )
+        return main.FALSE
+    except Exception:
+        main.log.exception( self.name + ": Uncaught exception!" )
+        main.cleanup()
+        main.exit()
+    main.log.info( "Checking Intent Paritions for Change in Leadership" )
+    mismatch = False
+    for dict1 in leaders1:
+        if "intent" in dict1.get( "topic", [] ):
+            for dict2 in leaders2:
+                if dict1.get( "topic", 0 ) == dict2.get( "topic", 0 ) and \
+                    dict1.get( "leader", 0 ) != dict2.get( "leader", 0 ):
+                    mismatch = True
+                    main.log.error( "{0} changed leader from {1} to {2}".\
+                        format( dict1.get( "topic", "no-topic" ),\
+                            dict1.get( "leader", "no-leader" ),\
+                            dict2.get( "leader", "no-leader" ) ) )
+    if mismatch:
+        return main.FALSE
+    else:
+        return main.TRUE
+
+
 def report( main ):
     """
         Report errors/warnings/exceptions
diff --git a/TestON/tests/FUNCintentRest/FUNCintentRest.py b/TestON/tests/FUNCintentRest/FUNCintentRest.py
index 258a9c1..ede3459 100644
--- a/TestON/tests/FUNCintentRest/FUNCintentRest.py
+++ b/TestON/tests/FUNCintentRest/FUNCintentRest.py
@@ -65,6 +65,7 @@
             main.cellData = {} # for creating cell file
             main.hostsData = {}
             main.CLIs = []
+            main.CLIs2 = []
             main.ONOSip = []
             main.scapyHostNames = main.params[ 'SCAPY' ][ 'HOSTNAMES' ].split( ',' )
             main.scapyHosts = []  # List of scapy hosts for iterating
@@ -77,6 +78,7 @@
             try:
                 for i in range( 1,  main.maxNodes + 1 ):
                     main.CLIs.append( getattr( main, 'ONOSrest' + str( i ) ) )
+                    main.CLIs2.append( getattr( main, 'ONOScli' + str( i ) ) )
             except AttributeError:
                 main.log.warn( "A " + str( main.maxNodes ) + " node cluster " +
                                "was defined in env variables, but only " +
@@ -106,7 +108,7 @@
                                               main.topology,
                                               main.Mininet1.home + "custom/",
                                               direction="to" )
-            if main.CLIs:
+            if main.CLIs and main.CLIs2:
                 stepResult = main.TRUE
             else:
                 main.log.error( "Did not properly created list of ONOS CLI handle" )
@@ -244,20 +246,20 @@
                                  onpass="ONOS service is ready",
                                  onfail="ONOS service did not start properly" )
 
-        # Revisit adding the cli after ensuring the test works without it
         # Start an ONOS cli to provide functionality that is not currently
-        # supported by the Rest API
+        # supported by the Rest API remove this when Leader Checking is supported
+        # by the REST API
 
-        # main.step( "Start ONOS cli" )
-        # cliResult = main.TRUE
-        # for i in range( main.numCtrls ):
-        #     cliResult = cliResult and \
-        #                 main.CLIs[ i ].startOnosCli( main.ONOSip[ i ] )
-        # stepResult = cliResult
-        # utilities.assert_equals( expect=main.TRUE,
-        #                          actual=stepResult,
-        #                          onpass="Successfully start ONOS cli",
-        #                          onfail="Failed to start ONOS cli" )
+        main.step( "Start ONOS cli" )
+        cliResult = main.TRUE
+        for i in range( main.numCtrls ):
+            cliResult = cliResult and \
+                        main.CLIs2[ i ].startOnosCli( main.ONOSip[ i ] )
+        stepResult = cliResult
+        utilities.assert_equals( expect=main.TRUE,
+                                 actual=stepResult,
+                                 onpass="Successfully start ONOS cli",
+                                 onfail="Failed to start ONOS cli" )
 
         # Remove the first element in main.scale list
         main.scale.remove( main.scale[ 0 ] )
@@ -719,6 +721,9 @@
         assert main.numSwitch, "Placed the total number of switch topology in \
                                 main.numSwitch"
 
+        # Save leader candidates
+        intentLeadersOld = main.CLIs2[ 0 ].leaderCandidates()
+
         main.case( "Host Intents Test - " + str( main.numCtrls ) +
                    " NODE(S) - OF " + main.OFProtocol )
         main.caseExplanation = "This test case tests Host intents using " +\
@@ -890,18 +895,18 @@
                                  onpass=main.assertReturnString,
                                  onfail=main.assertReturnString )
 
-        # Uncomment the following if a REST command is ever added to check leaders
-        # or if the cli is enabled
+        # Change the following to use the REST API when leader checking is
+        # supported by it
 
-        # main.step( "Confirm that ONOS leadership is unchanged")
-        # intentLeadersNew = main.CLIs[ 0 ].leaderCandidates()
-        # main.intentFunction.checkLeaderChange( intentLeadersOld,
-        #                                         intentLeadersNew )
+        main.step( "Confirm that ONOS leadership is unchanged")
+        intentLeadersNew = main.CLIs2[ 0 ].leaderCandidates()
+        main.intentFunction.checkLeaderChange( intentLeadersOld,
+                                                intentLeadersNew )
 
-        # utilities.assert_equals( expect=main.TRUE,
-        #                          actual=testResult,
-        #                          onpass="ONOS Leaders Unchanged",
-        #                          onfail="ONOS Leader Mismatch")
+        utilities.assert_equals( expect=main.TRUE,
+                                 actual=testResult,
+                                 onpass="ONOS Leaders Unchanged",
+                                 onfail="ONOS Leader Mismatch")
 
         main.intentFunction.report( main )
 
diff --git a/TestON/tests/FUNCintentRest/FUNCintentRest.topo b/TestON/tests/FUNCintentRest/FUNCintentRest.topo
index 6be48c6..5e710a2 100755
--- a/TestON/tests/FUNCintentRest/FUNCintentRest.topo
+++ b/TestON/tests/FUNCintentRest/FUNCintentRest.topo
@@ -44,6 +44,33 @@
             </COMPONENTS>
         </ONOSrest3>
 
+        <ONOScli1>
+            <host>localhost</host>
+            <user>admin</user>
+            <password></password>
+            <type>OnosCliDriver</type>
+            <connect_order>2</connect_order>
+            <COMPONENTS> </COMPONENTS>
+        </ONOScli1>
+
+        <ONOScli2>
+            <host>localhost</host>
+            <user>admin</user>
+            <password></password>
+            <type>OnosCliDriver</type>
+            <connect_order>3</connect_order>
+            <COMPONENTS> </COMPONENTS>
+        </ONOScli2>
+
+         <ONOScli3>
+            <host>localhost</host>
+            <user>admin</user>
+            <password></password>
+            <type>OnosCliDriver</type>
+            <connect_order>4</connect_order>
+            <COMPONENTS> </COMPONENTS>
+        </ONOScli3>
+
         <Mininet1>
             <host>localhost</host>
             <user>admin</user>