#!/bin/bash

# Rest API config
ping_mgmt_ip='<rw_mgmt_ip>'
ping_mgmt_port=18888

# VNF specific configuration
pong_server_ip='<rw_connection_point_name pong_vnfd/cp0>'
ping_rate=5
server_port=5555

# Make rest API calls to configure VNF
curl -D /dev/stdout \
    -H "Accept: application/vnd.yang.data+xml" \
    -H "Content-Type: application/vnd.yang.data+json" \
    -X POST \
    -d "{\"ip\":\"$pong_server_ip\", \"port\":$server_port}" \
    http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/server
rc=$?
if [ $rc -ne 0 ]
then
    echo "Failed to set server info for ping!"
    exit $rc
fi

curl -D /dev/stdout \
    -H "Accept: application/vnd.yang.data+xml" \
    -H "Content-Type: application/vnd.yang.data+json" \
    -X POST \
    -d "{\"rate\":$ping_rate}" \
    http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/rate
rc=$?
if [ $rc -ne 0 ]
then
    echo "Failed to set ping rate!"
    exit $rc
fi

output=$(curl -D /dev/stdout \
    -H "Accept: application/vnd.yang.data+xml" \
    -H "Content-Type: application/vnd.yang.data+json" \
    -X POST \
    -d "{\"enable\":true}" \
    http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/adminstatus/state)
if [[ $output == *"Internal Server Error"* ]]
then
    echo $output
    exit 3
else
    echo $output
fi


exit 0
