Refactored SAMPstartTemplate initial commit

Change-Id: I399aa1077d47b1f73c33624c88ecd36419060eb4

Changed connect function in the driver

Change-Id: If222a66b4ef403a5bc7690ab60994709e4e8c123
diff --git a/TestON/drivers/common/cli/emulator/mininetclidriver.py b/TestON/drivers/common/cli/emulator/mininetclidriver.py
index bdc9307..b877d93 100644
--- a/TestON/drivers/common/cli/emulator/mininetclidriver.py
+++ b/TestON/drivers/common/cli/emulator/mininetclidriver.py
@@ -35,6 +35,7 @@
 import re
 import sys
 import types
+import os
 sys.path.append( "../" )
 from math import pow
 from drivers.common.cli.emulatordriver import Emulator
@@ -61,6 +62,22 @@
                 vars( self )[ key ] = connectargs[ key ]
 
             self.name = self.options[ 'name' ]
+
+            try:
+                if os.getenv( str( self.ip_address ) ) != None:
+                    self.ip_address = os.getenv( str( self.ip_address ) )
+                else:
+                    main.log.info( self.name +
+                                   ": Trying to connect to " +
+                                   self.ip_address )
+
+            except KeyError:
+                main.log.info( "Invalid host name," +
+                               " connecting to local host instead" )
+                self.ip_address = 'localhost'
+            except Exception as inst:
+                main.log.error( "Uncaught exception: " + str( inst ) )
+
             self.handle = super(
                 MininetCliDriver,
                 self ).connect(
diff --git a/TestON/drivers/common/cli/emulator/remotemininetdriver.py b/TestON/drivers/common/cli/emulator/remotemininetdriver.py
index c2c011a..cea3eab 100644
--- a/TestON/drivers/common/cli/emulator/remotemininetdriver.py
+++ b/TestON/drivers/common/cli/emulator/remotemininetdriver.py
@@ -24,6 +24,7 @@
 import pexpect
 import re
 import sys
+import os
 sys.path.append( "../" )
 from drivers.common.cli.emulatordriver import Emulator
 
@@ -51,6 +52,22 @@
             vars( self )[ key ] = connectargs[ key ]
 
         self.name = self.options[ 'name' ]
+
+        try:
+            if os.getenv( str( self.ip_address ) ) != None:
+                self.ip_address = os.getenv( str( self.ip_address ) )
+            else:
+                main.log.info( self.name +
+                               ": Trying to connect to " +
+                               self.ip_address )
+
+        except KeyError:
+            main.log.info( "Invalid host name," +
+                           " connecting to local host instead" )
+            self.ip_address = 'localhost'
+        except Exception as inst:
+            main.log.error( "Uncaught exception: " + str( inst ) )
+
         self.handle = super(
             RemoteMininetDriver,
             self ).connect(
diff --git a/TestON/drivers/common/cli/onosclidriver.py b/TestON/drivers/common/cli/onosclidriver.py
index fca8f22..870293d 100644
--- a/TestON/drivers/common/cli/onosclidriver.py
+++ b/TestON/drivers/common/cli/onosclidriver.py
@@ -22,6 +22,7 @@
 import json
 import types
 import time
+import os
 sys.path.append( "../" )
 from drivers.common.clidriver import CLI
 
@@ -52,7 +53,28 @@
             if self.home is None or self.home == "":
                 self.home = "~/onos"
 
+            for key in self.options:
+                if key == 'onosIp':
+                    self.onosIp = self.options[ 'onosIp' ]
+                    break
+
             self.name = self.options[ 'name' ]
+
+            try:
+                if os.getenv( str( self.ip_address ) ) != None:
+                    self.ip_address = os.getenv( str( self.ip_address ) )
+                else:
+                    main.log.info( self.name +
+                                   ": Trying to connect to " +
+                                   self.ip_address )
+
+            except KeyError:
+                main.log.info( "Invalid host name," +
+                               " connecting to local host instead" )
+                self.ip_address = 'localhost'
+            except Exception as inst:
+                main.log.error( "Uncaught exception: " + str( inst ) )
+
             self.handle = super( OnosCliDriver, self ).connect(
                 user_name=self.user_name,
                 ip_address=self.ip_address,
diff --git a/TestON/drivers/common/cli/onosdriver.py b/TestON/drivers/common/cli/onosdriver.py
index 4c786a3..cbe291b 100644
--- a/TestON/drivers/common/cli/onosdriver.py
+++ b/TestON/drivers/common/cli/onosdriver.py
@@ -19,6 +19,7 @@
 import sys
 import time
 import pexpect
+import os
 import os.path
 from requests.models import Response
 sys.path.append( "../" )
@@ -39,6 +40,10 @@
     def connect( self, **connectargs ):
         """
         Creates ssh handle for ONOS "bench".
+        NOTE:
+        The ip_address would come from the topo file using the host tag, the
+        value can be an environment variable as well as a "localhost" to get
+        the ip address needed to ssh to the "bench"
         """
         try:
             for key in connectargs:
@@ -52,6 +57,57 @@
                 self.home = "~/onos"
 
             self.name = self.options[ 'name' ]
+
+            for key in self.options:
+                if key == "nodes":
+                    # Maximum number of ONOS nodes to run
+                    self.maxNodes = int( self.options[ 'nodes' ] )
+                    break
+                self.maxNodes = None
+
+
+            # Grabs all OC environment variables
+            self.onosIps = {}  # Dictionary of all possible ONOS ip
+
+            try:
+                if self.maxNodes:
+                    main.log.info( self.name + ": Creating cluster data with " +
+                                   str( self.maxNodes ) + " maximum number" +
+                                   " of nodes" )
+
+                    for i in range( self.maxNodes ):
+                        envString = "OC" + str( i + 1 )
+                        self.onosIps[ envString ] = os.getenv( envString )
+
+                    if not self.onosIps:
+                        main.log.info( "Could not read any environment variable"
+                                       + " please load a cell file with all" +
+                                        " onos IP" )
+                    else:
+                        main.log.info( self.name + ": Found " +
+                                       str( self.onosIps.values() ) +
+                                       " ONOS IPs" )
+
+            except KeyError:
+                main.log.info( "Invalid environment variable" )
+            except Exception as inst:
+                main.log.error( "Uncaught exception: " + str( inst ) )
+
+            try:
+                if os.getenv( str( self.ip_address ) ) != None:
+                    self.ip_address = os.getenv( str( self.ip_address ) )
+                else:
+                    main.log.info( self.name +
+                                   ": Trying to connect to " +
+                                   self.ip_address )
+
+            except KeyError:
+                main.log.info( "Invalid host name," +
+                               " connecting to local host instead" )
+                self.ip_address = 'localhost'
+            except Exception as inst:
+                main.log.error( "Uncaught exception: " + str( inst ) )
+
             self.handle = super( OnosDriver, self ).connect(
                 user_name=self.user_name,
                 ip_address=self.ip_address,
@@ -61,11 +117,13 @@
 
             self.handle.sendline( "cd " + self.home )
             self.handle.expect( "\$" )
+
             if self.handle:
                 return self.handle
             else:
                 main.log.info( "NO ONOS HANDLE" )
                 return main.FALSE
+
         except pexpect.EOF:
             main.log.error( self.name + ": EOF exception found" )
             main.log.error( self.name + ":     " + self.handle.before )
@@ -587,7 +645,7 @@
             main.exit()
 
     def createCellFile( self, benchIp, fileName, mnIpAddrs,
-                        appString, *onosIpAddrs ):
+                        appString, onosIpAddrs ):
         """
         Creates a cell file based on arguments
         Required:
@@ -623,7 +681,7 @@
         tempCount = 1
 
         # Create ONOSNIC ip address prefix
-        tempOnosIp = onosIpAddrs[ 0 ]
+        tempOnosIp = str( onosIpAddrs[ 0 ] )
         tempList = []
         tempList = tempOnosIp.split( "." )
         # Omit last element of list to format for NIC
@@ -1979,102 +2037,14 @@
             main.cleanup()
             main.exit()
 
-    def getOnosIps(self):
+    def getOnosIps( self ):
+        """
+            Get all onos IPs stored in
+        """
 
-        import os
-        
-        # reads env for OC variables, also saves file with OC variables. If file and env conflict 
-        # priority goes to env. If file has OCs that are not in the env, the file OCs are used. 
-        # In other words, if the env is set, the test will use those values. 
+        return sorted( self.onosIps.values() )
 
-        # returns a list of ip addresses for the onos nodes, will work with up to 7 nodes + OCN and OCI
-        # returns in format [ OC1 ip, OC2 ...ect. , OCN, OCI ]
-
-        envONOSIps = {}
-
-        x = 1
-        while True:
-            try:
-                temp = os.environ[ 'OC' + str(x) ]
-            except KeyError: 
-                break
-            envONOSIps[ ("OC" + str(x)) ] = temp 
-            x += 1 
-
-        try: 
-            temp = os.environ[ 'OCN' ] 
-            envONOSIps[ "OCN" ] = temp
-        except KeyError: 
-            main.log.info("OCN not set in env")
-
-        try:
-            temp = os.environ[ 'OCI' ]
-            envONOSIps[ "OCI" ] = temp
-        except:
-            main.log.error("OCI not set in env")
-
-        print(str(envONOSIps))
-
-        order = [ "OC1", "OC2", "OC3","OC4","OC5","OC6","OC7","OCN","OCI" ]
-        ONOSIps = []
-
-        try: 
-            if os.path.exists("myIps"):
-                ipFile = open("myIps","r+")
-            else: 
-                ipFile = open("myIps","w+")
-
-            fileONOSIps = ipFile.readlines()
-            ipFile.close()
-
-            print str(fileONOSIps)
-            
-            if str(fileONOSIps) == "[]": 
-                ipFile = open("myIps","w+")
-                for key in envONOSIps:
-                    ipFile.write(key+ "=" + envONOSIps[key] + "\n")
-                ipFile.close()
-                for i in order: 
-                    if i in envONOSIps: 
-                        ONOSIps.append(envONOSIps[i])
-                
-                return ONOSIps 
-
-            else: 
-                fileDict = {}
-                for line in fileONOSIps: 
-                    line = line.replace("\n","")
-                    line = line.split("=") 
-                    key = line[0]
-                    value = line[1]
-                    fileDict[key] = value 
-
-                for x in envONOSIps: 
-                    if x in fileDict: 
-                        if envONOSIps[x] == fileDict[x]: 
-                            continue
-                        else: 
-                            fileDict[x] = envONOSIps[x]
-                    else: 
-                        fileDict[x] = envONOSIps[x]
-
-                ipFile = open("myIps","w+")
-                for key in order: 
-                    if key in fileDict: 
-                        ipFile.write(key + "=" + fileDict[key] + "\n")
-                        ONOSIps.append(fileDict[key]) 
-                ipFile.close()
-
-                return ONOSIps 
-
-        except IOError as a:
-            main.log.error(a) 
-
-        except Exception as a:
-            main.log.error(a) 
-
-
-    def logReport(self, nodeIp, searchTerms, outputMode="s"):
+    def logReport( self, nodeIp, searchTerms, outputMode="s" ):
         '''
             - accepts either a list or a string for "searchTerms" these
               terms will be searched for in the log and have their