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>]) | (PA|ECS RESPONSE <correlation-id>)
27 # (Not for test scripts)
29 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
30 echo " (${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
32 if [ ! -z "$KUBE_PROXY_PATH" ]; then
33 if [ $KUBE_PROXY_HTTPX == "http" ]; then
34 proxyflag=" --proxy $KUBE_PROXY_PATH"
36 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
43 if [ $1 == "PA" ]; then
45 __ADAPTER_TYPE=$PA_ADAPTER_TYPE
46 __RETRY_CODES=$AGENT_RETRY_CODES
47 if [ $PMS_VERSION != "V1" ]; then
48 input_url=$PMS_API_PREFIX$3
50 elif [ $1 == "ECS" ]; then
51 __ADAPTER=$ECS_ADAPTER
52 __ADAPTER_TYPE=$ECS_ADAPTER_TYPE
53 __RETRY_CODES=$ECS_RETRY_CODES
54 elif [ $1 == "CR" ]; then
56 __ADAPTER_TYPE=$CR_ADAPTER_TYPE
58 elif [ $1 == "RC" ]; then
60 __ADAPTER_TYPE=$RC_ADAPTER_TYPE
62 elif [ $1 == "NGW" ]; then
63 __ADAPTER=$NGW_ADAPTER
64 __ADAPTER_TYPE=$NGW_ADAPTER_TYPE
66 elif [ $1 == "DMAAPADP" ]; then
67 __ADAPTER=$DMAAP_ADP_ADAPTER
68 __ADAPTER_TYPE=$DMAAP_ADP_ADAPTER_TYPE
70 elif [ $1 == "DMAAPMED" ]; then
71 __ADAPTER=$DMAAP_MED_ADAPTER
72 __ADAPTER_TYPE=$DMAAP_MED_ADAPTER_TYPE
74 elif [ $1 == "MRSTUB" ]; then
75 __ADAPTER=$MR_STUB_ADAPTER
76 __ADAPTER_TYPE=$MR_STUB_ADAPTER_TYPE
81 if [ $__ADAPTER_TYPE == "MR-HTTP" ]; then
82 __ADAPTER=$MR_ADAPTER_HTTP
84 if [ $__ADAPTER_TYPE == "MR-HTTPS" ]; then
85 __ADAPTER=$MR_ADAPTER_HTTPS
88 if [ $# -lt 3 ] || [ $# -gt 4 ]; then
94 httpcode=" -sw %{http_code}"
98 if [[ $2 == *"_BATCH" ]]; then
101 if [ $# -gt 3 ]; then
102 content=" -H Content-Type:application/json"
104 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
106 if [ $# -ne 3 ]; then
109 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
111 if [ $# -eq 4 ]; then
112 file=" --data-binary @$4"
114 accept=" -H accept:application/json"
115 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
117 accept=" -H accept:*/*"
118 if [ $# -eq 4 ]; then
119 file=" --data-binary @$4"
120 accept=" -H accept:application/json"
122 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
124 if [ $# -ne 3 ]; then
127 elif [ $2 == "RESPONSE" ]; then
129 if [ $# -ne 3 ]; then
132 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
133 if [ $__ADAPTER_TYPE == "REST" ]; then
141 if [ $paramError -eq 1 ]; then
143 echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
144 echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG
145 echo "-Returning response 000" >> $HTTPLOG
150 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
151 if [ $__ADAPTER_TYPE == "REST" ]; then
152 url=" "${__ADAPTER}${input_url}
154 curlString="curl -k $proxyflag "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
155 echo " CMD: "$curlString >> $HTTPLOG
156 if [ $# -eq 4 ]; then
157 echo " FILE: $(<$4)" >> $HTTPLOG
160 # Do retry for configured response codes, otherwise only one attempt
162 while [ $maxretries -ge 0 ]; do
164 let maxretries=maxretries-1
167 if [ $retcode -ne 0 ]; then
168 echo " RETCODE: "$retcode >> $HTTPLOG
173 echo " RESP: "$res >> $HTTPLOG
174 status=${res:${#res}-3}
175 if [ ! -z "${__RETRY_CODES}" ]; then
176 for retrycode in $__RETRY_CODES; do
177 if [ $retrycode -eq $status ]; then
178 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG
184 if [ $retry -eq 0 ]; then
191 if [ $oper != "RESPONSE" ]; then
192 requestUrl=$input_url
193 if [ $2 == "PUT" ] && [ $# -eq 4 ]; then
194 payload="$(cat $4 | tr -d '\n' | tr -d ' ' )"
195 echo "payload: "$payload >> $HTTPLOG
196 file=" --data-binary "$payload
197 elif [ $# -eq 4 ]; then
198 echo " FILE: $(cat $4)" >> $HTTPLOG
200 #urlencode the request url since it will be carried by send-request url
201 requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$input_url")
202 url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
203 curlString="curl -k $proxyflag -X POST${timeout}${httpcode}${content}${url}${file}"
204 echo " CMD: "$curlString >> $HTTPLOG
207 if [ $retcode -ne 0 ]; then
208 echo " RETCODE: "$retcode >> $HTTPLOG
212 echo " RESP: "$res >> $HTTPLOG
213 status=${res:${#res}-3}
214 if [ $status -ne 200 ]; then
218 cid=${res:0:${#res}-3}
219 if [[ $batch -eq 1 ]]; then
224 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
225 if [ $oper == "RESPONSE" ]; then
228 url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
229 curlString="curl -k $proxyflag -X GET"${timeout}${httpcode}${url}
230 echo " CMD: "$curlString >> $HTTPLOG
233 if [ $retcode -ne 0 ]; then
234 echo " RETCODE: "$retcode >> $HTTPLOG
238 echo " RESP: "$res >> $HTTPLOG
239 status=${res:${#res}-3}
241 # wait of the reply from the agent/ECS...
242 while [ $status -eq 204 ]; do
243 if [ $(($SECONDS - $TS)) -gt 90 ]; then
244 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
249 echo " CMD: "$curlString >> $HTTPLOG
251 if [ $retcode -ne 0 ]; then
252 echo " RETCODE: "$retcode >> $HTTPLOG
256 echo " RESP: "$res >> $HTTPLOG
257 status=${res:${#res}-3}
259 if [ $status -eq 200 ]; then
260 body=${res:0:${#res}-3}
264 echo "Status not 200, returning response 000" >> $HTTPLOG