blob: 1ea47dd6b3747150422d195433cccc694d318331 [file] [log] [blame]
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001#!/bin/bash
2
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
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
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=================================================
18#
19
20# Generic function to query the agent/ECS via the REST or DMAAP interface.
21# Used by all other agent/ECS api test functions
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010022# If operation sufffix is '_BATCH' the the send and get response is split in two sequences,
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020023# one for sending the requests and one for receiving the response
24# but only when using the DMAAP interface
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010025# 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>)
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020027# (Not for test scripts)
28__do_curl_to_api() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010029 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
30 echo " (${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020031 paramError=0
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010032 input_url=$3
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020033 if [ $# -gt 0 ]; then
34 if [ $1 == "PA" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010035 __ADAPTER=$PA_ADAPTER
36 __ADAPTER_TYPE=$PA_ADAPTER_TYPE
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020037 __RETRY_CODES=$AGENT_RETRY_CODES
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010038 if [ $PMS_VERSION != "V1" ]; then
39 input_url=$PMS_API_PREFIX$3
40 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020041 elif [ $1 == "ECS" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010042 __ADAPTER=$ECS_ADAPTER
43 __ADAPTER_TYPE=$ECS_ADAPTER_TYPE
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020044 __RETRY_CODES=$ECS_RETRY_CODES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010045 elif [ $1 == "CR" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010046 __ADAPTER=$CR_ADAPTER
47 __ADAPTER_TYPE=$CR_ADAPTER_TYPE
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010048 __RETRY_CODES=""
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +010049 elif [ $1 == "RC" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010050 __ADAPTER=$RC_ADAPTER
51 __ADAPTER_TYPE=$RC_ADAPTER_TYPE
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +010052 __RETRY_CODES=""
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020053 else
54 paramError=1
55 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010056 if [ $__ADAPTER_TYPE == "MR-HTTP" ]; then
57 __ADAPTER=$MR_ADAPTER_HTTP
58 fi
59 if [ $__ADAPTER_TYPE == "MR-HTTPS" ]; then
60 __ADAPTER=$MR_ADAPTER_HTTPS
61 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020062 fi
63 if [ $# -lt 3 ] || [ $# -gt 4 ]; then
64 paramError=1
65 else
66 timeout=""
67 oper=""
68 file=''
69 httpcode=" -sw %{http_code}"
70 accept=''
71 content=''
72 batch=0
73 if [[ $2 == *"_BATCH" ]]; then
74 batch=1
75 fi
76 if [ $# -gt 3 ]; then
77 content=" -H Content-Type:application/json"
78 fi
79 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
80 oper="GET"
81 if [ $# -ne 3 ]; then
82 paramError=1
83 fi
84 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
85 oper="PUT"
86 if [ $# -eq 4 ]; then
87 file=" --data-binary @$4"
88 fi
89 accept=" -H accept:application/json"
90 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
91 oper="POST"
92 accept=" -H accept:*/*"
93 if [ $# -ne 3 ]; then
94 paramError=1
95 fi
96 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
97 oper="DELETE"
98 if [ $# -ne 3 ]; then
99 paramError=1
100 fi
101 elif [ $2 == "RESPONSE" ]; then
102 oper="RESPONSE"
103 if [ $# -ne 3 ]; then
104 paramError=1
105 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100106 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
107 if [ $__ADAPTER_TYPE == "REST" ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200108 paramError=1
109 fi
110 else
111 paramError=1
112 fi
113 fi
114
115 if [ $paramError -eq 1 ]; then
116 ((RES_CONF_FAIL++))
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100117 echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200118 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
120 echo "-000"
121 return 1
122 fi
123
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100124 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
125 if [ $__ADAPTER_TYPE == "REST" ]; then
126 url=" "${__ADAPTER}${input_url}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200127 oper=" -X "$oper
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
132 fi
133
134 # Do retry for configured response codes, otherwise only one attempt
135 maxretries=5
136 while [ $maxretries -ge 0 ]; do
137
138 let maxretries=maxretries-1
139 res=$($curlString)
140 retcode=$?
141 if [ $retcode -ne 0 ]; then
142 echo " RETCODE: "$retcode >> $HTTPLOG
143 echo "000"
144 return 1
145 fi
146 retry=0
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
153 sleep 1
154 retry=1
155 fi
156 done
157 fi
158 if [ $retry -eq 0 ]; then
159 maxretries=-1
160 fi
161 done
162 echo $res
163 return 0
164 else
165 if [ $oper != "RESPONSE" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100166 requestUrl=$input_url
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200167 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
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100171 elif [ $# -eq 4 ]; then
172 echo " FILE: $(cat $4)" >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200173 fi
174 #urlencode the request url since it will be carried by send-request url
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100175 requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$input_url")
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200176 url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
177 curlString="curl -k -X POST${timeout}${httpcode}${content}${url}${file}"
178 echo " CMD: "$curlString >> $HTTPLOG
179 res=$($curlString)
180 retcode=$?
181 if [ $retcode -ne 0 ]; then
182 echo " RETCODE: "$retcode >> $HTTPLOG
183 echo "000"
184 return 1
185 fi
186 echo " RESP: "$res >> $HTTPLOG
187 status=${res:${#res}-3}
188 if [ $status -ne 200 ]; then
189 echo "000"
190 return 1
191 fi
192 cid=${res:0:${#res}-3}
193 if [[ $batch -eq 1 ]]; then
194 echo $cid"200"
195 return 0
196 fi
197 fi
198 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
199 if [ $oper == "RESPONSE" ]; then
200 cid=$3
201 fi
202 url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
203 curlString="curl -k -X GET"${timeout}${httpcode}${url}
204 echo " CMD: "$curlString >> $HTTPLOG
205 res=$($curlString)
206 retcode=$?
207 if [ $retcode -ne 0 ]; then
208 echo " RETCODE: "$retcode >> $HTTPLOG
209 echo "000"
210 return 1
211 fi
212 echo " RESP: "$res >> $HTTPLOG
213 status=${res:${#res}-3}
214 TS=$SECONDS
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
219 echo "000"
220 return 1
221 fi
222 sleep 0.01
223 echo " CMD: "$curlString >> $HTTPLOG
224 res=$($curlString)
225 if [ $retcode -ne 0 ]; then
226 echo " RETCODE: "$retcode >> $HTTPLOG
227 echo "000"
228 return 1
229 fi
230 echo " RESP: "$res >> $HTTPLOG
231 status=${res:${#res}-3}
232 done
233 if [ $status -eq 200 ]; then
234 body=${res:0:${#res}-3}
235 echo $body
236 return 0
237 fi
238 echo "Status not 200, returning response 000" >> $HTTPLOG
239 echo "0000"
240 return 1
241 fi
242 fi
243}