#!/bin/sh # # ============LICENSE_START=============================================== # org.onap.dmaap # ======================================================================== # Copyright © 2019 AT&T Intellectual Property. All rights reserved. # Copyright (C) 2021 Nordix Foundation. # ======================================================================== # 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. # ============LICENSE_END================================================= # ECOMP is a trademark and service mark of AT&T Intellectual Property. umask 0022 set -uex -o pipefail export PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin # RESP_CACHE is (/opt/app/config/cache) empty-dir volume mount for K8s env RESP_CACHE=${RESP_CACHE:-''} RESP=${RESP:-'/dev/null'} APP_ROOT=${APP_ROOT:-/opt/app/dbc-client} IF_PUB_SUB_EXIST=${IF_PUB_SUB_EXIST:-false} CONFIGMAP_ROOT=${CONFIGMAP_ROOT:-/opt/app/config} PORT=${PORT:-8443} DBC=${DBC:-dmaap-bc} PROTO=${PROTO:-https} PARAM=${PARAM:-'useExisting=true'} REQUESTID=${REQUESTID:-dbc-client} URL=${URL:-"${PROTO}"://"${DBC}":"${PORT}"/webapi/} CA_PEM=${CA_PEM:-ca.pem} KEY_PEM=${KEY_PEM:-key.pem} CLIENT_PEM=${CLIENT_PEM:-client.pem} PEM_DIR=${PEM_DIR:-/opt/app/osaaf/local} CERT_PWD=${CERT_PWD:-'2U[iOZzMHI:.#tdCwlBqc;}S'} BA_PWD=${BA_PWD:-'demo123456!'} AUTH_METHOD=${AUTH_METHOD:-basicAuth} BA_IDENTITY=${BA_IDENTITY:-dmaap-bc@dmaap-bc.onap.org} function xcurl() { curl -X POST \ -s "$CURL_CRED" \ -w "%{http_code}" \ -H "X-ECOMP-RequestID: $REQUESTID" \ -H "Content-Type: application/json" "$@" } function init_config() { if [ ! -d "$APP_ROOT" -a ! -d "$CONFIGMAP_ROOT" ]; then echo "Expected either App root directory $APP_ROOT Or ConfigMap directory $CONFIGMAP_ROOT does not exist." exit 1 fi cd "$PEM_DIR" if [ "$AUTH_METHOD" = "basicAuth" ]; then echo "-u ${BA_IDENTITY}:${BA_PWD}" >"$PEM_DIR"/curl.cred CURL_CRED="-K $PEM_DIR/curl.cred" elif [ -f "$CA_PEM" -a -f "$CLIENT_PEM" -a -f "$KEY_PEM" ]; then printf "key \"$PEM_DIR/$KEY_PEM\"\n cacert \"$PEM_DIR/$CA_PEM\"\n cert \"$PEM_DIR/${CLIENT_PEM}:${CERT_PWD}\"" >$PEM_DIR/curl.cred CURL_CRED="-K $PEM_DIR/curl.cred" else echo "PEM files for authorization not found..!" fi } function init_dbc_provisioning() { cd "$CONFIGMAP_ROOT" for dir in dmaap dcaeLocations mr_clusters topics mr_clients dr_nodes feeds dr_pubs dr_subs; do if [ -d ${dir} ]; then for file in $(ls ${dir}/*.json); do do_http_post "$file" "$dir" done fi done } function do_http_post() { RETRY_TIME=60 if [ -n "$RESP_CACHE" ]; then RESP="$RESP_CACHE"/"$(echo "${1##*/}" | cut -d "." -f1)"-resp.json fi while true; do if [ "$2" != "feeds" -a "$2" != "topics" ]; then req_body=$(cat "${1}" | envsubst) if [ -n "$RESP_CACHE" ] && [ "${2}" = "dr_pubs" -o "${2}" = "dr_subs" ]; then IF_PUB_SUB_EXIST=false check_pub_sub "${2}" "${req_body}" "${RESP}" if [ "${IF_PUB_SUB_EXIST}" = true ]; then echo "DR Publisher or Subscriber already exist.." break fi fi rc=$(xcurl -o "$RESP" -d "$req_body" "${URL}${2}") if [ "$rc" = "200" -o "$rc" = "201" -o "$rc" = "409" ]; then echo "Http Post request is successful with response code=$rc" break fi else rc=$(xcurl -o "$RESP" -d @"${1}" "${URL}${2}"/?"${PARAM}") if [ "$rc" = "200" -o "$rc" = "201" -o "$rc" = "409" ]; then echo "Http Post request for feed creation is successful with response code=$rc" break fi fi echo "$(date): Http Response code=$rc. Will retry after $RETRY_TIME seconds.." sleep "$RETRY_TIME" done } function check_pub_sub() { #Parameters: #${1} Param - String representing dir either dr_pubs or dr_subs #${2} Param - Request details (Dr Publisher/Subscriber creation input details) containing feedName, dr-userName, dr-Password, dr-LocationName #${3} Param - String representing RESP_CACHE Env to '/opt/app/config/cache' or '/dev/null' feed_config_resp="$RESP_CACHE"/"feedConfig-$(echo "${3//[!0-9]/}")-resp.json" if [ -f "${feed_config_resp}" ]; then echo "Checking if pub or sub exist.." val=${1#*_} dr_usr=$(echo "${2}" | jq -r '.username') dr_pwd=$(echo "${2}" | jq -r '.userpwd') pub_sub_cnt=$(jq ".${val} | length" "${feed_config_resp}") local i=0 while [ "$i" -lt "$pub_sub_cnt" ]; do _dr_usr=$(jq -r ".${val}[$i].username" "${feed_config_resp}") _dr_pwd=$(jq -r ".${val}[$i].userpwd" "${feed_config_resp}") if [ "${dr_usr}" = "${_dr_usr}" -a "${dr_pwd}" = "${_dr_pwd}" ]; then if [ "${1}" = "dr_pubs" ]; then echo "Publisher exist with username: ${dr_usr}, password: ${dr_pwd}" echo "$(jq -c ".${val}[$i]" "${feed_config_resp}")" >"${3}" IF_PUB_SUB_EXIST=true break elif [ "${1}" = "dr_subs" ]; then dr_loc=$(echo "${2}" | jq -r '.dcaeLocationName') _dr_loc=$(jq -r ".${val}[$i].dcaeLocationName" "${feed_config_resp}") dr_deliveryURL=$(echo "${2}" | jq -r '.deliveryURL') _dr_deliveryURL=$(jq -r ".${val}[$i].deliveryURL" "${feed_config_resp}") if [ "${dr_loc}" = "${_dr_loc}" -a "${dr_deliveryURL}" = "${_dr_deliveryURL}" ]; then echo "Subscriber exist with username: ${dr_usr}, password: ${dr_pwd}, dcaeLocationName: ${dr_loc}, deliveryURL: ${dr_deliveryURL}" echo "$(jq -c ".${val}[$i]" "${feed_config_resp}")" >"${3}" IF_PUB_SUB_EXIST=true break fi fi fi i=$((i + 1)) done else echo "Feed configuration doesn't exist." fi } init_config init_dbc_provisioning