Merge pull request #70 from pingping-lin/master

auto pull onos & add getBranchName driver & update test case & code style change
diff --git a/TestON/drivers/common/cli/onosdriver.py b/TestON/drivers/common/cli/onosdriver.py
index a78f4f2..d6c0a40 100644
--- a/TestON/drivers/common/cli/onosdriver.py
+++ b/TestON/drivers/common/cli/onosdriver.py
@@ -20,6 +20,7 @@
 import time
 import pexpect
 import os.path
+from requests.models import Response
 sys.path.append( "../" )
 from drivers.common.clidriver import CLI
 
@@ -483,6 +484,22 @@
             main.cleanup()
             main.exit()
 
+    def getBranchName( self ):
+        self.handle.sendline( "cd " + self.home )
+        self.handle.expect( "ONOS\$" )
+        self.handle.sendline( "git name-rev --name-only HEAD" )
+        self.handle.expect( "git name-rev --name-only HEAD" )
+        self.handle.expect( "\$" )
+
+        lines =  self.handle.before.splitlines()
+        if lines[1] == "master":
+            return "master"
+        elif lines[1] == "onos-1.0":
+            return "onos-1.0"
+        else:
+            main.log.info( lines[1] )
+            return "unexpected ONOS branch for SDN-IP test"
+
     def getVersion( self, report=False ):
         """
         Writes the COMMIT number to the report to be parsed
diff --git a/TestON/drivers/common/cli/quaggaclidriver.py b/TestON/drivers/common/cli/quaggaclidriver.py
index 1c63206..f391e3a 100644
--- a/TestON/drivers/common/cli/quaggaclidriver.py
+++ b/TestON/drivers/common/cli/quaggaclidriver.py
@@ -40,9 +40,9 @@
                 ip_address="1.1.1.1",
                 port=self.port,
                 pwd=self.pwd )
-        main.log.info( "connect parameters:" + str( self.user_name ) + ";"
-                       + str( self.ip_address ) + ";" + str( self.port )
-                       + ";" + str(self.pwd ) )
+        #main.log.info( "connect parameters:" + str( self.user_name ) + ";"
+        #               + str( self.ip_address ) + ";" + str( self.port )
+        #               + ";" + str(self.pwd ) )
 
         if self.handle:
             # self.handle.expect( "",timeout=10 )
@@ -186,7 +186,7 @@
         return intents
 
     # This method extracts all actual routes from ONOS CLI
-    def extractActualRoutes( self, getRoutesResult ):
+    def extractActualRoutesOneDotZero( self, getRoutesResult ):
         routesJsonObj = json.loads( getRoutesResult )
 
         allRoutesActual = []
@@ -199,6 +199,18 @@
 
         return sorted( allRoutesActual )
 
+    def extractActualRoutesMaster( self, getRoutesResult ):
+        routesJsonObj = json.loads( getRoutesResult )
+
+        allRoutesActual = []
+        for route in routesJsonObj['routes4']:
+            if route[ 'prefix' ] == '172.16.10.0/24':
+                continue
+            allRoutesActual.append(
+                route[ 'prefix' ] + "/" + route[ 'nextHop' ] )
+
+        return sorted( allRoutesActual )
+
     # This method extracts all actual route intents from ONOS CLI
     def extractActualRouteIntents( self, getIntentsResult ):
         intents = []
@@ -489,7 +501,7 @@
         if routesAdded == numRoutes:
             return main.TRUE
         return main.FALSE
-    
+
     # Please use deleteRoutes method instead of this one!
     def delRoute( self, net, numRoutes, routeRate ):
         try:
@@ -562,7 +574,7 @@
             child.expect( "Flow table show" )
             count = 0
             while True:
-                i = child.expect( [ '17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}', 
+                i = child.expect( [ '17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}',
                                    'CLI#', pexpect.TIMEOUT ] )
                 if i == 0:
                     count = count + 1
diff --git a/TestON/tests/SdnIpTest/SdnIpTest.params b/TestON/tests/SdnIpTest/SdnIpTest.params
index 7f7ef2d..e47adf6 100755
--- a/TestON/tests/SdnIpTest/SdnIpTest.params
+++ b/TestON/tests/SdnIpTest/SdnIpTest.params
@@ -1,6 +1,6 @@
 <PARAMS>
     
-    <testcases>4</testcases>
+    <testcases>100, [4]*5</testcases>
 
     #Environment variables
     <ENV>
@@ -8,13 +8,15 @@
     </ENV>
 
     <CTRL>
+    	<numCtrl>1</numCtrl>
         <ip1>10.128.4.52</ip1>
         <port1>6633</port1>
     </CTRL>
 
     <GIT>
-        <autoPull>off</autoPull>
-        <checkout>master</checkout>
+        <autoPull>on</autoPull>
+        <branch1>master</branch1>
+        <branch2>onos-1.0</branch2>
     </GIT>
 
     <JSON>
diff --git a/TestON/tests/SdnIpTest/SdnIpTest.py b/TestON/tests/SdnIpTest/SdnIpTest.py
index 85ac21e..ebbd57e 100644
--- a/TestON/tests/SdnIpTest/SdnIpTest.py
+++ b/TestON/tests/SdnIpTest/SdnIpTest.py
@@ -1,22 +1,86 @@
-# from cupshelpers.config import prefix
 
-# Testing the basic functionality of SDN-IP
-
-
+# Testing the functionality of SDN-IP with single ONOS instance
 class SdnIpTest:
 
     def __init__( self ):
         self.default = ''
+        global branchName
 
-# from cupshelpers.config import prefix
+    # This case is to setup ONOS
+    def CASE100( self, main ):
+        """
+           CASE100 is to compile ONOS and push it to the test machines
+           Startup sequence:
+           git pull
+           mvn clean install
+           onos-package
+           cell <name>
+           onos-verify-cell
+           onos-install -f
+           onos-wait-for-start
+        """
+        main.case( "Setting up test environment" )
 
-# Testing the basic functionality of SDN-IP
+        cellName = main.params[ 'ENV' ][ 'cellName' ]
+        ONOS1Ip = main.params[ 'CTRL' ][ 'ip1' ]
 
+        main.step( "Applying cell variable to environment" )
+        cellResult = main.ONOSbench.setCell( cellName )
+        verifyResult = main.ONOSbench.verifyCell()
 
-class SdnIpTest:
+        branchName = main.ONOSbench.getBranchName()
+        main.log.info( "ONOS is on branch: " + branchName )
 
-    def __init__( self ):
-        self.default = ''
+        main.log.report( "Uninstalling ONOS" )
+        main.ONOSbench.onosUninstall( ONOS1Ip )
+
+        cleanInstallResult = main.TRUE
+        gitPullResult = main.TRUE
+
+        main.step( "Git pull" )
+        gitPullResult = main.ONOSbench.gitPull()
+
+        main.step( "Using mvn clean & install" )
+        cleanInstallResult = main.TRUE
+#         if gitPullResult == main.TRUE:
+#             cleanInstallResult = main.ONOSbench.cleanInstall()
+#         else:
+#             main.log.warn( "Did not pull new code so skipping mvn " +
+#                             "clean install" )
+        cleanInstallResult = main.ONOSbench.cleanInstall()
+        main.ONOSbench.getVersion( report=True )
+
+        #cellResult = main.ONOSbench.setCell( cellName )
+        #verifyResult = main.ONOSbench.verifyCell()
+        main.step( "Creating ONOS package" )
+        packageResult = main.ONOSbench.onosPackage()
+
+        main.step( "Installing ONOS package" )
+        onos1InstallResult = main.ONOSbench.onosInstall( options="-f",
+                                                           node=ONOS1Ip )
+
+        main.step( "Checking if ONOS is up yet" )
+        for i in range( 2 ):
+            onos1Isup = main.ONOSbench.isup( ONOS1Ip )
+            if onos1Isup:
+                break
+        if not onos1Isup:
+            main.log.report( "ONOS1 didn't start!" )
+
+        cliResult = main.ONOScli.startOnosCli( ONOS1Ip )
+
+        case1Result = ( cleanInstallResult and packageResult and
+                        cellResult and verifyResult and
+                        onos1InstallResult and
+                        onos1Isup and cliResult )
+
+        utilities.assert_equals( expect=main.TRUE, actual=case1Result,
+                                 onpass="ONOS startup successful",
+                                 onfail="ONOS startup NOT successful" )
+
+        if case1Result == main.FALSE:
+            main.cleanup()
+            main.exit()
 
     def CASE4( self, main ):
         """
@@ -33,14 +97,12 @@
         import time
         import json
         from operator import eq
-        # from datetime import datetime
         from time import localtime, strftime
 
-        main.case("The test case is to help to setup the TestON environment \
-            and test new drivers" )
-        # SDNIPJSONFILEPATH = "../tests/SdnIpTest/sdnip.json"
+        main.case("This case is to testing the functionality of SDN-IP with \
+        single ONOS instance" )
         SDNIPJSONFILEPATH = \
-            "/home/admin/workspace/onos/tools/package/config/sdnip.json"
+            "/home/admin/ONOS/tools/package/config/sdnip.json"
         # all expected routes for all BGP peers
         allRoutesExpected = []
         main.step( "Start to generate routes for all BGP peers" )
@@ -79,31 +141,6 @@
         routeIntentsExpected = routeIntentsExpectedHost3 + \
             routeIntentsExpectedHost4 + routeIntentsExpectedHost5
 
-        cellName = main.params[ 'ENV' ][ 'cellName' ]
-        ONOS1Ip = main.params[ 'CTRL' ][ 'ip1' ]
-        main.step( "Set cell for ONOS-cli environment" )
-        main.ONOScli.setCell( cellName )
-        verifyResult = main.ONOSbench.verifyCell()
-
-        main.log.report( "Removing raft logs" )
-        main.ONOSbench.onosRemoveRaftLogs()
-        main.log.report( "Uninstalling ONOS" )
-        main.ONOSbench.onosUninstall( ONOS1Ip )
-
-        main.step( "Installing ONOS package" )
-        onos1InstallResult = main.ONOSbench.onosInstall(
-            options="-f", node=ONOS1Ip )
-
-        main.step( "Checking if ONOS is up yet" )
-        time.sleep( 150 )
-        onos1Isup = main.ONOSbench.isup( ONOS1Ip )
-        if not onos1Isup:
-            main.log.report( "ONOS1 didn't start!" )
-
-        main.step( "Start ONOS-cli" )
-
-        main.ONOScli.startOnosCli( ONOS1Ip )
-
         main.step( "Get devices in the network" )
         listResult = main.ONOScli.devices( jsonFormat=False )
         main.log.info( listResult )
@@ -138,8 +175,8 @@
             prefixesHostX = main.QuaggaCliHost.generatePrefixes( str( i ), 10 )
             main.log.info( prefixesHostX )
             for prefix in prefixesHostX:
-                allRoutesExpected.append(
-                    prefix + "/" + "192.168.40." + str( i - 100 ) )
+                allRoutesExpected.append( prefix + "/" + "192.168.40."
+                                           + str( i - 100 ) )
 
             routeIntentsExpectedHostX = \
             main.QuaggaCliHost.generateExpectedOnePeerRouteIntents(
@@ -158,13 +195,19 @@
             QuaggaCliHostX.addRoutes( prefixesHostX, 1 )
 
         time.sleep( 60 )
-
         # get routes inside SDN-IP
         getRoutesResult = main.ONOScli.routes( jsonFormat=True )
 
         # parse routes from ONOS CLI
-        allRoutesActual = \
-            main.QuaggaCliHost3.extractActualRoutes( getRoutesResult )
+        if branchName == "master":
+            allRoutesActual = \
+            main.QuaggaCliHost3.extractActualRoutesMaster( getRoutesResult )
+        elif branchName == "onos-1.0":
+            allRoutesActual = \
+            main.QuaggaCliHost3.extractActualRoutesOneDotZero( getRoutesResult )
+        else:
+            main.log("ONOS is on wrong branch")
+            exit
 
         allRoutesStrExpected = str( sorted( allRoutesExpected ) )
         allRoutesStrActual = str( allRoutesActual ).replace( 'u', "" )
@@ -189,7 +232,7 @@
 
         main.step( "Check MultiPointToSinglePointIntent intents installed" )
         # routeIntentsExpected are generated when generating routes
-        # get rpoute intents from ONOS CLI
+        # get route intents from ONOS CLI
         routeIntentsActual = \
             main.QuaggaCliHost3.extractActualRouteIntents(
                 getIntentsResult )
@@ -243,7 +286,7 @@
                 "***PointToPointIntent Intents in SDN-IP are wrong!***" )
 
         #============================= Ping Test ========================
-        # wait until all MultiPointToSinglePoint
+        # Wait until all MultiPointToSinglePoint intents are in system
         time.sleep( 20 )
         pingTestScript = "~/SDNIP/test-tools/CASE4-ping-as2host.sh"
         pingTestResultsFile = \
@@ -306,22 +349,19 @@
         time.sleep( 20 )
         pingTestScript = "~/SDNIP/test-tools/CASE4-ping-as2host.sh"
         pingTestResultsFile = \
-        "~/SDNIP/SdnIpIntentDemo/log/CASE4-ping-results-after-delete-routes-" \
+        "~/SDNIP/SdnIpIntentDemo/log/CASE4-ping-results-after-delete-routes-"\
             + strftime( "%Y-%m-%d_%H:%M:%S", localtime() ) + ".txt"
         pingTestResults = main.QuaggaCliHost.pingTest(
             "1.168.30.100", pingTestScript, pingTestResultsFile )
         main.log.info( pingTestResults )
         time.sleep( 100 )
 
-        # main.step( "Test whether Mininet is started" )
-        # main.Mininet2.handle.sendline( "xterm host1" )
-        # main.Mininet2.handle.expect( "mininet>" )
-
     def CASE3( self, main ):
         """
         Test the SDN-IP functionality
         allRoutesExpected: all expected routes for all BGP peers
-        routeIntentsExpected: all expected MultiPointToSinglePointIntent intents
+        routeIntentsExpected: all expected MultiPointToSinglePointIntent \
+        intents
         bgpIntentsExpected: expected PointToPointIntent intents
         allRoutesActual: all routes from ONOS LCI
         routeIntentsActual: actual MultiPointToSinglePointIntent intents from \
@@ -338,7 +378,7 @@
             environment and test new drivers" )
         # SDNIPJSONFILEPATH = "../tests/SdnIpTest/sdnip.json"
         SDNIPJSONFILEPATH = \
-            "/home/admin/workspace/onos/tools/package/config/sdnip.json"
+            "/home/admin/ONOS/tools/package/config/sdnip.json"
         # all expected routes for all BGP peers
         allRoutesExpected = []
         main.step( "Start to generate routes for all BGP peers" )