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