Implement subscription operatoin
[ric-app/hw-go.git] / hwApp.go
1 /*
2 ==================================================================================
3   Copyright (c) 2020 Samsung
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    This source code is part of the near-RT RIC (RAN Intelligent Controller)
18    platform project (RICP).
19 ==================================================================================
20 */
21 package main
22
23 import (
24        "encoding/json"
25        "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel"
26         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
27 )
28
29 type HWApp struct {
30 }
31
32 var (
33         A1_POLICY_QUERY      = 20013
34         POLICY_QUERY_PAYLOAD = "{\"policy_type_id\":20000}"
35         reqId               = int64(1)
36         seqId               = int64(1)
37         funId               = int64(1)
38         actionId            = int64(1)
39         actionType          = "report"
40         subsequestActioType = "continue"
41         timeToWait          = "w10ms"
42         direction           = int64(0)
43         procedureCode       = int64(27)
44         typeOfMessage       = int64(1)
45         subscriptionId      = ""
46         hPort               = int64(8080)
47         rPort               = int64(4560)
48         clientEndpoint      = clientmodel.SubscriptionParamsClientEndpoint{Host: "service-ricxapp-hw-go-http.ricxapp", HTTPPort: &hPort, RMRPort: &rPort}
49
50 )
51
52 func (e *HWApp) sendPolicyQuery() {
53         xapp.Logger.Info("Invoked method to send  policy query message")
54
55         // prepare and send policy query message over RMR
56         rmrParams := new(xapp.RMRParams)
57         rmrParams.Mtype = A1_POLICY_QUERY // A1_POLICY_QUERY
58         rmrParams.Payload = []byte(POLICY_QUERY_PAYLOAD)
59
60         // send rmr message
61         flg := xapp.Rmr.SendMsg(rmrParams)
62
63         if flg {
64                 xapp.Logger.Info("Successfully sent policy query message over RMR")
65         } else {
66                 xapp.Logger.Info("Failed to send policy query message over RMR")
67         }
68 }
69
70 func (e *HWApp) ConfigChangeHandler(f string) {
71         xapp.Logger.Info("Config file changed")
72 }
73
74 func (e *HWApp) getEnbList() ([]*xapp.RNIBNbIdentity, error){
75        enbs, err := xapp.Rnib.GetListEnbIds()
76
77        if err != nil {
78                xapp.Logger.Error("err: %s", err)
79                return nil, err
80        }
81
82        xapp.Logger.Info("List for connected eNBs :")
83        for index, enb := range enbs {
84                xapp.Logger.Info("%d. enbid: %s", index+1, enb.InventoryName)
85        }
86        return enbs, nil
87 }
88
89 func (e *HWApp) getGnbList() ([]*xapp.RNIBNbIdentity, error) {
90        gnbs, err := xapp.Rnib.GetListGnbIds()
91
92        if err != nil {
93                xapp.Logger.Error("err: %s", err)
94                return nil, err
95        }
96
97        xapp.Logger.Info("List of connected gNBs :")
98        for index, gnb := range gnbs {
99                xapp.Logger.Info("%d. gnbid : %s", index+1, gnb.InventoryName)
100        }
101        return gnbs, nil
102 }
103
104 func (e *HWApp) getnbList() ([]*xapp.RNIBNbIdentity) {
105        nbs := []*xapp.RNIBNbIdentity{}
106
107        if enbs , err := e.getEnbList(); err == nil {
108               nbs = append(nbs, enbs...)
109        }
110
111        if gnbs, err := e.getGnbList(); err == nil {
112               nbs = append(nbs, gnbs...)
113        }
114        return nbs
115 }
116
117 func (e *HWApp) sendSubscription(meid string) {
118
119        xapp.Logger.Info("sending subscription request for meid : %s", meid)
120
121        subscriptionParams := clientmodel.SubscriptionParams{
122                 ClientEndpoint: &clientEndpoint,
123                 Meid:           &meid,
124                 RANFunctionID:  &funId,
125                 SubscriptionDetails: clientmodel.SubscriptionDetailsList{
126                         &clientmodel.SubscriptionDetail{
127                                 RequestorID: &reqId,
128                                 InstanceID:  &seqId,
129                                 EventTriggers: &clientmodel.EventTriggerDefinition{
130                                         OctetString: "1234",
131                                 },
132                                 ActionToBeSetupList: clientmodel.ActionsToBeSetup{
133                                         &clientmodel.ActionToBeSetup{
134                                                 ActionDefinition: &clientmodel.ActionDefinition{
135                                                         OctetString: "5678",
136                                                 },
137                                                 ActionID:   &actionId,
138                                                 ActionType: &actionType,
139                                                 SubsequentAction: &clientmodel.SubsequentAction{
140                                                         SubsequentActionType: &subsequestActioType,
141                                                         TimeToWait:           &timeToWait,
142                                                 },
143                                         },
144                                 },
145                         },
146                 },
147         }
148
149         b, err := json.MarshalIndent(subscriptionParams, "", "  ")
150
151        if err != nil {
152                xapp.Logger.Error("Json marshaling failed : %s", err)
153                return
154        }
155
156         xapp.Logger.Info("*****body: %s ", string(b))
157
158        resp, err := xapp.Subscription.Subscribe(&subscriptionParams)
159
160        if err != nil {
161                xapp.Logger.Error("subscription failed (%s) with error: %s", meid, err)
162                return
163        }
164        xapp.Logger.Info("Successfully subcription done (%s), subscription id : %s", meid, *resp.SubscriptionID)
165 }
166
167 func (e *HWApp) xAppStartCB(d interface{}) {
168         xapp.Logger.Info("xApp ready call back received")
169
170        // get the list of all NBs
171        nbList := e.getnbList()
172
173        // send subscription request to each of the NBs
174        for _, nb := range nbList {
175        e.sendSubscription(nb.InventoryName)
176        }
177 }
178
179 func (e *HWApp) Consume(msg *xapp.RMRParams) (err error) {
180         id := xapp.Rmr.GetRicMessageName(msg.Mtype)
181
182         xapp.Logger.Info("Message received: name=%s meid=%s subId=%d txid=%s len=%d", id, msg.Meid.RanName, msg.SubId, msg.Xid, msg.PayloadLen)
183
184         switch id {
185         // policy request handler
186         case "A1_POLICY_REQUEST":
187                 xapp.Logger.Info("Recived policy instance list")
188
189         // health check request
190         case "RIC_HEALTH_CHECK_REQ":
191                 xapp.Logger.Info("Received health check request")
192
193         default:
194                 xapp.Logger.Info("Unknown message type '%d', discarding", msg.Mtype)
195         }
196
197         defer func() {
198                 xapp.Rmr.Free(msg.Mbuf)
199                 msg.Mbuf = nil
200         }()
201         return
202 }
203
204 func (e *HWApp) Run() {
205
206         // set MDC
207         xapp.Logger.SetMdc("HWApp", "0.0.1")
208
209         // set config change listener
210         xapp.AddConfigChangeListener(e.ConfigChangeHandler)
211
212         // register callback after xapp ready
213         xapp.SetReadyCB(e.xAppStartCB, true)
214
215         // reading configuration from config file
216         waitForSdl := xapp.Config.GetBool("db.waitForSdl")
217
218         // start xapp
219         xapp.RunWithParams(e, waitForSdl)
220
221 }
222
223 func main() {
224         hw := HWApp{}
225         hw.Run()
226 }