Clean autotest folder
[nonrtric.git] / test / common / ricsimulator_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
21 ### Admin API functions for the RIC simulator
22
23
24 # Excute a curl cmd towards a ricsimulator and check the response code.
25 # args: <expected-response-code> <curl-cmd-string> [<file>]
26 __execute_curl_to_sim() {
27         echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
28         echo " CMD: $2" >> $HTTPLOG
29         if [ $# -eq 3 ]; then
30                 echo " FILE: $(<$3)"  >> $HTTPLOG
31         fi
32         res="$($2)"
33         echo " RESP: $res" >> $HTTPLOG
34         retcode=$?
35     if [ $retcode -ne 0 ]; then
36                 echo " RETCODE: "$retcode
37         echo -e $RED"  ERROR - fatal error when executing curl."$ERED
38         return 1
39     fi
40     status=${res:${#res}-3}
41     if [ $status -eq $1 ]; then
42         echo -e $GREEN" OK"$EGREEN
43         return 0
44     fi
45     echo -e $RED"  ERROR - expected http response: "$1" but got http response: "$status $ERED
46     return 1
47 }
48
49 # Tests if a variable value in the ricsimulator is equal to a target value and and optional timeout.
50 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable is
51 # equal to the target or not.
52 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
53 # before setting pass or fail depending on if the variable value becomes equal to the target
54 # value or not.
55 # (Function for test scripts)
56 sim_equal() {
57
58         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
59                 app=$1
60                 port=$(__find_sim_port $app)
61                 __var_test $app "$LOCALHOST$port/counter/" $2 "=" $3 $4
62                 return 0
63         else
64                 ((RES_CONF_FAIL++))
65                 __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]"
66                 return 1
67         fi
68 }
69
70 # Print a variable value from the RIC sim.
71 # args: <ric-id> <variable-name>
72 # (Function for test scripts)
73 sim_print() {
74
75         if [ $# != 2 ]; then
76                 ((RES_CONF_FAIL++))
77         __print_err "need two args, <ric-id> <sim-param>" $@
78                 exit 1
79         fi
80         app=$1
81         port=$(__find_sim_port $app)
82         echo -e $BOLD"INFO(${BASH_LINENO[0]}): $app, $2 = $(__do_curl $LOCALHOST$port/counter/$2)"$EBOLD
83 }
84
85 # Simulator API: Put a policy type in a ric
86 # args: <response-code> <ric-id> <policy-type-id> <policy-type-file>
87 # (Function for test scripts)
88 sim_put_policy_type() {
89         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
90         if [ $# -ne 4 ]; then
91                 ((RES_CONF_FAIL++))
92                 __print_err "<response-code> <ric-id> <policy-type-id> <policy-type-file>" $@
93                 return 1
94         fi
95         app=$2
96         res=$(__find_sim_port $app)
97
98     curlString="curl -X PUT -sw %{http_code} $LOCALHOST"$res"/policytype?id="$3" -H Content-Type:application/json --data-binary @"$4
99
100         __execute_curl_to_sim $1 "$curlString" $4
101         return $?
102 }
103
104 # DSimulator API: Delete a policy type in a ric
105 # <response-code> <ric-id> <policy-type-id>
106 # (Function for test scripts)
107 sim_delete_policy_type() {
108         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
109         if [ $# -ne 3 ]; then
110                 ((RES_CONF_FAIL++))
111                 __print_err "<response-code> <ric-id> <policy_type_id>" $@
112                 return 1
113         fi
114         app=$2
115         res=$(__find_sim_port $app)
116
117     curlString="curl -X DELETE -sw %{http_code} $LOCALHOST"$res"/policytype?id="$3
118
119     __execute_curl_to_sim $1 "$curlString"
120         return $?
121 }
122
123 # Simulator API: Delete instances (and status), for one ric
124 # <response-code> <ric-id>
125 # (Function for test scripts)
126 sim_post_delete_instances() {
127         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
128         if [ $# -ne 2 ]; then
129                 ((RES_CONF_FAIL++))
130                 __print_err "<response-code> <ric-id>" $@
131                 return 1
132         fi
133         app=$2
134         res=$(__find_sim_port $app)
135
136     curlString="curl -X POST -sw %{http_code} $LOCALHOST"$res"/deleteinstances"
137
138     __execute_curl_to_sim $1 "$curlString"
139         return $?
140 }
141
142 # Simulator API: Delete all (instances/types/statuses/settings), for one ric
143 # <response-code> <ric-id>
144 # (Function for test scripts)
145 sim_post_delete_all() {
146         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
147         if [ $# -ne 3 ]; then
148                 ((RES_CONF_FAIL++))
149                 __print_err "<response-code> <numericic-id>" $@
150                 return 1
151         fi
152         app=$2
153         res=$(__find_sim_port $app)
154
155     curlString="curl -X POST -sw %{http_code} $LOCALHOST"$res"/deleteall"
156
157     __execute_curl_to_sim $1 "$curlString"
158         return $?
159 }
160
161 # Simulator API: Set (or reset) response code for next A1 message, for one ric
162 # <response-code> <ric-id> [<forced_response_code>]
163 # (Function for test scripts)
164 sim_post_forcedresponse() {
165         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
166         if [ $# -ne 3 ]; then
167                 ((RES_CONF_FAIL++))
168                 __print_err "<response-code> <ric-id> <forced_response_code>" $@
169                 return 1
170         fi
171         app=$2
172         res=$(__find_sim_port $app)
173
174     curlString="curl -X POST -sw %{http_code} $LOCALHOST"$res"/forceresponse"
175         if [ $# -eq 3 ]; then
176                 curlString=$curlString"?code="$3
177         fi
178
179     __execute_curl_to_sim $1 "$curlString"
180         return $?
181 }
182
183 # Simulator API: Set (or reset) A1 response delay, for one ric
184 # <response-code> <ric-id> [<delay-in-seconds>]
185 # (Function for test scripts)
186 sim_post_forcedelay() {
187         echo -e $BOLD"CONF(${BASH_LINENO[0]}): "${FUNCNAME[0]} $@ $EBOLD
188         if [ $# -ne 3 ]; then
189                 ((RES_CONF_FAIL++))
190                 __print_err "<response-code> <ric-id> [<delay-in-seconds>]" $@
191                 return 1
192         fi
193         app=$2
194         res=$(__find_sim_port $app)
195
196     curlString="curl -X POST -sw %{http_code} $LOCALHOST$res/delay"
197         if [ $# -eq 3 ]; then
198                 curlString=$curlString"?delay="$3
199         fi
200
201     __execute_curl_to_sim $1 "$curlString"
202         return $?
203 }