3 # ============LICENSE_START===============================================
4 # Copyright (C) 2020 Nordix Foundation. All rights reserved.
5 # ========================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=================================================
20 # Generic function to query the agent/ECS via the REST or DMAAP interface.
21 # Used by all other agent/ECS api test functions
22 # If operation sufffix is '_BATCH' the the send and get response is split in two sequences,
23 # one for sending the requests and one for receiving the response
24 # but only when using the DMAAP interface
25 # REST or DMAAP is controlled of the base url of $XX_ADAPTER
26 # arg: (PA|ECS|CR|RC GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url>|<correlation-id> [<file> [mime-type]]) | (PA|ECS RESPONSE <correlation-id>)
27 # Default mime type for file is application/json unless specified in parameter mime-type
28 # (Not for test scripts)
30 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
31 echo " (${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
33 if [ ! -z "$KUBE_PROXY_PATH" ]; then
34 if [ $KUBE_PROXY_HTTPX == "http" ]; then
35 proxyflag=" --proxy $KUBE_PROXY_PATH"
37 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
45 if [ $1 == "PA" ]; then
47 __ADAPTER_TYPE=$PA_ADAPTER_TYPE
48 __RETRY_CODES=$AGENT_RETRY_CODES
49 if [ $PMS_VERSION != "V1" ]; then
50 input_url=$PMS_API_PREFIX$3
52 elif [ $1 == "ECS" ]; then
53 __ADAPTER=$ECS_ADAPTER
54 __ADAPTER_TYPE=$ECS_ADAPTER_TYPE
55 __RETRY_CODES=$ECS_RETRY_CODES
56 elif [ $1 == "CR" ]; then
58 __ADAPTER_TYPE=$CR_ADAPTER_TYPE
60 elif [ $1 == "RC" ]; then
62 __ADAPTER_TYPE=$RC_ADAPTER_TYPE
64 elif [ $1 == "NGW" ]; then
65 __ADAPTER=$NGW_ADAPTER
66 __ADAPTER_TYPE=$NGW_ADAPTER_TYPE
68 elif [ $1 == "DMAAPADP" ]; then
69 __ADAPTER=$DMAAP_ADP_ADAPTER
70 __ADAPTER_TYPE=$DMAAP_ADP_ADAPTER_TYPE
72 elif [ $1 == "DMAAPMED" ]; then
73 __ADAPTER=$DMAAP_MED_ADAPTER
74 __ADAPTER_TYPE=$DMAAP_MED_ADAPTER_TYPE
76 elif [ $1 == "MRSTUB" ]; then
77 __ADAPTER=$MR_STUB_ADAPTER
78 __ADAPTER_TYPE=$MR_STUB_ADAPTER_TYPE
80 elif [ $1 == "DMAAPMR" ]; then
81 __ADAPTER=$MR_DMAAP_ADAPTER_HTTP
82 __ADAPTER_TYPE=$MR_DMAAP_ADAPTER_TYPE
87 if [ "$__ADAPTER_TYPE" == "MR-HTTP" ]; then
88 __ADAPTER=$MR_ADAPTER_HTTP
90 if [ "$__ADAPTER_TYPE" == "MR-HTTPS" ]; then
91 __ADAPTER=$MR_ADAPTER_HTTPS
94 if [ $# -lt 3 ] || [ $# -gt 5 ]; then
100 httpcode=" -sw %{http_code}"
104 if [[ $2 == *"_BATCH" ]]; then
107 if [ $# -gt 3 ]; then
108 content=" -H Content-Type:application/json"
110 if [ $# -gt 4 ]; then
111 content=" -H Content-Type:"$5
114 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
116 if [ $# -ne 3 ]; then
119 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
121 if [ $# -gt 3 ]; then
122 file=" --data-binary @$fname"
124 accept=" -H accept:application/json"
125 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
127 accept=" -H accept:*/*"
128 if [ $# -gt 3 ]; then
129 file=" --data-binary @$fname"
130 accept=" -H accept:application/json"
132 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
134 if [ $# -ne 3 ]; then
137 elif [ $2 == "RESPONSE" ]; then
139 if [ $# -ne 3 ]; then
142 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
143 if [ $__ADAPTER_TYPE == "REST" ]; then
151 if [ $paramError -eq 1 ]; then
153 echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
154 echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG
155 echo "-Returning response 000" >> $HTTPLOG
160 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
161 if [ $__ADAPTER_TYPE == "REST" ]; then
162 url=" "${__ADAPTER}${input_url}
164 curlString="curl -k $proxyflag "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
165 echo " CMD: "$curlString >> $HTTPLOG
166 if [ $# -gt 3 ]; then
167 echo " FILE: $(<$fname)" >> $HTTPLOG
170 # Do retry for configured response codes, otherwise only one attempt
172 while [ $maxretries -ge 0 ]; do
174 let maxretries=maxretries-1
177 if [ $retcode -ne 0 ]; then
178 echo " RETCODE: "$retcode >> $HTTPLOG
183 echo " RESP: "$res >> $HTTPLOG
184 status=${res:${#res}-3}
185 if [ ! -z "${__RETRY_CODES}" ]; then
186 for retrycode in $__RETRY_CODES; do
187 if [ $retrycode -eq $status ]; then
188 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG
194 if [ $retry -eq 0 ]; then
201 if [ $oper != "RESPONSE" ]; then
202 requestUrl=$input_url
203 if [ $2 == "PUT" ] && [ $# -gt 3 ]; then
204 payload="$(cat $fname | tr -d '\n' | tr -d ' ' )"
205 echo "payload: "$payload >> $HTTPLOG
206 file=" --data-binary "$payload
207 elif [ $# -gt 3 ]; then
208 echo " FILE: $(cat $fname)" >> $HTTPLOG
210 #urlencode the request url since it will be carried by send-request url
211 requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$input_url")
212 url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
213 curlString="curl -k $proxyflag -X POST${timeout}${httpcode}${content}${url}${file}"
214 echo " CMD: "$curlString >> $HTTPLOG
217 if [ $retcode -ne 0 ]; then
218 echo " RETCODE: "$retcode >> $HTTPLOG
222 echo " RESP: "$res >> $HTTPLOG
223 status=${res:${#res}-3}
224 if [ $status -ne 200 ]; then
228 cid=${res:0:${#res}-3}
229 if [[ $batch -eq 1 ]]; then
234 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
235 if [ $oper == "RESPONSE" ]; then
238 url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
239 curlString="curl -k $proxyflag -X GET"${timeout}${httpcode}${url}
240 echo " CMD: "$curlString >> $HTTPLOG
243 if [ $retcode -ne 0 ]; then
244 echo " RETCODE: "$retcode >> $HTTPLOG
248 echo " RESP: "$res >> $HTTPLOG
249 status=${res:${#res}-3}
251 # wait of the reply from the agent/ECS...
252 while [ $status -eq 204 ]; do
253 if [ $(($SECONDS - $TS)) -gt 90 ]; then
254 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
259 echo " CMD: "$curlString >> $HTTPLOG
261 if [ $retcode -ne 0 ]; then
262 echo " RETCODE: "$retcode >> $HTTPLOG
266 echo " RESP: "$res >> $HTTPLOG
267 status=${res:${#res}-3}
269 if [ $status -eq 200 ]; then
270 body=${res:0:${#res}-3}
274 echo "Status not 200, returning response 000" >> $HTTPLOG