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 | |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 17 | ================================================================================== |
| 18 | */ |
| 19 | package main |
| 20 | |
| 21 | import ( |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 22 | "encoding/json" |
| 23 | |
subhash kumar singh | 360ca5a | 2021-08-25 18:13:38 +0000 | [diff] [blame] | 24 | "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] | 25 | "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] | 26 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 27 | ) |
| 28 | |
| 29 | type HWApp struct { |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 30 | stats map[string]xapp.Counter |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 31 | } |
| 32 | |
subhash kumar singh | 83b7567 | 2021-06-01 19:38:57 +0530 | [diff] [blame] | 33 | var ( |
| 34 | A1_POLICY_QUERY = 20013 |
| 35 | POLICY_QUERY_PAYLOAD = "{\"policy_type_id\":20000}" |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 36 | reqId = int64(1) |
| 37 | seqId = int64(1) |
| 38 | funId = int64(1) |
| 39 | actionId = int64(1) |
| 40 | actionType = "report" |
| 41 | subsequestActioType = "continue" |
| 42 | timeToWait = "w10ms" |
| 43 | direction = int64(0) |
| 44 | procedureCode = int64(27) |
| 45 | typeOfMessage = int64(1) |
| 46 | subscriptionId = "" |
| 47 | hPort = int64(8080) |
| 48 | rPort = int64(4560) |
| 49 | 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] | 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 | |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 70 | func (e *HWApp) ConfigChangeHandler(f string) { |
| 71 | xapp.Logger.Info("Config file changed") |
| 72 | } |
| 73 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 74 | func (e *HWApp) getEnbList() ([]*xapp.RNIBNbIdentity, error) { |
| 75 | enbs, err := xapp.Rnib.GetListEnbIds() |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 76 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 77 | if err != nil { |
| 78 | xapp.Logger.Error("err: %s", err) |
| 79 | return nil, err |
| 80 | } |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 81 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 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 |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | func (e *HWApp) getGnbList() ([]*xapp.RNIBNbIdentity, error) { |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 90 | gnbs, err := xapp.Rnib.GetListGnbIds() |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 91 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 92 | if err != nil { |
| 93 | xapp.Logger.Error("err: %s", err) |
| 94 | return nil, err |
| 95 | } |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 96 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 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 |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 102 | } |
| 103 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 104 | func (e *HWApp) getnbList() []*xapp.RNIBNbIdentity { |
| 105 | nbs := []*xapp.RNIBNbIdentity{} |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 106 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 107 | if enbs, err := e.getEnbList(); err == nil { |
| 108 | nbs = append(nbs, enbs...) |
| 109 | } |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 110 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 111 | if gnbs, err := e.getGnbList(); err == nil { |
| 112 | nbs = append(nbs, gnbs...) |
| 113 | } |
| 114 | return nbs |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 115 | } |
| 116 | |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 117 | func (e *HWApp) sendSubscription(meid string) { |
| 118 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 119 | xapp.Logger.Info("sending subscription request for meid : %s", meid) |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 120 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 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 | } |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 148 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 149 | b, err := json.MarshalIndent(subscriptionParams, "", " ") |
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 | if err != nil { |
| 152 | xapp.Logger.Error("Json marshaling failed : %s", err) |
| 153 | return |
| 154 | } |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 155 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 156 | xapp.Logger.Info("*****body: %s ", string(b)) |
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 | resp, err := xapp.Subscription.Subscribe(&subscriptionParams) |
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 | if err != nil { |
| 161 | xapp.Logger.Error("subscription failed (%s) with error: %s", meid, err) |
subhash kumar singh | 360ca5a | 2021-08-25 18:13:38 +0000 | [diff] [blame] | 162 | |
| 163 | // subscription failed, raise alarm |
| 164 | err := xapp.Alarm.Raise(8086, alarm.SeverityCritical, meid, "subscriptionFailed") |
| 165 | if err != nil { |
| 166 | xapp.Logger.Error("Raising alarm failed with error %v", err) |
| 167 | } |
| 168 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 169 | return |
| 170 | } |
| 171 | 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] | 172 | } |
| 173 | |
subhash kumar singh | 02ac560 | 2021-06-01 18:31:07 +0530 | [diff] [blame] | 174 | func (e *HWApp) xAppStartCB(d interface{}) { |
| 175 | xapp.Logger.Info("xApp ready call back received") |
subhash kumar singh | 3c8e1c5 | 2021-08-11 00:57:39 +0530 | [diff] [blame] | 176 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 177 | // get the list of all NBs |
| 178 | nbList := e.getnbList() |
subhash kumar singh | e51b324 | 2021-08-19 19:45:25 +0000 | [diff] [blame] | 179 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 180 | // send subscription request to each of the NBs |
| 181 | for _, nb := range nbList { |
| 182 | e.sendSubscription(nb.InventoryName) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func (e *HWApp) handleRICIndication(ranName string, r *xapp.RMRParams) { |
| 187 | // update metrics for indication message |
| 188 | e.stats["RICIndicationRx"].Inc() |
subhash kumar singh | 02ac560 | 2021-06-01 18:31:07 +0530 | [diff] [blame] | 189 | } |
| 190 | |
subhash kumar singh | 2ba8bc2 | 2021-06-01 19:22:39 +0530 | [diff] [blame] | 191 | func (e *HWApp) Consume(msg *xapp.RMRParams) (err error) { |
| 192 | id := xapp.Rmr.GetRicMessageName(msg.Mtype) |
| 193 | |
| 194 | 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) |
| 195 | |
| 196 | switch id { |
| 197 | // policy request handler |
| 198 | case "A1_POLICY_REQUEST": |
| 199 | xapp.Logger.Info("Recived policy instance list") |
| 200 | |
| 201 | // health check request |
| 202 | case "RIC_HEALTH_CHECK_REQ": |
| 203 | xapp.Logger.Info("Received health check request") |
| 204 | |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 205 | // RIC INDICATION message |
| 206 | case "RIC_INDICATION": |
| 207 | xapp.Logger.Info("Received RIC Indication message") |
| 208 | e.handleRICIndication(msg.Meid.RanName, msg) |
| 209 | |
subhash kumar singh | 2ba8bc2 | 2021-06-01 19:22:39 +0530 | [diff] [blame] | 210 | default: |
| 211 | xapp.Logger.Info("Unknown message type '%d', discarding", msg.Mtype) |
| 212 | } |
| 213 | |
| 214 | defer func() { |
| 215 | xapp.Rmr.Free(msg.Mbuf) |
| 216 | msg.Mbuf = nil |
| 217 | }() |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 218 | return |
| 219 | } |
| 220 | |
| 221 | func (e *HWApp) Run() { |
| 222 | |
| 223 | // set MDC |
| 224 | xapp.Logger.SetMdc("HWApp", "0.0.1") |
| 225 | |
| 226 | // set config change listener |
| 227 | xapp.AddConfigChangeListener(e.ConfigChangeHandler) |
| 228 | |
subhash kumar singh | 02ac560 | 2021-06-01 18:31:07 +0530 | [diff] [blame] | 229 | // register callback after xapp ready |
| 230 | xapp.SetReadyCB(e.xAppStartCB, true) |
| 231 | |
subhash kumar singh | eb2fc4f | 2021-06-01 18:43:32 +0530 | [diff] [blame] | 232 | // reading configuration from config file |
| 233 | waitForSdl := xapp.Config.GetBool("db.waitForSdl") |
| 234 | |
| 235 | // start xapp |
| 236 | xapp.RunWithParams(e, waitForSdl) |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 237 | |
| 238 | } |
| 239 | |
| 240 | func main() { |
subhash kumar singh | 9dd2cfb | 2021-08-25 11:43:30 +0000 | [diff] [blame] | 241 | // Defind metrics counter that the xapp provides |
| 242 | metrics := []xapp.CounterOpts{ |
| 243 | { |
| 244 | Name: "RICIndicationRx", |
| 245 | Help: "Total number of RIC Indication message received", |
| 246 | }, |
| 247 | } |
| 248 | |
| 249 | hw := HWApp{ |
| 250 | stats: xapp.Metric.RegisterCounterGroup(metrics, "hw-go"), // register counter |
| 251 | } |
subhash kumar singh | e63192f | 2020-12-11 14:25:11 +0530 | [diff] [blame] | 252 | hw.Run() |
| 253 | } |