blob: b0ae64182e13edfa60095a0070c57ac862739420 [file] [log] [blame]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001#!/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
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020044 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"
BjornMagnussonXA80a92002020-03-19 14:31:06 +010048 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}
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020065 echo " JSON: "$body >> $HTTPLOG
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +020066 reply="./tmp/.sdnc-reply.json"
67 echo "$body" > $reply
68 res=$(python3 ../common/extract_sdnc_reply.py $reply)
BjornMagnussonXA80a92002020-03-19 14:31:06 +010069 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)
77controller_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
BjornMagnussonXA575869c2020-09-14 21:28:54 +020084 url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies"
BjornMagnussonXA80a92002020-03-19 14:31:06 +010085 paramError=0
86 elif [ $# -gt 2 ] && [ $2 == "STD" ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +020087 url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies"
BjornMagnussonXA80a92002020-03-19 14:31:06 +010088 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++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200103 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100104 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++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200110 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100111 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
BjornMagnussonXAad047782020-06-08 15:54:11 +0200124 targetJson=$targetJson"\"$UUID$pid\""
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100125 done
126 targetJson=$targetJson"]"
127
128 echo " TARGET JSON: $targetJson" >> $HTTPLOG
129
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200130 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100131
132 if [ $res -ne 0 ]; then
133 echo -e $RED" FAIL, returned body not correct"$ERED
134 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200135 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100136 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)
148controller_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
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200155 url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100156 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++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200171 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100172 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++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200178 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100179 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
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200189 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100190
191 if [ $res -ne 0 ]; then
192 echo -e $RED" FAIL, returned body not correct"$ERED
193 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200194 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100195 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)
207controller_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
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200214 url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100215 paramError=0
216 elif [ $# -eq 4 ] && [ $2 == "STD" ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200217 url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100218 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++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200233 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100234 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++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200240 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100241 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)
252controller_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
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200259 url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100260 body=$(sed 's/XXX/'${5}'/g' $6)
261
262 paramError=0
263 elif [ $# -eq 5 ] && [ $2 == "STD" ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200264 url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100265 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++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200281 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100282 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++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200288 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100289 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)
301controller_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
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200309 url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5/status"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100310 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
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200317 url="$RIC_SIM_HTTPX://$3:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4/status"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100318 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++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200340 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100341 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++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200347 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100348 return 1
349 fi
350
351 if [ ! -z "$targetJson" ]; then
352
353 body=${res:0:${#res}-3}
354 echo " TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200355 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100356
357 if [ $res -ne 0 ]; then
358 echo -e $RED" FAIL, returned body not correct"$ERED
359 ((RES_FAIL++))
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200360 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100361 return 1
362 fi
363 fi
364
365 ((RES_PASS++))
366 echo -e $GREEN" PASS"$EGREEN
367 return 0
368}