subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 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 ( |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 24 | "encoding/json" |
| 25 | |
subhash kumar singh | 360ca5a | 2021-08-25 18:13:38 +0000 | [diff] [blame^] | 26 | "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm" |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 27 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel" |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 28 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 29 | ) |
| 30 | |
| 31 | type HWApp struct { |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 32 | stats map[string]xapp.Counter |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 33 | } |
| 34 | |
subhash kumar singh | 83b7567 | 2021-06-01 19:38:57 +0530 | [diff] [blame] | 35 | var ( |
| 36 | A1_POLICY_QUERY = 20013 |
| 37 | POLICY_QUERY_PAYLOAD = "{\"policy_type_id\":20000}" |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 38 | reqId = int64(1) |
| 39 | seqId = int64(1) |
| 40 | funId = int64(1) |
| 41 | actionId = int64(1) |
| 42 | actionType = "report" |
| 43 | subsequestActioType = "continue" |
| 44 | timeToWait = "w10ms" |
| 45 | direction = int64(0) |
| 46 | procedureCode = int64(27) |
| 47 | typeOfMessage = int64(1) |
| 48 | subscriptionId = "" |
| 49 | hPort = int64(8080) |
| 50 | rPort = int64(4560) |
| 51 | clientEndpoint = clientmodel.SubscriptionParamsClientEndpoint{Host: "service-ricxapp-hw-go-http.ricxapp", HTTPPort: &hPort, RMRPort: &rPort} |
subhash kumar singh | 83b7567 | 2021-06-01 19:38:57 +0530 | [diff] [blame] | 52 | ) |
| 53 | |
| 54 | func (e *HWApp) sendPolicyQuery() { |
| 55 | xapp.Logger.Info("Invoked method to send policy query message") |
| 56 | |
| 57 | // prepare and send policy query message over RMR |
| 58 | rmrParams := new(xapp.RMRParams) |
| 59 | rmrParams.Mtype = A1_POLICY_QUERY // A1_POLICY_QUERY |
| 60 | rmrParams.Payload = []byte(POLICY_QUERY_PAYLOAD) |
| 61 | |
| 62 | // send rmr message |
| 63 | flg := xapp.Rmr.SendMsg(rmrParams) |
| 64 | |
| 65 | if flg { |
| 66 | xapp.Logger.Info("Successfully sent policy query message over RMR") |
| 67 | } else { |
| 68 | xapp.Logger.Info("Failed to send policy query message over RMR") |
| 69 | } |
| 70 | } |
| 71 | |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 72 | func (e *HWApp) ConfigChangeHandler(f string) { |
| 73 | xapp.Logger.Info("Config file changed") |
| 74 | } |
| 75 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 76 | func (e *HWApp) getEnbList() ([]*xapp.RNIBNbIdentity, error) { |
| 77 | enbs, err := xapp.Rnib.GetListEnbIds() |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 78 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 79 | if err != nil { |
| 80 | xapp.Logger.Error("err: %s", err) |
| 81 | return nil, err |
| 82 | } |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 83 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 84 | xapp.Logger.Info("List for connected eNBs :") |
| 85 | for index, enb := range enbs { |
| 86 | xapp.Logger.Info("%d. enbid: %s", index+1, enb.InventoryName) |
| 87 | } |
| 88 | return enbs, nil |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | func (e *HWApp) getGnbList() ([]*xapp.RNIBNbIdentity, error) { |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 92 | gnbs, err := xapp.Rnib.GetListGnbIds() |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 93 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 94 | if err != nil { |
| 95 | xapp.Logger.Error("err: %s", err) |
| 96 | return nil, err |
| 97 | } |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 98 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 99 | xapp.Logger.Info("List of connected gNBs :") |
| 100 | for index, gnb := range gnbs { |
| 101 | xapp.Logger.Info("%d. gnbid : %s", index+1, gnb.InventoryName) |
| 102 | } |
| 103 | return gnbs, nil |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 104 | } |
| 105 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 106 | func (e *HWApp) getnbList() []*xapp.RNIBNbIdentity { |
| 107 | nbs := []*xapp.RNIBNbIdentity{} |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 108 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 109 | if enbs, err := e.getEnbList(); err == nil { |
| 110 | nbs = append(nbs, enbs...) |
| 111 | } |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 112 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 113 | if gnbs, err := e.getGnbList(); err == nil { |
| 114 | nbs = append(nbs, gnbs...) |
| 115 | } |
| 116 | return nbs |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 117 | } |
| 118 | |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 119 | func (e *HWApp) sendSubscription(meid string) { |
| 120 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 121 | xapp.Logger.Info("sending subscription request for meid : %s", meid) |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 122 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 123 | subscriptionParams := clientmodel.SubscriptionParams{ |
| 124 | ClientEndpoint: &clientEndpoint, |
| 125 | Meid: &meid, |
| 126 | RANFunctionID: &funId, |
| 127 | SubscriptionDetails: clientmodel.SubscriptionDetailsList{ |
| 128 | &clientmodel.SubscriptionDetail{ |
| 129 | RequestorID: &reqId, |
| 130 | InstanceID: &seqId, |
| 131 | EventTriggers: &clientmodel.EventTriggerDefinition{ |
| 132 | OctetString: "1234", |
| 133 | }, |
| 134 | ActionToBeSetupList: clientmodel.ActionsToBeSetup{ |
| 135 | &clientmodel.ActionToBeSetup{ |
| 136 | ActionDefinition: &clientmodel.ActionDefinition{ |
| 137 | OctetString: "5678", |
| 138 | }, |
| 139 | ActionID: &actionId, |
| 140 | ActionType: &actionType, |
| 141 | SubsequentAction: &clientmodel.SubsequentAction{ |
| 142 | SubsequentActionType: &subsequestActioType, |
| 143 | TimeToWait: &timeToWait, |
| 144 | }, |
| 145 | }, |
| 146 | }, |
| 147 | }, |
| 148 | }, |
| 149 | } |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 150 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 151 | b, err := json.MarshalIndent(subscriptionParams, "", " ") |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 152 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 153 | if err != nil { |
| 154 | xapp.Logger.Error("Json marshaling failed : %s", err) |
| 155 | return |
| 156 | } |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 157 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 158 | xapp.Logger.Info("*****body: %s ", string(b)) |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 159 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 160 | resp, err := xapp.Subscription.Subscribe(&subscriptionParams) |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 161 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 162 | if err != nil { |
| 163 | xapp.Logger.Error("subscription failed (%s) with error: %s", meid, err) |
subhash kumar singh | 360ca5a | 2021-08-25 18:13:38 +0000 | [diff] [blame^] | 164 | |
| 165 | // subscription failed, raise alarm |
| 166 | err := xapp.Alarm.Raise(8086, alarm.SeverityCritical, meid, "subscriptionFailed") |
| 167 | if err != nil { |
| 168 | xapp.Logger.Error("Raising alarm failed with error %v", err) |
| 169 | } |
| 170 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 171 | return |
| 172 | } |
| 173 | xapp.Logger.Info("Successfully subcription done (%s), subscription id : %s", meid, *resp.SubscriptionID) |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 174 | } |
| 175 | |
subhash kumar singh | 02ac560 | 2021-06-01 18:31:07 +0530 | [diff] [blame] | 176 | func (e *HWApp) xAppStartCB(d interface{}) { |
| 177 | xapp.Logger.Info("xApp ready call back received") |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 178 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 179 | // get the list of all NBs |
| 180 | nbList := e.getnbList() |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 181 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 182 | // send subscription request to each of the NBs |
| 183 | for _, nb := range nbList { |
| 184 | e.sendSubscription(nb.InventoryName) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func (e *HWApp) handleRICIndication(ranName string, r *xapp.RMRParams) { |
| 189 | // update metrics for indication message |
| 190 | e.stats["RICIndicationRx"].Inc() |
subhash kumar singh | 02ac560 | 2021-06-01 18:31:07 +0530 | [diff] [blame] | 191 | } |
| 192 | |
subhash kumar singh | 2ba8bc2 | 2021-06-01 19:22:39 +0530 | [diff] [blame] | 193 | func (e *HWApp) Consume(msg *xapp.RMRParams) (err error) { |
| 194 | id := xapp.Rmr.GetRicMessageName(msg.Mtype) |
| 195 | |
| 196 | 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) |
| 197 | |
| 198 | switch id { |
| 199 | // policy request handler |
| 200 | case "A1_POLICY_REQUEST": |
| 201 | xapp.Logger.Info("Recived policy instance list") |
| 202 | |
| 203 | // health check request |
| 204 | case "RIC_HEALTH_CHECK_REQ": |
| 205 | xapp.Logger.Info("Received health check request") |
| 206 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 207 | // RIC INDICATION message |
| 208 | case "RIC_INDICATION": |
| 209 | xapp.Logger.Info("Received RIC Indication message") |
| 210 | e.handleRICIndication(msg.Meid.RanName, msg) |
| 211 | |
subhash kumar singh | 2ba8bc2 | 2021-06-01 19:22:39 +0530 | [diff] [blame] | 212 | default: |
| 213 | xapp.Logger.Info("Unknown message type '%d', discarding", msg.Mtype) |
| 214 | } |
| 215 | |
| 216 | defer func() { |
| 217 | xapp.Rmr.Free(msg.Mbuf) |
| 218 | msg.Mbuf = nil |
| 219 | }() |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 220 | return |
| 221 | } |
| 222 | |
| 223 | func (e *HWApp) Run() { |
| 224 | |
| 225 | // set MDC |
| 226 | xapp.Logger.SetMdc("HWApp", "0.0.1") |
| 227 | |
| 228 | // set config change listener |
| 229 | xapp.AddConfigChangeListener(e.ConfigChangeHandler) |
| 230 | |
subhash kumar singh | 02ac560 | 2021-06-01 18:31:07 +0530 | [diff] [blame] | 231 | // register callback after xapp ready |
| 232 | xapp.SetReadyCB(e.xAppStartCB, true) |
| 233 | |
subhash kumar singh | eb2fc4f | 2021-06-01 18:43:32 +0530 | [diff] [blame] | 234 | // reading configuration from config file |
| 235 | waitForSdl := xapp.Config.GetBool("db.waitForSdl") |
| 236 | |
| 237 | // start xapp |
| 238 | xapp.RunWithParams(e, waitForSdl) |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 239 | |
| 240 | } |
| 241 | |
| 242 | func main() { |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 243 | // Defind metrics counter that the xapp provides |
| 244 | metrics := []xapp.CounterOpts{ |
| 245 | { |
| 246 | Name: "RICIndicationRx", |
| 247 | Help: "Total number of RIC Indication message received", |
| 248 | }, |
| 249 | } |
| 250 | |
| 251 | hw := HWApp{ |
| 252 | stats: xapp.Metric.RegisterCounterGroup(metrics, "hw-go"), // register counter |
| 253 | } |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 254 | hw.Run() |
| 255 | } |