[DMAAP-BC] Avoid Creating duplicate DR Publisher, Subscriber
[dmaap/buscontroller.git] / dbc-client / misc / dbc-client
1 #!/bin/sh
2 #
3 # ============LICENSE_START===============================================
4 # org.onap.dmaap
5 # ========================================================================
6 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
7 # Copyright (C) 2021 Nordix Foundation.
8 # ========================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #        http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 # ============LICENSE_END=================================================
21 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
22
23 umask 0022
24 set -uex -o pipefail
25 export PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
26
27 # RESP_CACHE is (/opt/app/config/cache) empty-dir volume mount for K8s env
28 RESP_CACHE=${RESP_CACHE:-''}
29 RESP=${RESP:-'/dev/null'}
30 APP_ROOT=${APP_ROOT:-/opt/app/dbc-client}
31 IF_PUB_SUB_EXIST=${IF_PUB_SUB_EXIST:-false}
32 CONFIGMAP_ROOT=${CONFIGMAP_ROOT:-/opt/app/config}
33
34 PORT=${PORT:-8443}
35 DBC=${DBC:-dmaap-bc}
36 PROTO=${PROTO:-https}
37 PARAM=${PARAM:-'useExisting=true'}
38 REQUESTID=${REQUESTID:-dbc-client}
39 URL=${URL:-"${PROTO}"://"${DBC}":"${PORT}"/webapi/}
40
41 CA_PEM=${CA_PEM:-ca.pem}
42 KEY_PEM=${KEY_PEM:-key.pem}
43 CLIENT_PEM=${CLIENT_PEM:-client.pem}
44 PEM_DIR=${PEM_DIR:-/opt/app/osaaf/local}
45 CERT_PWD=${CERT_PWD:-'2U[iOZzMHI:.#tdCwlBqc;}S'}
46
47 BA_PWD=${BA_PWD:-'demo123456!'}
48 AUTH_METHOD=${AUTH_METHOD:-basicAuth}
49 BA_IDENTITY=${BA_IDENTITY:-dmaap-bc@dmaap-bc.onap.org}
50
51 function xcurl() {
52   curl -X POST \
53     -s "$CURL_CRED" \
54     -w "%{http_code}" \
55     -H "X-ECOMP-RequestID: $REQUESTID" \
56     -H "Content-Type: application/json" "$@"
57 }
58
59 function init_config() {
60   if [ ! -d "$APP_ROOT" -a ! -d "$CONFIGMAP_ROOT" ]; then
61     echo "Expected either App root directory $APP_ROOT Or ConfigMap directory $CONFIGMAP_ROOT does not exist."
62     exit 1
63   fi
64   cd "$PEM_DIR"
65   if [ "$AUTH_METHOD" = "basicAuth" ]; then
66     echo "-u ${BA_IDENTITY}:${BA_PWD}" >"$PEM_DIR"/curl.cred
67     CURL_CRED="-K $PEM_DIR/curl.cred"
68   elif [ -f "$CA_PEM" -a -f "$CLIENT_PEM" -a -f "$KEY_PEM" ]; then
69     printf "key \"$PEM_DIR/$KEY_PEM\"\n cacert \"$PEM_DIR/$CA_PEM\"\n cert \"$PEM_DIR/${CLIENT_PEM}:${CERT_PWD}\"" >$PEM_DIR/curl.cred
70     CURL_CRED="-K $PEM_DIR/curl.cred"
71   else
72     echo "PEM files for authorization not found..!"
73   fi
74 }
75
76 function init_dbc_provisioning() {
77   cd "$CONFIGMAP_ROOT"
78   for dir in dmaap dcaeLocations mr_clusters topics mr_clients dr_nodes feeds dr_pubs dr_subs; do
79     if [ -d ${dir} ]; then
80       for file in $(ls ${dir}/*.json); do
81         do_http_post "$file" "$dir"
82       done
83     fi
84   done
85 }
86
87 function do_http_post() {
88   RETRY_TIME=60
89   if [ -n "$RESP_CACHE" ]; then
90     RESP="$RESP_CACHE"/"$(echo "${1##*/}" | cut -d "." -f1)"-resp.json
91   fi
92   while true; do
93     if [ "$2" != "feeds" -a "$2" != "topics" ]; then
94       req_body=$(cat "${1}" | envsubst)
95       if [ -n "$RESP_CACHE" ] && [ "${2}" = "dr_pubs" -o "${2}" = "dr_subs" ]; then
96         IF_PUB_SUB_EXIST=false
97         check_pub_sub "${2}" "${req_body}" "${RESP}"
98         if [ "${IF_PUB_SUB_EXIST}" = true ]; then
99           echo "DR Publisher or Subscriber already exist.."
100           break
101         fi
102       fi
103       rc=$(xcurl -o "$RESP" -d "$req_body" "${URL}${2}")
104       if [ "$rc" = "200" -o "$rc" = "201" -o "$rc" = "409" ]; then
105         echo "Http Post request is successful with response code=$rc"
106         break
107       fi
108     else
109       rc=$(xcurl -o "$RESP" -d @"${1}" "${URL}${2}"/?"${PARAM}")
110       if [ "$rc" = "200" -o "$rc" = "201" -o "$rc" = "409" ]; then
111         echo "Http Post request for feed creation is successful with response code=$rc"
112         break
113       fi
114     fi
115     echo "$(date): Http Response code=$rc.  Will retry after $RETRY_TIME seconds.."
116     sleep "$RETRY_TIME"
117   done
118 }
119
120 function check_pub_sub() {
121   #Parameters:
122   #${1} Param - String representing dir either dr_pubs or dr_subs
123   #${2} Param - Request details (Dr Publisher/Subscriber creation input details) containing feedName, dr-userName, dr-Password, dr-LocationName
124   #${3} Param - String representing RESP_CACHE Env to '/opt/app/config/cache' or '/dev/null'
125   feed_config_resp="$RESP_CACHE"/"feedConfig-$(echo "${3//[!0-9]/}")-resp.json"
126   if [ -f "${feed_config_resp}" ]; then
127     echo "Checking if pub or sub exist.."
128     val=${1#*_}
129     dr_usr=$(echo "${2}" | jq -r '.username')
130     dr_pwd=$(echo "${2}" | jq -r '.userpwd')
131     pub_sub_cnt=$(jq ".${val} | length" "${feed_config_resp}")
132     local i=0
133     while [ "$i" -lt "$pub_sub_cnt" ]; do
134       _dr_usr=$(jq -r ".${val}[$i].username" "${feed_config_resp}")
135       _dr_pwd=$(jq -r ".${val}[$i].userpwd" "${feed_config_resp}")
136       if [ "${dr_usr}" = "${_dr_usr}" -a "${dr_pwd}" = "${_dr_pwd}" ]; then
137         if [ "${1}" = "dr_pubs" ]; then
138           echo "Publisher exist with username: ${dr_usr}, password: ${dr_pwd}"
139           echo "$(jq -c ".${val}[$i]" "${feed_config_resp}")" >"${3}"
140           IF_PUB_SUB_EXIST=true
141           break
142         elif [ "${1}" = "dr_subs" ]; then
143           dr_loc=$(echo "${2}" | jq -r '.dcaeLocationName')
144           _dr_loc=$(jq -r ".${val}[$i].dcaeLocationName" "${feed_config_resp}")
145           dr_deliveryURL=$(echo "${2}" | jq -r '.deliveryURL')
146           _dr_deliveryURL=$(jq -r ".${val}[$i].deliveryURL" "${feed_config_resp}")
147           if [ "${dr_loc}" = "${_dr_loc}" -a "${dr_deliveryURL}" = "${_dr_deliveryURL}" ]; then
148             echo "Subscriber exist with username: ${dr_usr}, password: ${dr_pwd}, dcaeLocationName: ${dr_loc}, deliveryURL: ${dr_deliveryURL}"
149             echo "$(jq -c ".${val}[$i]" "${feed_config_resp}")" >"${3}"
150             IF_PUB_SUB_EXIST=true
151             break
152           fi
153         fi
154       fi
155       i=$((i + 1))
156     done
157   else
158     echo "Feed configuration doesn't exist."
159   fi
160 }
161
162 init_config
163 init_dbc_provisioning