Add Bronze Get Started demo scripts
[it/dep.git] / demos / bronze / xapp-hw.sh
1 #!/bin/bash
2 ################################################################################
3 #   Copyright (c) 2020 AT&T Intellectual Property.                             #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17
18 #set -x
19
20 acknowledge() {
21   echo "$1"
22   read  -n 1 -p "Press any key to continue, or CTRL-C to abort" mainmenuinput
23   echo
24 }
25
26
27 acknowledge "This script demonstrates the deployment of HelloWorld xApp."
28 DIRNAME="/tmp/tsflow-$(date +%Y%m%d%H%M)"
29 mkdir $DIRNAME
30 cd $DIRNAME
31
32 echo "===>  Generating xApp on-boarding file"
33 echo '{
34   "config-file.json_url": "https://gerrit.o-ran-sc.org/r/gitweb?p=ric-app/hw.git;a=blob_plain;f=init/config-file.json;hb=HEAD"
35 }' > onboard.hw.url
36
37
38 echo "===>  On-boarding xApps"
39 curl --location --request POST "http://$(hostname):32080/onboard/api/v1/onboard/download" \
40      --header 'Content-Type: application/json' --data-binary "@./onboard.hw.url"
41 acknowledge "A \"Created\" status indicates on-boarding successful."
42
43 echo "======> Listing on boarded xApps"
44 curl --location --request GET "http://$(hostname):32080/onboard/api/v1/charts"
45 acknowledge "Verify that the on-boarded xApp indeed is listed."
46
47 echo "===>  Deploy xApps"
48 curl --location --request POST "http://$(hostname):32080/appmgr/ric/v1/xapps" \
49      --header 'Content-Type: application/json' --data-raw '{"xappName": "hwxapp"}'
50 acknowledge "A \"deployed\" status indicates that the xApp has been deployed."
51 # response: {"instances":null,"name":"trafficxapp","status":"deployed","version":"1.0"}
52
53 POD_HW=""
54 echo -n "Waiting for all newly deployed xApp pod(s) reaching running state."
55 while [ -z $POD_HW ]; do
56   echo -n "."
57   sleep 5
58   POD_HW=$(kubectl get pods -n ricxapp | grep Running | grep "\-hwxapp\-" | cut -f1 -d ' ')
59 done
60 echo && echo "Now all newly deployed xApp(s) have reached running state"
61
62 echo && echo "======> Status of xApps"
63 kubectl get pods -n ricxapp
64
65 POD_A1=$(kubectl get pods -n ricplt | grep Running | grep "\-a1mediator\-" | cut -f1 -d ' ')
66 echo
67 echo "To view the logs of the A1 midiator, run the following "
68 echo "command in a separate terminal window:"
69 echo "  kubectl logs -f -n ricplt $POD_A1"
70
71 echo "To view the logs of the Hello World xapp, run the following "
72 echo "command in a separate terminal window:"
73 echo "  kubectl logs -f -n ricxapp $POD_HW"
74
75 acknowledge "When ready, "
76
77
78
79 echo "====>  Pushing policy type to A1"
80 POLICY_TYPE_ID="1"
81 cat << EOF > hw-policy-type-${POLICY_TYPE_ID}.json
82 {
83   "name": "hwpolicy",
84   "description": "Hellow World policy type",
85   "policy_type_id": ${POLICY_TYPE_ID},
86   "create_schema": {
87     "\$schema": "http://json-schema.org/draft-07/schema#",
88     "title": "HW Policy",
89     "description": "HW policy type",
90     "type": "object",
91     "properties": {
92       "threshold": {
93         "type": "integer",
94         "default": 0
95       }
96     },
97     "additionalProperties": false
98   }
99 }
100 EOF
101 echo "A policy type definition file hw-policy-type-${POLICY_TYPE_ID}.json has been created."
102 echo && acknowledge "The next command will create a new policy type via A1 Mediator using this definition file.  Watch the logs of A1 Mediator."
103 curl -v -X PUT "http://$(hostname):32080/a1mediator/a1-p/policytypes/${POLICY_TYPE_ID}" \
104   -H "accept: application/json" -H "Content-Type: application/json" \
105   -d @./hw-policy-type-${POLICY_TYPE_ID}.json
106 # expect to see a 201
107 acknowledge "A 201 response indicates that the new policy type has been created."
108
109 echo && echo "======> Listing policy types"
110 curl -X GET --header "Content-Type: application/json" --header "accept: application/json" \
111   http://$(hostname):32080/a1mediator/a1-p/policytypes
112 acknowledge "Verify that the policy type ${POLICY_TYPE_ID} is in the listing."
113
114
115 echo && acknowledge "The next command will create a new policy instance of the newly created policy type.  Watch the logs of A1 Mediator and HW xApp."
116 POLICY_ID="hwpolicy321"
117 echo && echo "===> Deploy policy ID of ${POLICY_ID} of policy type ${POLICY_TYPE_ID}"
118 curl -v -X PUT --header "Content-Type: application/json" \
119   --data "{\"threshold\" : 9}" \
120   http://$(hostname):32080/a1mediator/a1-p/policytypes/${POLICY_TYPE_ID}/policies/${POLICY_ID}
121 acknowledge "A 202 response indicates that the new policy instance has been created.  In additoin, A1 Mediator and HW xApp log would indicate that the new policy instance has been distributed from A1 Mediator to the xApp."
122
123 # get policy instances
124 echo && echo "======> Listing policy instances of policy type ${POLICY_TYPE_ID}"
125 curl -X GET --header "Content-Type: application/json" --header "accept: application/json" \
126   http://$(hostname):32080/a1mediator/a1-p/policytypes/${POLICY_TYPE_ID}/policies
127 acknowledge "Verify that the new policy instance appears in the policy list"
128
129
130 echo && acknowledge "The above sequence has completed the Hello World xApp on-boarding, deployment, and policy distribution demonstration.  The remaining part of the script cleans up the HW xApp, policy type, and policy instance from the Near RT RIC"
131
132 echo && echo "===> Deleting policy instance ${POLICY_ID} of type ${POLICY_TYPE_ID}"
133 acknowledge "The next command will delete a policy instance.  Watch the logs of A1 Mediator andd HW xApp"
134 curl -X DELETE --header "Content-Type: application/json" --header "accept: application/json" \
135   http://$(hostname):32080/a1mediator/a1-p/policytypes/${POLICY_TYPE_ID}/policies/${POLICY_ID}
136 sleep 5
137
138 echo && echo "======> Listing policy instances"
139 curl -X GET --header "Content-Type: application/json" --header "accept: application/json" \
140   http://$(hostname):32080/a1mediator/a1-p/policytypes/${POLICY_TYPE_ID}/policies
141
142 echo && echo "===> Deleting policy type $POLICY_TYPE_ID"
143 acknowledge "The next command will delete a policy type.  Watch the logs of TS xApp"
144 curl -X DELETE -H "accept: application/json" -H "Content-Type: application/json" \
145   "http://$(hostname):32080/a1mediator/a1-p/policytypes/${POLICY_TYPE_ID}"
146 sleep 5
147
148 echo && echo "======> Listing policy types"
149 curl -X GET --header "Content-Type: application/json" --header "accept: application/json" \
150   http://$(hostname):32080/a1mediator/a1-p/policytypes
151
152
153 echo && echo "===> Undeploy the xApps"
154 acknowledge "The next command will delete the HW xApp."
155 curl -H "Content-Type: application/json" -X DELETE \
156   http://$(hostname):32080/appmgr/ric/v1/xapps/hwxapp
157 sleep 5
158
159 echo && echo "======> Listing xApps"
160 kubectl get pods -n ricxapp
161
162 echo
163 echo "That is all folks.  Thanks!"