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 |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 58 | port=$(__find_sim_port $1) |
| 59 | __var_test $1 "$RIC_SIM_LOCALHOST$port/counter/" $2 "=" $3 $4 |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 60 | return 0 |
| 61 | else |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 62 | __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]" |
| 63 | return 1 |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | # Print a variable value from the RIC sim. |
| 68 | # args: <ric-id> <variable-name> |
| 69 | # (Function for test scripts) |
| 70 | sim_print() { |
| 71 | |
| 72 | if [ $# != 2 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 73 | __print_err "need two args, <ric-id> <sim-param>" $@ |
| 74 | exit 1 |
| 75 | fi |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 76 | port=$(__find_sim_port $1) |
| 77 | echo -e $BOLD"INFO(${BASH_LINENO[0]}): $1, $2 = $(__do_curl $RIC_SIM_LOCALHOST$port/counter/$2)"$EBOLD |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 78 | } |
| 79 | |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 80 | # Tests if a variable value in the RIC simulator contains the target string and and optional timeout |
| 81 | # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable contains |
| 82 | # the target or not. |
| 83 | # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds |
| 84 | # before setting pass or fail depending on if the variable value contains the target |
| 85 | # value or not. |
| 86 | # (Function for test scripts) |
| 87 | sim_contains_str() { |
| 88 | |
| 89 | if [ $# -eq 3 ] || [ $# -eq 4 ]; then |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 90 | port=$(__find_sim_port $1) |
| 91 | __var_test $1 "$RIC_SIM_LOCALHOST$port/counter/" $2 "contain_str" $3 $4 |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 92 | return 0 |
| 93 | else |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 94 | __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]" |
| 95 | return 1 |
| 96 | fi |
| 97 | } |
| 98 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 99 | # Simulator API: Put a policy type in a ric |
| 100 | # args: <response-code> <ric-id> <policy-type-id> <policy-type-file> |
| 101 | # (Function for test scripts) |
| 102 | sim_put_policy_type() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 103 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 104 | if [ $# -ne 4 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 105 | __print_err "<response-code> <ric-id> <policy-type-id> <policy-type-file>" $@ |
| 106 | return 1 |
| 107 | fi |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 108 | res=$(__find_sim_port $2) |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 109 | curlString="curl -X PUT -skw %{http_code} $RIC_SIM_LOCALHOST"$res"/policytype?id="$3" -H Content-Type:application/json --data-binary @"$4 |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 110 | __execute_curl_to_sim $1 "$curlString" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 111 | return $? |
| 112 | } |
| 113 | |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 114 | # Simulator API: Delete a policy type in a ric |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 115 | # <response-code> <ric-id> <policy-type-id> |
| 116 | # (Function for test scripts) |
| 117 | sim_delete_policy_type() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 118 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 119 | if [ $# -ne 3 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 120 | __print_err "<response-code> <ric-id> <policy_type_id>" $@ |
| 121 | return 1 |
| 122 | fi |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 123 | res=$(__find_sim_port $2) |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 124 | 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] | 125 | __execute_curl_to_sim $1 "$curlString" |
| 126 | return $? |
| 127 | } |
| 128 | |
| 129 | # Simulator API: Delete instances (and status), for one ric |
| 130 | # <response-code> <ric-id> |
| 131 | # (Function for test scripts) |
| 132 | sim_post_delete_instances() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 133 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 134 | if [ $# -ne 2 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 135 | __print_err "<response-code> <ric-id>" $@ |
| 136 | return 1 |
| 137 | fi |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 138 | res=$(__find_sim_port $2) |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 139 | curlString="curl -X POST -skw %{http_code} $RIC_SIM_LOCALHOST"$res"/deleteinstances" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 140 | __execute_curl_to_sim $1 "$curlString" |
| 141 | return $? |
| 142 | } |
| 143 | |
| 144 | # Simulator API: Delete all (instances/types/statuses/settings), for one ric |
| 145 | # <response-code> <ric-id> |
| 146 | # (Function for test scripts) |
| 147 | sim_post_delete_all() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 148 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 149 | if [ $# -ne 3 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 150 | __print_err "<response-code> <numericic-id>" $@ |
| 151 | return 1 |
| 152 | fi |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 153 | res=$(__find_sim_port $2) |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 154 | curlString="curl -X POST -skw %{http_code} $RIC_SIM_LOCALHOST"$res"/deleteall" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 155 | __execute_curl_to_sim $1 "$curlString" |
| 156 | return $? |
| 157 | } |
| 158 | |
| 159 | # Simulator API: Set (or reset) response code for next A1 message, for one ric |
| 160 | # <response-code> <ric-id> [<forced_response_code>] |
| 161 | # (Function for test scripts) |
| 162 | sim_post_forcedresponse() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 163 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 164 | if [ $# -ne 3 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 165 | __print_err "<response-code> <ric-id> <forced_response_code>" $@ |
| 166 | return 1 |
| 167 | fi |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 168 | res=$(__find_sim_port $2) |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 169 | curlString="curl -X POST -skw %{http_code} $RIC_SIM_LOCALHOST"$res"/forceresponse" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 170 | if [ $# -eq 3 ]; then |
| 171 | curlString=$curlString"?code="$3 |
| 172 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 173 | __execute_curl_to_sim $1 "$curlString" |
| 174 | return $? |
| 175 | } |
| 176 | |
| 177 | # Simulator API: Set (or reset) A1 response delay, for one ric |
| 178 | # <response-code> <ric-id> [<delay-in-seconds>] |
| 179 | # (Function for test scripts) |
| 180 | sim_post_forcedelay() { |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 181 | __log_conf_start $@ |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 182 | if [ $# -ne 3 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 183 | __print_err "<response-code> <ric-id> [<delay-in-seconds>]" $@ |
| 184 | return 1 |
| 185 | fi |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 186 | res=$(__find_sim_port $2) |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 187 | curlString="curl -X POST -skw %{http_code} $RIC_SIM_LOCALHOST$res/forcedelay" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 188 | if [ $# -eq 3 ]; then |
| 189 | curlString=$curlString"?delay="$3 |
| 190 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 191 | __execute_curl_to_sim $1 "$curlString" |
| 192 | return $? |
| 193 | } |