blob: 4ebac805c09cc61e334ecc56378127571d309ee1 [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
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010031 proxyflag=""
32 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +020033 if [ ! -z "$KUBE_PROXY_PATH" ]; then
34 proxyflag=" --proxy $KUBE_PROXY_PATH"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010035 fi
36 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020037 paramError=0
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010038 input_url=$3
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020039 if [ $# -gt 0 ]; then
40 if [ $1 == "PA" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010041 __ADAPTER=$PA_ADAPTER
42 __ADAPTER_TYPE=$PA_ADAPTER_TYPE
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020043 __RETRY_CODES=$AGENT_RETRY_CODES
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010044 if [ $PMS_VERSION != "V1" ]; then
45 input_url=$PMS_API_PREFIX$3
46 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020047 elif [ $1 == "ECS" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010048 __ADAPTER=$ECS_ADAPTER
49 __ADAPTER_TYPE=$ECS_ADAPTER_TYPE
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020050 __RETRY_CODES=$ECS_RETRY_CODES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010051 elif [ $1 == "CR" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010052 __ADAPTER=$CR_ADAPTER
53 __ADAPTER_TYPE=$CR_ADAPTER_TYPE
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010054 __RETRY_CODES=""
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +010055 elif [ $1 == "RC" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010056 __ADAPTER=$RC_ADAPTER
57 __ADAPTER_TYPE=$RC_ADAPTER_TYPE
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +010058 __RETRY_CODES=""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010059 elif [ $1 == "NGW" ]; then
60 __ADAPTER=$NGW_ADAPTER
61 __ADAPTER_TYPE=$NGW_ADAPTER_TYPE
62 __RETRY_CODES=""
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020063 else
64 paramError=1
65 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010066 if [ $__ADAPTER_TYPE == "MR-HTTP" ]; then
67 __ADAPTER=$MR_ADAPTER_HTTP
68 fi
69 if [ $__ADAPTER_TYPE == "MR-HTTPS" ]; then
70 __ADAPTER=$MR_ADAPTER_HTTPS
71 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020072 fi
73 if [ $# -lt 3 ] || [ $# -gt 4 ]; then
74 paramError=1
75 else
76 timeout=""
77 oper=""
78 file=''
79 httpcode=" -sw %{http_code}"
80 accept=''
81 content=''
82 batch=0
83 if [[ $2 == *"_BATCH" ]]; then
84 batch=1
85 fi
86 if [ $# -gt 3 ]; then
87 content=" -H Content-Type:application/json"
88 fi
89 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
90 oper="GET"
91 if [ $# -ne 3 ]; then
92 paramError=1
93 fi
94 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
95 oper="PUT"
96 if [ $# -eq 4 ]; then
97 file=" --data-binary @$4"
98 fi
99 accept=" -H accept:application/json"
100 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
101 oper="POST"
102 accept=" -H accept:*/*"
103 if [ $# -ne 3 ]; then
104 paramError=1
105 fi
106 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
107 oper="DELETE"
108 if [ $# -ne 3 ]; then
109 paramError=1
110 fi
111 elif [ $2 == "RESPONSE" ]; then
112 oper="RESPONSE"
113 if [ $# -ne 3 ]; then
114 paramError=1
115 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100116 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
117 if [ $__ADAPTER_TYPE == "REST" ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200118 paramError=1
119 fi
120 else
121 paramError=1
122 fi
123 fi
124
125 if [ $paramError -eq 1 ]; then
126 ((RES_CONF_FAIL++))
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100127 echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200128 echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG
129 echo "-Returning response 000" >> $HTTPLOG
130 echo "-000"
131 return 1
132 fi
133
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100134 #if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
135 if [ $__ADAPTER_TYPE == "REST" ]; then
136 url=" "${__ADAPTER}${input_url}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200137 oper=" -X "$oper
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100138 curlString="curl -k $proxyflag "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200139 echo " CMD: "$curlString >> $HTTPLOG
140 if [ $# -eq 4 ]; then
141 echo " FILE: $(<$4)" >> $HTTPLOG
142 fi
143
144 # Do retry for configured response codes, otherwise only one attempt
145 maxretries=5
146 while [ $maxretries -ge 0 ]; do
147
148 let maxretries=maxretries-1
149 res=$($curlString)
150 retcode=$?
151 if [ $retcode -ne 0 ]; then
152 echo " RETCODE: "$retcode >> $HTTPLOG
153 echo "000"
154 return 1
155 fi
156 retry=0
157 echo " RESP: "$res >> $HTTPLOG
158 status=${res:${#res}-3}
159 if [ ! -z "${__RETRY_CODES}" ]; then
160 for retrycode in $__RETRY_CODES; do
161 if [ $retrycode -eq $status ]; then
162 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED >> $HTTPLOG
163 sleep 1
164 retry=1
165 fi
166 done
167 fi
168 if [ $retry -eq 0 ]; then
169 maxretries=-1
170 fi
171 done
172 echo $res
173 return 0
174 else
175 if [ $oper != "RESPONSE" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100176 requestUrl=$input_url
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200177 if [ $2 == "PUT" ] && [ $# -eq 4 ]; then
178 payload="$(cat $4 | tr -d '\n' | tr -d ' ' )"
179 echo "payload: "$payload >> $HTTPLOG
180 file=" --data-binary "$payload
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100181 elif [ $# -eq 4 ]; then
182 echo " FILE: $(cat $4)" >> $HTTPLOG
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200183 fi
184 #urlencode the request url since it will be carried by send-request url
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100185 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 +0200186 url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100187 curlString="curl -k $proxyflag -X POST${timeout}${httpcode}${content}${url}${file}"
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200188 echo " CMD: "$curlString >> $HTTPLOG
189 res=$($curlString)
190 retcode=$?
191 if [ $retcode -ne 0 ]; then
192 echo " RETCODE: "$retcode >> $HTTPLOG
193 echo "000"
194 return 1
195 fi
196 echo " RESP: "$res >> $HTTPLOG
197 status=${res:${#res}-3}
198 if [ $status -ne 200 ]; then
199 echo "000"
200 return 1
201 fi
202 cid=${res:0:${#res}-3}
203 if [[ $batch -eq 1 ]]; then
204 echo $cid"200"
205 return 0
206 fi
207 fi
208 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
209 if [ $oper == "RESPONSE" ]; then
210 cid=$3
211 fi
212 url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100213 curlString="curl -k $proxyflag -X GET"${timeout}${httpcode}${url}
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200214 echo " CMD: "$curlString >> $HTTPLOG
215 res=$($curlString)
216 retcode=$?
217 if [ $retcode -ne 0 ]; then
218 echo " RETCODE: "$retcode >> $HTTPLOG
219 echo "000"
220 return 1
221 fi
222 echo " RESP: "$res >> $HTTPLOG
223 status=${res:${#res}-3}
224 TS=$SECONDS
225 # wait of the reply from the agent/ECS...
226 while [ $status -eq 204 ]; do
227 if [ $(($SECONDS - $TS)) -gt 90 ]; then
228 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
229 echo "000"
230 return 1
231 fi
232 sleep 0.01
233 echo " CMD: "$curlString >> $HTTPLOG
234 res=$($curlString)
235 if [ $retcode -ne 0 ]; then
236 echo " RETCODE: "$retcode >> $HTTPLOG
237 echo "000"
238 return 1
239 fi
240 echo " RESP: "$res >> $HTTPLOG
241 status=${res:${#res}-3}
242 done
243 if [ $status -eq 200 ]; then
244 body=${res:0:${#res}-3}
245 echo $body
246 return 0
247 fi
248 echo "Status not 200, returning response 000" >> $HTTPLOG
249 echo "0000"
250 return 1
251 fi
252 fi
253}