2 # ============LICENSE_START=======================================================
3 # dcaegen2-collectors-veshv
4 # ================================================================================
5 # Copyright (C) 2018 NOKIA
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
25 echo "Send messages to hv-ves from multiple xNF simulators"
26 echo "Usage: $0 [-h|--help] [-v|--verbose] [--messages-in-batch] [--docker-network] [--xnf-logs-directory]"
27 echo " <hv ves hostname> <hv ves port> <simulators amount> <messages batches amount per simulator> <messages sending interval>"
29 echo " - hv ves hostname : HighVolume VES Collector network hostname"
30 echo " - hv ves port : HighVolume VES Collector network port"
31 echo " - simulators amount : Amount of xNF simulators to be launched"
32 echo " - messages amount per simulator : Amount of messages to be sent from each xNF simulator to HV-VES"
33 echo " - messages sending interval : interval in seconds between sending messages from xNFs"
34 echo "Optional parameters:"
35 echo " - messages-in-batch : Amount of messages sent on each request"
36 echo " - docker-network : Docker network to which xNF simulators should be added"
37 echo " - xnf-logs-directory : Path to directory where logs from all xNF simulators should be stored"
38 echo "Example invocations:"
39 echo "./start-simulation.sh --messages-in-batch=5 --docker-network=development_default ves-hv-collector 6061 10 20 0.5"
40 echo "./start-simulation.sh --messages-in-batch=5 --xnf-logs-directory=/tmp/xnf-simulation localhost 6061 10 20 0.5"
44 function verbose_log() {
45 if [ -n "${VERBOSE+x}" ]; then
50 function create_logs_dir() {
51 if [ -n "${XNF_LOGS_DIRECTORY+x}" ]; then
52 if [ ! -d "${XNF_LOGS_DIRECTORY}" ]; then
53 mkdir ${XNF_LOGS_DIRECTORY}
58 function create_xNFs_simulators() {
59 for i in $(seq 1 ${XNFS_AMOUNT}); do
60 local XNF_PORT=$(get_unoccupied_port 32000 65000)
61 verbose_log "Starting xNF simulator container on port ${XNF_PORT} using run-xnf-simulator script"
62 XNF_CONTAINER_ID=$(${DEVELOPMENT_BIN_DIRECTORY}/run-xnf-simulator.sh $XNF_PORT ${DOCKER_NETWORK:-})
63 CREATED_XNF_SIMULATORS_PORTS+=(${XNF_PORT})
64 verbose_log "Container id: ${XNF_CONTAINER_ID}"
65 CREATED_XNF_SIMULATORS_IDS+=(${XNF_CONTAINER_ID})
69 function get_unoccupied_port() {
73 local MPORT=$[$LPORT + ($RANDOM % $UPORT)];
74 local LISTENING_PORTS=$(osqueryi --header=false --list "select port from listening_ports order by port");
75 if (echo "${LISTENING_PORTS[@]}" | grep -xqv $MPORT); then
82 function wait_for_containers_startup_or_fail() {
83 local seconds_to_wait=10
84 local all_containers_healthy=1
86 verbose_log "Waiting ${seconds_to_wait}s for containers startup"
88 for i in $(seq 1 ${seconds_to_wait}); do
89 verbose_log "Try no. ${i}"
90 all_containers_healthy=1
91 for port in ${CREATED_XNF_SIMULATORS_PORTS[@]}; do
92 verbose_log "Checking container on port ${port}"
93 local status_code=$(curl -s -o /dev/null -I -w "%{http_code}" localhost:${port}/healthcheck)
94 if [ $status_code -ne 200 ]; then
95 verbose_log "Container on port ${port} is unhealthy "
96 all_containers_healthy=0
100 if [ $all_containers_healthy -eq 1 ]; then
107 if [ $all_containers_healthy -ne 1 ]; then
108 echo "Some xNFs simulators failed at startup. Trying to cleanup..."
115 function start_simulation() {
116 verbose_log "Simulation: every xNF will send ${MESSAGES_IN_BATCH} messages to hv-ves
117 ${MESSAGE_BATCHES_AMOUNT} times, once every ${MESSAGES_SENDING_INTERVAL}"
118 for port in ${CREATED_XNF_SIMULATORS_PORTS[@]}; do
119 start_single_simulation $port $MESSAGES_IN_BATCH &
123 function start_single_simulation() {
125 local messages_to_be_sent=$2
126 local message_type="VALID"
127 for i in $(seq 1 ${MESSAGE_BATCHES_AMOUNT}); do
128 ${DEVELOPMENT_BIN_DIRECTORY}/xnf-simulation.sh $port $messages_to_be_sent $message_type > /dev/null &
129 sleep $MESSAGES_SENDING_INTERVAL
133 function assure_all_xNFs_requests_were_sent {
134 WAIT_TIME_FOR_REQUESTS_TO_BE_SENT=$(echo ";1 + $MESSAGES_SENDING_INTERVAL * $MESSAGE_BATCHES_AMOUNT" | bc)
135 echo "Waiting ${WAIT_TIME_FOR_REQUESTS_TO_BE_SENT}s for all xNF requests to be sent"
136 sleep $WAIT_TIME_FOR_REQUESTS_TO_BE_SENT
139 function wait_for_simulators_to_finish_sending_messages() {
140 local seconds_to_wait=$1
141 local all_containers_finished=1
143 echo "Waiting up to ${seconds_to_wait}s for xNFs simulators to finish sending messages"
144 for i in $(seq 1 ${seconds_to_wait}); do
145 verbose_log "Wait no. ${i}"
146 all_containers_finished=1
147 for port in ${CREATED_XNF_SIMULATORS_PORTS[@]}; do
148 local container_status=$(curl --request GET -s localhost:${port}/healthcheck | jq -r '.["Detailed status"]')
150 verbose_log "Container on port ${port} status: ${container_status}"
151 if [ "${container_status}" = "Busy" ]; then
152 all_containers_finished=0
156 if [ $all_containers_finished -eq 1 ]; then
157 echo "All containers finished sending messages"
167 for container_id in ${CREATED_XNF_SIMULATORS_IDS[@]}; do
168 verbose_log "Stopping container: ${container_id}"
169 docker stop $container_id > /dev/null
170 if [ -n "${XNF_LOGS_DIRECTORY+x}" ]; then
171 local log_file=${XNF_LOGS_DIRECTORY}/${container_id}.log
172 verbose_log "Writing container logs to: ${log_file}"
173 docker logs ${container_id} > $log_file
175 verbose_log "Removing container: ${container_id}"
176 docker rm $container_id > /dev/null
182 function parse_long_opts_with_arguments() {
183 if [[ ${OPTARG} =~ .*=.* ]] # is option in --key=value format
186 ((${#OPT} <= 1)) && {
187 echo "Invalid option '$OPT'" >&2
192 echo -e "No value provided for ${OPTARG}. Please use \"--${OPTARG}=VALUE\" format." >&2
198 optspec=":vh-:" # catch v, h and -
199 while getopts "$optspec" arg; do
208 parse_long_opts_with_arguments
211 MESSAGES_IN_BATCH=$OPTARG ;;
213 DOCKER_NETWORK=$OPTARG ;;
215 XNF_LOGS_DIRECTORY=$OPTARG ;;
225 echo "Unknown option -${OPTARG}" >&2
231 [ $# -le 4 ] && (echo -e "Unsufficient arguments"; usage)
234 DEVELOPMENT_BIN_DIRECTORY=$(realpath $(dirname "$0"))
238 MESSAGE_BATCHES_AMOUNT=${4}
239 MESSAGES_SENDING_INTERVAL=${5}
241 # set defaults if absent
242 [ -z "${MESSAGES_IN_BATCH}" ] && MESSAGES_IN_BATCH=1
247 CREATED_XNF_SIMULATORS_PORTS=()
248 CREATED_XNF_SIMULATORS_IDS=()
249 echo "Creating ${XNFS_AMOUNT} xNFs simulators"
250 trap cleanup SIGINT SIGTERM
251 create_xNFs_simulators
253 wait_for_containers_startup_or_fail
255 echo "All xNFs containers are healthy, starting simulation"
258 assure_all_xNFs_requests_were_sent
260 assumed_message_sending_time=$(echo ";0.00025 * $XNFS_AMOUNT" | bc)
261 seconds_to_wait=$(echo ";$assumed_message_sending_time * $MESSAGE_BATCHES_AMOUNT * $MESSAGES_IN_BATCH" | bc)
262 wait_for_simulators_to_finish_sending_messages $seconds_to_wait
263 # there might be network lag between moment when xNF finished sending messages and they actually are received by hv-ves
264 # thus we cannot start removing xNFs immediately to prevent closing socket channels