Changed checkState function

Also added log:set function

Change-Id: Ia8102e8762f44e60d243b2397e43377a3648ebc9
diff --git a/TestON/drivers/common/api/controller/onosrestdriver.py b/TestON/drivers/common/api/controller/onosrestdriver.py
index 08f242d..15df67a 100644
--- a/TestON/drivers/common/api/controller/onosrestdriver.py
+++ b/TestON/drivers/common/api/controller/onosrestdriver.py
@@ -1760,18 +1760,18 @@
 
     def checkStatus(
             self,
-            topologyResult,
             numoswitch,
             numolink,
+            numoctrl = -1,
             logLevel="info" ):
         """
         Checks the number of switches & links that ONOS sees against the
         supplied values. By default this will report to main.log, but the
         log level can be specific.
 
-        Params: topologyResult = the output of topology command
-                numoswitch = expected number of switches
+        Params: numoswitch = expected number of switches
                 numolink = expected number of links
+                numoctrl = expected number of controllers
                 logLevel = level to log to.
                 Currently accepts 'info', 'warn' and 'report'
 
@@ -1780,19 +1780,21 @@
                  and main.ERROR otherwise
         """
         try:
-            topology = self.getTopology( topologyResult )
+            topology = self.getTopology( self.topology() )
             if topology == {}:
                 return main.ERROR
             output = ""
             # Is the number of switches is what we expected
             devices = topology.get( 'devices', False )
             links = topology.get( 'links', False )
-            if devices is False or links is False:
+            nodes = topology.get( 'nodes' , False )
+            if devices is False or links is False or nodes is False:
                 return main.ERROR
             switchCheck = ( int( devices ) == int( numoswitch ) )
             # Is the number of links is what we expected
             linkCheck = ( int( links ) == int( numolink ) )
-            if switchCheck and linkCheck:
+            nodeCheck = ( int(nodes) == int(numoctrl) )or int(numoctrl) == -1
+            if switchCheck and linkCheck and nodeCheck:
                 # We expected the correct numbers
                 output = output + "The number of links and switches match "\
                     + "what was expected"
@@ -1806,6 +1808,9 @@
             output = output + " (%i expected) " % int( numoswitch )
             output = output + "and %i links " % int( links )
             output = output + "(%i expected)" % int( numolink )
+            if int( numoctrl ) > 0
+                output = output + "and %i controllers " % int( nodes )
+                output = output + "(%i expected)" % int( numoctrl )
             if logLevel == "report":
                 main.log.report( output )
             elif logLevel == "warn":
@@ -1821,4 +1826,4 @@
         except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
-            main.exit()
\ No newline at end of file
+            main.exit()
diff --git a/TestON/drivers/common/cli/onosclidriver.py b/TestON/drivers/common/cli/onosclidriver.py
old mode 100644
new mode 100755
index 7ef347b..a881000
--- a/TestON/drivers/common/cli/onosclidriver.py
+++ b/TestON/drivers/common/cli/onosclidriver.py
@@ -2789,20 +2789,15 @@
             main.cleanup()
             main.exit()
 
-    def checkStatus(
-            self,
-            topologyResult,
-            numoswitch,
-            numolink,
-            logLevel="info" ):
+    def checkStatus(self, numoswitch, numolink, numoctrl = -1, logLevel="info"):
         """
         Checks the number of switches & links that ONOS sees against the
         supplied values. By default this will report to main.log, but the
         log level can be specific.
 
-        Params: topologyResult = the output of topology command
-                numoswitch = expected number of switches
+        Params: numoswitch = expected number of switches
                 numolink = expected number of links
+                numoctrl = expected number of controllers
                 logLevel = level to log to.
                 Currently accepts 'info', 'warn' and 'report'
 
@@ -2810,20 +2805,25 @@
                  main.FALSE if the number of switches and links is incorrect,
                  and main.ERROR otherwise
         """
+        import json
         try:
-            topology = self.getTopology( topologyResult )
-            if topology == {} or topology == None:
+            topology = self.getTopology( self.topology() )
+            summary = json.loads( self.summary() )
+            
+            if topology == {} or topology == None or summary == {} or summary == None:
                 return main.ERROR
             output = ""
             # Is the number of switches is what we expected
             devices = topology.get( 'devices', False )
             links = topology.get( 'links', False )
-            if devices is False or links is False:
+            nodes = summary.get( 'nodes', False )
+            if devices is False or links is False or nodes is False:
                 return main.ERROR
             switchCheck = ( int( devices ) == int( numoswitch ) )
             # Is the number of links is what we expected
             linkCheck = ( int( links ) == int( numolink ) )
-            if switchCheck and linkCheck:
+            nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1
+            if switchCheck and linkCheck and nodeCheck:
                 # We expected the correct numbers
                 output = output + "The number of links and switches match "\
                     + "what was expected"
@@ -2837,6 +2837,9 @@
             output = output + " (%i expected) " % int( numoswitch )
             output = output + "and %i links " % int( links )
             output = output + "(%i expected)" % int( numolink )
+            if int( numoctrl ) > 0
+                output = output + "and %i controllers " % int( nodes )
+                output = output + "(%i expected)" % int( numoctrl )
             if logLevel == "report":
                 main.log.report( output )
             elif logLevel == "warn":
@@ -4800,3 +4803,71 @@
             main.cleanup()
             main.exit()
 
+    def portstate(self, dpid='of:0000000000000102', port='2', state='enable'):
+        '''
+        Description:
+             Changes the state of port in an OF switch by means of the
+             PORTSTATUS OF messages.
+        params:
+            dpid - (string) Datapath ID of the device
+            port - (string) target port in the device
+            state - (string) target state (enable or disabled)
+        returns:
+            main.TRUE if no exceptions were thrown and no Errors are
+            present in the resoponse. Otherwise, returns main.FALSE
+        '''
+        try:
+            cmd =  "portstate {} {} {}".format( dpid, port, state )
+            response = self.sendline( cmd, showResponse=True )
+            assert response is not None, "Error in sendline"
+            assert "Command not found:" not in response, response
+            if "Error" in response or "Failure" in response:
+                main.log.error( response )
+                return main.FALSE
+            return main.TRUE
+        except AssertionError:
+            main.log.exception( "" )
+            return None
+        except TypeError:
+            main.log.exception( self.name + ": Object not as expected" )
+            return main.FALSE
+        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 logSet( self, level="INFO", app="org.onosproject" ):
+        """
+        Set the logging level to lvl for a specific app
+        returns main.TRUE on success
+        returns main.FALSE if Error occurred
+        if noExit is True, TestON will not exit, but clean up
+        Available level: DEBUG, TRACE, INFO, WARN, ERROR
+        Level defaults to INFO
+        """
+        try:
+            self.handle.sendline( "log:set %s %s" %( level, app ) )
+            self.handle.expect( "onos>" )
+
+            response = self.handle.before
+            if re.search( "Error", response ):
+                return main.FALSE
+            return main.TRUE
+        except pexpect.TIMEOUT:
+            main.log.exception( self.name + ": TIMEOUT exception found" )
+            main.cleanup()
+            main.exit()
+        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()
diff --git a/TestON/tests/CHO/CHOtest/dependencies/CHOtestFunctions.py b/TestON/tests/CHO/CHOtest/dependencies/CHOtestFunctions.py
old mode 100644
new mode 100755
index c8d40ac..7a8a08b
--- a/TestON/tests/CHO/CHOtest/dependencies/CHOtestFunctions.py
+++ b/TestON/tests/CHO/CHOtest/dependencies/CHOtestFunctions.py
@@ -139,9 +139,7 @@
         linkResult = main.TRUE
         for e in range( int( main.numCtrls ) ):
             main.log.info( "Checking link number on ONOS%s" % (e+1) )
-            topology_output = main.CLIs[e].topology()
-            linkResultIndividual = main.ONOScli1.checkStatus( topology_output,
-                                                               main.numMNswitches,
+            linkResultIndividual = main.CLIs[e].checkStatus( main.numMNswitches,
                                                                str( linkNum ) )
             if not linkResultIndividual:
                 main.log.warn( "Link %s not discovered by ONOS%s" % ( linkEvent, (e+1) ) )
diff --git a/TestON/tests/CHOTestMonkey/dependencies/events/CheckEvent.py b/TestON/tests/CHOTestMonkey/dependencies/events/CheckEvent.py
old mode 100644
new mode 100755
index d0408f9..76722f0
--- a/TestON/tests/CHOTestMonkey/dependencies/events/CheckEvent.py
+++ b/TestON/tests/CHOTestMonkey/dependencies/events/CheckEvent.py
@@ -102,8 +102,7 @@
         for controller in main.controllers:
             if controller.isUp():
                 with controller.CLILock:
-                    topologyOutput = controller.CLI.topology()
-                    topoState = controller.CLI.checkStatus( topologyOutput, upDeviceNum, upLinkNum )
+                    topoState = controller.CLI.checkStatus( upDeviceNum, upLinkNum )
                     #if not topoState:
                     #    main.log.warn( "Topo Check - link or device number discoverd by ONOS%s is incorrect" % ( controller.index ) )
                     #    checkResult = EventStates().FAIL
diff --git a/TestON/tests/FUNC/FUNCintent/dependencies/FuncIntentFunction.py b/TestON/tests/FUNC/FUNCintent/dependencies/FuncIntentFunction.py
old mode 100644
new mode 100755
index 1caa472..5b4bee1
--- a/TestON/tests/FUNC/FUNCintent/dependencies/FuncIntentFunction.py
+++ b/TestON/tests/FUNC/FUNCintent/dependencies/FuncIntentFunction.py
@@ -1607,9 +1607,7 @@
     main.log.info( itemName + ": Checking ONOS topology " )
 
     for i in range( main.numCtrls ):
-        topologyResult = main.CLIs[ i ].topology()
-        statusResult = main.CLIs[ i ].checkStatus( topologyResult,
-                                                   main.numSwitch,
+        statusResult = main.CLIs[ i ].checkStatus( main.numSwitch,
                                                    expectedLink )\
                        and statusResult
     if not statusResult:
diff --git a/TestON/tests/FUNC/FUNCintentRest/dependencies/FuncIntentFunction.py b/TestON/tests/FUNC/FUNCintentRest/dependencies/FuncIntentFunction.py
old mode 100644
new mode 100755
index cefb077..5d526de
--- a/TestON/tests/FUNC/FUNCintentRest/dependencies/FuncIntentFunction.py
+++ b/TestON/tests/FUNC/FUNCintentRest/dependencies/FuncIntentFunction.py
@@ -1419,9 +1419,7 @@
     main.log.info( itemName + ": Checking ONOS topology " )
 
     for i in range( main.numCtrls ):
-        topologyResult = main.CLIs[ i ].topology()
-        statusResult = main.CLIs[ i ].checkStatus( topologyResult,
-                                                   main.numSwitch,
+        statusResult = main.CLIs[ i ].checkStatus( main.numSwitch,
                                                    expectedLink )\
                        and statusResult
     if not statusResult:
diff --git a/TestON/tests/FUNC/FUNCipv6Intent/dependencies/FUNCIpv6IntentFunction.py b/TestON/tests/FUNC/FUNCipv6Intent/dependencies/FUNCIpv6IntentFunction.py
old mode 100644
new mode 100755
index 40a125b..0d71604
--- a/TestON/tests/FUNC/FUNCipv6Intent/dependencies/FUNCIpv6IntentFunction.py
+++ b/TestON/tests/FUNC/FUNCipv6Intent/dependencies/FUNCIpv6IntentFunction.py
@@ -1844,9 +1844,7 @@
     main.log.info( itemName + ": Checking ONOS topology " )
 
     for i in range( main.numCtrls ):
-        topologyResult = main.CLIs[ i ].topology()
-        statusResult = main.CLIs[ i ].checkStatus( topologyResult,
-                                                   main.numSwitch,
+        statusResult = main.CLIs[ i ].checkStatus( main.numSwitch,
                                                    expectedLink )\
                        and statusResult
     if not statusResult:
diff --git a/TestON/tests/FUNC/FUNCoptical/dependencies/FuncIntentFunction.py b/TestON/tests/FUNC/FUNCoptical/dependencies/FuncIntentFunction.py
old mode 100644
new mode 100755
index 1d45f62..616e71e
--- a/TestON/tests/FUNC/FUNCoptical/dependencies/FuncIntentFunction.py
+++ b/TestON/tests/FUNC/FUNCoptical/dependencies/FuncIntentFunction.py
@@ -1436,9 +1436,7 @@
     main.log.info( itemName + ": Checking ONOS topology " )
 
     for i in range( main.numCtrls ):
-        topologyResult = main.CLIs[ i ].topology()
-        statusResult = main.CLIs[ i ].checkStatus( topologyResult,
-                                                   main.numSwitch,
+        statusResult = main.CLIs[ i ].checkStatus( main.numSwitch,
                                                    expectedLink )\
                        and statusResult
     if not statusResult: