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
34 if [ $1 == "PA" ]; then
36 __ADAPTER_TYPE=$PA_ADAPTER_TYPE
37 __RETRY_CODES=$AGENT_RETRY_CODES
38 if [ $PMS_VERSION != "V1" ]; then
39 input_url=$PMS_API_PREFIX$3
41 elif [ $1 == "ECS" ]; then
42 __ADAPTER=$ECS_ADAPTER
43 __ADAPTER_TYPE=$ECS_ADAPTER_TYPE
44 __RETRY_CODES=$ECS_RETRY_CODES
45 elif [ $1 == "CR" ]; then
47 __ADAPTER_TYPE=$CR_ADAPTER_TYPE
49 elif [ $1 == "RC" ]; then
51 __ADAPTER_TYPE=$RC_ADAPTER_TYPE
56 if [ $__ADAPTER_TYPE == "MR-HTTP" ]; then
57 __ADAPTER=$MR_ADAPTER_HTTP
59 if [ $__ADAPTER_TYPE == "MR-HTTPS" ]; then
60 __ADAPTER=$MR_ADAPTER_HTTPS
63 if [ $# -lt 3 ] || [ $# -gt 4 ]; then
69 httpcode=" -sw %{http_code}"
73 if [[ $2 == *"_BATCH" ]]; then
77 content=" -H Content-Type:application/json"
79 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
84 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
87 file=" --data-binary @$4"
89 accept=" -H accept:application/json"
90 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
92 accept=" -H accept:*/*"
96 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
101 elif [ $2 == "RESPONSE" ]; then
103 if [ $# -ne 3 ]; then
106 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
107 if [ $__ADAPTER_TYPE == "REST" ]; then
115 if [ $paramError -eq 1 ]; then
117 echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
118 echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG
119 echo "-Returning response 000" >> $HTTPLOG
124 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
125 if [ $__ADAPTER_TYPE == "REST" ]; then
126 url=" "${__ADAPTER}${input_url}
128 curlString="curl -k "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
129 echo " CMD: "$curlString >> $HTTPLOG
130 if [ $# -eq 4 ]; then
131 echo " FILE: $(<$4)" >> $HTTPLOG
134 # Do retry for configured response codes, otherwise only one attempt
136 while [ $maxretries -ge 0 ]; do
138 let maxretries=maxretries-1
141 if [ $retcode -ne 0 ]; then
142 echo " RETCODE: "$retcode >> $HTTPLOG
147 echo " RESP: "$res >> $HTTPLOG
148 status=${res:${#res}-3}
149 if [ ! -z "${__RETRY_CODES}" ]; then
150 for retrycode in $__RETRY_CODES; do
151 if [ $retrycode -eq $status ]; then
152 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG
158 if [ $retry -eq 0 ]; then
165 if [ $oper != "RESPONSE" ]; then
166 requestUrl=$input_url
167 if [ $2 == "PUT" ] && [ $# -eq 4 ]; then
168 payload="$(cat $4 | tr -d '\n' | tr -d ' ' )"
169 echo "payload: "$payload >> $HTTPLOG
170 file=" --data-binary "$payload
171 elif [ $# -eq 4 ]; then
172 echo " FILE: $(cat $4)" >> $HTTPLOG
174 #urlencode the request url since it will be carried by send-request url
175 requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$input_url")
176 url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
177 curlString="curl -k -X POST${timeout}${httpcode}${content}${url}${file}"
178 echo " CMD: "$curlString >> $HTTPLOG
181 if [ $retcode -ne 0 ]; then
182 echo " RETCODE: "$retcode >> $HTTPLOG
186 echo " RESP: "$res >> $HTTPLOG
187 status=${res:${#res}-3}
188 if [ $status -ne 200 ]; then
192 cid=${res:0:${#res}-3}
193 if [[ $batch -eq 1 ]]; then
198 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
199 if [ $oper == "RESPONSE" ]; then
202 url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
203 curlString="curl -k -X GET"${timeout}${httpcode}${url}
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}
215 # wait of the reply from the agent/ECS...
216 while [ $status -eq 204 ]; do
217 if [ $(($SECONDS - $TS)) -gt 90 ]; then
218 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
223 echo " CMD: "$curlString >> $HTTPLOG
225 if [ $retcode -ne 0 ]; then
226 echo " RETCODE: "$retcode >> $HTTPLOG
230 echo " RESP: "$res >> $HTTPLOG
231 status=${res:${#res}-3}
233 if [ $status -eq 200 ]; then
234 body=${res:0:${#res}-3}
238 echo "Status not 200, returning response 000" >> $HTTPLOG