#!/bin/bash
#/////////////////////////////////////////////////////////////////////////
#//
#// (c) University of Southampton IT Innovation Centre, 2018
#//
#// Copyright in this software belongs to University of Southampton
#// IT Innovation Centre of Gamma House, Enterprise Road,
#// Chilworth Science Park, Southampton, SO16 7NS, UK.
#//
#// This software may not be used, sold, licensed, transferred, copied
#// or reproduced in whole or in part in any manner or form or in or
#// on any media by any person other than in accordance with the terms
#// of the Licence Agreement supplied with the software, or otherwise
#// without the prior written consent of the copyright owners.
#//
#// This software is distributed WITHOUT ANY WARRANTY, without even the
#// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#// PURPOSE, except where stated in the Licence Agreement supplied with
#// the software.
#//
#//      Created By :            Nikolay Sanchev
#//      Created Date :          21/02/2019
#//      Created for Project :   FLAME
#//
#/////////////////////////////////////////////////////////////////////////

set -euo pipefail

CLMC_IP="localhost"

JSON_CONFIG=$1  # expects the JSON configuration passed to the execute_graph_pipeline API endpoint

# extract and delete some of the configuration details, which are used by this script
fields=$(echo ${JSON_CONFIG} | jq  -r '"\(.query_period) \(.service_function_chain) \(.results_measurement_name)"')
read query_period db_name results_measurement <<< ${fields}

# extract the list of ues
ues=($(echo ${JSON_CONFIG} | jq  -r '.ues | .[]'))  # convert the jq array to bash array

# delete these fields
JSON_CONFIG=$(echo ${JSON_CONFIG} | jq 'del(.query_period, .results_measurement_name, .ues)')


while true
do
    echo "Building temporal graph..."

    end=$(date +%s)
    start=$((${end}-${query_period}))

    echo "Start - ${start}, End - ${end}"
    JSON_STRING=$(echo ${JSON_CONFIG} | jq --argjson from ${start} --argjson to ${end} '. + {from: $from, to: $to}')
    echo "Sending build request to CLMC"
    echo "Request body - ${JSON_STRING}"
    response=$(curl -s -X POST -d "${JSON_STRING}" http://${CLMC_IP}/clmc-service/graph/temporal)

    fields=$(echo ${response} | jq  -r '"\(.graph.time_range.to) \(.graph.uuid)"')
    read timestamp graph_uuid <<< ${fields}

    endpoints=($(echo ${response} | jq  -r '.graph.endpoints | .[]'))  # convert the jq array to bash array

    echo "Received request uuid ${graph_uuid}"
    echo "Timestamp to use for measurement ${timestamp}"

    # check the count of the received endpoints, otherwise an empty array will be considered as an unset variable
    if [[ ${#endpoints[@]} -eq 0 ]]; then
        echo "The list of endpoints returned by the build request is empty"
    else
        echo "Received endpoints: ${endpoints[@]}"
        for endpoint in ${endpoints[@]}; do
            for ue in ${ues[@]}; do

                echo "Querying for round-trip time..."
                response=$(curl -s -X GET "http://${CLMC_IP}/clmc-service/graph/temporal/${graph_uuid}/round-trip-time?startpoint=${ue}&endpoint=${endpoint}")

                global_tags=$(echo ${response} | jq -r '.global_tags | to_entries | map("\(.key)=\(.value|tostring)") | join(",")')
                echo "Global tags: ${global_tags}"

                local_tags=$(echo ${response} | jq -r '.local_tags | to_entries | map("\(.key)=\(.value|tostring)") | join (",")')
                echo "Local tags: ${local_tags}"

                fields=$(echo ${response} | jq -r '. | "\(.round_trip_time) \(.response_time) \(.total_forward_latency)"')
                read rtt service_delay network_delay <<< ${fields}

                echo "Round-trip-time: ${rtt}"
                echo "Service delay: ${service_delay}"
                echo "Network latency ${network_delay}"

                measurement_line="${results_measurement},${global_tags},${local_tags} round_trip_time=${rtt},service_delay=${service_delay},network_delay=${network_delay} ${timestamp}"
                echo "Measurement line: ${measurement_line}"
                response=$(curl -si -X POST "http://${CLMC_IP}/influxdb/write?db=${db_name}" --data-binary "${measurement_line}")
                echo "InfluxDB response: ${response}"

            done
        done
    fi

    echo "Deleting temporal graph..."
    response=$(curl -s -X DELETE "http://${CLMC_IP}/clmc-service/graph/temporal/${graph_uuid}")
    echo ${response}

    echo "Sleeping ${query_period} seconds"
    sleep $((${query_period}-1))

done