#!groovy
// Copyright 2017 Open Networking Foundation (ONF)
//
// Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
// the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
// or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
//
//     TestON is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 2 of the License, or
//     (at your option) any later version.
//
//     TestON is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
//
//     You should have received a copy of the GNU General Public License
//     along with TestON.  If not, see <http://www.gnu.org/licenses/>.

// This is the Jenkins script for vm-pipeline-trigger or bm-pipeline-trigger

// set the functions of the dependencies.
funcs = evaluate readTrusted( 'TestON/JenkinsFile/dependencies/JenkinsCommonFuncs.groovy' )
triggerFuncs = evaluate readTrusted( 'TestON/JenkinsFile/dependencies/TriggerFuncs.groovy' )
fileRelated = evaluate readTrusted( 'TestON/JenkinsFile/dependencies/JenkinsPathAndFiles.groovy' )
test_list = evaluate readTrusted( 'TestON/JenkinsFile/dependencies/JenkinsTestONTests.groovy' )

onos_tag = null
manually_run = null
now = null
today = null
onos_branches = null
day = null
post_result = null
branchesParam = null
isFabric = null
testsParam = null

dayMap = [:]
fullDayMap = [:]
all_testcases = [:]
runTest = [:]
selectedTests = [:]
graphPaths = [:]

main()

def main() {
    init()
    runTests()
    generateGraphs()
}

// **************
// Initialization
// **************

// initialize file scope vars
def init(){
    // get the name of the job.
    jobName = env.JOB_NAME

    // set the versions of the onos
    fileRelated.init()
    test_list.init()
    readParams()

    funcs.initializeTrend( "Fabric" )
    funcs.initialize( "Fabric" )

    funcs.initializeTrend( "VM" )

    triggerFuncs.init( funcs )

    // list of the tests to be run will be saved in each choices.
    day = ""

    initDates()
    onos_branches = getONOSBranches()
    selectedTests = getONOSTests()

    echo "selectedTests: " + selectedTests
}

def readParams(){
    // get post result from the params for manually run.
    post_result = params.PostResult
    manually_run = params.manual_run
    onos_tag = params.ONOSTag
    branchesParam = params.branches
    isOldFlow = params.isOldFlow
    testsParam = params.Tests
    isFabric = params.isFabric
}

// Set tests based on day of week
def initDates(){
    echo "-> initDates()"
    now = funcs.getCurrentTime()
    today = now[ Calendar.DAY_OF_WEEK ]
    dayMap = [ ( Calendar.MONDAY )    : "mon",
               ( Calendar.TUESDAY )   : "tue",
               ( Calendar.WEDNESDAY ) : "wed",
               ( Calendar.THURSDAY )  : "thu",
               ( Calendar.FRIDAY )    : "fri",
               ( Calendar.SATURDAY )  : "sat",
               ( Calendar.SUNDAY )    : "sun" ]
    fullDayMap = [ ( Calendar.MONDAY )    : "Monday",
                   ( Calendar.TUESDAY )   : "Tuesday",
                   ( Calendar.WEDNESDAY ) : "Wednesday",
                   ( Calendar.THURSDAY )  : "Thursday",
                   ( Calendar.FRIDAY )    : "Friday",
                   ( Calendar.SATURDAY )  : "Saturday",
                   ( Calendar.SUNDAY )    : "Sunday" ]
    print now.toString()
}

// gets ONOS branches from params or string parameter
def getONOSBranches(){
    echo "-> getONOSBranches()"
    if ( manually_run ){
        return branchesParam.tokenize( "\n;, " )
    } else {
        return test_list.getBranchesFromDay( dayMap[ today ] )
    }
}

def getONOSTests(){
    echo "-> getONOSTests()"
    if ( manually_run ){
        return test_list.getTestsFromStringList( testsParam.tokenize( "\n;, " ) )
    } else {
        day = dayMap[ today ]
        return test_list.getTestsFromDay( day )
    }
}

// init paths for the files and directories.
def initGraphPaths(){
    graphPaths[ "histogramMultiple" ] = fileRelated.histogramMultiple
    graphPaths[ "pieMultiple" ] = fileRelated.pieMultiple
    graphPaths[ "saveDirectory" ] = fileRelated.jenkinsWorkspace + "postjob-VM/"
}

// **********************
// Determine Tests to Run
// **********************

def printTestsToRun( runList ){
    if ( manually_run ){
        println "Tests to be run manually:"
    } else {
        if ( isFabric ){
            postToSlackSR()
        }
        if ( today == Calendar.MONDAY ){
            postToSlackTestsToRun()
        }
        println "Defaulting to " + day + " tests:"
    }
    for ( list in runList ){
    //     triggerFuncs.print_tests( all_testcases[ b ] )
        echo "" + list
    }
}

def postToSlackSR(){
    // If it is automated running, it will post the beginning message to the channel.
    slackSend( channel: 'sr-failures', color: '#03CD9F',
               message: ":sparkles:" * 16 + "\n" +
                        "Starting tests on : " + now.toString() +
                        "\n" + ":sparkles:" * 16 )
}

def postToSlackTestsToRun(){
    slackSend( color: '#FFD988',
               message: "Tests to be run this weekdays : \n" +
                        triggerFuncs.printDaysForTest() )
}

// *********
// Run Tests
// *********

def generateRunList(){
    runList = [:]
    for ( branch in onos_branches ){
        runBranch = []
        nodeLabels = test_list.getAllNodeLabels( branch, selectedTests )
        for ( nodeLabel in nodeLabels ){
            selectedNodeLabelTests = test_list.getTestsFromNodeLabel( nodeLabel, branch, selectedTests )
            selectedNodeLabelCategories = test_list.getAllTestCategories( selectedNodeLabelTests )
            for ( category in selectedNodeLabelCategories ){
                selectedNodeLabelCategoryTests = test_list.getTestsFromCategory( category, selectedNodeLabelTests )
                exeTestList = test_list.getTestListAsString( selectedNodeLabelCategoryTests )
                runList.put( branch + "-" + nodeLabel, triggerFuncs.trigger_pipeline( branch, exeTestList, nodeLabel, category, manually_run, onos_tag ) )
            }
        }
    }
    return runList
}

def runTests(){
    runList = generateRunList()
    printTestsToRun( runList )
    parallel runList
}

// ***************
// Generate Graphs
// ***************

def generateGraphs(){
    // If it is automated running, it will generate the stats graph on VM.
    if ( !manually_run ){
        for ( String b in onos_branches ){
            funcs.generateStatGraph( "TestStation-VMs",
                                     funcs.branchWithPrefix( b ),
                                     graphPaths[ "histogramMultiple" ],
                                     graphPaths[ "pieMultiple" ],
                                     graphPaths[ "saveDirectory" ] )
        }
    }
}
