Merge branch 'ONOS-Next' of https://github.com/OPENNETWORKINGLAB/ONLabTest into ONOS-Next
diff --git a/TestON/drivers/common/cli/emulator/mininetclidriver.py b/TestON/drivers/common/cli/emulator/mininetclidriver.py
index 03fc5ca..9c48e32 100644
--- a/TestON/drivers/common/cli/emulator/mininetclidriver.py
+++ b/TestON/drivers/common/cli/emulator/mininetclidriver.py
@@ -163,7 +163,6 @@
             pattern = 'Results\:\s0\%\sdropped\s'
             #FIXME:Pending Mininet Pull Request #408
             #pattern = 'Results\:\s0\.00\%\sdropped\s'
-            print response
             #if utilities.assert_matches(expect=pattern,actual=response,onpass="All hosts are reaching",onfail="Unable to reach all the hosts"):
             if re.search(pattern,response):
                 main.log.info(self.name+": All hosts are reachable")
@@ -799,6 +798,62 @@
             main.cleanup()
             main.exit()
 
+    def compare_switches(self, topo, switches_json):
+        '''
+        Compare mn and onos switches
+        topo: sts TestONTopology object
+        switches_json: parsed json object from the onos devices api
+
+        This uses the sts TestONTopology object
+
+        '''
+        import json
+        results = main.TRUE
+        output = {"switches":[]}
+        for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info
+            #print vars(switch)
+            ports = []
+            for port in switch.ports.values():
+                #print port.hw_addr.toStr(separator = '')
+                ports.append({'of_port': port.port_no, 'mac': str(port.hw_addr).replace('\'',''), 'name': port.name})
+            output['switches'].append({"name": switch.name, "dpid": str(switch.dpid).zfill(16), "ports": ports })
+        #print output
+
+        #print json.dumps(output, sort_keys=True,indent=4,separators=(',', ': '))
+
+
+        # created sorted list of dpid's in MN and ONOS for comparison
+        mnDPIDs=[]
+        for switch in output['switches']:
+            mnDPIDs.append(switch['dpid'])
+        mnDPIDs.sort()
+        #print mnDPIDs
+        if switches_json == "":#if rest call fails
+            main.log.error(self.name + ".compare_topo(): Empty JSON object given from ONOS")
+            return main.FALSE
+        onos=switches_json
+        onosDPIDs=[]
+        for switch in onos:
+            onosDPIDs.append(switch['id'].replace(":",'').replace("of",''))
+            #print switch
+        onosDPIDs.sort()
+        #print onosDPIDs
+
+        if mnDPIDs!=onosDPIDs:
+            switch_results = main.FALSE
+            main.log.report( "Switches in MN but not in ONOS:")
+            main.log.report( str([switch for switch in mnDPIDs if switch not in onosDPIDs]))
+            main.log.report( "Switches in ONOS but not in MN:")
+            main.log.report(  str([switch for switch in onosDPIDs if switch not in mnDPIDs]))
+        else:#list of dpid's match in onos and mn
+            #main.log.report("DEBUG: The dpid's of the switches in Mininet and ONOS match")
+            switch_results = main.TRUE
+        return switch_results
+
+
+
+
+
     def compare_topo(self, topo, onos_json):
         '''
         compares mn topology with ONOS topology
diff --git a/TestON/drivers/common/cli/onosclidriver.py b/TestON/drivers/common/cli/onosclidriver.py
index 109a3b2..5573ee5 100644
--- a/TestON/drivers/common/cli/onosclidriver.py
+++ b/TestON/drivers/common/cli/onosclidriver.py
@@ -24,6 +24,7 @@
 import traceback
 import os.path
 import pydoc
+import re
 sys.path.append("../")
 from drivers.common.clidriver import CLI
 
@@ -145,7 +146,7 @@
 
             #Wait for onos start (-w) and enter onos cli
             self.handle.sendline("onos -w "+str(ONOS_ip))
-            self.handle.expect("onos>")
+            self.handle.expect("onos>", timeout = 60)
 
         except pexpect.EOF:
             main.log.error(self.name + ": EOF exception found")
@@ -416,6 +417,20 @@
                         str(grep_str)+"'")
                     self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
                     self.handle.expect("onos>")
+                handle = self.handle.before
+                '''
+                handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
+                To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
+                In json.loads(somestring), this somestring variable is a actually repr(somestring) and json.loads would fail with the escape sequence.
+                So we take off that escape sequence using 
+                ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
+                handle1 = ansi_escape.sub('', handle) 
+                '''
+                #print "repr(handle) =", repr(handle)
+                ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
+                handle1 = ansi_escape.sub('', handle)
+                #print "repr(handle1) = ", repr(handle1)
+                return handle1
             else:
                 if not grep_str:
                     self.handle.sendline("devices")
@@ -428,10 +443,9 @@
                     self.handle.expect("onos>")
                     self.handle.sendline("")
                     self.handle.expect("onos>")
-           
-            handle = self.handle.before
-            
-            return handle
+                handle = self.handle.before
+                print "handle =",handle
+                return handle
         except pexpect.EOF:
             main.log.error(self.name + ": EOF exception found")
             main.log.error(self.name + ":    " + self.handle.before)
@@ -464,6 +478,20 @@
                         str(grep_str)+"'")
                     self.handle.expect("links -j | grep '"+str(grep_str)+"'")
                     self.handle.expect("onos>")
+                handle = self.handle.before
+                '''
+                handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
+                To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
+                In json.loads(somestring), this somestring variable is a actually repr(somestring) and json.loads would fail with the escape sequence.
+                So we take off that escape sequence using 
+                ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
+                handle1 = ansi_escape.sub('', handle) 
+                '''
+                #print "repr(handle) =", repr(handle)
+                ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
+                handle1 = ansi_escape.sub('', handle)
+                #print "repr(handle1) = ", repr(handle1)
+                return handle1
             else:
                 if not grep_str:
                     self.handle.sendline("links")
@@ -476,10 +504,9 @@
                     self.handle.expect("onos>")
                     self.handle.sendline("")
                     self.handle.expect("onos>")
-           
-            handle = self.handle.before
-            
-            return handle
+                handle = self.handle.before
+                print "handle =",handle
+                return handle
         except pexpect.EOF:
             main.log.error(self.name + ": EOF exception found")
             main.log.error(self.name + ":    " + self.handle.before)
@@ -513,6 +540,21 @@
                         str(grep_str)+"'")
                     self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
                     self.handle.expect("onos>")
+                handle = self.handle.before
+                '''
+                handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
+                To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
+                In json.loads(somestring), this somestring variable is a actually repr(somestring) and json.loads would fail with the escape sequence.
+                So we take off that escape sequence using 
+                ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
+                handle1 = ansi_escape.sub('', handle) 
+                '''
+                #print "repr(handle) =", repr(handle)
+                ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
+                handle1 = ansi_escape.sub('', handle)
+                #print "repr(handle1) = ", repr(handle1)
+                return handle1
+
             else:
                 if not grep_str:
                     self.handle.sendline("ports")
@@ -525,10 +567,9 @@
                     self.handle.expect("onos>")
                     self.handle.sendline("")
                     self.handle.expect("onos>")
-           
-            handle = self.handle.before
-            
-            return handle
+                handle = self.handle.before
+                print "handle =",handle
+                return handle  
         except pexpect.EOF:
             main.log.error(self.name + ": EOF exception found")
             main.log.error(self.name + ":    " + self.handle.before)