Merge "Add docker-compose.yaml file for r-app-catalogue"
[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 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)
28 __do_curl_to_api() {
29         TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
30     echo " (${BASH_LINENO[0]}) - ${TIMESTAMP}: ${FUNCNAME[0]}" $@ >> $HTTPLOG
31         paramError=0
32
33     if [ $# -gt 0 ]; then
34         if [ $1 == "PA" ]; then
35             __ADAPTER=$ADAPTER
36             __RESTBASE=$RESTBASE
37             __RESTBASE_SECURE=$RESTBASE_SECURE
38             __RETRY_CODES=$AGENT_RETRY_CODES
39         elif [ $1 == "ECS" ]; then
40             __ADAPTER=$ECS_ADAPTER
41             __RESTBASE=$ECS_RESTBASE
42             __RESTBASE_SECURE=$ECS_RESTBASE_SECURE
43             __RETRY_CODES=$ECS_RETRY_CODES
44                 elif [ $1 == "CR" ]; then
45                     __ADAPTER=$CR_ADAPTER
46             __RESTBASE=$CR_RESTBASE
47             __RESTBASE_SECURE=$CR_RESTBASE_SECURE
48             __RETRY_CODES=""
49         else
50             paramError=1
51         fi
52     fi
53     if [ $# -lt 3 ] || [ $# -gt 4 ]; then
54                 paramError=1
55     else
56                 timeout=""
57                 oper=""
58                 file=''
59                 httpcode=" -sw %{http_code}"
60                 accept=''
61                 content=''
62                 batch=0
63                 if [[ $2 == *"_BATCH" ]]; then
64                         batch=1
65                 fi
66                 if [ $# -gt 3 ]; then
67                         content=" -H Content-Type:application/json"
68                 fi
69                 if [ $2 == "GET" ] || [ $2 == "GET_BATCH" ]; then
70                         oper="GET"
71                         if [ $# -ne 3 ]; then
72                                 paramError=1
73                         fi
74                 elif [ $2 == "PUT" ] || [ $2 == "PUT_BATCH" ]; then
75                         oper="PUT"
76                         if [ $# -eq 4 ]; then
77                                 file=" --data-binary @$4"
78                         fi
79                         accept=" -H accept:application/json"
80                 elif [ $2 == "POST" ] || [ $2 == "POST_BATCH" ]; then
81                         oper="POST"
82                         accept=" -H accept:*/*"
83                         if [ $# -ne 3 ]; then
84                                 paramError=1
85                         fi
86                 elif [ $2 == "DELETE" ] || [ $2 == "DELETE_BATCH" ]; then
87                         oper="DELETE"
88                         if [ $# -ne 3 ]; then
89                                 paramError=1
90                         fi
91                 elif [ $2 == "RESPONSE" ]; then
92                         oper="RESPONSE"
93                         if [ $# -ne 3 ]; then
94                                 paramError=1
95                         fi
96                         if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
97                                 paramError=1
98                         fi
99                 else
100                         paramError=1
101                 fi
102         fi
103
104     if [ $paramError -eq 1 ]; then
105                 ((RES_CONF_FAIL++))
106         echo "-Incorrect number of parameters to __do_curl_to_api " $@ >> $HTTPLOG
107         echo "-Expected: (PA|ECS GET|PUT|POST|DELETE|GET_BATCH|PUT_BATCH|POST_BATCH|DELETE_BATCH <url> [<file>]) | (PA|ECS RESPONSE <correlation-id>)" >> $HTTPLOG
108         echo "-Returning response 000" >> $HTTPLOG
109         echo "-000"
110         return 1
111     fi
112
113     if [ $__ADAPTER == $__RESTBASE ] || [ $__ADAPTER == $__RESTBASE_SECURE ]; then
114         url=" "${__ADAPTER}${3}
115         oper=" -X "$oper
116         curlString="curl -k "${oper}${timeout}${httpcode}${accept}${content}${url}${file}
117         echo " CMD: "$curlString >> $HTTPLOG
118                 if [ $# -eq 4 ]; then
119                         echo " FILE: $(<$4)" >> $HTTPLOG
120                 fi
121
122                 # Do retry for configured response codes, otherwise only one attempt
123                 maxretries=5
124                 while [ $maxretries -ge 0 ]; do
125
126                         let maxretries=maxretries-1
127                         res=$($curlString)
128                         retcode=$?
129                         if [ $retcode -ne 0 ]; then
130                                 echo " RETCODE: "$retcode >> $HTTPLOG
131                                 echo "000"
132                                 return 1
133                         fi
134                         retry=0
135                         echo " RESP: "$res >> $HTTPLOG
136                         status=${res:${#res}-3}
137                         if [ ! -z "${__RETRY_CODES}" ]; then
138                                 for retrycode in $__RETRY_CODES; do
139                                         if [ $retrycode -eq $status ]; then
140                                                 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED  >> $HTTPLOG
141                                                 sleep 1
142                                                 retry=1
143                                         fi
144                                 done
145                         fi
146                         if [ $retry -eq 0 ]; then
147                                 maxretries=-1
148                         fi
149                 done
150         echo $res
151         return 0
152     else
153                 if [ $oper != "RESPONSE" ]; then
154                         requestUrl=$3
155                         if [ $2 == "PUT" ] && [ $# -eq 4 ]; then
156                                 payload="$(cat $4 | tr -d '\n' | tr -d ' ' )"
157                                 echo "payload: "$payload >> $HTTPLOG
158                                 file=" --data-binary "$payload
159                         elif [ $# -eq 4 ]; then
160                                 echo " FILE: $(cat $4)" >> $HTTPLOG
161                         fi
162                         #urlencode the request url since it will be carried by send-request url
163                         requestUrl=$(python3 -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))"  "$3")
164                         url=" "${__ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
165                         curlString="curl -k -X POST${timeout}${httpcode}${content}${url}${file}"
166                         echo " CMD: "$curlString >> $HTTPLOG
167                         res=$($curlString)
168                         retcode=$?
169                         if [ $retcode -ne 0 ]; then
170                                 echo " RETCODE: "$retcode >> $HTTPLOG
171                                 echo "000"
172                                 return 1
173                         fi
174                         echo " RESP: "$res >> $HTTPLOG
175                         status=${res:${#res}-3}
176                         if [ $status -ne 200 ]; then
177                                 echo "000"
178                                 return 1
179                         fi
180                         cid=${res:0:${#res}-3}
181                         if [[ $batch -eq 1 ]]; then
182                                 echo $cid"200"
183                                 return 0
184                         fi
185                 fi
186                 if [ $oper == "RESPONSE" ] || [ $batch -eq 0 ]; then
187                         if [ $oper == "RESPONSE" ]; then
188                                 cid=$3
189                         fi
190                         url=" "${__ADAPTER}"/receive-response?correlationid="${cid}
191                         curlString="curl -k -X GET"${timeout}${httpcode}${url}
192                         echo " CMD: "$curlString >> $HTTPLOG
193                         res=$($curlString)
194                         retcode=$?
195                         if [ $retcode -ne 0 ]; then
196                                 echo " RETCODE: "$retcode >> $HTTPLOG
197                                 echo "000"
198                                 return 1
199                         fi
200                         echo " RESP: "$res >> $HTTPLOG
201                         status=${res:${#res}-3}
202                         TS=$SECONDS
203                         # wait of the reply from the agent/ECS...
204                         while [ $status -eq 204 ]; do
205                                 if [ $(($SECONDS - $TS)) -gt 90 ]; then
206                                         echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
207                                         echo "000"
208                                         return 1
209                                 fi
210                                 sleep 0.01
211                                 echo " CMD: "$curlString >> $HTTPLOG
212                                 res=$($curlString)
213                                 if [ $retcode -ne 0 ]; then
214                                         echo " RETCODE: "$retcode >> $HTTPLOG
215                                         echo "000"
216                                         return 1
217                                 fi
218                                 echo " RESP: "$res >> $HTTPLOG
219                                 status=${res:${#res}-3}
220                         done
221                         if [ $status -eq 200 ]; then
222                                 body=${res:0:${#res}-3}
223                                 echo $body
224                                 return 0
225                         fi
226                         echo "Status not 200, returning response 000" >> $HTTPLOG
227                         echo "0000"
228                         return 1
229                 fi
230     fi
231 }