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