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