Fixed the query parameter 'owner'
[nonrtric.git] / test / common / controller_api_functions.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 # This is a script that contains specific test functions for A1 Controller API
21
22 # Generic function to query the RICs via the A1-controller API.
23 # args: <operation> <url> [<body>]
24 # <operation>: getA1Policy,putA1Policy,getA1PolicyType,deleteA1Policy,getA1PolicyStatus
25 # response: <json-body><3-digit-response-code>
26 # (Not for test scripts)
27 __do_curl_to_controller() {
28     echo " (${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
29     if [ $# -ne 2 ] && [ $# -ne 3 ]; then
30                 ((RES_CONF_FAIL++))
31         echo "-Incorrect number of parameters to __do_curl_to_controller " $@ >> $HTTPLOG
32         echo "-Expected: <operation> <url> [<body>]" >> $HTTPLOG
33         echo "-Returning response 000" >> $HTTPLOG
34         echo "000"
35         return 1
36     fi
37     if [ $# -eq 2 ]; then
38         json='{"input":{"near-rt-ric-url":"'$2'"}}'
39     else
40         # Escape quotes in the body
41         body=$(echo "$3" | sed 's/"/\\"/g')
42         json='{"input":{"near-rt-ric-url":"'$2'","body":"'"$body"'"}}'
43     fi
44     echo "$json" > .sdnc.payload.json
45     echo "  FILE: $json"  >> $HTTPLOG
46     curlString="curl -skw %{http_code} -X POST $SDNC_HTTPX://$SDNC_USER:$SDNC_PWD@localhost:$SDNC_LOCAL_PORT$SDNC_API_URL$1 -H accept:application/json -H Content-Type:application/json --data-binary @.sdnc.payload.json"
47     echo "  CMD: "$curlString >> $HTTPLOG
48     res=$($curlString)
49     retcode=$?
50     echo "  RESP: "$res >> $HTTPLOG
51     if [ $retcode -ne 0 ]; then
52         echo "  RETCODE: "$retcode >> $HTTPLOG
53         echo "000"
54         return 1
55     fi
56
57         status=${res:${#res}-3}
58
59     if [ $status -ne 200 ]; then
60         echo "000"
61         return 1
62     fi
63     body=${res:0:${#res}-3}
64         echo "  JSON: "$body >> $HTTPLOG
65     echo "$body" > .sdnc-reply.json
66     res=$(python3 ../common/extract_sdnc_reply.py .sdnc-reply.json)
67     echo "  EXTRACED BODY+CODE: "$res >> $HTTPLOG
68     echo "$res"
69     return 0
70 }
71
72 # Controller API Test function: getA1Policy (return ids only)
73 # arg: <response-code> (OSC <ric-id> <policy-type-id> [ <policy-id> [<policy-id>]* ]) | ( STD <ric-id> [ <policy-id> [<policy-id>]* ] )
74 # (Function for test scripts)
75 controller_api_get_A1_policy_ids() {
76         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
77     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
78         ((RES_TEST++))
79
80     paramError=1
81     if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
82         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies"
83                 paramError=0
84     elif [ $# -gt 2 ] && [ $2 == "STD" ]; then
85         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies"
86         paramError=0
87         fi
88
89     if [ $paramError -ne 0 ]; then
90                 __print_err "<response-code> (OSC <ric-id> <policy-type-id> [ <policy-id> [<policy-id>]* ]) | ( STD <ric-id> [ <policy-id> [<policy-id>]* ] )" $@
91                 return 1
92         fi
93
94     res=$(__do_curl_to_controller getA1Policy "$url")
95     retcode=$?
96     status=${res:${#res}-3}
97
98     if [ $? -ne 0 ]; then
99                 echo -e $RED" FAIL. Exepected status "$1", got "$status "(likely remote server error)"$ERED
100                 ((RES_FAIL++))
101                 __check_stop_at_error
102                 return 1
103         fi
104
105         if [ $status -ne $1 ]; then
106                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
107                 ((RES_FAIL++))
108                 __check_stop_at_error
109                 return 1
110         fi
111     body=${res:0:${#res}-3}
112
113         targetJson="["
114     start=4
115     if [ $2 == "OSC" ]; then
116         start=5
117     fi
118     for pid in ${@:$start} ; do
119         if [ "$targetJson" != "[" ]; then
120             targetJson=$targetJson","
121         fi
122         targetJson=$targetJson"\"$UUID$pid\""
123     done
124     targetJson=$targetJson"]"
125
126         echo " TARGET JSON: $targetJson" >> $HTTPLOG
127
128         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
129
130         if [ $res -ne 0 ]; then
131                 echo -e $RED" FAIL, returned body not correct"$ERED
132                 ((RES_FAIL++))
133                 __check_stop_at_error
134                 return 1
135         fi
136
137         ((RES_PASS++))
138         echo -e $GREEN" PASS"$EGREEN
139         return 0
140 }
141
142
143 # Controller API Test function: getA1PolicyType
144 # arg: <response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]
145 # (Function for test scripts)
146 controller_api_get_A1_policy_type() {
147         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
148     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
149         ((RES_TEST++))
150
151     paramError=1
152     if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
153         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4"
154                 paramError=0
155         fi
156
157     if [ $paramError -ne 0 ]; then
158                 __print_err "<response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]" $@
159                 return 1
160         fi
161
162     res=$(__do_curl_to_controller getA1PolicyType "$url")
163     retcode=$?
164     status=${res:${#res}-3}
165
166     if [ $? -ne 0 ]; then
167                 echo -e $RED" FAIL. Exepected status "$1", got "$status "(likely remote server error)"$ERED
168                 ((RES_FAIL++))
169                 __check_stop_at_error
170                 return 1
171         fi
172
173         if [ $status -ne $1 ]; then
174                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
175                 ((RES_FAIL++))
176                 __check_stop_at_error
177                 return 1
178         fi
179     body=${res:0:${#res}-3}
180
181         if [ $# -eq 5 ]; then
182
183                 body=${res:0:${#res}-3}
184
185                 targetJson=$(< $5)
186                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
187                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
188
189                 if [ $res -ne 0 ]; then
190                         echo -e $RED" FAIL, returned body not correct"$ERED
191                         ((RES_FAIL++))
192                         __check_stop_at_error
193                         return 1
194                 fi
195         fi
196
197         ((RES_PASS++))
198         echo -e $GREEN" PASS"$EGREEN
199         return 0
200 }
201
202 # Controller API Test function: deleteA1Policy
203 # arg: <response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)
204 # (Function for test scripts)
205 controller_api_delete_A1_policy() {
206         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
207     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
208         ((RES_TEST++))
209
210     paramError=1
211     if [ $# -eq 5 ] && [ $2 == "OSC" ]; then
212         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
213                 paramError=0
214     elif [ $# -eq 4 ] && [ $2 == "STD" ]; then
215         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
216         paramError=0
217         fi
218
219     if [ $paramError -ne 0 ]; then
220                 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
221                 return 1
222         fi
223
224     res=$(__do_curl_to_controller deleteA1Policy "$url")
225     retcode=$?
226     status=${res:${#res}-3}
227
228     if [ $? -ne 0 ]; then
229                 echo -e $RED" FAIL. Exepected status "$1", got "$status "(likely remote server error)"$ERED
230                 ((RES_FAIL++))
231                 __check_stop_at_error
232                 return 1
233         fi
234
235         if [ $status -ne $1 ]; then
236                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
237                 ((RES_FAIL++))
238                 __check_stop_at_error
239                 return 1
240         fi
241
242         ((RES_PASS++))
243         echo -e $GREEN" PASS"$EGREEN
244         return 0
245 }
246
247 # Controller API Test function: putA1Policy
248 # arg: <response-code> (STD <ric-id> <policy-id> <template-file> ) | (OSC <ric-id> <policy-type-id> <policy-id> <template-file>)
249 # (Function for test scripts)
250 controller_api_put_A1_policy() {
251         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
252     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
253         ((RES_TEST++))
254
255     paramError=1
256     if [ $# -eq 6 ] && [ $2 == "OSC" ]; then
257         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
258         body=$(sed 's/XXX/'${5}'/g' $6)
259
260                 paramError=0
261     elif [ $# -eq 5 ] && [ $2 == "STD" ]; then
262         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
263         body=$(sed 's/XXX/'${4}'/g' $5)
264         paramError=0
265         fi
266
267     if [ $paramError -ne 0 ]; then
268                 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
269                 return 1
270         fi
271
272     res=$(__do_curl_to_controller putA1Policy "$url" "$body")
273     retcode=$?
274     status=${res:${#res}-3}
275
276     if [ $? -ne 0 ]; then
277                 echo -e $RED" FAIL. Exepected status "$1", got "$status "(likely remote server error)"$ERED
278                 ((RES_FAIL++))
279                 __check_stop_at_error
280                 return 1
281         fi
282
283         if [ $status -ne $1 ]; then
284                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
285                 ((RES_FAIL++))
286                 __check_stop_at_error
287                 return 1
288         fi
289
290         ((RES_PASS++))
291         echo -e $GREEN" PASS"$EGREEN
292         return 0
293 }
294
295
296 # Controller API Test function: getA1PolicyStatus
297 # arg: <response-code> (STD <ric-id> <policy-id> <enforce-status> [<reason>]) | (OSC <ric-id> <policy-type-id> <policy-id> <instance-status> <has-been-deleted>)
298 # (Function for test scripts)
299 controller_api_get_A1_policy_status() {
300         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
301     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
302         ((RES_TEST++))
303
304     targetJson=""
305     paramError=1
306     if [ $# -ge 5 ] && [ $2 == "OSC" ]; then
307         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5/status"
308         if [ $# -gt 5 ]; then
309             targetJson="{\"instance_status\":\"$6\""
310             targetJson=$targetJson",\"has_been_deleted\":\"$7\""
311             targetJson=$targetJson",\"created_at\":\"????\"}"
312         fi
313                 paramError=0
314     elif [ $# -ge 4 ] && [ $2 == "STD" ]; then
315         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4/status"
316         if [ $# -gt 4 ]; then
317             targetJson="{\"enforceStatus\":\"$5\""
318             if [ $# -eq 6 ]; then
319                 targetJson=$targetJson",\"reason\":\"$6\""
320             fi
321             targetJson=$targetJson"}"
322         fi
323         paramError=0
324         fi
325
326     if [ $paramError -ne 0 ]; then
327                 __print_err "<response-code> (STD <ric-id> <policy-id> <enforce-status> [<reason>]) | (OSC <ric-id> <policy-type-id> <policy-id> <instance-status> <has-been-deleted>)" $@
328                 return 1
329         fi
330
331     res=$(__do_curl_to_controller getA1PolicyStatus "$url")
332     retcode=$?
333     status=${res:${#res}-3}
334
335     if [ $? -ne 0 ]; then
336                 echo -e $RED" FAIL. Exepected status "$1", got "$status "(likely remote server error)"$ERED
337                 ((RES_FAIL++))
338                 __check_stop_at_error
339                 return 1
340         fi
341
342         if [ $status -ne $1 ]; then
343                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
344                 ((RES_FAIL++))
345                 __check_stop_at_error
346                 return 1
347         fi
348
349         if [ ! -z "$targetJson" ]; then
350
351                 body=${res:0:${#res}-3}
352                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
353                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
354
355                 if [ $res -ne 0 ]; then
356                         echo -e $RED" FAIL, returned body not correct"$ERED
357                         ((RES_FAIL++))
358                         __check_stop_at_error
359                         return 1
360                 fi
361         fi
362
363         ((RES_PASS++))
364         echo -e $GREEN" PASS"$EGREEN
365         return 0
366 }