[ONOS-7994]: Combine TestON Pipeline Scripts Containing Similar Code

Change-Id: I1727233b87ca957c8e23c6c839232b22121847dd
diff --git a/TestON/JenkinsFile/dependencies/JenkinsCommonFuncs.groovy b/TestON/JenkinsFile/dependencies/JenkinsCommonFuncs.groovy
index f57fefb..be93b7f 100644
--- a/TestON/JenkinsFile/dependencies/JenkinsCommonFuncs.groovy
+++ b/TestON/JenkinsFile/dependencies/JenkinsCommonFuncs.groovy
@@ -114,18 +114,13 @@
     }
 }
 
-def printType(){
-    // print the test type and test machine that was initialized.
-
-    echo testType
-    echo testMachine
-}
-
-def getProperties( category ){
+def getProperties( category, branchWithPrefix ){
     // get the properties of the test by reading the TestONOS.property
 
+    filePath = '''/var/jenkins/TestONOS-''' + category + '''-''' + branchWithPrefix + '''.property'''
+
     node( testMachine ) {
-        return readProperties( file: '/var/jenkins/TestONOS-' + category + '.property' )
+        return readProperties( file: filePath )
     }
 }
 
@@ -142,20 +137,6 @@
     return new Date()
 }
 
-def getTotalTime( start, end ){
-    // get total time of the test using start and end time.
-
-    return TimeCategory.minus( end, start )
-}
-
-def printTestToRun( testList ){
-    // printout the list of the test in the list.
-
-    for ( String test : testList ){
-        println test
-    }
-}
-
 def sendResultToSlack( start, isManualRun, branch ){
     // send the result of the test to the slack when it is not manually running.
     // start : start time of the test
@@ -334,7 +315,6 @@
     node( testMachine ) {
         def alarmFile = workSpace + "/" + pureTestName + "Alarm.txt"
         if ( fileExists( alarmFile ) ) {
-            print "Abnormal test result logged"
             def alarmContents = readFile( alarmFile )
             slackSend( channel: getSlackChannel(),
                        color: "FF0000",
@@ -348,10 +328,10 @@
                                 ( resultURL != "" ? ( "\n[Karaf log] : \n" +
                                                       resultURL + "artifact/" ) : "" ),
                        teamDomain: 'onosproject' )
-            Failed
+            throw new Exception( "Abnormal test result." )
         }
         else {
-            print "Test results are normal"
+            print "Test results are normal."
         }
     }
 }
@@ -573,27 +553,4 @@
                                 "ALL", onos_branch, testListParam, graph_saved_directory, pieTestListParam )
 }
 
-def branchWithPrefix( branch ){
-    // get the branch with the prefix ( "onos-" )
-    return ( ( branch != "master" ) ? "onos-" : "" ) + branch
-}
-
-def testBranchWithPrefix( branch ){
-    // get TestON branch with the prefix ( "onos-" )
-    if ( branch == "1.12" )
-        return "onos-1.13"
-    else if ( branch == "1.13" )
-        return "onos-1.13"
-    else if ( branch == "1.14" )
-        return "onos-1.15"
-    else if ( branch == "1.15" )
-        return "onos-1.15"
-    else if ( branch == "2.0" )
-        return "onos-2.0"
-    else if ( branch == "2.1" )
-        return "onos-2.1"
-    else
-        return "master"
-}
-
 return this
diff --git a/TestON/JenkinsFile/dependencies/JenkinsTestONTests.groovy b/TestON/JenkinsFile/dependencies/JenkinsTestONTests.groovy
index a93fa1c..2a358df 100644
--- a/TestON/JenkinsFile/dependencies/JenkinsTestONTests.groovy
+++ b/TestON/JenkinsFile/dependencies/JenkinsTestONTests.groovy
@@ -23,6 +23,7 @@
 schedules = [:]
 branches = [:]
 
+// read all json files and save as maps
 def init(){
     def tests_buffer = readTrusted( "TestON/JenkinsFile/dependencies/tests.json" )
     def schedules_buffer = readTrusted( "TestON/JenkinsFile/dependencies/schedule.json" )
@@ -60,6 +61,7 @@
     return str_result[ 0..-2 ]
 }
 
+// given a list of tests as a string, returns the list of tests as a map, similar formatted to tests.json
 def getTestsFromStringList( list ){
     testsResult = [:]
     for ( item in list ){
@@ -85,14 +87,15 @@
     return schedulePropertyResult
 }
 
-def getAllBranches(){
-    return branches
-}
-
 // ********
 // Branches
 // ********
 
+// returns all branches from branches.json
+def getAllBranches(){
+    return branches
+}
+
 // given a day, returns all branches that are run on that day
 def getBranchesFromDay( day, tests=[:] ){
     branchesFromDayResult = []
@@ -125,6 +128,8 @@
     return branch_code
 }
 
+// given a branch, returns the corresponding branch code (hack)
+// Example: given "onos-1.15", returns "onos-1.x"
 def convertBranchToBranchCode( branch ){
     if ( branch == "master" ){
         return branch
@@ -135,6 +140,16 @@
     }
 }
 
+// given a branch without a prefix, returns the branch with the "onos-" prefix
+def addPrefixToBranch( branchNoPrefix ){
+    if ( branchNoPrefix == "master" ){
+        return "master"
+    } else {
+        return "onos-" + branchNoPrefix
+    }
+}
+
+// given a branch with the prefix "onos-", returns the branch without the prefix
 def removePrefixFromBranch( branchWithPrefix ){
     return branchWithPrefix.minus( "onos-" )
 }
@@ -157,6 +172,7 @@
     return testsFromCategoryResult
 }
 
+// given the test name, returns the category (FUNC, HA, etc.) of that test
 def getCategoryOfTest( test_name, tests=[:] ){
     if ( tests == [:] ){
         tests = allTests
@@ -164,6 +180,7 @@
     return tests[ test_name ][ "category" ]
 }
 
+// returns all categories of all tests, or the given test list
 def getAllTestCategories( tests=[:] ){
     testCategoriesResult = []
     if ( tests == [:] ){
@@ -232,7 +249,7 @@
 }
 
 // Given a test name and branch, return the node label associated.
-def getNodeLabel( test_name, branch, tests ){
+def getNodeLabel( test_name, branch, tests=[:] ){
     if ( tests == [:] ){
         tests = allTests
     }
@@ -244,7 +261,8 @@
     }
 }
 
-def getAllNodeLabels( branch, tests ){
+// given a branch, returns all nodeLabels from all tests, or a given test list
+def getAllNodeLabels( branch, tests=[:] ){
     nodeLabelResult = []
     if ( tests == [:] ){
         tests = allTests
diff --git a/TestON/JenkinsFile/dependencies/TriggerFuncs.groovy b/TestON/JenkinsFile/dependencies/TriggerFuncs.groovy
index 1795ba3..2aba5d2 100644
--- a/TestON/JenkinsFile/dependencies/TriggerFuncs.groovy
+++ b/TestON/JenkinsFile/dependencies/TriggerFuncs.groovy
@@ -30,15 +30,6 @@
     funcs = commonFuncs
 }
 
-def lastCommaRemover( str ){
-    // function that will remove the last comma from the string
-
-    if ( str.size() > 0 && str[ str.size() - 1 ] == ',' ){
-        str = str.substring( 0, str.size() - 1 )
-    }
-    return str
-}
-
 def printDaysForTest(){
     // Print the days for what test has.
     AllTheTests = test_list.getAllTests()
@@ -46,9 +37,9 @@
     result = ""
     for ( String test in AllTheTests.keySet() ){
         result += test + ": ["
-        test_schedule = test_list.getTestSchedule( test )
-        for ( String sch in test_schedule.keySet() ){
-            for ( String day in test_list.convertScheduleKeyToDays( sch ) ){
+        test_schedule = AllTheTests[ test ][ "schedules" ]
+        for ( String sch_dict in test_schedule ){
+            for ( String day in test_list.convertScheduleKeyToDays( sch_dict[ "branch" ] ) ){
                 result += day + " "
             }
         }
@@ -57,58 +48,13 @@
     return result
 }
 
-def runTestSeq( testList ){
-    // Running the test sequentially
-    return {
-        for ( test in testList.keySet() ){
-            testList[ test ].call()
-        }
-    }
-}
-
-def print_tests( tests ){
-    // print the list of the tsets to be run
-
-    for ( String test in tests.keySet() ){
-        if ( tests[ test ][ "tests" ] != "" ){
-            println test + ":"
-            println tests[ test ][ "tests" ]
-        }
-    }
-}
-
-def organize_tests( tests, testcases ){
-    // organize the test to its category using its name.
-    // most of the time it will use the first two character of the test name
-    // but there are some exceptions like FUNCbgpls or FUNCvirNetNB since they are now under USECASE
-
-    // depends on the first two letters of the test name, it will decide which category to put the test into.
-    def prefixes = [
-            "FU": "FUNC",
-            "HA": "HA",
-            "PL": "USECASE",
-            "SA": "USECASE",
-            "SC": "SCPF",
-            "SR": "SR",
-            "US": "USECASE",
-            "VP": "USECASE"
-    ]
-
-    def testList = tests.tokenize( "\n;, " )
-    for ( String test in testList ){
-        String prefix = ( test == "FUNCbgpls" || test == "FUNCvirNetNB" ) ? "US" : ( test[ 0..1 ] )
-        testcases[ prefixes[ prefix ] ][ "tests" ] += test + ","
-    }
-    return testcases
-}
-
 def trigger( branch, tests, nodeName, jobOn, manuallyRun, onosTag ){
     // triggering function that will setup the environment and determine which pipeline to trigger
 
     println "Job name: " + jobOn + "-pipeline-" + ( manuallyRun ? "manually" : branch )
     def wiki = branch
-    def onos_branch = funcs.branchWithPrefix( branch )
-    def test_branch = funcs.testBranchWithPrefix( branch )
+    def onos_branch = test_list.addPrefixToBranch( branch )
+    def test_branch = test_list.addPrefixToBranch( branch )
     println "onos_branch with prefix: " + onos_branch
     println "test_branch with prefix: " + test_branch
     node( "TestStation-" + nodeName + "s" ) {
@@ -117,7 +63,7 @@
     }
 
     jobToRun = jobOn + "-pipeline-" + ( manuallyRun ? "manually" : wiki )
-    build job: jobToRun, propagate: false
+    build job: jobToRun, propagate: false, parameters: [ [ $class: 'StringParameterValue', name: 'Category', value: jobOn ], [ $class: 'StringParameterValue', name: 'Branch', value: branch ] ]
 }
 
 def trigger_pipeline( branch, tests, nodeName, jobOn, manuallyRun, onosTag ){
@@ -134,7 +80,7 @@
 def exportEnvProperty( onos_branch, test_branch, jobOn, wiki, tests, postResult, manually_run, onosTag, isOldFlow ){
     // export environment properties to the machine.
 
-    filePath = "/var/jenkins/TestONOS-" + jobOn + ".property"
+    filePath = "/var/jenkins/TestONOS-" + jobOn + "-" + onos_branch + ".property"
 
     stage( "export Property" ) {
         sh '''