Clean autotest folder
[nonrtric.git] / test / common / agent_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 Policy Agent API
21
22 ### API functiond towards the Policy Agent
23
24 # Generic function to query the agent via the REST or DMAAP interface.
25 # Used by all other agent api test functions
26 # REST or DMAAP is controlled of the base url of $ADAPTER
27 # arg: GET|PUT|POST|DELETE <url> [<file>]
28 # (Not for test scripts)
29 __do_curl_to_agent() {
30     echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
31         paramError=0
32
33     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
34                 paramError=1
35     else
36                 timeout=""
37                 oper=""
38                 file=''
39                 httpcode=" -sw %{http_code}"
40                 accept=''
41                 content=''
42
43                 if [ $# -gt 2 ]; then
44                         content=" -H Content-Type:application/json"
45                 fi
46                 if [ $1 == "GET" ]; then
47                         oper="GET"
48                         if [ $# -ne 2 ];then
49                                 paramError=1
50                         fi
51                 elif [ $1 == "PUT" ]; then
52                         oper="PUT"
53                         if [ $# -eq 3 ]; then
54                                 file=" --data-binary @$3"
55                         fi
56                         accept=" -H accept:application/json"
57                 elif [ $1 == "POST" ]; then
58                         oper="POST"
59                         accept=" -H accept:*/*"
60                         if [ $# -ne 2 ];then
61                                 paramError=1
62                         fi
63                 elif [ $1 == "DELETE" ]; then
64                         oper="DELETE"
65                         if [ $# -ne 2 ];then
66                                 paramError=1
67                         fi
68                 else
69                         paramError=1
70                 fi
71         fi
72
73     if [ $paramError -eq 1 ]; then
74                 ((RES_CONF_FAIL++))
75         echo "-Incorrect number of parameters to __do_curl_agent " $@ >> $HTTPLOG
76         echo "-Expected: GET|PUT|POST|DELETE <url> [<file>]" >> $HTTPLOG
77         echo "-Returning response 000" >> $HTTPLOG
78         echo "-000"
79         return 1
80     fi
81
82     if [ $ADAPTER == $RESTBASE ]; then
83         url=" "${ADAPTER}${2}
84         oper=" -X "$oper
85         curlString="curl"${oper}${timeout}${httpcode}${accept}${content}${url}${file}
86         echo " CMD: "$curlString >> $HTTPLOG
87                 if [ $# -eq 3 ]; then
88                         echo " FILE: $(<$3)" >> $HTTPLOG
89                 fi
90
91                 # Do retry for configured response codes, otherwise only one attempt
92                 maxretries=5
93                 while [ $maxretries -ge 0 ]; do
94
95                         let maxretries=maxretries-1
96                         res=$($curlString)
97                         retcode=$?
98                         if [ $retcode -ne 0 ]; then
99                                 echo " RETCODE: "$retcode >> $HTTPLOG
100                                 echo "000"
101                                 return 1
102                         fi
103                         retry=0
104                         echo " RESP: "$res >> $HTTPLOG
105                         status=${res:${#res}-3}
106                         if [ ! -z "${AGENT_RETRY_CODES}" ]; then
107                                 for retrycode in $AGENT_RETRY_CODES; do
108                                         if [ $retrycode -eq $status ]; then
109                                                 echo -e $RED" Retrying (according to set codes for retry), got status $status....."$ERED  >> $HTTPLOG
110                                                 sleep 1
111                                                 retry=1
112                                         fi
113                                 done
114                         fi
115                         if [ $retry -eq 0 ]; then
116                                 maxretries=-1
117                         fi
118                 done
119         echo $res
120         return 0
121     else
122         requestUrl=$2
123         if [ $1 == "PUT" ] && [ $# -eq 3 ]; then
124             payload="$(cat $3 | tr -d '\n' | tr -d ' ' )"
125             echo "payload: "$payload >> $HTTPLOG
126             file=" --data-binary "$payload
127         fi
128                 #urlencode the request url since it will be carried by send-request url
129                 requestUrl=$(python -c "from __future__ import print_function; import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))"  "$2")
130         url=" "${ADAPTER}"/send-request?url="${requestUrl}"&operation="${oper}
131         curlString="curl -X POST${timeout}${httpcode}${content}${url}${file}"
132         echo " CMD: "$curlString >> $HTTPLOG
133         res=$($curlString)
134         retcode=$?
135         if [ $retcode -ne 0 ]; then
136             echo " RETCODE: "$retcode >> $HTTPLOG
137             echo "000"
138             return 1
139         fi
140         echo " RESP: "$res >> $HTTPLOG
141         status=${res:${#res}-3}
142         if [ $status -ne 200 ]; then
143             echo "000"
144             return 1
145         fi
146         cid=${res:0:${#res}-3}
147         url=" "${ADAPTER}"/receive-response?correlationid="${cid}
148         curlString="curl -X GET"${timeout}${httpcode}${url}
149         echo " CMD: "$curlString >> $HTTPLOG
150         res=$($curlString)
151         retcode=$?
152         if [ $retcode -ne 0 ]; then
153             echo " RETCODE: "$retcode >> $HTTPLOG
154             echo "000"
155             return 1
156         fi
157         echo " RESP: "$res >> $HTTPLOG
158         status=${res:${#res}-3}
159                 TS=$SECONDS
160                 # wait of the reply from the agent...
161         while [ $status -eq 204 ]; do
162                         if [ $(($SECONDS - $TS)) -gt 90 ]; then
163                 echo " RETCODE: (timeout after 90s)" >> $HTTPLOG
164                 echo "000"
165                 return 1
166                         fi
167             sleep 1
168             echo " CMD: "$curlString >> $HTTPLOG
169             res=$($curlString)
170             if [ $retcode -ne 0 ]; then
171                 echo " RETCODE: "$retcode >> $HTTPLOG
172                 echo "000"
173                 return 1
174             fi
175             echo " RESP: "$res >> $HTTPLOG
176             status=${res:${#res}-3}
177         done
178         if [ $status -eq 200 ]; then
179             body=${res:0:${#res}-3}
180             echo $body
181             return 0
182         fi
183         echo "Status not 200, returning response 000" >> $HTTPLOG
184         echo "0000"
185         return 1
186     fi
187 }
188
189
190 #########################################################
191 #### Test case functions A1 Policy management service
192 #########################################################
193
194 # This function compare the size, towards a target value, of a json array returned from <url> of the Policy Agent.
195 # This is done immediately by setting PASS or FAIL or wait up to and optional timeout before setting PASS or FAIL
196 # args: json:<url> <target-value> [<timeout-in-seconds]
197 # (Function for test scripts)
198 api_equal() {
199     echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
200         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
201                 if [[ $1 == "json:"* ]]; then
202                         __var_test "Policy Agent" $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT"/" $1 "=" $2 $3
203                         return 0
204                 fi
205         fi
206
207         ((RES_CONF_FAIL++))
208         __print_err "needs two or three args: json:<json-array-param> <target-value> [ timeout ]" $@
209         return 1
210 }
211
212 # API Test function: GET /policies
213 # args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-ype-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <template-file>]*]
214 # (Function for test scripts)
215 api_get_policies() {
216         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
217     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
218         ((RES_TEST++))
219         paramError=0
220         if [ $# -lt 4 ]; then
221                 paramError=1
222         elif [ $# -eq 5 ] && [ $5 != "NOID" ]; then
223                 paramError=1
224         elif [ $# -gt 4 ] && [ $(($#%5)) -ne 4 ]; then
225                 paramError=1
226         fi
227
228     if [ $paramError -ne 0 ]; then
229         __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-ype-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <template-file>]*]" $@
230         return 1
231     fi
232         queryparams=""
233         if [ $2 != "NORIC" ]; then
234                 queryparams="?ric="$2
235         fi
236         if [ $3 != "NOSERVICE" ]; then
237                 if [ -z $queryparams ]; then
238                         queryparams="?service="$3
239                 else
240                         queryparams=$queryparams"&service="$3
241                 fi
242         fi
243         if [ $4 != "NOTYPE" ]; then
244                 if [ -z $queryparams ]; then
245                         queryparams="?type="$4
246                 else
247                         queryparams=$queryparams"&type="$4
248                 fi
249         fi
250
251         query="/policies"$queryparams
252     res="$(__do_curl_to_agent GET $query)"
253     status=${res:${#res}-3}
254
255         if [ $status -ne $1 ]; then
256                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
257                 ((RES_FAIL++))
258                 return 1
259         fi
260
261         if [ $# -gt 4 ]; then
262                 if [ $# -eq 5 ] && [ $5 == "NOID" ]; then
263                         targetJson="["
264                 else
265                         body=${res:0:${#res}-3}
266                         targetJson="["
267                         arr=(${@:5})
268
269                         for ((i=0; i<$(($#-4)); i=i+5)); do
270
271                                 if [ "$targetJson" != "[" ]; then
272                                         targetJson=$targetJson","
273                                 fi
274                                 targetJson=$targetJson"{\"id\":\"${arr[$i]}\",\"lastModified\":\"????\",\"ric\":\"${arr[$i+1]}\",\"service\":\"${arr[$i+2]}\",\"type\":"
275                                 if [ "${arr[$i+3]}" == "EMPTY" ]; then
276                                         targetJson=$targetJson"\"\","
277                                 else
278                                         targetJson=$targetJson"\"${arr[$i+3]}\","
279                                 fi
280                                 file=".p.json"
281                                 sed 's/XXX/'${arr[$i]}'/g' ${arr[$i+4]} > $file
282                                 json=$(cat $file)
283                                 targetJson=$targetJson"\"json\":"$json"}"
284                         done
285                 fi
286
287                 targetJson=$targetJson"]"
288                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
289                 res=$(python ../common/compare_json.py "$targetJson" "$body" "id")
290
291                 if [ $res -ne 0 ]; then
292                         echo -e $RED" FAIL, returned body not correct"$ERED
293                         ((RES_FAIL++))
294                         return 1
295                 fi
296         fi
297
298         ((RES_PASS++))
299         echo -e $GREEN" PASS"$EGREEN
300         return 0
301
302 }
303
304 # API Test function: GET /policy
305 #args: <response-code>  <policy-id> [<template-file>]
306 # (Function for test scripts)
307 api_get_policy() {
308         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
309     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
310         ((RES_TEST++))
311
312     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
313         __print_err "<response-code>  <policy-id> [<template-file>] " $@
314         return 1
315     fi
316
317         query="/policy?id=$2"
318         res="$(__do_curl_to_agent GET $query)"
319         status=${res:${#res}-3}
320
321         if [ $status -ne $1 ]; then
322                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
323                 ((RES_FAIL++))
324                 return 1
325         fi
326
327         if [ $# -eq 3 ]; then
328                 #Create a policy json to compare with
329                 body=${res:0:${#res}-3}
330                 file=".p.json"
331                 sed 's/XXX/'${2}'/g' $3 > $file
332                 targetJson=$(< $file)
333                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
334                 res=$(python ../common/compare_json.py "$targetJson" "$body")
335                 if [ $res -ne 0 ]; then
336                         echo -e $RED" FAIL, returned body not correct"$ERED
337                         ((RES_FAIL++))
338                         return 1
339                 fi
340         fi
341
342         ((RES_PASS++))
343         echo -e $GREEN" PASS"$EGREEN
344         return 0
345 }
346
347 # API Test function: PUT /policy
348 # args: <response-code> <service-name> <ric-id> <policytype-id> <policy-id> <template-file> [<count>]
349 # (Function for test scripts)
350 api_put_policy() {
351         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
352     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
353         ((RES_TEST++))
354
355     if [ $# -lt 6 ] || [ $# -gt 7 ]; then
356         __print_err "<response-code> <service-name> <ric-id> <policytype-id> <policy-id> <template-file> [<count>]" $@
357         return 1
358     fi
359
360         ric=$3
361         count=0
362         max=1
363
364         if [ $# -eq 7 ]; then
365                 max=$7
366         fi
367
368         pid=$5
369         file=$6
370
371         while [ $count -lt $max ]; do
372                 query="/policy?id=$pid&ric=$ric&service=$2"
373
374                 if [ $4 == "NOTYPE" ]; then
375                         query="/policy?id=$pid&ric=$ric&service=$2"
376                 else
377                         query="/policy?id=$pid&ric=$ric&service=$2&type=$4"
378                 fi
379
380                 file=".p.json"
381                 sed 's/XXX/'${pid}'/g' $6 > $file
382         res="$(__do_curl_to_agent PUT $query $file)"
383         status=${res:${#res}-3}
384                 echo -ne " Creating "$count"("$max")\033[0K\r"
385
386                 if [ $status -ne $1 ]; then
387                         let pid=$pid+1
388                         echo " Created "$count"?("$max")"
389                         echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
390                         ((RES_FAIL++))
391                         return 1
392                 fi
393
394                 let pid=$pid+1
395                 let count=$count+1
396                 echo -ne " Created  "$count"("$max")\033[0K\r"
397         done
398         echo ""
399
400         ((RES_PASS++))
401         echo -e $GREEN" PASS"$EGREEN
402         return 0
403 }
404
405
406 # API Test function: DELETE /policy
407 # args: <response-code> <policy-id> [count]
408 # (Function for test scripts)
409 api_delete_policy() {
410         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
411     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
412         ((RES_TEST++))
413
414     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
415         __print_err "<response-code> <policy-id> [count]" $@
416         return 1
417     fi
418
419         count=0
420         max=1
421
422         if [ $# -eq 3 ]; then
423                 max=$3
424         fi
425
426         pid=$2
427
428         while [ $count -lt $max ]; do
429                 query="/policy?id="$pid
430                 res="$(__do_curl_to_agent DELETE $query)"
431                 status=${res:${#res}-3}
432                 echo -ne " Deleting "$count"("$max")\033[0K\r"
433
434                 if [ $status -ne $1 ]; then
435                         echo " Deleted "$count"?("$max")"
436                         echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
437                         ((RES_FAIL++))
438                         return 1
439                 fi
440                 let pid=$pid+1
441                 let count=$count+1
442                 echo -ne " Deleted  "$count"("$max")\033[0K\r"
443         done
444         echo ""
445
446         ((RES_PASS++))
447         echo -e $GREEN" PASS"$EGREEN
448         return 0
449 }
450
451 # API Test function: GET /policy_ids
452 # args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)
453 # (Function for test scripts)
454 api_get_policy_ids() {
455         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
456     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
457         ((RES_TEST++))
458
459     if [ $# -lt 4 ]; then
460                 __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)" $@
461                 return 1
462         fi
463
464         queryparams=""
465
466         if [ $2 != "NORIC" ]; then
467                 queryparams="?ric="$2
468         fi
469
470         if [ $3 != "NOSERVICE" ]; then
471                 if [ -z $queryparams ]; then
472                         queryparams="?service="$3
473                 else
474                         queryparams=$queryparams"&service="$3
475                 fi
476         fi
477         if [ $4 != "NOTYPE" ]; then
478                 if [ -z $queryparams ]; then
479                         queryparams="?type="$4
480                 else
481                         queryparams=$queryparams"&type="$4
482                 fi
483         fi
484
485         query="/policy_ids"$queryparams
486     res="$(__do_curl_to_agent GET $query)"
487     status=${res:${#res}-3}
488
489         if [ $status -ne $1 ]; then
490                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
491                 ((RES_FAIL++))
492                 return 1
493         fi
494
495         if [ $# -gt 4 ]; then
496                 body=${res:0:${#res}-3}
497                 targetJson="["
498
499                 for pid in ${@:5} ; do
500                         if [ "$targetJson" != "[" ]; then
501                                 targetJson=$targetJson","
502                         fi
503                         if [ $pid != "NOID" ]; then
504                                 targetJson=$targetJson"\"$pid\""
505                         fi
506                 done
507
508                 targetJson=$targetJson"]"
509                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
510                 res=$(python ../common/compare_json.py "$targetJson" "$body")
511
512                 if [ $res -ne 0 ]; then
513                         echo -e $RED" FAIL, returned body not correct"$ERED
514                         ((RES_FAIL++))
515                         return 1
516                 fi
517         fi
518
519         ((RES_PASS++))
520         echo -e $GREEN" PASS"$EGREEN
521         return 0
522 }
523
524 # API Test function: GET /policy_schema
525 # args: <response-code> <policy-type-id> [<schema-file>]
526 # (Function for test scripts)
527 api_get_policy_schema() {
528         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
529     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
530         ((RES_TEST++))
531
532     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
533         __print_err "<response-code> <policy-type-id> [<schema-file>]" $@
534         return 1
535     fi
536
537         query="/policy_schema?id=$2"
538         res="$(__do_curl_to_agent GET $query)"
539         status=${res:${#res}-3}
540
541         if [ $status -ne $1 ]; then
542                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
543                 ((RES_FAIL++))
544                 return 1
545         fi
546
547         if [ $# -eq 3 ]; then
548
549                 body=${res:0:${#res}-3}
550
551                 targetJson=$(< $3)
552                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
553                 res=$(python ../common/compare_json.py "$targetJson" "$body")
554
555                 if [ $res -ne 0 ]; then
556                         echo -e $RED" FAIL, returned body not correct"$ERED
557                         ((RES_FAIL++))
558                         return 1
559                 fi
560         fi
561
562         ((RES_PASS++))
563         echo -e $GREEN" PASS"$EGREEN
564         return 0
565 }
566
567 # API Test function: GET /policy_schemas
568 # args: <response-code>  <ric-id>|NORIC [<schema-file>|NOFILE]*
569 # (Function for test scripts)
570 api_get_policy_schemas() {
571         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
572     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
573         ((RES_TEST++))
574
575     if [ $# -lt 2 ]; then
576         __print_err "<response-code> <ric-id>|NORIC [<schema-file>|NOFILE]*" $@
577         return 1
578     fi
579
580         query="/policy_schemas"
581         if [ $2 != "NORIC" ]; then
582                 query=$query"?ric="$2
583         fi
584
585         res="$(__do_curl_to_agent GET $query)"
586         status=${res:${#res}-3}
587
588         if [ $status -ne $1 ]; then
589                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
590                 ((RES_FAIL++))
591                 return 1
592         fi
593
594         if [ $# -gt 2 ]; then
595                 body=${res:0:${#res}-3}
596                 targetJson="["
597
598                 for file in ${@:3} ; do
599                         if [ "$targetJson" != "[" ]; then
600                                 targetJson=$targetJson","
601                         fi
602                         if [ $file == "NOFILE" ]; then
603                                 targetJson=$targetJson"{}"
604                         else
605                                 targetJson=$targetJson$(< $3)
606                         fi
607                 done
608
609                 targetJson=$targetJson"]"
610                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
611                 res=$(python ../common/compare_json.py "$targetJson" "$body")
612
613                 if [ $res -ne 0 ]; then
614                         echo -e $RED" FAIL, returned body not correct"$ERED
615                         ((RES_FAIL++))
616                         return 1
617                 fi
618         fi
619
620         ((RES_PASS++))
621         echo -e $GREEN" PASS"$EGREEN
622         return 0
623 }
624
625 # API Test function: GET /policy_status
626 # arg: <response-code> <policy-id> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)
627 # (Function for test scripts)
628 api_get_policy_status() {
629         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
630     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
631         ((RES_TEST++))
632     if [ $# -lt 4 ] || [ $# -gt 5 ]; then
633                 __print_err "<response-code> <policy-id> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
634                 return 1
635         fi
636
637         targetJson=""
638
639         if [ $3 == "STD" ]; then
640                 targetJson="{\"enforceStatus\":\"$4\""
641                 if [ $# -eq 5 ]; then
642                         targetJson=$targetJson",\"reason\":\"$5\""
643                 fi
644                 targetJson=$targetJson"}"
645         elif [ $3 == "OSC" ]; then
646                 targetJson="{\"instance_status\":\"$4\""
647                 if [ $# -eq 5 ]; then
648                         targetJson=$targetJson",\"has_been_deleted\":\"$5\""
649                 fi
650                 targetJson=$targetJson",\"created_at\":\"????\"}"
651         else
652                 __print_err "<response-code> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
653                 return 1
654         fi
655
656         query="/policy_status?id="$2
657
658         res="$(__do_curl_to_agent GET $query)"
659     status=${res:${#res}-3}
660
661         if [ $status -ne $1 ]; then
662                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
663                 ((RES_FAIL++))
664                 return 1
665         fi
666
667         echo "TARGET JSON: $targetJson" >> $HTTPLOG
668         body=${res:0:${#res}-3}
669         res=$(python ../common/compare_json.py "$targetJson" "$body")
670
671         if [ $res -ne 0 ]; then
672                 echo -e $RED" FAIL, returned body not correct"$ERED
673                 ((RES_FAIL++))
674                 return 1
675         fi
676
677         ((RES_PASS++))
678         echo -e $GREEN" PASS"$EGREEN
679         return 0
680 }
681
682 # API Test function: GET /policy_types
683 # args: <response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]
684 # (Function for test scripts)
685 api_get_policy_types() {
686         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
687     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
688         ((RES_TEST++))
689
690     if [ $# -lt 1 ]; then
691                 __print_err "<response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]" $@
692                 return 1
693         fi
694
695         if [ $# -eq 1 ]; then
696                 query="/policy_types"
697         elif [ $2 == "NORIC" ]; then
698                 query="/policy_types"
699         else
700                 query="/policy_types?ric=$2"
701         fi
702
703     res="$(__do_curl_to_agent GET $query)"
704     status=${res:${#res}-3}
705
706         if [ $status -ne $1 ]; then
707                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
708                 ((RES_FAIL++))
709                 return 1
710         fi
711
712         if [ $# -gt 2 ]; then
713                 body=${res:0:${#res}-3}
714                 targetJson="["
715
716                 for pid in ${@:3} ; do
717                         if [ "$targetJson" != "[" ]; then
718                                 targetJson=$targetJson","
719                         fi
720                         if [ $pid == "EMPTY" ]; then
721                                 pid=""
722                         fi
723                         targetJson=$targetJson"\"$pid\""
724                 done
725
726                 targetJson=$targetJson"]"
727                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
728                 res=$(python ../common/compare_json.py "$targetJson" "$body")
729
730                 if [ $res -ne 0 ]; then
731                         echo -e $RED" FAIL, returned body not correct"$ERED
732                         ((RES_FAIL++))
733                         return 1
734                 fi
735         fi
736
737         ((RES_PASS++))
738         echo -e $GREEN" PASS"$EGREEN
739         return 0
740 }
741
742 #########################################################
743 #### Test case functions Health check
744 #########################################################
745
746 # API Test function: GET /status
747 # args: <response-code>
748 # (Function for test scripts)
749 api_get_status() {
750         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
751     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
752         ((RES_TEST++))
753     if [ $# -ne 1 ]; then
754                 __print_err "<response-code>" $@
755                 return 1
756         fi
757     query="/status"
758     res="$(__do_curl_to_agent GET $query)"
759     status=${res:${#res}-3}
760
761         if [ $status -ne $1 ]; then
762                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
763                 ((RES_FAIL++))
764                 return 1
765         fi
766
767         ((RES_PASS++))
768         echo -e $GREEN" PASS"$EGREEN
769         return 0
770 }
771
772 #########################################################
773 #### Test case functions RIC Repository
774 #########################################################
775
776 # API Test function: GET /ric
777 # args: <reponse-code> <management-element-id> [<ric-id>]
778 # (Function for test scripts)
779 api_get_ric() {
780         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
781     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
782         ((RES_TEST++))
783     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
784                 __print_err "<reponse-code> <management-element-id> [<ric-id>]" $@
785                 return 1
786         fi
787
788         query="/ric?managedElementId="$2
789
790     res="$(__do_curl_to_agent GET $query)"
791     status=${res:${#res}-3}
792
793         if [ $status -ne $1 ]; then
794                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
795                 ((RES_FAIL++))
796                 return 1
797         fi
798
799         if [ $# -eq 3 ]; then
800                 body=${res:0:${#res}-3}
801                 if [ "$body" != "$3" ]; then
802                         echo -e $RED" FAIL, returned body not correct"$ERED
803                         ((RES_FAIL++))
804                         return 1
805                 fi
806         fi
807
808         ((RES_PASS++))
809         echo -e $GREEN" PASS"$EGREEN
810         return 0
811 }
812
813 # API test function: GET /rics
814 # args: <reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]
815 # example of <space-separate-string-of-ricinfo> = "ricsim_g1_1:me1_ricsim_g1_1,me2_ricsim_g1_1:1,2,4 ricsim_g1_1:me2_........."
816 # format of ric-info:  <ric-id>:<list-of-mes>:<list-of-policy-type-ids>
817 # (Function for test scripts)
818 api_get_rics() {
819         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
820     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
821         ((RES_TEST++))
822
823     if [ $# -lt 2 ]; then
824                 __print_err "<reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]" $@
825                 return 1
826         fi
827
828         query="/rics"
829         if [ $2 != "NOTYPE" ]; then
830         query="/rics?policyType="$2
831         fi
832
833     res="$(__do_curl_to_agent GET $query)"
834     status=${res:${#res}-3}
835
836         if [ $status -ne $1 ]; then
837                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
838                 ((RES_FAIL++))
839                 return 1
840         fi
841
842         if [ $# -gt 2 ]; then
843                 body=${res:0:${#res}-3}
844                 res=$(python ../common/create_rics_json.py ".tmp_rics.json" "$3" )
845                 if [ $res -ne 0 ]; then
846                         echo -e $RED" FAIL, could not create target ric info json"$ERED
847                         ((RES_FAIL++))
848                         return 1
849                 fi
850
851                 targetJson=$(<.tmp_rics.json)
852         echo "TARGET JSON: $targetJson" >> $HTTPLOG
853                 res=$(python ../common/compare_json.py "$targetJson" "$body")
854                 if [ $res -ne 0 ]; then
855                         echo -e $RED" FAIL, returned body not correct"$ERED
856                         ((RES_FAIL++))
857                         return 1
858                 fi
859         fi
860
861         ((RES_PASS++))
862         echo -e $GREEN" PASS"$EGREEN
863         return 0
864 }
865
866 ##################################################################
867 #### API Test case functions Service registry and supervision ####
868 ##################################################################
869
870 # API test function: PUT /service
871 # args: <response-code>  <service-name> <keepalive-timeout> <callbackurl>
872 # (Function for test scripts)
873 api_put_service() {
874         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
875     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
876         ((RES_TEST++))
877     if [ $# -ne 4 ]; then
878         __print_err "<response-code>  <service-name> <keepalive-timeout> <callbackurl>" $@
879         return 1
880     fi
881
882     query="/service"
883     json="{\"callbackUrl\": \""$4"\",\"keepAliveIntervalSeconds\": \""$3"\",\"serviceName\": \""$2"\"}"
884     file=".tmp.json"
885         echo "$json" > $file
886
887     res="$(__do_curl_to_agent PUT $query $file)"
888     status=${res:${#res}-3}
889
890         if [ $status -ne $1 ]; then
891                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
892                 ((RES_FAIL++))
893                 return 1
894         fi
895
896         ((RES_PASS++))
897         echo -e $GREEN" PASS"$EGREEN
898         return 0
899 }
900
901 # API test function: GET /services
902 #args: <response-code> [ (<query-service-name> <target-service-name> <keepalive-timeout> <callbackurl>) | (NOSERVICE <target-service-name> <keepalive-timeout> <callbackurl> [<target-service-name> <keepalive-timeout> <callbackurl>]* )]
903 # (Function for test scripts)
904 api_get_services() {
905         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
906     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
907         ((RES_TEST++))
908
909         #Number of accepted parameters: 1, 2, 4, 7, 10, 13,...
910         paramError=1
911         if [ $# -eq 1 ]; then
912                 paramError=0
913         elif [ $# -eq 2 ] && [ $2 != "NOSERVICE" ]; then
914                 paramError=0
915         elif [ $# -eq 5 ]; then
916                 paramError=0
917         elif [ $# -gt 5 ] && [ $2 == "NOSERVICE" ]; then
918                 argLen=$(($#-2))
919                 if [ $(($argLen%3)) -eq 0 ]; then
920                         paramError=0
921                 fi
922         fi
923
924     if [ $paramError -ne 0 ]; then
925                 __print_err "<response-code> [ (<query-service-name> <target-service-name> <keepalive-timeout> <callbackurl>) | (NOSERVICE <target-service-name> <keepalive-timeout> <callbackurl> [<target-service-name> <keepalive-timeout> <callbackurl>]* )]" $@
926                 return 1
927         fi
928
929     query="/services"
930
931     if [ $# -gt 1 ] && [ $2 != "NOSERVICE" ]; then
932         query="/services?name="$2
933         fi
934
935     res="$(__do_curl_to_agent GET $query)"
936     status=${res:${#res}-3}
937
938         if [ $status -ne $1 ]; then
939                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
940                 ((RES_FAIL++))
941                 return 1
942         fi
943
944         if [ $# -gt 2 ]; then
945                 variableArgCount=$(($#-2))
946                 body=${res:0:${#res}-3}
947         targetJson="["
948                 shift; shift;
949                 cntr=0
950                 while [ $cntr -lt $variableArgCount ]; do
951                         servicename=$1; shift;
952                         timeout=$1; shift;
953                         callback=$1; shift;
954                         if [ $cntr -gt 0 ]; then
955                                 targetJson=$targetJson","
956                         fi
957                         # timeSinceLastActivitySeconds value cannot be checked since value varies
958                         targetJson=$targetJson"{\"serviceName\": \""$servicename"\",\"keepAliveIntervalSeconds\": "$timeout",\"timeSinceLastActivitySeconds\":\"????\",\"callbackUrl\": \""$callback"\"}"
959                         let cntr=cntr+3
960                 done
961                 targetJson=$targetJson"]"
962                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
963                 res=$(python ../common/compare_json.py "$targetJson" "$body" "serviceName")
964                 if [ $res -ne 0 ]; then
965                         echo -e $RED" FAIL, returned body not correct"$ERED
966                         ((RES_FAIL++))
967                         return 1
968                 fi
969         fi
970
971         ((RES_PASS++))
972         echo -e $GREEN" PASS"$EGREEN
973         return 0
974 }
975
976 # API test function: GET /services  (only checking service names)
977 # args: <response-code> [<service-name>]*"
978 # (Function for test scripts)
979 api_get_service_ids() {
980         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
981     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
982         ((RES_TEST++))
983
984     if [ $# -lt 1 ]; then
985                 __print_err "<response-code> [<service-name>]*" $@
986                 return 1
987         fi
988
989     query="/services"
990     res="$(__do_curl_to_agent GET $query)"
991     status=${res:${#res}-3}
992
993         if [ $status -ne $1 ]; then
994                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
995                 ((RES_FAIL++))
996                 return 1
997         fi
998
999         body=${res:0:${#res}-3}
1000         targetJson="["
1001         for rapp in ${@:2} ; do
1002                 if [ "$targetJson" != "[" ]; then
1003                         targetJson=$targetJson","
1004                 fi
1005                 targetJson=$targetJson"{\"callbackUrl\":\"????\",\"keepAliveIntervalSeconds\":\"????\",\"serviceName\":\""$rapp"\",\"timeSinceLastActivitySeconds\":\"????\"}"
1006         done
1007
1008         targetJson=$targetJson"]"
1009         echo "TARGET JSON: $targetJson" >> $HTTPLOG
1010         res=$(python ../common/compare_json.py "$targetJson" "$body" "serviceName")
1011
1012         if [ $res -ne 0 ]; then
1013                 echo -e $RED" FAIL, returned body not correct"$ERED
1014                 ((RES_FAIL++))
1015                 return 1
1016         fi
1017
1018         ((RES_PASS++))
1019         echo -e $GREEN" PASS"$EGREEN
1020         return 0
1021 }
1022
1023 # API test function: DELETE /services
1024 # args: <response-code> <service-name>
1025 # (Function for test scripts)
1026 api_delete_services() {
1027         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1028     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1029         ((RES_TEST++))
1030
1031     if [ $# -ne 2 ]; then
1032                 __print_err "<response-code> <service-name>" $@
1033                 return 1
1034         fi
1035
1036     query="/services?name="$2
1037     res="$(__do_curl_to_agent DELETE $query)"
1038     status=${res:${#res}-3}
1039
1040         if [ $status -ne $1 ]; then
1041                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1042                 ((RES_FAIL++))
1043                 return 1
1044         fi
1045
1046         ((RES_PASS++))
1047         echo -e $GREEN" PASS"$EGREEN
1048         return 0
1049 }
1050
1051 # API test function: PUT /services/keepalive
1052 # args: <response-code> <service-name>
1053 # (Function for test scripts)
1054 api_put_services_keepalive() {
1055         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1056     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1057         ((RES_TEST++))
1058
1059     if [ $# -ne 2 ]; then
1060                 __print_err "<response-code> <service-name>" $@
1061                 return 1
1062         fi
1063
1064     query="/services/keepalive?name="$2
1065     res="$(__do_curl_to_agent PUT $query)"
1066     status=${res:${#res}-3}
1067
1068         if [ $status -ne $1 ]; then
1069                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1070                 ((RES_FAIL++))
1071                 return 1
1072         fi
1073
1074         ((RES_PASS++))
1075         echo -e $GREEN" PASS"$EGREEN
1076         return 0
1077 }
1078