Merge pull request #110 from opennetworkinglab/devl/startNet

Devl/start net
diff --git a/TestON/core/teston.py b/TestON/core/teston.py
index 3210f9a..961e824 100644
--- a/TestON/core/teston.py
+++ b/TestON/core/teston.py
@@ -248,6 +248,7 @@
                 self.stepCount = self.stepCount + 1
                 self.log.exception(e)
                 self.cleanup()
+                self.exit()
             return main.TRUE
         
         if cli.stop:
@@ -290,18 +291,18 @@
 
 
         #utilities.send_mail()
-        try :
-            for component in self.componentDictionary.keys():
+        for component in self.componentDictionary.keys():
+            try :
                 tempObject  = vars(self)[component]
-                print "Disconnecting " + str(tempObject)
+                print "Disconnecting from " + str(tempObject.name) + ": " + \
+                      str(tempObject)
                 tempObject.disconnect()
             #tempObject.execute(cmd="exit",prompt="(.*)",timeout=120)
 
-        except(Exception):
-            self.log.exception( "Exception while disconnecting from " +
-                                 str( component ) )
-            #print " There is an error with closing hanldes"
-            result = self.FALSE
+            except (Exception):
+                self.log.exception( "Exception while disconnecting from " +
+                                     str( component ) )
+                result = self.FALSE
         # Closing all the driver's session files
         for driver in self.componentDictionary.keys():
            vars(self)[driver].close_log_handles()
diff --git a/TestON/drivers/common/cli/emulator/mininetclidriver.py b/TestON/drivers/common/cli/emulator/mininetclidriver.py
index 2868856..2d7c0a5 100644
--- a/TestON/drivers/common/cli/emulator/mininetclidriver.py
+++ b/TestON/drivers/common/cli/emulator/mininetclidriver.py
@@ -103,9 +103,9 @@
                 main.FALSE otherwise
         """
         if self.handle:
-            main.log.info(
-                self.name +
-                ": Clearing any residual state or processes" )
+            # make sure old networks are cleaned up
+            main.log.info( self.name +
+                           ": Clearing any residual state or processes" )
             self.handle.sendline( "sudo mn -c" )
             i = self.handle.expect( [ 'password\sfor\s',
                                       'Cleanup\scomplete',
@@ -113,6 +113,7 @@
                                       pexpect.TIMEOUT ],
                                     timeout )
             if i == 0:
+                # Sudo asking for password
                 main.log.info( self.name + ": Sending sudo password" )
                 self.handle.sendline( self.pwd )
                 i = self.handle.expect( [ '%s:' % self.user,
@@ -125,86 +126,75 @@
             elif i == 2:
                 main.log.error( self.name + ": Connection terminated" )
             elif i == 3:  # timeout
-                main.log.error(
-                    self.name +
-                    ": Something while cleaning MN took too long... " )
-            if topoFile == '' and args == '':
-                main.log.info( self.name + ": building fresh mininet" )
-                # for reactive/PARP enabled tests
-                cmdString = "sudo mn " + self.options[ 'arg1' ] +\
-                    " " + self.options[ 'arg2' ] +\
-                    " --mac --controller " +\
-                    self.options[ 'controller' ] + " " +\
-                    self.options[ 'arg3' ]
-
-                argList = self.options[ 'arg1' ].split( "," )
-                global topoArgList
-                topoArgList = argList[ 0 ].split( " " )
-                argList = map( int, argList[ 1: ] )
-                topoArgList = topoArgList[ 1: ] + argList
-
-                self.handle.sendline( cmdString )
-                self.handle.expect( [ "sudo mn",
-                                    pexpect.EOF,
-                                    pexpect.TIMEOUT ] )
-                while True:
-                    i = self.handle.expect( [ 'mininet>',
-                                              '\*\*\*',
-                                              'Exception',
-                                              pexpect.EOF,
-                                              pexpect.TIMEOUT ],
-                                            timeout )
-                    if i == 0:
-                        main.log.info( self.name + ": mininet built" )
-                        return main.TRUE
-                    if i == 1:
-                        self.handle.expect(
-                            [ "\n", pexpect.EOF, pexpect.TIMEOUT ] )
-                        main.log.info( self.handle.before )
-                    elif i == 2:
-                        main.log.error(
-                            self.name +
-                            ": Launching mininet failed..." )
-                        return main.FALSE
-                    elif i == 3:
-                        main.log.error( self.name + ": Connection timeout" )
-                        return main.FALSE
-                    elif i == 4:  # timeout
-                        main.log.error(
-                            self.name +
-                            ": Something took too long... " )
-                        return main.FALSE
-                return main.TRUE
-            else:
-                main.log.info( "Starting topo file " + topoFile )
+                main.log.error( self.name + ": Something while cleaning " +
+                                "Mininet took too long... " )
+            # Craft the string to start mininet
+            cmdString = "sudo "
+            if topoFile is None or topoFile == '':  # If no file is given
+                main.log.info( self.name + ": building fresh Mininet" )
+                cmdString += "mn "
+                if args is None or args == '':
+                    # If no args given, use args from .topo file
+                    args = self.options[ 'arg1' ] +\
+                                 " " + self.options[ 'arg2' ] +\
+                                 " --mac --controller " +\
+                                 self.options[ 'controller' ] + " " +\
+                                 self.options[ 'arg3' ]
+                else:  # else only use given args
+                    pass
+                    # TODO: allow use of topo args and method args?
+            else:  # Use given topology file
+                main.log.info( "Starting Mininet from topo file " + topoFile )
+                cmdString += topoFile + " "
                 if args is None:
                     args = ''
-                else:
-                    main.log.info( "args = " + args)
-                self.handle.sendline( 'sudo ' + topoFile + ' ' + args)
+                    # TODO: allow use of args from .topo file?
+            cmdString += args
+            # Send the command and check if network started
+            self.handle.sendline( "" )
+            self.handle.expect( '\$' )
+            main.log.info( "Sending '" + cmdString + "' to " + self.name )
+            self.handle.sendline( cmdString )
+            while True:
                 i = self.handle.expect( [ 'mininet>',
+                                          'Exception',
+                                          '\*\*\*',
                                           pexpect.EOF,
                                           pexpect.TIMEOUT ],
-                                        timeout)
+                                        timeout )
                 if i == 0:
-                    main.log.info(self.name + ": Network started")
+                    main.log.info( self.name + ": Mininet built" )
                     return main.TRUE
                 elif i == 1:
+                    response = str( self.handle.before +
+                                    self.handle.after )
+                    self.handle.expect( '\$' )
+                    response += str( self.handle.before +
+                                    self.handle.after )
+                    main.log.error(
+                        self.name +
+                        ": Launching Mininet failed: " + response )
+                    return main.FALSE
+                elif i == 2:
+                    self.handle.expect( [ "\n",
+                                          pexpect.EOF,
+                                          pexpect.TIMEOUT ],
+                                        timeout )
+                    main.log.info( self.handle.before )
+                elif i == 3:
                     main.log.error( self.name + ": Connection timeout" )
                     return main.FALSE
-                elif i == 2:  # timeout
+                elif i == 4:  # timeout
                     main.log.error(
                         self.name +
                         ": Something took too long... " )
                     return main.FALSE
-                return main.TRUE
+            # Why did we hit this part?
+            main.log.error( "startNet did not return correctly" )
+            return main.FASLE
         else:  # if no handle
-            main.log.error(
-                self.name +
-                ": Connection failed to the host " +
-                self.user_name +
-                "@" +
-                self.ip_address )
+            main.log.error( self.name + ": Connection failed to the host " +
+                            self.user_name + "@" + self.ip_address )
             main.log.error( self.name + ": Failed to connect to the Mininet" )
             return main.FALSE
 
@@ -245,7 +235,15 @@
         return topoDict
 
     def calculateSwAndLinks( self ):
-        topoDict = self.numSwitchesN_links( *topoArgList )
+        """
+            Calculate the number of switches and links in a topo."""
+        # TODO: combine this function and numSwitchesNlinks
+        argList = self.options[ 'arg1' ].split( "," )
+        topoArgList = argList[ 0 ].split( " " )
+        argList = map( int, argList[ 1: ] )
+        topoArgList = topoArgList[ 1: ] + argList
+
+        topoDict = self.numSwitchesNlinks( *topoArgList )
         return topoDict
 
     def pingall( self, timeout=300, shortCircuit=False, acceptableFailed=0):
diff --git a/TestON/drivers/common/cli/onosclidriver.py b/TestON/drivers/common/cli/onosclidriver.py
index 2bd8480..d815e4c 100644
--- a/TestON/drivers/common/cli/onosclidriver.py
+++ b/TestON/drivers/common/cli/onosclidriver.py
@@ -100,7 +100,7 @@
             main.log.error( self.name + ": EOF exception found" )
             main.log.error( self.name + ":     " + self.handle.before )
         except ValueError:
-            main.log.exception( "Exception in discconect of " + self.name )
+            main.log.exception( "Exception in disconnect of " + self.name )
             response = main.TRUE
         except Exception:
             main.log.exception( self.name + ": Connection failed to the host" )
@@ -321,7 +321,7 @@
                 print response
                 try:
                     print self.handle.after
-                except:
+                except TypeError:
                     pass
             # TODO: do something with i
             main.log.info( "Command '" + str( cmdStr ) + "' sent to "
@@ -2389,7 +2389,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -2417,7 +2417,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -2456,7 +2456,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -2497,7 +2497,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -2539,7 +2539,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -2605,7 +2605,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -2659,7 +2659,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -2711,7 +2711,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -2774,7 +2774,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -2809,7 +2809,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -2883,7 +2883,7 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
diff --git a/TestON/drivers/common/cli/onosdriver.py b/TestON/drivers/common/cli/onosdriver.py
index 27c2d92..ee3999a 100644
--- a/TestON/drivers/common/cli/onosdriver.py
+++ b/TestON/drivers/common/cli/onosdriver.py
@@ -90,7 +90,7 @@
             main.log.error( self.name + ": EOF exception found" )
             main.log.error( self.name + ":     " + self.handle.before )
         except ValueError:
-            main.log.exception( "Exception in discconect of " + self.name )
+            main.log.exception( "Exception in disconnect of " + self.name )
             response = main.TRUE
         except Exception:
             main.log.exception( self.name + ": Connection failed to the host" )
@@ -1819,7 +1819,7 @@
         except TypeError:
             main.log.exception( self.name + ": Object not as expected" )
             return None
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -1853,8 +1853,7 @@
         
         except AssertionError:
             main.log.error("Incorrect Config settings: " + verification)
-
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
@@ -1888,12 +1887,10 @@
             main.log.error( self.name + ":    " + self.handle.before )
             main.cleanup()
             main.exit()
-        
         except AssertionError:
             main.log.info("Settings did not post to ONOS")
             main.log.error(varification)            
-
-        except:
+        except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.log.error(varification)
             main.cleanup()
diff --git a/TestON/drivers/common/cli/quaggaclidriver.py b/TestON/drivers/common/cli/quaggaclidriver.py
index bcd83f6..1c63206 100644
--- a/TestON/drivers/common/cli/quaggaclidriver.py
+++ b/TestON/drivers/common/cli/quaggaclidriver.py
@@ -606,7 +606,7 @@
             self.handle.sendline( "" )
             # self.handle.expect( "config-router" )
             self.handle.expect( "config-router", timeout=5 )
-        except:
+        except Exception:
             main.log.warn( "Probably not in config-router mode!" )
             self.disconnect()
         main.log.info( "Start to disable peer" )
@@ -615,7 +615,7 @@
         try:
             self.handle.sendline( cmd )
             self.handle.expect( "bgpd", timeout=5 )
-        except:
+        except Exception:
             main.log.warn( "Failed to disable peer" )
             self.disconnect()
 
@@ -626,7 +626,7 @@
             self.handle.sendline( "" )
             # self.handle.expect( "config-router" )
             self.handle.expect( "config-router", timeout=5 )
-        except:
+        except Exception:
             main.log.warn( "Probably not in config-router mode!" )
             self.disconnect()
         main.log.info( "Start to disable peer" )
@@ -635,7 +635,7 @@
         try:
             self.handle.sendline( cmd )
             self.handle.expect( "bgpd", timeout=5 )
-        except:
+        except Exception:
             main.log.warn( "Failed to enable peer" )
             self.disconnect()