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