#!/bin/groovy
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

pipeline {
    agent {
        label 'caracal'
    }
    options {
        disableConcurrentBuilds()
	timestamps()
        ansiColor('xterm')
    }
    stages {
        stage ('Retrieve latest from branch') {
            steps {
                script {
                    checkout([$class: 'GitSCM', branches: [[name: '*/gNB-nrUE-USRP']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://gitlab.eurecom.fr/oai/openairinterface5g.git']]])
                    sh "git clean -x -d -ff"
                    sh "git log -n1"
                }
            }
        }
        stage ("Print latest commit info") {
            steps {
                echo "Building on: "
                echo "  Repository -- ${GIT_URL}"
                echo "  Branch -- ${GIT_BRANCH}"
                echo "  Commit -- ${GIT_COMMIT}"
                sh "git log -n1"
            }
        }
        stage ("Verify Parameters") {
            steps {
                script {
                    echo '\u2705 \u001B[32mVerify Parameters\u001B[0m'
                    def allParametersPresent = true

                    // It is already too late to check it
                    if (params.pythonExecutor != null) {
                       echo "eNB CI executor node  :   ${pythonExecutor}"
                    }
                    // If not present picking a default XML file
                    if (params.pythonTestXmlFile == null) {
                        // picking default
                        testXMLFile = 'xml_files/gnb_usrp_build.xml'
                        echo "Test XML file(default):   ${testXMLFile}"
                        mainPythonAllXmlFiles += "--XMLTestFile=" + testXMLFile + " "
                    } else {
                        String[] myXmlTestSuite = testXMLFile.split("\\r?\\n")
                        for (xmlFile in myXmlTestSuite) {
                            if (fileExists("ci-scripts/" + xmlFile)) {
                                mainPythonAllXmlFiles += "--XMLTestFile=" + xmlFile + " "
                                echo "Test XML file         :   ${xmlFile}"
                            }
                        }
                    }
                    // If not present picking a default Stage Name
                    if (params.pipelineTestStageName == null) {
                        // picking default
                        testStageName = 'Template Test Stage'
                    }
                    if (params.eNB_IPAddress == null) {
                        allParametersPresent = false
                    }
                    if (params.UE_IPAddress == null) {
                        allParametersPresent = false
                    }
                    if (params.eNB_SourceCodePath == null) {
                        allParametersPresent = false
                    }
                    if (params.eNB_Credentials == null) {
                        allParametersPresent = false
                    }                   
                    if (params.UE_Credentials == null) {
                        allParametersPresent = false
                    }
                    if (params.eNB_Repository == null) {
                        eNB_Repository = env.GIT_URL
                    } else {
                        eNB_Repository = params.eNB_Repository
                    }

                    if (allParametersPresent) {
                        echo "All parameters are present"
                        sh "git fetch"
                        sh "git checkout -f ${eNB_CommitID}"
                    } else {
                        echo "Some parameters are missing"
                        sh "./ci-scripts/fail.sh"
                    }
                }
            }
        }
    }
}