#!/bin/bash

#
#	License type: BSD 3-Clause License
#	License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
#
#	Created by Stephane Bourque on 2021-03-04.
#	Arilia Wireless Inc.
#

if [[ "$(which jq)" == "" ]]
then
  echo "You need the package jq installed to use this script."
  exit 1
fi

if [[ "$(which curl)" == "" ]]
then
  echo "You need the package curl installed to use this script."
  exit 1
fi

if [[ -z "${OWSEC}" || -z "${OWSEC_USERNAME}" || -z "${OWSEC_PASSWORD}" ]]
then
        echo "You must set the variables OWSEC, OWSEC_USERNAME, and OWSEC_PASSWORD in order to use this script. Something like"
echo "export OWSEC=security.isp.com:16001"
echo "export OWSEC_USERNAME=theusername@domain.com"
echo "export OWSEC_PASSWORD=mytoughpassword"
exit 1
fi

username=${OWSEC_USERNAME}
password=${OWSEC_PASSWORD}

if [[ "${FLAGS}" == "" ]]
then
	FLAGS="-s"
fi

token=""
result_file=result.json
browser_list=(firefox sensible-browser xdg-open w3m links links2 lynx youtube-dl)
browser=""

login() {
  payload="{ \"userId\" : \"$username\" , \"password\" : \"$password\" }"
  token=$(curl ${FLAGS} -X POST -H "Content-Type: application/json" -d "$payload" "https://${OWSEC}/api/v1/oauth2" | jq -r '.access_token')

  if [[ "${token}" == "null" ]]
  then
  	echo "Could not login. Please verify the host and username/password."
    exit 13
  fi
  echo "${token}" > token.json
  setprov
}

findbrowser() {
	if [[ "${browser}" != "" ]]
	then
		echo
	elif [[ "$(uname)" == "Darwin" ]]
	then
		browser=open
	else
		BROWSER_LIST=(firefox sensible-browser xdg-open w3m links links2 lynx youtube-dl)
		for br_name in "${browser_list[@]}"
		do
			if [[ $(which ${br_name}) != "" ]]
			then
				browser=${br_name}
				break
			fi
		done
	fi
}

setprov() {
if [ -z ${OWPROV_OVERRIDE+x} ]; then
	curl  ${FLAGS} -X GET "https://${OWSEC}/api/v1/systemEndpoints" \
        -H "Accept: application/json" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}"  > ${result_file}
	rawurl="$(cat ${result_file} | jq -r '.endpoints[] | select( .type == "owprov" ) | .uri')"
	if [[ ! -z "${rawurl}" ]]; then
		proto="$(echo $rawurl | grep :// | sed -e's,^\(.*://\).*,\1,g')"
		url="$(echo ${rawurl/$proto/})"
		user="$(echo $url | grep @ | cut -d@ -f1)"
		hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
		host="$(echo $hostport | sed -e 's,:.*,,g')"
		port="$(echo $hostport | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
		path="$(echo $url | grep / | cut -d/ -f2-)"
		export OWPROV=${url}
		echo "Using PROV=${OWPROV}..."
	else
		echo "OWPROV endpoint is not found:"
		jq < ${result_file}
		exit 1
	fi
else
       export OWPROV=${OWPROV_OVERRIDE}
fi
}

setrrm() {
    if [ -z ${OWRRM_OVERRIDE+x} ]; then
    curl  ${FLAGS} -X GET "https://${OWSEC}/api/v1/systemEndpoints" \
        -H "Accept: application/json" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}"  > ${result_file}
    rawurl="$(cat ${result_file} | jq -r '.endpoints[] | select( .type == "owrrm" ) | .uri')"
    if [[ ! -z "${rawurl}" ]]; then
            proto="$(echo $rawurl | grep :// | sed -e's,^\(.*://\).*,\1,g')"
    url="$(echo ${rawurl/$proto/})"
    user="$(echo $url | grep @ | cut -d@ -f1)"
    hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
    host="$(echo $hostport | sed -e 's,:.*,,g')"
    port="$(echo $hostport | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
    path="$(echo $url | grep / | cut -d/ -f2-)"
    export OWRRM=${url}
    echo "Using RRM=${OWRRM}..."
    else
    echo "OWRRM endpoint is not found:"
    jq < ${result_file}
    exit 1
    fi
    else
    export OWRRM=${OWRRM_OVERRIDE}
    fi
}

logout() {
  curl  ${FLAGS} -X DELETE "https://${OWSEC}/api/v1/oauth2/${token}" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}"
  rm -rf token.json
}

getroot() {
    curl    ${FLAGS} "https://${OWPROV}/api/v1/entity/0000-0000-0000" \
            -H "Content-Type: application/json" \
            -H "Authorization: Bearer ${token}" \
            -H "accept: application/json" > ${result_file}
    jq < ${result_file}
}

setroot() {
    payload="{ \"name\" : \"Arilia Root\", \"description\" : \"This is the top level entry in the hierarchy.\" }";
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/entity/0000-0000-0000" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" \
        -d "$payload" > ${result_file}
    jq < ${result_file}
}

addentity() {
    payload="{ \"name\" : \"$1\", \"description\" : \"sub entity.\", \"parent\" : \"0000-0000-0000\", \"notes\" : [ {\"note\" : \"CLI added\"} ] }";
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/entity/1" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" \
        -d "$payload" > ${result_file}
    jq < ${result_file}
}

getentity() {
    curl    ${FLAGS} "https://${OWPROV}/api/v1/entity/$1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

getentities() {
    curl    ${FLAGS} "https://${OWPROV}/api/v1/entity" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

tree() {
    curl    ${FLAGS} "https://${OWPROV}/api/v1/entity?getTree=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

importtree() {
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/entity?setTree=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json " \
    --data "@$1" > ${result_file}
    jq < ${result_file}
}

gettag() {
    curl    ${FLAGS} "https://${OWPROV}/api/v1/inventory?entity=$1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

addtag() {
    payload="{ \"name\" : \"$1\", \"serialNumber\" : \"$1\", \"deviceType\" : \"linksys_ea8300\" }";
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/inventory/1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d "$payload" > ${result_file}
    jq < ${result_file}
}

addunassignedtag() {
    payload="{ \"name\" : \"$1\", \"serialNumber\" : \"$1\", \"deviceType\" : \"linksys_ea8300\" }";
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/inventory/1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d "$payload" > ${result_file}
    jq < ${result_file}
}

assigntagentity() {
    payload="{\"serialNumber\" : \"$1\", \"entity\" : \"$2\"}";
    curl    ${FLAGS} -X PUT "https://${OWPROV}/api/v1/inventory/$1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d "$payload" > ${result_file}
    jq < ${result_file}
}

assigntagvenue() {
    payload="{\"serialNumber\" : \"$1\", \"venue\" : \"$2\"}";
    curl    ${FLAGS} -X PUT "https://${OWPROV}/api/v1/inventory/$1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d "$payload" > ${result_file}
    jq < ${result_file}
}

unassigntag() {
    payload="{\"serialNumber\" : \"$1\" }";
    curl    ${FLAGS} -X PUT "https://${OWPROV}/api/v1/inventory/$1?unassign=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d "$payload" > ${result_file}
    jq < ${result_file}
}

gettags() {
    curl ${FLAGS} "https://${OWPROV}/api/v1/inventory" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

getunassignedtags(){
    curl ${FLAGS} "https://${OWPROV}/api/v1/inventory?unassigned=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

tagcount() {
    curl ${FLAGS} "https://${OWPROV}/api/v1/inventory?countOnly=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

entitycount() {
    curl ${FLAGS} "https://${OWPROV}/api/v1/entity?countOnly=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

venuecount() {
    curl ${FLAGS} "https://${OWPROV}/api/v1/venue?countOnly=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

contactcount() {
    curl ${FLAGS} "https://${OWPROV}/api/v1/contact?countOnly=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

locationcount() {
    curl ${FLAGS} "https://${OWPROV}/api/v1/location?countOnly=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

addconfig() {
    payload="{ \"name\" : \"$1\", \"deviceTypes\" : [\"*\"], \"configuration\" : [ { \"name\" : \"radios\" , \"weight\" : 100, \"configuration\" : { \"radios\": [{ \"band\": \"5G\", \"channel\": 52, \"channel-mode\": \"HE\", \"channel-width\": 80, \"country\": \"CA\" }, { \"band\": \"2G\", \"channel\": 11, \"channel-mode\": \"HE\", \"channel-width\": 20, \"country\": \"CA\" } ] } }] }"
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/configuration/1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d "$payload" > ${result_file}
    echo ${payload}
    jq < ${result_file}
}

addconfigtx() {
    payload="{ \"name\" : \"$1\", \"deviceTypes\" : [\"*\"], \"configuration\" : [ { \"name\" : \"radios TX\" , \"weight\" : 0, \"configuration\" : { \"radios\": [{ \"band\": \"2G\", \"tx-power\": 45 } ] } }] }"
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/configuration/1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d "$payload" > ${result_file}
    echo ${payload}
    jq < ${result_file}
}

addconfigmetrics() {
    payload="{ \"name\" : \"$1\", \"deviceTypes\" : [\"*\"], \"configuration\" : [ { \"name\" : \"metrics gen\" , \"weight\" : 0, \"configuration\" : { \"metrics\":  { \"statistics\": { \"interval\": 120, \"types\": [ \"ssids\", \"lldp\", \"clients\" ] } } } }] }"
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/configuration/1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d "$payload" > ${result_file}
    echo ${payload}
    jq < ${result_file}
}

addconfigfile() {
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/configuration/1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d @"$1" > ${result_file}
    jq < ${result_file}
}

getconfig() {
    curl    ${FLAGS}  "https://${OWPROV}/api/v1/configuration/$1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

deleteconfig() {
    curl    ${FLAGS} -X DELETE  "https://${OWPROV}/api/v1/configuration/$1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

setentityconfig() {
    payload="{\"deviceConfiguration\" : \"$2\"}";
    curl    ${FLAGS} -X PUT "https://${OWPROV}/api/v1/entity/$1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d "$payload" > ${result_file}
    jq < ${result_file}
}

listconfigs() {
    curl    ${FLAGS}  "https://${OWPROV}/api/v1/configuration" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

deviceconfig() {
    curl    ${FLAGS} "https://${OWPROV}/api/v1/inventory/$1?config=true&explain=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

devicefwoptions() {
    curl    ${FLAGS} "https://${OWPROV}/api/v1/inventory/$1?firmwareOptions=true" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

adddeviceconfig() {
    payload="{\"deviceConfiguration\" : \"$2\"}";
    curl    ${FLAGS} -X PUT "https://${OWPROV}/api/v1/inventory/$1" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${token}" \
    -H "Accept: application/json" \
    -d "$payload" > ${result_file}
    jq < ${result_file}
}

validateconfig() {
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/configuration/1?validateOnly=true" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" \
        -d @"$1" > ${result_file}
    jq < ${result_file}
}

setloglevel() {
    payload="{ \"command\" : \"setloglevel\" , \"subsystems\" : [ { \"tag\" : \"$1\" , \"value\" : \"$2\" } ] }"
    curl  ${FLAGS} -X POST "https://${OWPROV}/api/v1/system" \
        -H "Content-Type: application/json" \
        -H "Accept: application/json" \
        -H "Authorization: Bearer ${token}" \
        -d "$payload"
}

getloglevels() {
    payload="{ \"command\" : \"getloglevels\" }"
    curl  ${FLAGS} -X POST "https://${OWPROV}/api/v1/system" \
        -H "Accept: application/json" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -d "$payload"
}

getloglevelnames() {
    payload="{ \"command\" : \"getloglevelnames\" }"
    curl  ${FLAGS} -X POST "https://${OWPROV}/api/v1/system" \
        -H "Accept: application/json" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -d "$payload"
}

getsubsystemnames() {
    payload="{ \"command\" : \"getsubsystemnames\" }"
    curl  ${FLAGS} -X POST "https://${OWPROV}/api/v1/system" \
        -H "Accept: application/json" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -d "$payload"
}

systeminfo() {
    curl  ${FLAGS} -X GET "https://${OWPROV}/api/v1/system?command=info" \
        -H "Content-Type: application/json" \
        -H "Accept: application/json" \
        -H "Authorization: Bearer ${token}" > ${result_file}
    jq < ${result_file}
}

reloadsubsystem() {
    payload="{ \"command\" : \"reload\", \"subsystems\" : [ \"$1\" ] }"
    curl  ${FLAGS} -X POST "https://${OWPROV}/api/v1/system" \
        -H "Content-Type: application/json" \
        -H "Accept: application/json" \
        -H "Authorization: Bearer ${token}" \
        -d "$payload"
}

getsubdevs() {
    curl ${FLAGS} -X GET "https://${OWPROV}/api/v1/inventory?subscriber=$1" \
        -H "Content-Type: application/json" \
        -H "Accept: application/json" \
        -H "Authorization: Bearer ${token}" > ${result_file}
    jq < ${result_file}
}

addresscheck() {
    echo "Token:${token}"
    wscat -c wss://${OWPROV}/api/v1/ws
}

affecteddevices() {
    curl    ${FLAGS} -X GET "https://${OWPROV}/api/v1/configuration/${1}?computedAffected=true" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

signup() {
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/signup?email=stephane.bourque@gmail.com&macAddress=04f8f8fc3771" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

getsignup() {
    curl    ${FLAGS} -X GET "https://${OWPROV}/api/v1/signup?signupUUID=$1" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

listvenues() {
    curl    ${FLAGS} -X GET "https://${OWPROV}/api/v1/venue" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

getvenuedevices() {
    curl    ${FLAGS} -X GET "https://${OWPROV}/api/v1/venue/$1?getDevices=true&getChildren=true" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

getvenuesperrrm() {
    curl    ${FLAGS} -X GET "https://${OWPROV}/api/v1/venue?RRMvendor=$1" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

listrrmalgos() {
    setrrm
    curl    ${FLAGS} -X GET "http://${OWRRM}/api/v1/algorithms" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

rrmprovider() {
    setrrm
    curl    ${FLAGS} -X GET "http://${OWRRM}/api/v1/provider" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}


notifications() {
    if [[ -z "$1" ]]
        then
                timeout=30
            else
    timeout=$1
    fi
            socket="wss://${OWPROV}/api/v1/ws"
    echo ${socket}
    if [[ "$(which wscat)" == "" ]]
        then
        echo "wscat command not found. Cannot start a websocket session."
    else
    wscat \
		--connect "${socket}" "token:${token}" \
 		--wait $timeout \
 		--execute "token:${token}"
    fi
}

listoverrides() {
    curl    ${FLAGS} -X GET "https://${OWPROV}/api/v1/configurationOverrides/$1" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

addoverride() {
    payload="{ \"serialNumber\" : \"$1\" , \"overrides\" : [ { \"reason\" : \"test\" , \"parameterName\" : \"radios.0.channel\" , \"parameterType\" : \"string\" , \"parameterValue\" : \"14\"} ] }"
    curl    ${FLAGS} -X PUT "https://${OWPROV}/api/v1/configurationOverrides/$1?source=test" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" \
        -d "$payload"  > ${result_file}
    jq < ${result_file}
}

deleteoverride() {
    curl    ${FLAGS} -X DELETE "https://${OWPROV}/api/v1/configurationOverrides/$1?source=test" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

venueupgraderevisions() {
    curl    ${FLAGS} -X PUT "https://${OWPROV}/api/v1/venue/$1?upgradeAllDevices=true&revisionsAvailable=true" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

getsystemconfiguration() {
    curl    ${FLAGS} -X GET "https://${OWPROV}/api/v1/systemConfiguration?entries=$1" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

creategraccount() {
    payload="{ \"name\" : \"Test account\" , \"country\" : \"CA\", \"province\" : \"BC\" , \"city\" : \"Vancouver\", \"organization\" : \"Arilia Wireless Inc.\", \"commonName\" : \"arilia.com\", \"GlobalReachAcctId\" : \"bd63aaa7-b14d-4cdb-85ae-8de6cf2cfa31\", \"privateKey\" : \"-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgl1FpARtuOtw1F9sR2DD82jh6sZFGRn619IY0rmNIFEuhRANCAATB7ji6OF/+heGRCocgVNhw4QGvaL9Kp8F6ZqqZ3aMewRMOfzi3TQaXN12FNBsvXnptx5vk8GAzZk6UAzzvMBVK\n-----END PRIVATE KEY-----\" }"
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/openroaming/globalreach/account/0" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" \
        -d "$payload"  > ${result_file}
    jq < ${result_file}
}

getgraccount() {
    curl    ${FLAGS} -X GET "https://${OWPROV}/api/v1/openroaming/globalreach/account/$1" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

deletegraccount() {
    curl    ${FLAGS} -X DELETE "https://${OWPROV}/api/v1/openroaming/globalreach/account/$1" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

getgraccounts() {
    curl    ${FLAGS} -X GET "https://${OWPROV}/api/v1/openroaming/globalreach/accounts" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" > ${result_file}
    jq < ${result_file}
}

creategrcert() {
    payload="{ \"name\" : \"$2\" }"
    curl    ${FLAGS} -X POST "https://${OWPROV}/api/v1/openroaming/globalreach/certificate/$1/0" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${token}" \
        -H "Accept: application/json" \
        -d "$payload"  > ${result_file}
    jq < ${result_file}

}

shopt -s nocasematch
case "$1" in
    "login") login; echo "You are logged in..."  ; logout ;;
    "getroot") login; getroot; logout;;
    "setroot") login; setroot; logout;;
    "addentity") login; addentity "$2" ; logout;;
    "getentity") login; getentity "$2" ; logout;;
    "getentities") login; getentities ; logout;;
    "entitycount") login; entitycount ; logout;;
    "tree") login; tree ; logout;;
    "importtree") login; importtree $2; logout;;
    "deviceconfig") login; deviceconfig "$2"; logout;;
    "devicefwoptions") login; devicefwoptions "$2"; logout;;
    "addtag") login; addtag "$2"; logout;;
    "unassigntag") login; unassigntag $2; logout;;
    "addunassignedtag") login; addunassignedtag $2 ; logout;;
    "getunassignedtags") login; getunassignedtags ; logout;;
    "gettags") login; gettags ; logout;;
    "gettag") login; gettag $2 ; logout;;
    "tagcount") login; tagcount ; logout;;
    "venuecount") login; venuecount ; logout;;
    "contactcount") login; contactcount ; logout;;
    "locationcount") login; locationcount ; logout;;
    "addconfig") login; addconfig "$2"; logout;;
    "addconfigtx") login; addconfigtx "$2"; logout;;
    "listconfigs") login; listconfigs ; logout;;
    "getconfig") login; getconfig "$2"; logout;;
    "deleteconfig") login; deleteconfig "$2"; logout;;
    "assigntagentity") login; assigntagentity "$2" "$3"; logout;;
    "assigntagvenue") login; assigntagvenue "$2" "$3"; logout;;
    "setentityconfig") login; setentityconfig "$2" "$3"; logout;;
    "adddeviceconfig") login; adddeviceconfig "$2" "$3"; logout;;
    "addconfigmetrics") login; addconfigmetrics "$2" ; logout;;
    "addconfigfile") login; addconfigfile "$2" ; logout;;
    "validateconfig") login; validateconfig "$2"; logout;;
    "setloglevel") login; setloglevel "$2" "$3"  ; logout ;;
    "getloglevels") login; getloglevels; logout ;;
    "getloglevelnames") login; getloglevelnames; logout ;;
    "getsubsystemnames") login; getsubsystemnames; logout ;;
    "reloadsubsystem") login; reloadsubsystem "$2"; logout ;;
    "systeminfo") login; systeminfo ; logout;;
    "addresscheck") login; addresscheck; logout ;;
    "affecteddevices") login; affecteddevices "$2"; logout;;
    "signup") login; signup ; logout;;
    "getsignup") login; getsignup $2; logout;;
    "getsubdevs") login; getsubdevs $2; logout;;
    "listvenues") login; listvenues $2; logout;;
    "getvenuedevices") login; getvenuedevices $2; logout;;
    "listrrmalgos") login; listrrmalgos; logout;;
    "rrmprovider") login; rrmprovider; logout;;
    "notifications") login; notifications "$2"; logout;;
    "listoverrides") login; listoverrides "$2"; logout;;
    "addoverride") login; addoverride "$2"; logout;;
    "deleteoverride") login; deleteoverride "$2"; logout;;
    "venueupgraderevisions") login; venueupgraderevisions "$2"; logout;;
    "getsystemconfiguration") login; getsystemconfiguration "$2"; logout;;
    "creategraccount") login; creategraccount ; logout;;
    "getgraccount") login; getgraccount "$2"; logout;;
    "getgraccounts") login; getgraccounts ; logout;;
    "creategrcert") login; creategrcert "$2" "$3"; logout;;
    "deletegraccount") login; deletegraccount "$2"; logout;;
    "getvenuesperrrm") login; getvenuesperrrm "$2"; logout;;
    *) help ;;
esac

