#!/bin/bash
true=t
echo "Getting ready to upload model for subscription events"
while :
do
sleep 30
echo "Checking that NCMP dataspace and anchor exist ..."
ncmpDataspaceExists=$(psql -h db -d cpsdb -U cps -t -c "SELECT EXISTS (SELECT FROM public.dataspace WHERE name = 'NCMP-Admin');")
ncmpAnchorExists=$(psql -h db -d cpsdb -U cps -t -c "SELECT EXISTS (SELECT FROM public.anchor WHERE name = 'ncmp-dmi-registry');")
echo "NCMP dataspace exist: $ncmpDataspaceExists"
echo "NCMP anchor exist: $ncmpAnchorExists"
echo "$ncmpDataspaceExists == $true and $ncmpAnchorExists == $true"
if [ $ncmpDataspaceExists == $true ] && [ $ncmpAnchorExists == $true ]
then
echo "Uploading model ..."
cpsAndNcmpAsHostAddress=$(ping -c1 cps-and-ncmp | sed -nE 's/^PING[^(]+\(([^)]+)\).*/\1/p')
curl -X 'POST' -i 'http://'"$cpsAndNcmpAsHostAddress"':8080/cps/api/v2/dataspaces?dataspace-name=my-dataspace' -H 'accept: */*' --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE='
curl -X 'POST' 'http://'"$cpsAndNcmpAsHostAddress"':8080/cps/api/v2/dataspaces/my-dataspace/schema-sets?schema-set-name=my-schema-set' -H 'accept: */*' --form "file=@"/model/subscription.yang"" --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE='
curl -X 'POST' 'http://'"$cpsAndNcmpAsHostAddress"':8080/cps/api/v2/dataspaces/my-dataspace/anchors?schema-set-name=my-schema-set&anchor-name=my-anchor' -H 'accept: */*' --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE='
echo "Model upload finish ..."
break
fi
sleep 10 &
echo $!
echo $?
done |