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