Define and onboard model for subscription events
[cps.git] / docker-compose / initfile / onboardSubscriptionModel.sh
1 #!/bin/bash
2 #
3 # ============LICENSE_START=======================================================
4 #   Copyright (C) 2023 Nordix Foundation.
5 # ================================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #
18 #  SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20
21
22 echo "Getting ready to upload model for subscription events ..."
23
24 createSchemaSetBasePath="/cps/api/v2/dataspaces/NCMP-Admin/schema-sets?schema-set-name=subscriptions"
25 createAnchorBasePath="/cps/api/v2/dataspaces/NCMP-Admin/anchors?schema-set-name=subscriptions&anchor-name=AVC-subscriptions"
26
27 ATTEMPT_COUNT=0
28 while :
29 do
30   status="UP"
31   hostIpAddress=$(ip -4 route show default | cut -d" " -f3)
32
33   if  curl -X 'GET' 'http://'"$hostIpAddress"':'"$CPS_CORE_MANAGEMENT_PORT"'/manage/health/readiness' | grep -q "$status"
34   then
35         echo "Checking that NCMP dataspace exists ..."
36         ncmpDataspaceExists=$(curl --write-out %{http_code} --silent --output /dev/null -X 'GET' 'http://'"$hostIpAddress"':'"$CPS_CORE_PORT"'/cps/api/v2/admin/dataspaces/NCMP-Admin' -H 'accept: */*' --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE=')
37
38         echo "NCMP dataspace exist:  $ncmpDataspaceExists"
39
40         if [ "$ncmpDataspaceExists" == 200 ]
41         then
42           echo "Uploading model ..."
43           create_schema_set_status_code=$(curl --write-out %{http_code} --silent --output /dev/null -X 'POST' 'http://'"$hostIpAddress"':'"$CPS_CORE_PORT"''"$createSchemaSetBasePath"'' -H 'accept: */*' --form "file=@"/model/subscription.yang"" --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE=')
44           create_anchor_status_code=$(curl --write-out %{http_code} --silent --output /dev/null -X 'POST' 'http://'"$hostIpAddress"':'"$CPS_CORE_PORT"''"$createAnchorBasePath"'' -H 'accept: */*' --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE=')
45
46           echo "create schema set status: $create_schema_set_status_code"
47           echo "create anchor status: $create_anchor_status_code"
48
49           if [ "$create_schema_set_status_code" == 201 ] && [ "$create_anchor_status_code" == 201 ]
50           then
51             echo "Model upload finish!"
52             echo "Exiting container ..."
53             echo "Bye Bye!"
54             break
55           fi
56         fi
57   fi
58
59   if [ $ATTEMPT_COUNT == 20 ]
60   then
61     echo -e "Creating schema set last status:\n $(curl -X 'POST' 'http://'"$hostIpAddress"':'"$CPS_CORE_PORT"''"$createSchemaSetBasePath"'' -H 'accept: */*' --form "file=@"/model/subscription.yang"" --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE=')\n"
62     echo -e "Creating anchor last status:\n $(curl -X 'POST' 'http://'"$hostIpAddress"':'"$CPS_CORE_PORT"''"$createAnchorBasePath"'' -H 'accept: */*' --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE=')\n"
63     echo -e "\nToo many attempts. Bye Bye!"
64     break
65   else
66     echo "RETRYING ...[ $ATTEMPT_COUNT attempt(s) ]"
67     ATTEMPT_COUNT=$(($ATTEMPT_COUNT +1))
68     sleep 10
69   fi
70 done