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 prefix 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 $ADAPTER
26 # arg: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)
27 # (Not for test scripts)
29 echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
33 if [ $1 == "PA" ]; then
36 __RESTBASE_SECURE=$RESTBASE_SECURE
37 __RETRY_CODES=$AGENT_RETRY_CODES
38 elif [ $1 == "ECS" ]; then
39 __ADAPTER=$ECS_ADAPTER
40 __RESTBASE=$ECS_RESTBASE
41 __RESTBASE_SECURE=$ECS_RESTBASE_SECURE
42 __RETRY_CODES=$ECS_RETRY_CODES
47 if [ $# -lt 3 ] || [ $# -gt 4 ]; then
53 httpcode=" -sw %{http_code}"
57 if [[ $2 == *"_BATCH" ]]; then
61 content=" -H Content-Type:application/json"
63 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
68 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
71 file=" --data-binary @$4"
73 accept=" -H accept:application/json"
74 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
76 accept=" -H accept:*/*"
80 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
85 elif [ $2 == "RESPONSE" ]; then
90 if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
98 if [ $paramError -eq 1 ]; then
100 echo "-Incorrect number of parameters to __do_curl_agent " $@ >> $HTTPLOG
101 echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG
102 echo "-Returning response 000" >> $HTTPLOG
107 if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
108 url=" "${__ADAPTER}${3}
110 curlString="curl -k "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
111 echo " CMD: "$curlString >> $HTTPLOG
112 if [ $# -eq 4 ]; then
113 echo " FILE: $(<$4)" >> $HTTPLOG
116 # Do retry for configured response codes, otherwise only one attempt
118 while [ $maxretries -ge 0 ]; do
120 let maxretries=maxretries-1
123 if [ $retcode -ne 0 ]; then
124 echo " RETCODE: "$retcode >> $HTTPLOG
129 echo " RESP: "$res >> $HTTPLOG
130 status=${res:${#res}-3}
131 if [ ! -z "${__RETRY_CODES}" ]; then
132 for retrycode in $__RETRY_CODES; do
133 if [ $retrycode -eq $status ]; then
134 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG
140 if [ $retry -eq 0 ]; then
147 if [ $oper != "RESPONSE" ]; then
149 if [ $2 == "PUT" ] && [ $# -eq 4 ]; then
150 payload="$(cat $4 | tr -d '\n' | tr -d ' ' )"
151 echo "payload: "$payload >> $HTTPLOG
152 file=" --data-binary "$payload
154 #urlencode the request url since it will be carried by send-request url
155 requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$3")
156 url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
157 curlString="curl -k -X POST${timeout}${httpcode}${content}${url}${file}"
158 echo " CMD: "$curlString >> $HTTPLOG
161 if [ $retcode -ne 0 ]; then
162 echo " RETCODE: "$retcode >> $HTTPLOG
166 echo " RESP: "$res >> $HTTPLOG
167 status=${res:${#res}-3}
168 if [ $status -ne 200 ]; then
172 cid=${res:0:${#res}-3}
173 if [[ $batch -eq 1 ]]; then
178 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
179 if [ $oper == "RESPONSE" ]; then
182 url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
183 curlString="curl -k -X GET"${timeout}${httpcode}${url}
184 echo " CMD: "$curlString >> $HTTPLOG
187 if [ $retcode -ne 0 ]; then
188 echo " RETCODE: "$retcode >> $HTTPLOG
192 echo " RESP: "$res >> $HTTPLOG
193 status=${res:${#res}-3}
195 # wait of the reply from the agent/ECS...
196 while [ $status -eq 204 ]; do
197 if [ $(($SECONDS - $TS)) -gt 90 ]; then
198 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
203 echo " CMD: "$curlString >> $HTTPLOG
205 if [ $retcode -ne 0 ]; then
206 echo " RETCODE: "$retcode >> $HTTPLOG
210 echo " RESP: "$res >> $HTTPLOG
211 status=${res:${#res}-3}
213 if [ $status -eq 200 ]; then
214 body=${res:0:${#res}-3}
218 echo "Status not 200, returning response 000" >> $HTTPLOG