blob: 85794f85771500abdf7b079695387373b073c457 [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
BjornMagnussonXA007b6452021-11-29 08:03:38 +010020# Generic function to query the agent/ICS via the REST or DMAAP interface.
21# Used by all other agent/ICS 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
BjornMagnussonXA007b6452021-11-29 08:03:38 +010026# arg: (PA|ICS|CR|RC GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url>|<correlation-id> [<file> [mime-type]]) | (PA|ICS RESPONSE <correlation-id>)
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010027# Default mime type for file is application/json unless specified in parameter mime-type
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020028# (Not for test scripts)
29__do_curl_to_api() {
BjornMagnussonXA7b36db62020-11-23 10:57:57 +010030 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
31 echo " (${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010032 proxyflag=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +010033 if [ ! -z "$KUBE_PROXY_PATH" ]; then
34 if [ $KUBE_PROXY_HTTPX == "http" ]; then
35 proxyflag=" --proxy $KUBE_PROXY_PATH"
36 else
37 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010038 fi
39 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +010040
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020041 paramError=0
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010042 input_url=$3
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010043 fname=$4
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020044 if [ $# -gt 0 ]; then
45 if [ $1 == "PA" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010046 __ADAPTER=$PA_ADAPTER
47 __ADAPTER_TYPE=$PA_ADAPTER_TYPE
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020048 __RETRY_CODES=$AGENT_RETRY_CODES
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010049 if [ $PMS_VERSION != "V1" ]; then
50 input_url=$PMS_API_PREFIX$3
51 fi
BjornMagnussonXA007b6452021-11-29 08:03:38 +010052 elif [ $1 == "ICS" ]; then
53 __ADAPTER=$ICS_ADAPTER
54 __ADAPTER_TYPE=$ICS_ADAPTER_TYPE
55 __RETRY_CODES=$ICS_RETRY_CODES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010056 elif [ $1 == "CR" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010057 __ADAPTER=$CR_ADAPTER
58 __ADAPTER_TYPE=$CR_ADAPTER_TYPE
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010059 __RETRY_CODES=""
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +010060 elif [ $1 == "RC" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010061 __ADAPTER=$RC_ADAPTER
62 __ADAPTER_TYPE=$RC_ADAPTER_TYPE
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +010063 __RETRY_CODES=""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010064 elif [ $1 == "NGW" ]; then
65 __ADAPTER=$NGW_ADAPTER
66 __ADAPTER_TYPE=$NGW_ADAPTER_TYPE
67 __RETRY_CODES=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +010068 elif [ $1 == "DMAAPADP" ]; then
69 __ADAPTER=$DMAAP_ADP_ADAPTER
70 __ADAPTER_TYPE=$DMAAP_ADP_ADAPTER_TYPE
71 __RETRY_CODES=""
72 elif [ $1 == "DMAAPMED" ]; then
73 __ADAPTER=$DMAAP_MED_ADAPTER
74 __ADAPTER_TYPE=$DMAAP_MED_ADAPTER_TYPE
75 __RETRY_CODES=""
76 elif [ $1 == "MRSTUB" ]; then
77 __ADAPTER=$MR_STUB_ADAPTER
78 __ADAPTER_TYPE=$MR_STUB_ADAPTER_TYPE
79 __RETRY_CODES=""
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010080 elif [ $1 == "DMAAPMR" ]; then
81 __ADAPTER=$MR_DMAAP_ADAPTER_HTTP
82 __ADAPTER_TYPE=$MR_DMAAP_ADAPTER_TYPE
83 __RETRY_CODES=""
BjornMagnussonXA05bfe482021-12-02 08:47:41 +010084 elif [ $1 == "KAFKAPC" ]; then
85 __ADAPTER=$KAFKAPC_ADAPTER
86 __ADAPTER_TYPE=$KAFKAPC_ADAPTER_TYPE
87 __RETRY_CODES=""
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010088 else
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020089 paramError=1
90 fi
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010091 if [ "$__ADAPTER_TYPE" == "MR-HTTP" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010092 __ADAPTER=$MR_ADAPTER_HTTP
93 fi
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010094 if [ "$__ADAPTER_TYPE" == "MR-HTTPS" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010095 __ADAPTER=$MR_ADAPTER_HTTPS
96 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020097 fi
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010098 if [ $# -lt 3 ] || [ $# -gt 5 ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020099 paramError=1
100 else
101 timeout=""
102 oper=""
103 file=''
104 httpcode=" -sw %{http_code}"
105 accept=''
106 content=''
107 batch=0
108 if [[ $2 == *"_BATCH" ]]; then
109 batch=1
110 fi
111 if [ $# -gt 3 ]; then
112 content=" -H Content-Type:application/json"
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100113 fname=$4
114 if [ $# -gt 4 ]; then
115 content=" -H Content-Type:"$5
116 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200117 fi
118 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
119 oper="GET"
120 if [ $# -ne 3 ]; then
121 paramError=1
122 fi
123 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
124 oper="PUT"
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100125 if [ $# -gt 3 ]; then
126 file=" --data-binary @$fname"
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200127 fi
128 accept=" -H accept:application/json"
129 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
130 oper="POST"
131 accept=" -H accept:*/*"
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100132 if [ $# -gt 3 ]; then
133 file=" --data-binary @$fname"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100134 accept=" -H accept:application/json"
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200135 fi
136 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
137 oper="DELETE"
138 if [ $# -ne 3 ]; then
139 paramError=1
140 fi
141 elif [ $2 == "RESPONSE" ]; then
142 oper="RESPONSE"
143 if [ $# -ne 3 ]; then
144 paramError=1
145 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100146 if [ $__ADAPTER_TYPE == "REST" ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200147 paramError=1
148 fi
149 else
150 paramError=1
151 fi
152 fi
153
154 if [ $paramError -eq 1 ]; then
155 ((RES_CONF_FAIL++))
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100156 echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
BjornMagnussonXA05bfe482021-12-02 08:47:41 +0100157 echo "-Expected: (PA|ICS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file> [mime-type]]) | (PA|ICS RESPONSE <correlation-id>)" >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200158 echo "-Returning response 000" >> $HTTPLOG
159 echo "-000"
160 return 1
161 fi
162
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100163 if [ $__ADAPTER_TYPE == "REST" ]; then
164 url=" "${__ADAPTER}${input_url}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200165 oper=" -X "$oper
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100166 curlString="curl -k $proxyflag "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200167 echo " CMD: "$curlString >> $HTTPLOG
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100168 if [ $# -gt 3 ]; then
169 echo " FILE: $(<$fname)" >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200170 fi
171
172 # Do retry for configured response codes, otherwise only one attempt
173 maxretries=5
174 while [ $maxretries -ge 0 ]; do
175
176 let maxretries=maxretries-1
177 res=$($curlString)
178 retcode=$?
179 if [ $retcode -ne 0 ]; then
180 echo " RETCODE: "$retcode >> $HTTPLOG
181 echo "000"
182 return 1
183 fi
184 retry=0
185 echo " RESP: "$res >> $HTTPLOG
186 status=${res:${#res}-3}
187 if [ ! -z "${__RETRY_CODES}" ]; then
188 for retrycode in $__RETRY_CODES; do
189 if [ $retrycode -eq $status ]; then
190 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG
191 sleep 1
192 retry=1
193 fi
194 done
195 fi
196 if [ $retry -eq 0 ]; then
197 maxretries=-1
198 fi
199 done
200 echo $res
201 return 0
202 else
203 if [ $oper != "RESPONSE" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100204 requestUrl=$input_url
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100205 if [ $2 == "PUT" ] && [ $# -gt 3 ]; then
206 payload="$(cat $fname | tr -d '\n' | tr -d ' ' )"
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200207 echo "payload: "$payload >> $HTTPLOG
208 file=" --data-binary "$payload
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100209 elif [ $# -gt 3 ]; then
210 echo " FILE: $(cat $fname)" >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200211 fi
212 #urlencode the request url since it will be carried by send-request url
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100213 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 +0200214 url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100215 curlString="curl -k $proxyflag -X POST${timeout}${httpcode}${content}${url}${file}"
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200216 echo " CMD: "$curlString >> $HTTPLOG
217 res=$($curlString)
218 retcode=$?
219 if [ $retcode -ne 0 ]; then
220 echo " RETCODE: "$retcode >> $HTTPLOG
221 echo "000"
222 return 1
223 fi
224 echo " RESP: "$res >> $HTTPLOG
225 status=${res:${#res}-3}
226 if [ $status -ne 200 ]; then
227 echo "000"
228 return 1
229 fi
230 cid=${res:0:${#res}-3}
231 if [[ $batch -eq 1 ]]; then
232 echo $cid"200"
233 return 0
234 fi
235 fi
236 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
237 if [ $oper == "RESPONSE" ]; then
238 cid=$3
239 fi
240 url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100241 curlString="curl -k $proxyflag -X GET"${timeout}${httpcode}${url}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200242 echo " CMD: "$curlString >> $HTTPLOG
243 res=$($curlString)
244 retcode=$?
245 if [ $retcode -ne 0 ]; then
246 echo " RETCODE: "$retcode >> $HTTPLOG
247 echo "000"
248 return 1
249 fi
250 echo " RESP: "$res >> $HTTPLOG
251 status=${res:${#res}-3}
252 TS=$SECONDS
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100253 # wait of the reply from the agent/ICS...
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200254 while [ $status -eq 204 ]; do
255 if [ $(($SECONDS - $TS)) -gt 90 ]; then
256 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
257 echo "000"
258 return 1
259 fi
260 sleep 0.01
261 echo " CMD: "$curlString >> $HTTPLOG
262 res=$($curlString)
263 if [ $retcode -ne 0 ]; then
264 echo " RETCODE: "$retcode >> $HTTPLOG
265 echo "000"
266 return 1
267 fi
268 echo " RESP: "$res >> $HTTPLOG
269 status=${res:${#res}-3}
270 done
271 if [ $status -eq 200 ]; then
272 body=${res:0:${#res}-3}
273 echo $body
274 return 0
275 fi
276 echo "Status not 200, returning response 000" >> $HTTPLOG
277 echo "0000"
278 return 1
279 fi
280 fi
281}