+# This is a script that contains container/service management functions and test functions for A1 Controller API
+
+################ Test engine functions ################
+
+# Create the image var used during the test
+# arg: <image-tag-suffix> (selects staging, snapshot, release etc)
+# <image-tag-suffix> is present only for images with staging, snapshot,release tags
+__SDNC_imagesetup() {
+
+ sdnc_suffix_tag=$1
+
+ for oia_name in $ONAP_IMAGES_APP_NAMES; do
+ if [ "$oia_name" == "SDNC" ]; then
+ sdnc_suffix_tag="REMOTE_RELEASE_ONAP"
+ fi
+ done
+ __check_and_create_image_var SDNC "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_IMAGE_BASE" "SDNC_A1_CONTROLLER_IMAGE_TAG" $sdnc_suffix_tag "$SDNC_DISPLAY_NAME"
+ __check_and_create_image_var SDNC "SDNC_DB_IMAGE" "SDNC_DB_IMAGE_BASE" "SDNC_DB_IMAGE_TAG" REMOTE_PROXY "SDNC DB"
+
+}
+
+# Pull image from remote repo or use locally built image
+# arg: <pull-policy-override> <pull-policy-original>
+# <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
+# <pull-policy-original> Shall be used for images that does not allow overriding
+# Both var may contain: 'remote', 'remote-remove' or 'local'
+__SDNC_imagepull() {
+ __check_and_pull_image $1 "$SDNC_DISPLAY_NAME" $SDNC_APP_NAME SDNC_A1_CONTROLLER_IMAGE
+ __check_and_pull_image $2 "SDNC DB" $SDNC_APP_NAME SDNC_DB_IMAGE
+}
+
+# Build image (only for simulator or interfaces stubs owned by the test environment)
+# arg: <image-tag-suffix> (selects staging, snapshot, release etc)
+# <image-tag-suffix> is present only for images with staging, snapshot,release tags
+__SDNC_imagebuild() {
+ echo -e $RED" Image for app SDNC shall never be built"$ERED
+}
+
+# Generate a string for each included image using the app display name and a docker images format string
+# If a custom image repo is used then also the source image from the local repo is listed
+# arg: <docker-images-format-string> <file-to-append>
+__SDNC_image_data() {
+ echo -e "$SDNC_DISPLAY_NAME\t$(docker images --format $1 $SDNC_A1_CONTROLLER_IMAGE)" >> $2
+ if [ ! -z "$SDNC_A1_CONTROLLER_IMAGE_SOURCE" ]; then
+ echo -e "-- source image --\t$(docker images --format $1 $SDNC_A1_CONTROLLER_IMAGE_SOURCE)" >> $2
+ fi
+ echo -e "SDNC DB\t$(docker images --format $1 $SDNC_DB_IMAGE)" >> $2
+ if [ ! -z "$SDNC_DB_IMAGE_SOURCE" ]; then
+ echo -e "-- source image --\t$(docker images --format $1 $SDNC_DB_IMAGE_SOURCE)" >> $2
+ fi
+}
+
+# Scale kubernetes resources to zero
+# All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
+# This function is called for apps fully managed by the test script
+__SDNC_kube_scale_zero() {
+ __kube_scale_all_resources $KUBE_SNDC_NAMESPACE autotest SDNC
+}
+
+# Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
+# This function is called for prestarted apps not managed by the test script.
+__SDNC_kube_scale_zero_and_wait() {
+ echo -e " SDNC replicas kept as is"
+}
+
+# Delete all kube resouces for the app
+# This function is called for apps managed by the test script.
+__SDNC_kube_delete_all() {
+ __kube_delete_all_resources $KUBE_SNDC_NAMESPACE autotest SDNC
+}
+
+# Store docker logs
+# This function is called for apps managed by the test script.
+# args: <log-dir> <file-prexix>
+__SDNC_store_docker_logs() {
+ if [ $RUNMODE == "KUBE" ]; then
+ kubectl logs -l "autotest=SDNC" -n $KUBE_SNDC_NAMESPACE --tail=-1 > $1$2_SDNC.log 2>&1
+ podname=$(kubectl get pods -n $KUBE_SNDC_NAMESPACE -l "autotest=SDNC" -o custom-columns=":metadata.name")
+ kubectl exec -t -n $KUBE_SNDC_NAMESPACE $podname -- cat $SDNC_KARAF_LOG> $1$2_SDNC_karaf.log 2>&1
+ else
+ docker exec -t $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $1$2_SDNC_karaf.log 2>&1
+ fi
+}
+
+# Initial setup of protocol, host and ports
+# This function is called for apps managed by the test script.
+# args: -
+__SDNC_initial_setup() {
+ use_sdnc_http
+}
+
+#######################################################
+
+# Set http as the protocol to use for all communication to SDNC
+# args: -
+# (Function for test scripts)
+use_sdnc_http() {
+ __sdnc_set_protocoll "http" $SDNC_INTERNAL_PORT $SDNC_EXTERNAL_PORT
+}
+
+# Set https as the protocol to use for all communication to SDNC
+# args: -
+# (Function for test scripts)
+use_sdnc_https() {
+ __sdnc_set_protocoll "https" $SDNC_INTERNAL_SECURE_PORT $SDNC_EXTERNAL_SECURE_PORT
+}
+
+# Setup paths to svc/container for internal and external access
+# args: <protocol> <internal-port> <external-port>
+__sdnc_set_protocoll() {
+ echo -e $BOLD"$SDNC_DISPLAY_NAME protocol setting"$EBOLD
+ echo -e " Using $BOLD http $EBOLD towards $SDNC_DISPLAY_NAME"
+
+ ## Access to SDNC
+
+ SDNC_SERVICE_PATH=$1"://"$SDNC_APP_NAME":"$2 # docker access, container->container and script->container via proxy
+ SDNC_SERVICE_API_PATH=$1"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_APP_NAME":"$1$SDNC_API_URL
+ if [ $RUNMODE == "KUBE" ]; then
+ SDNC_SERVICE_PATH=$1"://"$SDNC_APP_NAME.$KUBE_SNDC_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
+ SDNC_SERVICE_API_PATH=$1"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_APP_NAME.KUBE_SNDC_NAMESPACE":"$1$SDNC_API_URL
+ fi
+ echo ""
+
+}
+
+# Export env vars for config files, docker compose and kube resources
+# args:
+__sdnc_export_vars() {
+ export KUBE_SNDC_NAMESPACE
+ export DOCKER_SIM_NWNAME
+
+ export SDNC_APP_NAME
+ export SDNC_DISPLAY_NAME
+
+ export SDNC_A1_CONTROLLER_IMAGE
+ export SDNC_INTERNAL_PORT
+ export SDNC_EXTERNAL_PORT
+ export SDNC_INTERNAL_SECURE_PORT
+ export SDNC_EXTERNAL_SECURE_PORT
+ export SDNC_A1_TRUSTSTORE_PASSWORD
+ export SDNC_DB_APP_NAME
+ export SDNC_DB_IMAGE
+ export SDNC_USER
+ export SDNC_PWD
+}
+
+##################
+### SDNC functions
+##################
+
+# Start the SDNC A1 Controller
+# args: -
+# (Function for test scripts)
+start_sdnc() {
+
+ echo -e $BOLD"Starting $SDNC_DISPLAY_NAME"$EBOLD
+
+ if [ $RUNMODE == "KUBE" ]; then
+
+ # Check if app shall be fully managed by the test script
+ __check_included_image "SDNC"
+ retcode_i=$?
+
+ # Check if app shall only be used by the testscipt
+ __check_prestarted_image "SDNC"
+ retcode_p=$?
+
+ if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
+ echo -e $RED"The $SDNC_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
+ echo -e $RED"The $SDNC_APP_NAME will not be started"$ERED
+ exit
+ fi
+ if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
+ echo -e $RED"The $SDNC_APP_NAME app is included both as managed and prestarted in this test script"$ERED
+ echo -e $RED"The $SDNC_APP_NAME will not be started"$ERED
+ exit
+ fi
+
+
+ if [ $retcode_p -eq 0 ]; then
+ echo -e " Using existing $SDNC_APP_NAME deployment and service"
+ echo " Setting SDNC replicas=1"
+ __kube_scale deployment $SDNC_APP_NAME $KUBE_SNDC_NAMESPACE 1
+ fi
+
+ # Check if app shall be fully managed by the test script
+ if [ $retcode_i -eq 0 ]; then
+
+ echo -e " Creating $SDNC_APP_NAME app and expose service"
+
+ #Check if namespace exists, if not create it
+ __kube_create_namespace $KUBE_SNDC_NAMESPACE
+
+ __sdnc_export_vars
+
+ # Create service
+ input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"svc.yaml
+ output_yaml=$PWD/tmp/sdnc_svc.yaml
+ __kube_create_instance service $SDNC_APP_NAME $input_yaml $output_yaml
+
+ # Create app
+ input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"$SDNC_KUBE_APP_FILE
+ output_yaml=$PWD/tmp/sdnc_app.yaml
+ __kube_create_instance app $SDNC_APP_NAME $input_yaml $output_yaml
+
+ fi
+
+ __check_service_start $SDNC_APP_NAME $SDNC_SERVICE_PATH$SDNC_ALIVE_URL
+ else
+
+ __check_included_image 'SDNC'
+ if [ $? -eq 1 ]; then
+ echo -e $RED"The SDNC A1 Controller app is not included in this test script"$ERED
+ echo -e $RED"The Policy Agent will not be started"$ERED
+ exit
+ fi
+
+ __sdnc_export_vars
+
+ __start_container $SDNC_COMPOSE_DIR $SDNC_COMPOSE_FILE NODOCKERARGS 1 $SDNC_APP_NAME
+
+ __check_service_start $SDNC_APP_NAME $SDNC_SERVICE_PATH$SDNC_ALIVE_URL
+ fi
+ echo ""
+ return 0
+}
+
+
+# Stop the sndc
+# args: -
+# args: -
+# (Function for test scripts)
+stop_sdnc() {
+ echo -e $BOLD"Stopping $SDNC_DISPLAY_NAME"$EBOLD
+
+ if [ $RUNMODE == "KUBE" ]; then
+ __log_conf_fail_not_supported " Cannot stop sndc in KUBE mode"
+ return 1
+ else
+ docker stop $SDNC_APP_NAME &> ./tmp/.dockererr
+ if [ $? -ne 0 ]; then
+ __print_err "Could not stop $SDNC_APP_NAME" $@
+ cat ./tmp/.dockererr
+ ((RES_CONF_FAIL++))
+ return 1
+ fi
+ fi
+ echo -e $BOLD$GREEN"Stopped"$EGREEN$EBOLD
+ echo ""
+ return 0
+}
+
+# Start a previously stopped sdnc
+# args: -
+# (Function for test scripts)
+start_stopped_sdnc() {
+ echo -e $BOLD"Starting (the previously stopped) $SDNC_DISPLAY_NAME"$EBOLD
+
+ if [ $RUNMODE == "KUBE" ]; then
+ __log_conf_fail_not_supported " Cannot restart sndc in KUBE mode"
+ return 1
+ else
+ docker start $SDNC_APP_NAME &> ./tmp/.dockererr
+ if [ $? -ne 0 ]; then
+ __print_err "Could not start (the stopped) $SDNC_APP_NAME" $@
+ cat ./tmp/.dockererr
+ ((RES_CONF_FAIL++))
+ return 1
+ fi
+ fi
+ __check_service_start $SDNC_APP_NAME $SDNC_SERVICE_PATH$SDNC_ALIVE_URL
+ if [ $? -ne 0 ]; then
+ return 1
+ fi
+ echo ""
+ return 0
+}
+
+# Check the agent logs for WARNINGs and ERRORs
+# args: -
+# (Function for test scripts)
+check_sdnc_logs() {
+ __check_container_logs "SDNC A1 Controller" $SDNC_APP_NAME $SDNC_KARAF_LOG WARN ERROR
+}