#!/usr/bin/env groovy
// -----------------------------------------------------------------------
// Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors
//
// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
//
// 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.
// -----------------------------------------------------------------------
// this keyword is dedicated to deploy a single VOLTHA stack with infra
// If you need to deploy different configurations you can use the
// volthaInfraDeploy and volthaStackDeploy DSL keywords
// -----------------------------------------------------------------------

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
String getIam(String func) {
    // Cannot rely on a stack trace due to jenkins manipulation
    String src = 'vars/volthaDeploy.groovy'
    String iam = [src, func].join('::')
    return iam
}

// -----------------------------------------------------------------------
// Intent: Log progress message
// -----------------------------------------------------------------------
void enter(String name) {
    // Announce ourselves for log usability
    String iam = getIam(name)
    println("${iam}: ENTER")
    return
}

// -----------------------------------------------------------------------
// Intent: Log progress message
// -----------------------------------------------------------------------
void leave(String name) {
    // Announce ourselves for log usability
    String iam = getIam(name)
    println("${iam}: LEAVE")
    return
}

// -----------------------------------------------------------------------
// Intent: Display a message with visibility for logging
// -----------------------------------------------------------------------
String banner(String message) {
    String iam = getIam('banner')

    println("""

** -----------------------------------------------------------------------
** IAM: $iam
** ${message}
** -----------------------------------------------------------------------
""")
    return
}

// -----------------------------------------------------------------------
// Intent: Perform volthaDeploy stuff
// -----------------------------------------------------------------------
void process(Map config)
{
    // note that I can't define this outside the function as there's no global scope in Groovy
    // [joey] A class method or library call can be used in place of globals, fqdn needed.
    def defaultConfig = [
        onosReplica: 1,
        atomixReplica: 1,
        kafkaReplica: 1,
        etcdReplica: 1,
        bbsimReplica: 1,
        infraNamespace   : 'infra',
        volthaNamespace  : 'voltha',
        stackName        : 'voltha',
        stackId: 1,
        workflow         : 'att',
        vgcEnabled       : false,
        withMacLearning: false,
        withFttb: false,
        extraHelmFlags   : '',
        localCharts: false, // Local or upstream (?) (for local we assume they are stored in $WORKSPACE/voltha-helm-charts)
        dockerRegistry   : '', // Different registry for images eg: "mirror.registry.opennetworking.org"
        kubeconfig: null, // location of the kubernetes config file, if null we assume it's stored in the $KUBECONFIG environment variable
        withVolthaInfra: true,
        withVolthaStack: true,
    ]

    if (!config) {
        config = [:]
    }

    def cfg = defaultConfig + config

    if (cfg.dockerRegistry != '') {
        def registryFlags = " --set global.image_registry=${cfg.dockerRegistry}/ "
        registryFlags += " --set etcd.image.registry=${cfg.dockerRegistry} "
        registryFlags += " --set kafka.image.registry=${cfg.dockerRegistry} "
        registryFlags += " --set kafka.zookeper.image.registry=${cfg.dockerRegistry} "
        registryFlags += " --set onos-classic.image.repository=${cfg.dockerRegistry}/voltha/voltha-onos "
        registryFlags += " --set onos-classic.atomix.image.repository=${cfg.dockerRegistry}/atomix/atomix "
        registryFlags += " --set freeradius.images.radius.registry=${cfg.dockerRegistry}/ "

        // we want to always leave the user provided flags at the end, to override changes
        cfg.extraHelmFlags = registryFlags + " " + cfg.extraHelmFlags
    }

    // ---------------------
    banner("Updating helm repos")

    sh(label  : 'Configure helm repo',
       script : """
helm repo add onf https://charts.lfbroadband.org || helm repo update
""")

    // ---------------------
    banner("Deploying VOLTHA with the following parameters: ${cfg}")

    if (cfg.withVolthaInfra) {
        volthaInfraDeploy(cfg)
    }

    if (cfg.withVolthaStack) {
        volthaStackDeploy(cfg)
    }

    return
}

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
def call(Map config=[:]) { // Function return type(?)
    String iam = getIam('main')
    Boolean ans = true

    try {
        enter('main')
        process(config)
    }
    catch (Exception err) {  // groovylint-disable-line CatchException
        ans = false
        println("** ${iam}: EXCEPTION ${err}")
        throw err
    }
    finally {
        leave('main')
    }

    return(ans)
}

// [EOF]
