Enhanced ECS function tests
[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         __log_test_start $@
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 [ $retcode -ne 0 ]; then
99                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
100                 return 1
101         fi
102
103         if [ $status -ne $1 ]; then
104                 __log_test_fail_status_code $1 $status
105                 return 1
106         fi
107     body=${res:0:${#res}-3}
108
109         targetJson="["
110     start=4
111     if [ $2 == "OSC" ]; then
112         start=5
113     fi
114     for pid in ${@:$start} ; do
115         if [ "$targetJson" != "[" ]; then
116             targetJson=$targetJson","
117         fi
118         targetJson=$targetJson"\"$UUID$pid\""
119     done
120     targetJson=$targetJson"]"
121
122         echo " TARGET JSON: $targetJson" >> $HTTPLOG
123
124         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
125
126         if [ $res -ne 0 ]; then
127                 __log_test_fail_body
128                 return 1
129         fi
130
131         __log_test_pass
132         return 0
133 }
134
135
136 # Controller API Test function: getA1PolicyType
137 # arg: <response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]
138 # (Function for test scripts)
139 controller_api_get_A1_policy_type() {
140         __log_test_start $@
141
142     paramError=1
143     if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
144         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4"
145                 paramError=0
146         fi
147
148     if [ $paramError -ne 0 ]; then
149                 __print_err "<response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]" $@
150                 return 1
151         fi
152
153     res=$(__do_curl_to_controller getA1PolicyType "$url")
154     retcode=$?
155     status=${res:${#res}-3}
156
157     if [ $retcode -ne 0 ]; then
158                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
159                 return 1
160         fi
161
162         if [ $status -ne $1 ]; then
163                 __log_test_fail_status_code $1 $status
164                 return 1
165         fi
166     body=${res:0:${#res}-3}
167
168         if [ $# -eq 5 ]; then
169
170                 body=${res:0:${#res}-3}
171
172                 targetJson=$(< $5)
173                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
174                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
175
176                 if [ $res -ne 0 ]; then
177                         __log_test_fail_body
178                         return 1
179                 fi
180         fi
181
182         __log_test_pass
183         return 0
184 }
185
186 # Controller API Test function: deleteA1Policy
187 # arg: <response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)
188 # (Function for test scripts)
189 controller_api_delete_A1_policy() {
190         __log_test_start $@
191
192     paramError=1
193     if [ $# -eq 5 ] && [ $2 == "OSC" ]; then
194         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
195                 paramError=0
196     elif [ $# -eq 4 ] && [ $2 == "STD" ]; then
197         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
198         paramError=0
199         fi
200
201     if [ $paramError -ne 0 ]; then
202                 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
203                 return 1
204         fi
205
206     res=$(__do_curl_to_controller deleteA1Policy "$url")
207     retcode=$?
208     status=${res:${#res}-3}
209
210     if [ $retcode -ne 0 ]; then
211                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
212                 return 1
213         fi
214
215         if [ $status -ne $1 ]; then
216                 __log_test_fail_status_code $1 $status
217                 return 1
218         fi
219
220         __log_test_pass
221         return 0
222 }
223
224 # Controller API Test function: putA1Policy
225 # arg: <response-code> (STD <ric-id> <policy-id> <template-file> ) | (OSC <ric-id> <policy-type-id> <policy-id> <template-file>)
226 # (Function for test scripts)
227 controller_api_put_A1_policy() {
228         __log_test_start $@
229
230     paramError=1
231     if [ $# -eq 6 ] && [ $2 == "OSC" ]; then
232         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
233         body=$(sed 's/XXX/'${5}'/g' $6)
234
235                 paramError=0
236     elif [ $# -eq 5 ] && [ $2 == "STD" ]; then
237         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
238         body=$(sed 's/XXX/'${4}'/g' $5)
239         paramError=0
240         fi
241
242     if [ $paramError -ne 0 ]; then
243                 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
244                 return 1
245         fi
246
247     res=$(__do_curl_to_controller putA1Policy "$url" "$body")
248     retcode=$?
249     status=${res:${#res}-3}
250
251     if [ $retcode -ne 0 ]; then
252                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
253                 return 1
254         fi
255
256         if [ $status -ne $1 ]; then
257                 __log_test_fail_status_code $1 $status
258                 return 1
259         fi
260
261         __log_test_pass
262         return 0
263 }
264
265
266 # Controller API Test function: getA1PolicyStatus
267 # arg: <response-code> (STD <ric-id> <policy-id> <enforce-status> [<reason>]) | (OSC <ric-id> <policy-type-id> <policy-id> <instance-status> <has-been-deleted>)
268 # (Function for test scripts)
269 controller_api_get_A1_policy_status() {
270         __log_test_start $@
271
272     targetJson=""
273     paramError=1
274     if [ $# -ge 5 ] && [ $2 == "OSC" ]; then
275         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5/status"
276         if [ $# -gt 5 ]; then
277             targetJson="{\"instance_status\":\"$6\""
278             targetJson=$targetJson",\"has_been_deleted\":\"$7\""
279             targetJson=$targetJson",\"created_at\":\"????\"}"
280         fi
281                 paramError=0
282     elif [ $# -ge 4 ] && [ $2 == "STD" ]; then
283         url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4/status"
284         if [ $# -gt 4 ]; then
285             targetJson="{\"enforceStatus\":\"$5\""
286             if [ $# -eq 6 ]; then
287                 targetJson=$targetJson",\"reason\":\"$6\""
288             fi
289             targetJson=$targetJson"}"
290         fi
291         paramError=0
292         fi
293
294     if [ $paramError -ne 0 ]; then
295                 __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>)" $@
296                 return 1
297         fi
298
299     res=$(__do_curl_to_controller getA1PolicyStatus "$url")
300     retcode=$?
301     status=${res:${#res}-3}
302
303     if [ $retcode -ne 0 ]; then
304                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
305                 return 1
306         fi
307
308         if [ $status -ne $1 ]; then
309                 __log_test_fail_status_code $1 $status
310                 return 1
311         fi
312
313         if [ ! -z "$targetJson" ]; then
314
315                 body=${res:0:${#res}-3}
316                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
317                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
318
319                 if [ $res -ne 0 ]; then
320                         __log_test_fail_body
321                         return 1
322                 fi
323         fi
324
325         __log_test_pass
326         return 0
327 }