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