#!/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
 */

def node = "porcepix"
def resource = "CI-NEU-CI"

pipeline {
  agent {
    label node
  }
  options {
    timestamps()
    ansiColor('xterm')
    timeout(time: 3, unit: 'HOURS')
  }
  stages {
    stage ("Trigger NEU CI") {
      steps {
        lock (resource) {
          script {
            withCredentials([
              [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.ColosseumCredentials}", usernameVariable: 'col_username', passwordVariable: 'col_password'],
            ]) {
              // use eNB target branch variable if eNB repository is empty
              def git_Branch = ""
              if (params.eNB_Branch.isEmpty()) {
                echo 'eNB_Branch parameter is empty, using eNB_TargetBranch instead'
                git_Branch = params.eNB_TargetBranch
              } else {
                git_Branch = params.eNB_Branch
              }

              // use default 10011 rf scenario if not specified
              if (params.Colosseum_Rf_Scenario.isEmpty()) {
                echo 'Colosseum_Rf_Scenario parameter is empty, defaulting to 10011'
                rf_scenario = "10011"
              } else {
                rf_scenario = params.Colosseum_Rf_Scenario
              }

              sh "echo Testing reachability of Colosseum endpoint"
              sh "ping -c 3 10.100.1.253"

              sh "./ci-scripts/colosseum_scripts/launch-job.sh ${col_username} ${col_password} ${currentBuild.number} ${params.eNB_Repository} ${git_Branch} ${rf_scenario} ${env.JOB_URL}"
            }
          }
        }
      }
    }
    stage ("Wait for job to finish") {
      steps {
        lock (resource) {
          script {
            withCredentials([
              [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.ColosseumCredentials}", usernameVariable: 'col_username', passwordVariable: 'col_password'],
            ]) {
              timeout (time: 2, unit: 'HOURS') {
                sh "./ci-scripts/colosseum_scripts/wait-job-end.sh ${col_username} ${col_password}"
              }
            }
          }
        }
      }
    }
    stage ("Get test results") {
      steps {
        lock (resource) {
          script {
            withCredentials([
              [$class: 'UsernamePasswordMultiBinding', credentialsId: "${params.ColosseumCredentials}", usernameVariable: 'col_username', passwordVariable: 'col_password'],
            ]) {
              sh "./ci-scripts/colosseum_scripts/get-test-results.sh ${col_username} ${col_password}"
            }

            if(fileExists("results.tar.xz")) {
              archiveArtifacts "results.tar.xz"

              sh "mkdir -p results"
              sh "tar -xf results.tar.xz -C results --strip-components=1"

              if(fileExists("results/test_summary.html")) {
                archiveArtifacts "results/test_summary.html"
              }

              // check if test passed
              sh "./ci-scripts/colosseum_scripts/check-results.sh results/test_summary.html"

              sh "rm -Rf ./results"
            }
          }
        }
      }
    }
    stage ("Set job status") {
      steps {
        lock (resource) {
          script {
            echo 'Set job status'
            sh "./ci-scripts/colosseum_scripts/set-job-status.sh"
          }
        }
      }
    }
  }
}
