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