rajalakshmisv | 21b61dd | 2021-12-07 04:53:03 +0000 | [diff] [blame^] | 1 | package control
|
| 2 |
|
| 3 | import (
|
| 4 | "errors"
|
| 5 | "fmt"
|
| 6 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
|
| 7 | "gerrit.o-ran-sc.org/r/scp/ric-app/protocol/grpc/ricmsgcommrpc/rc"
|
| 8 | "log"
|
| 9 | "os"
|
| 10 | "strconv"
|
| 11 | "sync"
|
| 12 | "time"
|
| 13 | )
|
| 14 |
|
| 15 | var (
|
| 16 | gControlData *Control
|
| 17 | gChan_RicControlReq_handle = make(chan *rc.RicControlGrpcReq, 2000) //Make it configurable
|
| 18 | )
|
| 19 |
|
| 20 | func NewControl() Control {
|
| 21 |
|
| 22 | file := "/opt/rc.log"
|
| 23 | logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
|
| 24 | if err != nil {
|
| 25 | panic(err)
|
| 26 | }
|
| 27 | log.SetOutput(logFile)
|
| 28 | log.SetPrefix("[qSkipTool]")
|
| 29 | log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC)
|
| 30 | logLevel := xapp.Config.GetInt("controls.logLevel")
|
| 31 | xapp.Logger.SetLevel(logLevel)
|
| 32 |
|
| 33 | xapp.Logger.Debug("GRPC Server Port = %v ", xapp.Config.GetString("controls.ricHOControlgRpcServerPort"))
|
| 34 | xapp.Logger.Debug("Log Level = %d ", xapp.Config.GetInt("controls.logLevel"))
|
| 35 |
|
| 36 | return Control{5,
|
| 37 | make(chan *xapp.RMRParams, 1000), //Make it configurable
|
| 38 | make(map[int]bool),
|
| 39 | &sync.Mutex{},
|
| 40 | 0}
|
| 41 | }
|
| 42 |
|
| 43 | func ReadyCB(i interface{}) {
|
| 44 | gControlData = i.(*Control)
|
| 45 |
|
| 46 | go controlLoop()
|
| 47 |
|
| 48 | //Start gRPC Server for Receiving messages
|
| 49 |
|
| 50 | go StartgRPCRCControlCommServerRoutine()
|
| 51 | xapp.Logger.Info("StartgRPCRCControlCommServerRoutine done")
|
| 52 |
|
| 53 | //To Handle RIC Control Message
|
| 54 | go StartHandleControlReqRoutine()
|
| 55 |
|
| 56 | }
|
| 57 |
|
| 58 | func (aControlData *Control) Run() {
|
| 59 | xapp.SetReadyCB(ReadyCB, aControlData)
|
| 60 | xapp.Run(aControlData)
|
| 61 | }
|
| 62 |
|
| 63 | func (aControlData *Control) Consume(rp *xapp.RMRParams) (err error) {
|
| 64 | gControlData.rcChan <- rp
|
| 65 | return
|
| 66 | }
|
| 67 |
|
| 68 | func (aControlData *Control) rmrSend(params *xapp.RMRParams) (err error) {
|
| 69 | if !xapp.Rmr.Send(params, false) {
|
| 70 | err = errors.New("rmr.Send() failed")
|
| 71 | xapp.Logger.Error("Failed to rmrSend to %v", err)
|
| 72 | log.Printf("Failed to rmrSend to %v", err)
|
| 73 | }
|
| 74 | return
|
| 75 | }
|
| 76 |
|
| 77 | func (aControlData *Control) rmrReplyToSender(params *xapp.RMRParams) (err error) {
|
| 78 | if !xapp.Rmr.Send(params, true) {
|
| 79 | err = errors.New("rmr.Send() failed")
|
| 80 | xapp.Logger.Error("Failed to rmrReplyToSender to %v", err)
|
| 81 | log.Printf("Failed to rmrReplyToSender to %v", err)
|
| 82 | }
|
| 83 | return
|
| 84 | }
|
| 85 |
|
| 86 | func controlLoop() {
|
| 87 | for {
|
| 88 | msg := <-gControlData.rcChan
|
| 89 | xapp.Logger.Debug("Received message type: %d", msg.Mtype)
|
| 90 | log.Printf("Received message type: %d", msg.Mtype)
|
| 91 | switch msg.Mtype {
|
| 92 | case 12041:
|
| 93 | go HandleControlResponse(msg)
|
| 94 | case 12042:
|
| 95 | go HandleControlFailure(msg)
|
| 96 | default:
|
| 97 | err := errors.New("Message Type " + strconv.Itoa(msg.Mtype) + " is discarded")
|
| 98 | xapp.Logger.Error("Unknown message type: %v", err)
|
| 99 | log.Printf("Unknown message type: %v", err)
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | func StartHandleControlReqRoutine() {
|
| 105 |
|
| 106 | log.Printf("Starting Go Routine for Handling GRPC RIC Control msg ")
|
| 107 | xapp.Logger.Info("Starting Go Routine for Handling GRPC RIC Control msg ")
|
| 108 | for {
|
| 109 | _, ok := <-gChan_RicControlReq_handle
|
| 110 | if ok == false {
|
| 111 | xapp.Logger.Error("Invalid grpc request received, dont process further")
|
| 112 | break
|
| 113 | }
|
| 114 | HandlegRPCRICControlMsgReq(<-gChan_RicControlReq_handle)
|
| 115 | }
|
| 116 | xapp.Logger.Debug("StartHandleControlReqRoutine Done")
|
| 117 | }
|
| 118 |
|
| 119 | func HandlegRPCRICControlMsgReq(aPtrRicControlGrpcReq *rc.RicControlGrpcReq) {
|
| 120 |
|
| 121 | lRicHoControlMsg := RicHoControlMsg{}
|
| 122 | lRicHoControlMsg.RicControlGrpcReqPtr = aPtrRicControlGrpcReq
|
| 123 |
|
| 124 | lUEID := lRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID
|
| 125 | xapp.Logger.Debug("HandlegRPCRICControlMsgReq UEID = %s ", lUEID)
|
| 126 | //Mandatory parameters validation
|
| 127 | if lRicHoControlMsg.RicControlGrpcReqPtr.E2NodeID == "" ||
|
| 128 | lRicHoControlMsg.RicControlGrpcReqPtr.RICControlMessageData.TargetCellID == "" ||
|
| 129 | lRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID == "" ||
|
| 130 | lRicHoControlMsg.RicControlGrpcReqPtr.PlmnID == "" ||
|
| 131 | lRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.ControlActionId < 0 ||
|
| 132 | lRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.ControlStyle < 0 ||
|
| 133 | lRicHoControlMsg.RicControlGrpcReqPtr.RICControlMessageData.RICControlCellTypeVal < 0 ||
|
| 134 | lRicHoControlMsg.RicControlGrpcReqPtr.RICE2APHeaderData.RICRequestorID < 0 ||
|
| 135 | lRicHoControlMsg.RicControlGrpcReqPtr.RICE2APHeaderData.RanFuncId < 0 {
|
| 136 | xapp.Logger.Error("Mandaroty parameters missing, dont send control request ")
|
| 137 | return
|
| 138 | }
|
| 139 |
|
| 140 | lRicHoControlMsg.GetSequenceNumber()
|
| 141 |
|
| 142 | go lRicHoControlMsg.SendRicControlRequest(lRicHoControlMsg.GetSequenceNumber())
|
| 143 |
|
| 144 | return
|
| 145 | }
|
| 146 |
|
| 147 | func (aRicHoControlMsg *RicHoControlMsg) GetSequenceNumber() int {
|
| 148 |
|
| 149 | //Incrementing the RIC Requestor Instance Id to make the request unique and traceable.
|
| 150 | gControlData.eventRicControlReqExpiredMu.Lock()
|
| 151 | gControlData.ricRequestInstanceID = gControlData.ricRequestInstanceID + 1
|
| 152 | gControlData.eventRicControlReqExpiredMu.Unlock()
|
| 153 |
|
| 154 | return gControlData.ricRequestInstanceID
|
| 155 | }
|
| 156 |
|
| 157 | func (aRicHoControlMsg *RicHoControlMsg) setEventRicControlCreateExpiredTimer(aSeqNum int) {
|
| 158 |
|
| 159 | gControlData.eventRicControlReqExpiredMu.Lock()
|
| 160 | gControlData.eventRicControlReqExpiredMap[aSeqNum] = false
|
| 161 | gControlData.eventRicControlReqExpiredMu.Unlock()
|
| 162 |
|
| 163 | timer := time.NewTimer(time.Duration(gControlData.eventRicControlReqTimePeriod) * time.Second)
|
| 164 | go func(t *time.Timer) {
|
| 165 | defer t.Stop()
|
| 166 | xapp.Logger.Debug("RIC_CONTROL_REQ[%s]: Waiting for RIC_CONTROL_RESP...", aSeqNum)
|
| 167 | log.Printf("RIC_CONTROL_REQ[%s]: Waiting for RIC_CONTROL_RESP...", aSeqNum)
|
| 168 | for {
|
| 169 | select {
|
| 170 | case <-t.C:
|
| 171 | gControlData.eventRicControlReqExpiredMu.Lock()
|
| 172 | isResponsed := gControlData.eventRicControlReqExpiredMap[aSeqNum]
|
| 173 | delete(gControlData.eventRicControlReqExpiredMap, aSeqNum)
|
| 174 | gControlData.eventRicControlReqExpiredMu.Unlock()
|
| 175 | if !isResponsed {
|
| 176 | xapp.Logger.Debug("RIC_CONTROL_REQ[%s]: RIC Event Create Timer experied!", aSeqNum)
|
| 177 | log.Printf("RIC_CONTROL_REQ[%s]: RIC Event Create Timer experied!", aSeqNum)
|
| 178 | //Send ErrorIndication message on Timeout
|
| 179 | return
|
| 180 | }
|
| 181 | default:
|
| 182 | gControlData.eventRicControlReqExpiredMu.Lock()
|
| 183 | flag := gControlData.eventRicControlReqExpiredMap[aSeqNum]
|
| 184 | if flag {
|
| 185 | delete(gControlData.eventRicControlReqExpiredMap, aSeqNum)
|
| 186 | gControlData.eventRicControlReqExpiredMu.Unlock()
|
| 187 | xapp.Logger.Debug("RIC_CONTROL_REQ[%s]: RIC Event Create Timer canceled!", aSeqNum)
|
| 188 | log.Printf("RIC_CONTROL_REQ[%s]: RIC Event Create Timer canceled!", aSeqNum)
|
| 189 | return
|
| 190 | } else {
|
| 191 | gControlData.eventRicControlReqExpiredMu.Unlock()
|
| 192 | }
|
| 193 | }
|
| 194 | time.Sleep(100 * time.Millisecond)
|
| 195 | }
|
| 196 | }(timer)
|
| 197 | }
|
| 198 | func (aRicHoControlMsg *RicHoControlMsg) SendRicControlRequest(aRequestSN int) (err error) {
|
| 199 | var e2ap *E2ap
|
| 200 | var e2sm *E2sm
|
| 201 |
|
| 202 | xapp.Logger.Info("SendRicControlRequest Enter for RanName = %s", aRicHoControlMsg.RicControlGrpcReqPtr.RanName)
|
| 203 |
|
| 204 | if aRicHoControlMsg.RicControlGrpcReqPtr == nil {
|
| 205 | return err
|
| 206 | }
|
| 207 |
|
| 208 | var lRicControlStyleType int64 = aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.ControlStyle
|
| 209 | var lRicControlActionID int64 = aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.ControlActionId
|
| 210 | lUEID := aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID
|
| 211 |
|
| 212 | lUeIdBuf := []byte(aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
|
| 213 |
|
| 214 | xapp.Logger.Debug("UEID:%s, lUeIdBuf: %v", aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID, lUeIdBuf)
|
| 215 |
|
| 216 | var lRicControlHeader []byte = make([]byte, 256) //Check the Size
|
| 217 | lRicControlHeaderEncoded, err := e2sm.SetRicControlHeader(lRicControlHeader, lUeIdBuf, lRicControlStyleType, lRicControlActionID)
|
| 218 | if err != nil {
|
| 219 | xapp.Logger.Error("SetRicControlHeader Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
|
| 220 | log.Printf("SetRicControlHeader Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
|
| 221 | return err
|
| 222 | } else {
|
| 223 | xapp.Logger.Info("SetRicControlHeader is success: %x", lRicControlHeaderEncoded)
|
| 224 | fmt.Fprintf(os.Stderr, "Encoded RIC Control Header PDU:\n")
|
| 225 | for i := 0; i < len(lRicControlHeaderEncoded); i++ {
|
| 226 | fmt.Fprintf(os.Stderr, " %02x", lRicControlHeaderEncoded[i])
|
| 227 | }
|
| 228 | fmt.Fprintf(os.Stderr, "\n")
|
| 229 | }
|
| 230 |
|
| 231 | var lTargetPrimaryCell int64 = RIC_CONTROL_TARGET_PRIMARY_CELL
|
| 232 | var lTargetCell int64 = RIC_CONTROL_TARGET_CELL
|
| 233 | var lNrCGIOrECGI int64 = RIC_CONTROL_CGI_TYPE
|
| 234 |
|
| 235 | lNrOrEUtraCellType := aRicHoControlMsg.RicControlGrpcReqPtr.RICControlMessageData.RICControlCellTypeVal
|
| 236 | lTargetCellVal := aRicHoControlMsg.RicControlGrpcReqPtr.RICControlMessageData.TargetCellID
|
| 237 | lTargetCellValBuf := []byte(lTargetCellVal)
|
| 238 |
|
| 239 | var lRicControlMessage []byte = make([]byte, 1024)
|
| 240 | lRicControlMessageEncoded, err := e2sm.SetRicControlMessage(lRicControlMessage, lTargetPrimaryCell, lTargetCell, lNrCGIOrECGI, int64(lNrOrEUtraCellType), lTargetCellValBuf)
|
| 241 | if err != nil {
|
| 242 | xapp.Logger.Error("SetRicControlMessage Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
|
| 243 | log.Printf("SetRicControlMessage Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
|
| 244 | return err
|
| 245 | } else {
|
| 246 | xapp.Logger.Debug("SetRicControlMessage is success: %x", lRicControlMessageEncoded)
|
| 247 | fmt.Fprintf(os.Stderr, "Encoded RIC Control Message PDU:\n")
|
| 248 | for i := 0; i < len(lRicControlMessageEncoded); i++ {
|
| 249 | fmt.Fprintf(os.Stderr, " %02x", lRicControlMessageEncoded[i])
|
| 250 | }
|
| 251 | fmt.Fprintf(os.Stderr, "\n")
|
| 252 | }
|
| 253 |
|
| 254 | lParams := &xapp.RMRParams{}
|
| 255 | lParams.Mtype = 12040 //RIC_CONTROL_REQ
|
| 256 | lParams.SubId = -1
|
| 257 |
|
| 258 | var lRequestorId uint16 = uint16(aRicHoControlMsg.RicControlGrpcReqPtr.RICE2APHeaderData.RICRequestorID)
|
| 259 | var lFuncId uint16 = uint16(aRicHoControlMsg.RicControlGrpcReqPtr.RICE2APHeaderData.RanFuncId)
|
| 260 |
|
| 261 | lParams.Payload = make([]byte, 2048)
|
| 262 | lParams.Payload, err = e2ap.SetRicControlRequestPayload(lParams.Payload, lRequestorId, uint16(aRequestSN), lFuncId,
|
| 263 | lRicControlHeaderEncoded, lRicControlMessageEncoded)
|
| 264 | if err != nil {
|
| 265 | xapp.Logger.Error("SetRicControlRequestPayload Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
|
| 266 | log.Printf("SetRicControlRequestPayload Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
|
| 267 | return err
|
| 268 | } else {
|
| 269 | xapp.Logger.Debug("Encoding RicControlRequestPayload is success. UEID: %s, Payload: %x", lUEID, lParams.Payload)
|
| 270 | fmt.Fprintf(os.Stderr, "Encoded RIC Control Req PDU:\n")
|
| 271 | for i := 0; i < len(lParams.Payload); i++ {
|
| 272 | fmt.Fprintf(os.Stderr, " %02x", lParams.Payload[i])
|
| 273 | }
|
| 274 | fmt.Fprintf(os.Stderr, "\n")
|
| 275 | }
|
| 276 |
|
| 277 | valEnbId := aRicHoControlMsg.RicControlGrpcReqPtr.E2NodeID
|
| 278 | valRanName := aRicHoControlMsg.RicControlGrpcReqPtr.RanName
|
| 279 | valPlmnId := aRicHoControlMsg.RicControlGrpcReqPtr.PlmnID
|
| 280 | lParams.Meid = &xapp.RMRMeid{PlmnID: valPlmnId, EnbID: valEnbId, RanName: valRanName}
|
| 281 |
|
| 282 | xapp.Logger.Debug("The RIC Control RMR message to be sent is with MsgType:%d SubId=%d, lParams.Meid: %v, UEID: %s", lParams.Mtype, lParams.SubId, lParams.Meid, lUEID)
|
| 283 |
|
| 284 | xapp.Logger.Debug("Sending RIC Control message to RanName: %s, UEID: %s ", aRicHoControlMsg.RicControlGrpcReqPtr.RanName, lUEID)
|
| 285 |
|
| 286 | err = gControlData.rmrSend(lParams)
|
| 287 | if err != nil {
|
| 288 | xapp.Logger.Error("Failed to send RIC_CONTROL_REQ: %v", err)
|
| 289 | log.Printf("Failed to send RIC_CONTROL_REQ: %v", err)
|
| 290 | return err
|
| 291 | }
|
| 292 |
|
| 293 | xapp.Logger.Info("Sending RIC Control message to RanName: %s, UEID: %s Success", aRicHoControlMsg.RicControlGrpcReqPtr.RanName, lUEID)
|
| 294 |
|
| 295 | aRicHoControlMsg.setEventRicControlCreateExpiredTimer(aRequestSN) //TODO check if this is required as we are not expecting Control ACK
|
| 296 |
|
| 297 | return nil
|
| 298 | }
|
| 299 |
|
| 300 | func HandleControlResponse(params *xapp.RMRParams) (err error) {
|
| 301 | xapp.Logger.Debug("The SubId in RIC_CONTROL_RESP is %d", params.SubId)
|
| 302 | log.Printf("The SubId in RIC_CONTROL_RESP is %d", params.SubId)
|
| 303 |
|
| 304 | return nil
|
| 305 | }
|
| 306 |
|
| 307 | func HandleControlFailure(params *xapp.RMRParams) (err error) {
|
| 308 |
|
| 309 | xapp.Logger.Debug("The SubId in RIC_CONTROL_FAILURE is %d", params.SubId)
|
| 310 | log.Printf("The SubId in RIC_CONTROL_FAILURE is %d", params.SubId)
|
| 311 |
|
| 312 | return nil
|
| 313 | }
|