Temporary skip to push sampleapp images
[nonrtric.git] / test / common / api_curl.sh
1 #!/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 A1PMS/ICS via the REST or DMAAP interface.
21 # Used by all other A1PMS/ICS api test functions
22 # If operation suffix 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: (A1PMS|ICS|CR|RC GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url>|<correlation-id> [<file> [mime-type]]) | (A1PMS|ICS RESPONSE <correlation-id>)
27 # Default mime type for file is application/json unless specified in parameter mime-type
28 # (Not for test scripts)
29 __do_curl_to_api() {
30         TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
31     echo " (${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
32         proxyflag=""
33         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"
38                 fi
39         fi
40
41         paramError=0
42         input_url=$3
43         fname=$4
44     if [ $# -gt 0 ]; then
45         if [ $1 == "A1PMS" ]; then
46                         __ADAPTER=$A1PMS_ADAPTER
47                         __ADAPTER_TYPE=$A1PMS_ADAPTER_TYPE
48             __RETRY_CODES=$A1PMS_RETRY_CODES
49                         if [ $A1PMS_VERSION != "V1" ]; then
50                                 input_url=$A1PMS_API_PREFIX$3
51                         fi
52         elif [ $1 == "ICS" ]; then
53                         __ADAPTER=$ICS_ADAPTER
54                         __ADAPTER_TYPE=$ICS_ADAPTER_TYPE
55             __RETRY_CODES=$ICS_RETRY_CODES
56                 elif [ $1 == "CR" ]; then
57                         __ADAPTER=$CR_ADAPTER
58                         __ADAPTER_TYPE=$CR_ADAPTER_TYPE
59             __RETRY_CODES=""
60                 elif [ $1 == "RC" ]; then
61                         __ADAPTER=$RC_ADAPTER
62                         __ADAPTER_TYPE=$RC_ADAPTER_TYPE
63             __RETRY_CODES=""
64         elif [ $1 == "NGW" ]; then
65                         __ADAPTER=$NGW_ADAPTER
66                         __ADAPTER_TYPE=$NGW_ADAPTER_TYPE
67             __RETRY_CODES=""
68         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=""
80         elif [ $1 == "DMAAPMR" ]; then
81                         __ADAPTER=$MR_DMAAP_ADAPTER_HTTP
82                         __ADAPTER_TYPE=$MR_DMAAP_ADAPTER_TYPE
83             __RETRY_CODES=""
84         elif [ $1 == "KAFKAPC" ]; then
85                         __ADAPTER=$KAFKAPC_ADAPTER
86                         __ADAPTER_TYPE=$KAFKAPC_ADAPTER_TYPE
87             __RETRY_CODES=""
88                 else
89             paramError=1
90         fi
91                 if [ "$__ADAPTER_TYPE" == "MR-HTTP" ]; then
92                         __ADAPTER=$MR_ADAPTER_HTTP
93                 fi
94                 if [ "$__ADAPTER_TYPE" == "MR-HTTPS" ]; then
95                         __ADAPTER=$MR_ADAPTER_HTTPS
96                 fi
97     fi
98     if [ $# -lt 3 ] || [ $# -gt 5 ]; then
99                 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"
113                         fname=$4
114                         if [ $# -gt 4 ]; then
115                                 content=" -H Content-Type:"$5
116                         fi
117                 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"
125                         if [ $# -gt 3 ]; then
126                                 file=" --data-binary @$fname"
127                         fi
128                         accept=" -H accept:application/json"
129                 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
130                         oper="POST"
131                         accept=" -H accept:*/*"
132                         if [ $# -gt 3 ]; then
133                                 file=" --data-binary @$fname"
134                                 accept=" -H accept:application/json"
135                         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
146                         if [ $__ADAPTER_TYPE == "REST" ]; then
147                                 paramError=1
148                         fi
149                 else
150                         paramError=1
151                 fi
152         fi
153
154     if [ $paramError -eq 1 ]; then
155                 ((RES_CONF_FAIL++))
156         echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
157         echo "-Expected: (A1PMS|ICS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file> [mime-type]]) | (A1PMS|ICS RESPONSE <correlation-id>)" >> $HTTPLOG
158         echo "-Returning response 000" >> $HTTPLOG
159         echo "-000"
160         return 1
161     fi
162         jwt=""
163         if [ ! -z "$KUBE_PROXY_CURL_JWT" ]; then
164                 jwt=" -H "\""Authorization: Bearer $KUBE_PROXY_CURL_JWT"\"
165         fi
166         if [ $__ADAPTER_TYPE == "REST" ]; then
167         url=" "${__ADAPTER}${input_url}
168         oper=" -X "$oper
169         curlString="curl -k $proxyflag "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
170         echo " CMD: $curlString $jwt" >> $HTTPLOG
171                 if [ $# -gt 3 ]; then
172                         echo " FILE: $(<$fname)" >> $HTTPLOG
173                 fi
174
175                 # Do retry for configured response codes, otherwise only one attempt
176                 maxretries=5
177                 while [ $maxretries -ge 0 ]; do
178
179                         let maxretries=maxretries-1
180                         if [ ! -z "$KUBE_PROXY_CURL_JWT" ]; then
181                                 res=$($curlString -H "Authorization: Bearer $KUBE_PROXY_CURL_JWT")
182                         else
183                                 res=$($curlString)
184                         fi
185                         retcode=$?
186                         if [ $retcode -ne 0 ]; then
187                                 echo " RETCODE: "$retcode >> $HTTPLOG
188                                 echo "000"
189                                 return 1
190                         fi
191                         retry=0
192                         echo " RESP: "$res >> $HTTPLOG
193                         status=${res:${#res}-3}
194                         if [ ! -z "${__RETRY_CODES}" ]; then
195                                 for retrycode in $__RETRY_CODES; do
196                                         if [ $retrycode -eq $status ]; then
197                                                 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED  >> $HTTPLOG
198                                                 sleep 1
199                                                 retry=1
200                                         fi
201                                 done
202                         fi
203                         if [ $retry -eq 0 ]; then
204                                 maxretries=-1
205                         fi
206                 done
207         echo $res
208         return 0
209     else
210                 if [ $oper != "RESPONSE" ]; then
211                         requestUrl=$input_url
212                         if [ $2 == "PUT" ] && [ $# -gt 3 ]; then
213                                 payload="$(cat $fname | tr -d '\n' | tr -d ' ' )"
214                                 echo "payload: "$payload >> $HTTPLOG
215                                 file=" --data-binary "$payload
216                         elif [ $# -gt 3 ]; then
217                                 echo " FILE: $(cat $fname)" >> $HTTPLOG
218                         fi
219                         #urlencode the request url since it will be carried by send-request url
220                         requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))"  "$input_url")
221                         url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
222                         curlString="curl -k $proxyflag -X POST${timeout}${httpcode}${content}${url}${file}"
223                         echo " CMD: "$curlString >> $HTTPLOG
224                         res=$($curlString)
225                         retcode=$?
226                         if [ $retcode -ne 0 ]; then
227                                 echo " RETCODE: "$retcode >> $HTTPLOG
228                                 echo "000"
229                                 return 1
230                         fi
231                         echo " RESP: "$res >> $HTTPLOG
232                         status=${res:${#res}-3}
233                         if [ $status -ne 200 ]; then
234                                 echo "000"
235                                 return 1
236                         fi
237                         cid=${res:0:${#res}-3}
238                         if [[ $batch -eq 1 ]]; then
239                                 echo $cid"200"
240                                 return 0
241                         fi
242                 fi
243                 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
244                         if [ $oper == "RESPONSE" ]; then
245                                 cid=$3
246                         fi
247                         url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
248                         curlString="curl -k $proxyflag  -X GET"${timeout}${httpcode}${url}
249                         echo " CMD: "$curlString >> $HTTPLOG
250                         res=$($curlString)
251                         retcode=$?
252                         if [ $retcode -ne 0 ]; then
253                                 echo " RETCODE: "$retcode >> $HTTPLOG
254                                 echo "000"
255                                 return 1
256                         fi
257                         echo " RESP: "$res >> $HTTPLOG
258                         status=${res:${#res}-3}
259                         TS=$SECONDS
260                         # wait of the reply from the A1PMS/ICS...
261                         while [ $status -eq 204 ]; do
262                                 if [ $(($SECONDS - $TS)) -gt 90 ]; then
263                                         echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
264                                         echo "000"
265                                         return 1
266                                 fi
267                                 sleep 0.01
268                                 echo " CMD: "$curlString >> $HTTPLOG
269                                 res=$($curlString)
270                                 if [ $retcode -ne 0 ]; then
271                                         echo " RETCODE: "$retcode >> $HTTPLOG
272                                         echo "000"
273                                         return 1
274                                 fi
275                                 echo " RESP: "$res >> $HTTPLOG
276                                 status=${res:${#res}-3}
277                         done
278                         if [ $status -eq 200 ]; then
279                                 body=${res:0:${#res}-3}
280                                 echo $body
281                                 return 0
282                         fi
283                         echo "Status not 200, returning response 000" >> $HTTPLOG
284                         echo "0000"
285                         return 1
286                 fi
287     fi
288 }