Check link/switch/port status in topology comparison functions

    Comment out debug statements
    rename sum variable as it is a reserved keyword
diff --git a/TestON/drivers/common/cli/emulator/mininetclidriver.py b/TestON/drivers/common/cli/emulator/mininetclidriver.py
index 9875064..669fb0d 100644
--- a/TestON/drivers/common/cli/emulator/mininetclidriver.py
+++ b/TestON/drivers/common/cli/emulator/mininetclidriver.py
@@ -115,18 +115,18 @@
             if fanout is None:     #In tree topology, if fanout arg is not given, by default it is 2
                 fanout = 2
             k = 0
-            sum = 0
+            count = 0
             while(k <= depth-1): 
-                sum = sum + pow(fanout,k)
+                count = count + pow(fanout,k)
                 k = k+1
-                num_switches = sum
+                num_switches = count
             while(k <= depth-2): 
                 '''depth-2 gives you only core links and not considering edge links as seen by ONOS
                     If all the links including edge links are required, do depth-1
                 '''
-                sum = sum + pow(fanout,k)
+                count = count + pow(fanout,k)
                 k = k+1
-            num_links = sum * fanout
+            num_links = count * fanout
             #print "num_switches for %s(%d,%d) = %d and links=%d" %(topoType,depth,fanout,num_switches,num_links)
         
         elif topoType =='linear':
@@ -387,7 +387,7 @@
             return information dict about interfaces connected to the node
         '''
         if self.handle :
-            cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,isUp=%s" % (i.name, i.MAC(), i.IP(), i.isUp())'
+            cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s" % (i.name, i.MAC(), i.IP(), i.isUp())'
             cmd += ' for i in %s.intfs.values()])' % node
             try:
                 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
@@ -490,11 +490,11 @@
         '''
         Bring link(s) between two nodes up or down
         '''
-        main.log.info('Bring link(s) between two nodes up or down')
         args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
         end1 = args["END1"] if args["END1"] != None else ""
         end2 = args["END2"] if args["END2"] != None else ""
         option = args["OPTION"] if args["OPTION"] != None else ""
+        main.log.info("Bring link between '"+ end1 +"' and '" + end2 + "' '" + option + "'")
         command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
         try:
             #response = self.execute(cmd=command,prompt="mininet>",timeout=10)
@@ -716,7 +716,6 @@
             pattern = "flow_count=(\d+)"
             result = re.search(pattern, response, re.MULTILINE)
             if result is None:
-                print "no flow on switch print test"
                 main.log.info("Couldn't find flows on switch '', found: %s" % (switch, response))
                 return main.FALSE
             return result.group(1)
@@ -811,13 +810,10 @@
         #main.log.debug("Switches_json string: ", switches_json)
         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 "mn"
         #print json.dumps(output, sort_keys=True,indent=4,separators=(',', ': '))
@@ -830,6 +826,7 @@
         for switch in output['switches']:
             mnDPIDs.append(switch['dpid'])
         mnDPIDs.sort()
+        #print "List of Mininet switch DPID's"
         #print mnDPIDs
         if switches_json == "":#if rest call fails
             main.log.error(self.name + ".compare_switches(): Empty JSON object given from ONOS")
@@ -837,9 +834,13 @@
         onos=switches_json
         onosDPIDs=[]
         for switch in onos:
-            onosDPIDs.append(switch['id'].replace(":",'').replace("of",''))
-            #print switch
+            if switch['available'] == True:
+                onosDPIDs.append(switch['id'].replace(":",'').replace("of",''))
+            #else:
+                #print "Switch is unavailable:"
+                #print switch
         onosDPIDs.sort()
+        #print "List of ONOS switch DPID's"
         #print onosDPIDs
 
         if mnDPIDs!=onosDPIDs:
@@ -872,14 +873,18 @@
         port_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 "compare_ports 'output' variable:"
-        #print output
+                ports.append({
+                    'of_port': port.port_no,
+                    'mac': str(port.hw_addr).replace('\'',''),
+                    'name': port.name, 
+                    'enabled':port.enabled })
+            output['switches'].append({
+                "name": switch.name,
+                "dpid": str(switch.dpid).zfill(16),
+                "ports": ports })
 
 
         ################ports#############
@@ -887,29 +892,38 @@
             mn_ports = []
             onos_ports = []
             for port in switch['ports']:
-                mn_ports.append(port['of_port'])
+                if port['enabled'] == True:
+                    mn_ports.append(port['of_port'])
             for onos_switch in ports_json:
-                if onos_switch['device'].replace(':','').replace("of", '') == switch['dpid']:
-                    for port in onos_switch['ports']:
-                        onos_ports.append(int(port['port'])) 
+                #print "Iterating through a new switch as seen by ONOS"
+                #print onos_switch
+                if onos_switch['device']['available'] == True:
+                    if onos_switch['device']['id'].replace(':','').replace("of", '') == switch['dpid']:
+                        for port in onos_switch['ports']:
+                            if port['isEnabled']:
+                                #print "Iterating through available ports on the switch"
+                                #print port
+                                onos_ports.append(int(port['port'])) 
             mn_ports.sort(key=float)
             onos_ports.sort(key=float)
             #print "\nPorts for Switch %s:" % (switch['name'])
             #print "\tmn_ports[] = ", mn_ports
             #print "\tonos_ports[] = ", onos_ports
             
-            #if mn_ports == onos_ports:
-                #pass #don't set results to true here as this is just one of many checks and it might override a failure
-
-            #For OF1.3, the OFP_local port number is 0xfffffffe or 4294967294 instead of 0xfffe or 65534 in OF1.0, ONOS topology
-            #sees the correct port number, however MN topology as read from line 151 of https://github.com/ucb-sts/sts/blob/
-            #topology_refactoring2/sts/entities/teston_entities.py is 0xfffe which doesn't work correctly with OF1.3 switches.
-            #So a short term fix is to ignore the case when mn_port == 65534 and onos_port ==4294967294.
+            #NOTE:For OF1.3, the OFP_local port number is 0xfffffffe or 4294967294 instead of 0xfffe or 65534 in OF1.0,
+            #   ONOS topology sees the correct port number, however MN topology as read from line 151 of
+            #   https://github.com/ucb-sts/sts/blob/topology_refactoring2/sts/entities/teston_entities.py 
+            #   is 0xfffe which doesn't work correctly with OF1.3 switches.
             
-            #ONOS-Next is abstracting port numbers to 64bit unsigned number. So we will be converting the OF reserved ports to these numbers
+            #NOTE: ONOS is abstracting port numbers to 64bit unsigned number(long). So we will be converting the 
+            #   OF reserved ports to these numbers
+
+
             #TODO: handle other reserved port numbers besides LOCAL
             for mn_port,onos_port in zip(mn_ports,onos_ports):
-                if mn_port == onos_port or (mn_port == 65534 and onos_port ==int(uint64(-2))):
+                #print "mn == onos port?"
+                #print mn_port, onos_port
+                if mn_port == onos_port or (mn_port == 65534 and onos_port == long(uint64(-2))):
                     continue
                     #don't set results to true here as this is just one of many checks and it might override a failure
                 else:  #the ports of this switch don't match
@@ -939,7 +953,8 @@
         output = {"switches":[]}
         onos = links_json
         for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info
-            #print vars(switch)
+            # print "Iterating though switches as seen by Mininet"
+            # print switch
             ports = []
             for port in switch.ports.values():
                 #print port.hw_addr.toStr(separator = '')
@@ -947,19 +962,21 @@
             output['switches'].append({"name": switch.name, "dpid": str(switch.dpid).zfill(16), "ports": ports })
         #######Links########
 
-        if 2*len(topo.patch_panel.network_links) == len(onos):
+        mn_links = [link for link in topo.patch_panel.network_links if (link.port1.enabled and link.port2.enabled)]
+        #print "mn_links:"
+        #print mn_links
+        if 2*len(mn_links) == len(onos):
             link_results = main.TRUE
         else:
             link_results = main.FALSE
-            main.log.report("Mininet has %i bidirectional links and ONOS has %i unidirectional links" % (len(topo.patch_panel.network_links), len(onos) ))
+            main.log.report("Mininet has %i bidirectional links and ONOS has %i unidirectional links" % (len(mn_links), len(onos) ))
 
 
         # iterate through MN links and check if an ONOS link exists in both directions
         # NOTE: Will currently only show mn links as down if they are cut through STS. 
         #       We can either do everything through STS or wait for up_network_links 
         #       and down_network_links to be fully implemented.
-        for link in topo.patch_panel.network_links: 
-            #print "\n"
+        for link in mn_links: 
             #print "Link: %s" % link
             #TODO: Find a more efficient search method
             node1 = None
@@ -1050,6 +1067,26 @@
 
         return host_list 
 
+
+    def update(self):
+        '''
+        updates the port address and status information for each port in mn
+        '''
+        #TODO: Add error checking. currently the mininet command has no output
+        main.log.info("Updateing MN port information")
+        self.handle.sendline("")
+        self.handle.expect("mininet>")
+        
+        self.handle.sendline("update")
+        self.handle.expect("mininet>")
+
+        self.handle.sendline("")
+        self.handle.expect("mininet>")
+
+        return main.TRUE 
+
+        
+
 if __name__ != "__main__":
     import sys
     sys.modules[__name__] = MininetCliDriver()