Minor fixes after the big refactor

Make sure modified config files are cleaned up
Fix EOF exception when closing ssh connections
Fix wiki formating issues when printing nodes

Change-Id: Idd82e1cb191b53f13720cc884757fa6b8ef4cb18
diff --git a/TestON/drivers/common/clidriver.py b/TestON/drivers/common/clidriver.py
index 6f26cfc..55f05ba 100644
--- a/TestON/drivers/common/clidriver.py
+++ b/TestON/drivers/common/clidriver.py
@@ -438,11 +438,16 @@
         return handle
 
     def exitFromSsh( self, handle, ipAddress ):
-        handle.sendline( "logout" )
         try:
+            handle.sendline( "logout" )
             handle.expect( "closed." )
             main.log.info ( "Successfully closed ssh connection from " + ipAddress )
         except pexpect.EOF:
             main.log.error( "Failed to close the connection from " + ipAddress )
-        handle.sendline( "" )
-        handle.expect( self.prompt )
\ No newline at end of file
+        try:
+            # check that this component handle still works
+            self.handle.sendline( "" )
+            self.handle.expect( self.prompt )
+        except pexpect.EOF:
+            main.log.error( self.handle.before )
+            main.log.error( "EOF after closing ssh connection" )
diff --git a/TestON/tests/HA/HAkillNodes/HAkillNodes.py b/TestON/tests/HA/HAkillNodes/HAkillNodes.py
index 63e34cb..4a95206 100644
--- a/TestON/tests/HA/HAkillNodes/HAkillNodes.py
+++ b/TestON/tests/HA/HAkillNodes/HAkillNodes.py
@@ -90,15 +90,14 @@
         main.testSetUp.evnSetupConclusion( stepResult )
         main.HA.generateGraph( "HAkillNodes" )
 
-        main.step( "Make sure ONOS service doesn't automatically respawn" )
-        main.ONOSbench.preventAutoRespawn()
-
         main.testSetUp.ONOSSetUp( main.Mininet1, main.Cluster, cellName=cellName, removeLog=True,
-                                 extraApply=[ main.HA.startingMininet,
-                                              main.HA.customizeOnosGenPartitions ],
-                                 extraClean=main.HA.cleanUpGenPartition )
+                                  extraApply=[ main.HA.startingMininet,
+                                               main.HA.customizeOnosGenPartitions,
+                                               main.HA.copyBackupConfig,
+                                               main.ONOSbench.preventAutoRespawn ],
+                                  extraClean= main.HA.cleanUpGenPartition )
 
-        main.HA.initialSetUp()
+        main.HA.initialSetUp( serviceClean=True )
 
     def CASE2( self, main ):
         """
@@ -150,7 +149,9 @@
             main.kill.append( main.Cluster.runningNodes[ p - 1 ] )
             # NOTE: This only works for cluster sizes of 3,5, or 7.
 
-        main.step( "Killing nodes: " + str( main.kill ) )
+        #NOTE: This is to fix an issue with wiki formating
+        nodeNames = [ node.name for node in main.kill ]
+        main.step( "Killing nodes: " + str( nodeNames ) )
         killResults = main.TRUE
         for ctrl in main.kill:
             killResults = killResults and\
diff --git a/TestON/tests/HA/HAscaling/HAscaling.py b/TestON/tests/HA/HAscaling/HAscaling.py
index cecd09e..f986cc2 100644
--- a/TestON/tests/HA/HAscaling/HAscaling.py
+++ b/TestON/tests/HA/HAscaling/HAscaling.py
@@ -79,9 +79,8 @@
             from tests.dependencies.ONOSSetup import ONOSSetup
             main.testSetUp = ONOSSetup()
         except ImportError:
-            main.log.error( "ONOSSetup not found exiting the test" )
+            main.log.error( "ONOSSetup not found. exiting the test" )
             main.cleanAndExit()
-
         main.testSetUp.envSetupDescription()
         try:
             from tests.HA.dependencies.HA import HA
@@ -99,14 +98,15 @@
         main.HA.generateGraph( "HAscaling", index=1 )
 
         main.testSetUp.ONOSSetUp( main.Mininet1, main.Cluster, cellName=cellName, removeLog=True,
-                                 extraApply=[ main.HA.setServerForCluster,
-                                              main.HA.scalingMetadata,
-                                              main.HA.startingMininet,
-                                              main.HA.copyingBackupConfig ],
-                                 extraClean=main.HA.cleanUpOnosService,
-                                 installMax=True )
+                                  extraApply=[ main.HA.setServerForCluster,
+                                               main.HA.scalingMetadata,
+                                               main.HA.startingMininet,
+                                               main.HA.copyBackupConfig,
+                                               main.HA.setMetadataUrl ],
+                                  extraClean=main.HA.cleanUpOnosService,
+                                  installMax=True )
 
-        main.HA.initialSetUp( True )
+        main.HA.initialSetUp( serviceClean=True )
 
     def CASE2( self, main ):
         """
diff --git a/TestON/tests/HA/HAsingleInstanceRestart/HAsingleInstanceRestart.py b/TestON/tests/HA/HAsingleInstanceRestart/HAsingleInstanceRestart.py
index 53cccb3..eeb4310 100644
--- a/TestON/tests/HA/HAsingleInstanceRestart/HAsingleInstanceRestart.py
+++ b/TestON/tests/HA/HAsingleInstanceRestart/HAsingleInstanceRestart.py
@@ -105,10 +105,10 @@
                                   extraApply=[ main.testSetUp.createApplyCell,
                                                main.HA.startingMininet,
                                                main.testSetUp.createApplyCell ],
-                                  arg=[ [ main.Cluster, True, cellName, main.Mininet1, True, ip ],
-                                        None,
-                                        [ main.Cluster, True, "SingleHA", main.Mininet1,
-                                        True, main.Cluster.runningNodes[ 0 ].ipAddress ] ] )
+                                  applyArgs=[ [ main.Cluster, True, cellName, main.Mininet1, True, ip ],
+                                              None,
+                                              [ main.Cluster, True, "SingleHA", main.Mininet1,
+                                                True, main.Cluster.runningNodes[ 0 ].ipAddress ] ] )
 
         main.HA.initialSetUp()
 
diff --git a/TestON/tests/HA/HAstopNodes/HAstopNodes.py b/TestON/tests/HA/HAstopNodes/HAstopNodes.py
index 2c4f059..e7bdcd4 100644
--- a/TestON/tests/HA/HAstopNodes/HAstopNodes.py
+++ b/TestON/tests/HA/HAstopNodes/HAstopNodes.py
@@ -92,10 +92,11 @@
 
         main.testSetUp.ONOSSetUp( main.Mininet1, main.Cluster, cellName=cellName, removeLog=True,
                                   extraApply=[ main.HA.startingMininet,
-                                               main.HA.customizeOnosGenPartitions ],
-                                  extraClean=main.HA.cleanUpGenPartition )
+                                               main.HA.customizeOnosGenPartitions,
+                                               main.HA.copyBackupConfig ],
+                                  extraClean= main.HA.cleanUpGenPartition )
 
-        main.HA.initialSetUp()
+        main.HA.initialSetUp( serviceClean=True )
 
     def CASE2( self, main ):
         """
@@ -147,7 +148,9 @@
             main.kill.append( main.Cluster.runningNodes[ p - 1 ] )
             # NOTE: This only works for cluster sizes of 3,5, or 7.
 
-        main.step( "Stopping nodes: " + str( main.kill ) )
+        #NOTE: This is to fix an issue with wiki formating
+        nodeNames = [ node.name for node in main.kill ]
+        main.step( "Stopping nodes: " + str( nodeNames ) )
         killResults = main.TRUE
         for ctrl in main.kill:
             killResults = killResults and\
diff --git a/TestON/tests/HA/HAswapNodes/HAswapNodes.py b/TestON/tests/HA/HAswapNodes/HAswapNodes.py
index 2de9f87..1fa8106 100644
--- a/TestON/tests/HA/HAswapNodes/HAswapNodes.py
+++ b/TestON/tests/HA/HAswapNodes/HAswapNodes.py
@@ -101,7 +101,8 @@
                                  extraApply=[ main.HA.setServerForCluster,
                                               main.HA.swapNodeMetadata,
                                               main.HA.startingMininet,
-                                              main.HA.copyingBackupConfig ],
+                                              main.HA.copyBackupConfig,
+                                              main.HA.setMetadataUrl ],
                                  extraClean=main.HA.cleanUpOnosService,
                                  installMax=True )
         main.HA.initialSetUp()
diff --git a/TestON/tests/HA/dependencies/HA.py b/TestON/tests/HA/dependencies/HA.py
index 3d18eaa..f1ddaef 100644
--- a/TestON/tests/HA/dependencies/HA.py
+++ b/TestON/tests/HA/dependencies/HA.py
@@ -112,7 +112,7 @@
                                  onpass="Server started",
                                  onfail="Failled to start SimpleHTTPServer" )
 
-    def copyingBackupConfig( self ):
+    def copyBackupConfig( self ):
         main.step( "Copying backup config files" )
         main.onosServicepath = main.ONOSbench.home + "/tools/package/bin/onos-service"
         cp = main.ONOSbench.scp( main.ONOSbench,
@@ -124,6 +124,9 @@
                                  actual=cp,
                                  onpass="Copy backup config file succeeded",
                                  onfail="Copy backup config file failed" )
+
+    def setMetadataUrl( self ):
+        # NOTE: You should probably backup the config before and reset the config after the test
         # we need to modify the onos-service file to use remote metadata file
         # url for cluster metadata file
         iface = main.params[ 'server' ].get( 'interface' )
diff --git a/TestON/tests/dependencies/ONOSSetup.py b/TestON/tests/dependencies/ONOSSetup.py
index 4cfd694..8a73e32 100644
--- a/TestON/tests/dependencies/ONOSSetup.py
+++ b/TestON/tests/dependencies/ONOSSetup.py
@@ -18,15 +18,15 @@
     You should have received a copy of the GNU General Public License
     along with TestON.  If not, see <http://www.gnu.org/licenses/>.
 """
-import time
 import re
-import imp
 
 class ONOSSetup:
     main = None
+
     def __init__( self ):
         self.default = ''
-    def envSetupDescription ( self ):
+
+    def envSetupDescription( self ):
         """
         Introduction part of the test. It will initialize some basic vairables.
         """
@@ -60,7 +60,8 @@
                                      actual=stepResult,
                                      onpass="Successfully checkout onos branch.",
                                      onfail="Failed to checkout onos branch. Exiting test..." )
-            if not stepResult: main.cleanAndExit()
+            if not stepResult:
+                main.cleanAndExit()
 
             main.step( "Git Pull on ONOS branch:" + gitBranch )
             stepResult = main.ONOSbench.gitPull()
@@ -68,7 +69,8 @@
                                      actual=stepResult,
                                      onpass="Successfully pull onos. ",
                                      onfail="Failed to pull onos. Exiting test ..." )
-            if not stepResult: main.cleanAndExit()
+            if not stepResult:
+                main.cleanAndExit()
 
         else:
             main.log.info( "Skipped git checkout and pull as they are disabled in params file" )
@@ -303,9 +305,19 @@
                                  onfail="Failed to start ONOS cli" )
         return startCliResult
 
+    def processList( self, functions, args ):
+        if functions is not None:
+            if isinstance( functions, list ):
+                i = 0
+                for f in functions:
+                    f( *( args[ i ] ) ) if args is not None and args[ i ] is not None else f()
+                    i += 1
+            else:
+                functions( *args ) if args is not None else functions()
+
     def ONOSSetUp( self, Mininet, cluster, hasMultiNodeRounds=False, startOnos=True, newCell=True,
-                   cellName="temp", removeLog=False, extraApply=None, arg=None, extraClean=None,
-                   skipPack=False, installMax=False, useSSH=True, killRemoveMax=True,
+                   cellName="temp", removeLog=False, extraApply=None, applyArgs=None, extraClean=None,
+                   cleanArgs=None, skipPack=False, installMax=False, useSSH=True, killRemoveMax=True,
                    stopOnos=False, installParallel=True ):
         """
         Description:
@@ -330,9 +342,10 @@
             * newCell - True for making a new cell and False for not making it.
             * cellName - Name of the cell that will be used.
             * removeLog - True if wish to remove raft logs
-            * extraApply - Function(s) that will be applied. Default to None.
-            * arg - argument of the functon(s) of the extraApply. Should be in list.
-            * extraClean - extra Clean up process. Function(s) will be passed.
+            * extraApply - Function(s) that will be called before building ONOS. Default to None.
+            * applyArgs - argument of the functon(s) of the extraApply. Should be in list.
+            * extraClean - Function(s) that will be called after building ONOS. Defaults to None.
+            * cleanArgs - argument of the functon(s) of the extraClean. Should be in list.
             * skipPack - True if wish to skip some packing.
             * installMax - True if wish to install onos max number of nodes
             False if wish to install onos of running nodes only
@@ -346,7 +359,7 @@
         self.setNumCtrls( hasMultiNodeRounds )
 
         main.case( "Starting up " + str( cluster.numCtrls ) +
-                  " node(s) ONOS cluster" )
+                   " node(s) ONOS cluster" )
         main.caseExplanation = "Set up ONOS with " + str( cluster.numCtrls ) + \
                                " node(s) ONOS cluster"
         killResult = self.killingAllOnos( cluster, killRemoveMax, stopOnos )
@@ -369,24 +382,12 @@
                 main.ONOSbench.onosRemoveRaftLogs()
 
             onosUninstallResult = self.uninstallOnos( cluster, killRemoveMax )
-
-            if extraApply is not None:
-                if isinstance( extraApply, list ):
-                    i = 0
-                    for apply in extraApply:
-                        apply( *(arg[ i ]) ) if arg is not None \
-                                                            and arg[ i ] is not None else apply()
-                        i += 1
-                else:
-                    extraApply( *arg ) if arg is not None else extraApply()
-
-
+            self.processList( extraApply, applyArgs )
             packageResult = self.buildOnos( cluster )
 
         onosInstallResult = self.installOnos( cluster, installMax, installParallel )
 
-        if extraClean is not None:
-            extraClean()
+        self.processList( extraClean, cleanArgs )
         secureSshResult = self.setupSsh( cluster )
 
         onosServiceResult = self.checkOnosService( cluster )
@@ -395,4 +396,4 @@
             onosCliResult = self.startOnosClis( cluster )
 
         return killResult and cellResult and packageResult and onosUninstallResult and \
-               onosInstallResult and secureSshResult and onosServiceResult and onosCliResult
\ No newline at end of file
+               onosInstallResult and secureSshResult and onosServiceResult and onosCliResult