#!/bin/bash

_XTRACE_ODL_RELEASE_COMMON=$(set +o | grep xtrace)
set -o xtrace

# code name -> version(Major.Minor) relationship
declare -A ODL_NAME_TO_MAJOR_MINOR
ODL_NAME_TO_MAJOR_MINOR["Helium"]="0.2"
ODL_NAME_TO_MAJOR_MINOR["Lithium"]="0.3"
ODL_NAME_TO_MAJOR_MINOR["Beryllium"]="0.4"
ODL_NAME_TO_MAJOR_MINOR["Boron"]="0.5"
ODL_NAME_TO_MAJOR_MINOR["Carbon"]="0.6"
ODL_NAME_TO_MAJOR_MINOR["Nitrogen"]="0.7"
ODL_NAME_TO_MAJOR_MINOR["Oxygen"]="0.8"

_odl_release=$1
if [[ "$_odl_release" =~ -snapshot ]]; then
    # <release name>-snapshot-<N>.<N>.<N> -> <N>.<N>.<N>-SNAPSHOT
    _odl_version=${_odl_release/[[:alpha:]]*-snapshot-/}
    if [[ "$_odl_release" == "latest-snapshot" ]]; then
        # get latest revision of snapshot
        _odl_version=$(odl_snapshot_full_version $ODL_DIR $ODL_URL_PREFIX "latest" $OFFLINE)
        # update ODL_RELEASE to prevent odl_snapshot_full_version from being called
        # every time networking-odl/devstack/plugin.sh is called by devstack
        # latest-snapshot -> latest-snapshot-<N>.<N>.<N>
        ODL_RELEASE=${ODL_RELEASE}-${_odl_version}
    elif [[ "${_odl_version}" =~ ^[[:digit:]]\.[[:digit:]]$ ]]; then
        # get latest revision of given major.minor
        # <major>.<minor> -> <major>.<minor>.<revision>
        _odl_version=$(odl_snapshot_full_version $ODL_DIR $ODL_URL_PREFIX $_odl_version $OFFLINE)
        # update ODL_RELEASE to prevent odl_snapshot_full_version from being called
        # every time networking-odl/devstack/plugin.sh is called by devstack
        # <release name>-snapshot-<N>.<N> -> <release name>-snapshot-<N>.<N>.<N>
        _odl_revision=${_odl_version/[[:digit:]]\.[[:digit:]]\./}
        ODL_RELEASE=${ODL_RELEASE}.${_odl_revision}
    fi
    _odl_bundleversion_default=${_odl_version}-SNAPSHOT
    export ODL_BUNDLEVERSION=${ODL_BUNDLEVERSION:-${_odl_bundleversion_default}}
    export ODL_SNAPSHOT_VERSION=${ODL_SNAPSHOT_VERSION:-latest}
else
    if [[ "$_odl_release" == "latest-release" ]]; then
        _odl_bundleversion_default=$(odl_release_bundleversion $ODL_DIR $ODL_URL_PREFIX "latest" $OFFLINE)
    elif [[ "$_odl_release" =~ "-latest" ]]; then
        # <releasename>-latest
        _name=$(echo ${_odl_release} | awk -F- '{print toupper(substr($1, 1, 1))substr($1, 2)}')
        _odl_bundleversion_default=$(odl_release_bundleversion $ODL_DIR $ODL_URL_PREFIX $_name $OFFLINE)
    else
        # <release name>-<N>.<N>.<N>[-SR<N>] -> <N>.<N>.<N>-<Release name>[-SR<N>]
        _name=$(echo ${_odl_release} | awk -F- '{print toupper(substr($1, 1, 1))substr($1, 2)}')
        _version=$(echo ${_odl_release} | awk -F- '{print $2}')
        _sr=$(echo ${_odl_release} | awk -F- '{print $3}')
        _odl_bundleversion_default=${_version}-${_name}
        if [[ -n $_sr ]]; then
            _odl_bundleversion_default=${_odl_bundleversion_default}-${_sr}
        fi
    fi
    export ODL_BUNDLEVERSION=${ODL_BUNDLEVERSION:-${_odl_bundleversion_default}}
fi


# Java major version required to run OpenDaylight: 7, 8, ...
# by default, ODL uses jdk 8 as of Boron
export ODL_REQUIRED_JAVA_VERSION=${ODL_REQUIRED_JAVA_VERSION:-8}

# artifact id
_default_artifact_id=$(odl_artifact_id ${ODL_DIR} ${ODL_URL_PREFIX} ${ODL_BUNDLEVERSION} ${OFFLINE})
export ODL_ARTIFACT_ID=${ODL_ARTIFACT_ID:-${_default_artifact_id}}
# karaf distribution name of ODL to download
export ODL_NAME=${ODL_NAME:-${ODL_ARTIFACT_ID}-${ODL_BUNDLEVERSION}}

# The network virtualization older feature name (ovsdb based)
export ODL_NETVIRT_KARAF_FEATURE_OVSDB=${ODL_NETVIRT_KARAF_FEATURE_OVSDB:-odl-ovsdb-openstack}

# The network virtualization newer feature name (vpnservice based)
export ODL_NETVIRT_KARAF_FEATURE_VPNSERVICE=${ODL_NETVIRT_KARAF_FEATURE_VPNSERVICE:-odl-netvirt-openstack}

ODL_NETVIRT_KARAF_FEATURE_DEFAULT=odl-neutron-service,odl-restconf-all,odl-aaa-authn,odl-dlux-core,odl-mdsal-apidocs
# new netvirt has been introduced into netvirt from Boron release
# odl-neutron-logger has been introduced from Boron release
case "$ODL_BUNDLEVERSION" in
    0.5.?-*)
        # 0.5.?-*
        ODL_NETVIRT_KARAF_FEATURE_DEFAULT+=,$ODL_NETVIRT_KARAF_FEATURE_VPNSERVICE
        ODL_NETVIRT_KARAF_FEATURE_DEFAULT+=,odl-neutron-logger
        ;;
    *)
        # 0.6.?-* or later
        ODL_NETVIRT_KARAF_FEATURE_DEFAULT+=,$ODL_NETVIRT_KARAF_FEATURE_VPNSERVICE
        ODL_NETVIRT_KARAF_FEATURE_DEFAULT+=,odl-neutron-hostconfig-ovs
        ODL_NETVIRT_KARAF_FEATURE_DEFAULT+=,odl-neutron-logger
        ;;
esac

# The network virtualization feature used by opendaylight loaded by Karaf
export ODL_NETVIRT_KARAF_FEATURE=${ODL_NETVIRT_KARAF_FEATURE:-$ODL_NETVIRT_KARAF_FEATURE_DEFAULT}

# The url that this version of ODL netvirt can use to know ODL is fully up
export ODL_BOOT_WAIT_URL=${ODL_BOOT_WAIT_URL:-restconf/operational/network-topology:network-topology/topology/netvirt:1}

$_XTRACE_ODL_RELEASE_COMMON
