blob: 41cfc92e20fb867d5aae3b308312bd65efa5d663 [file] [log] [blame]
rajalakshmisvb8bc34d2022-01-03 06:14:50 +00001package control
rajalakshmisv21b61dd2021-12-07 04:53:03 +00002
3import (
4 "errors"
5 "fmt"
6 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
praneeth20211c42f732021-12-07 08:15:45 +00007 "gerrit.o-ran-sc.org/r/ric-app/rc/protocol/grpc/ricmsgcommrpc/rc"
rajalakshmisv21b61dd2021-12-07 04:53:03 +00008 "log"
9 "os"
10 "strconv"
11 "sync"
12 "time"
13)
14
15var (
16 gControlData *Control
17 gChan_RicControlReq_handle = make(chan *rc.RicControlGrpcReq, 2000) //Make it configurable
18)
19
20func 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
43func 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
58func (aControlData *Control) Run() {
59 xapp.SetReadyCB(ReadyCB, aControlData)
60 xapp.Run(aControlData)
61}
62
63func (aControlData *Control) Consume(rp *xapp.RMRParams) (err error) {
64 gControlData.rcChan <- rp
65 return
66}
67
68func (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
77func (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
86func 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
104func 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 {
rajalakshmisv21b61dd2021-12-07 04:53:03 +0000109 HandlegRPCRICControlMsgReq(<-gChan_RicControlReq_handle)
110 }
111 xapp.Logger.Debug("StartHandleControlReqRoutine Done")
112}
113
114func HandlegRPCRICControlMsgReq(aPtrRicControlGrpcReq *rc.RicControlGrpcReq) {
115
116 lRicHoControlMsg := RicHoControlMsg{}
117 lRicHoControlMsg.RicControlGrpcReqPtr = aPtrRicControlGrpcReq
118
119 lUEID := lRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID
120 xapp.Logger.Debug("HandlegRPCRICControlMsgReq UEID = %s ", lUEID)
121 //Mandatory parameters validation
122 if lRicHoControlMsg.RicControlGrpcReqPtr.E2NodeID == "" ||
123 lRicHoControlMsg.RicControlGrpcReqPtr.RICControlMessageData.TargetCellID == "" ||
124 lRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID == "" ||
125 lRicHoControlMsg.RicControlGrpcReqPtr.PlmnID == "" ||
126 lRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.ControlActionId < 0 ||
127 lRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.ControlStyle < 0 ||
128 lRicHoControlMsg.RicControlGrpcReqPtr.RICControlMessageData.RICControlCellTypeVal < 0 ||
129 lRicHoControlMsg.RicControlGrpcReqPtr.RICE2APHeaderData.RICRequestorID < 0 ||
130 lRicHoControlMsg.RicControlGrpcReqPtr.RICE2APHeaderData.RanFuncId < 0 {
131 xapp.Logger.Error("Mandaroty parameters missing, dont send control request ")
132 return
133 }
134
135 lRicHoControlMsg.GetSequenceNumber()
136
137 go lRicHoControlMsg.SendRicControlRequest(lRicHoControlMsg.GetSequenceNumber())
138
139 return
140}
141
142func (aRicHoControlMsg *RicHoControlMsg) GetSequenceNumber() int {
143
144 //Incrementing the RIC Requestor Instance Id to make the request unique and traceable.
145 gControlData.eventRicControlReqExpiredMu.Lock()
146 gControlData.ricRequestInstanceID = gControlData.ricRequestInstanceID + 1
147 gControlData.eventRicControlReqExpiredMu.Unlock()
148
149 return gControlData.ricRequestInstanceID
150}
151
152func (aRicHoControlMsg *RicHoControlMsg) setEventRicControlCreateExpiredTimer(aSeqNum int) {
153
154 gControlData.eventRicControlReqExpiredMu.Lock()
155 gControlData.eventRicControlReqExpiredMap[aSeqNum] = false
156 gControlData.eventRicControlReqExpiredMu.Unlock()
157
158 timer := time.NewTimer(time.Duration(gControlData.eventRicControlReqTimePeriod) * time.Second)
159 go func(t *time.Timer) {
160 defer t.Stop()
161 xapp.Logger.Debug("RIC_CONTROL_REQ[%s]: Waiting for RIC_CONTROL_RESP...", aSeqNum)
162 log.Printf("RIC_CONTROL_REQ[%s]: Waiting for RIC_CONTROL_RESP...", aSeqNum)
163 for {
164 select {
165 case <-t.C:
166 gControlData.eventRicControlReqExpiredMu.Lock()
167 isResponsed := gControlData.eventRicControlReqExpiredMap[aSeqNum]
168 delete(gControlData.eventRicControlReqExpiredMap, aSeqNum)
169 gControlData.eventRicControlReqExpiredMu.Unlock()
170 if !isResponsed {
171 xapp.Logger.Debug("RIC_CONTROL_REQ[%s]: RIC Event Create Timer experied!", aSeqNum)
172 log.Printf("RIC_CONTROL_REQ[%s]: RIC Event Create Timer experied!", aSeqNum)
173 //Send ErrorIndication message on Timeout
174 return
175 }
176 default:
177 gControlData.eventRicControlReqExpiredMu.Lock()
178 flag := gControlData.eventRicControlReqExpiredMap[aSeqNum]
179 if flag {
180 delete(gControlData.eventRicControlReqExpiredMap, aSeqNum)
181 gControlData.eventRicControlReqExpiredMu.Unlock()
182 xapp.Logger.Debug("RIC_CONTROL_REQ[%s]: RIC Event Create Timer canceled!", aSeqNum)
183 log.Printf("RIC_CONTROL_REQ[%s]: RIC Event Create Timer canceled!", aSeqNum)
184 return
185 } else {
186 gControlData.eventRicControlReqExpiredMu.Unlock()
187 }
188 }
189 time.Sleep(100 * time.Millisecond)
190 }
191 }(timer)
192}
193func (aRicHoControlMsg *RicHoControlMsg) SendRicControlRequest(aRequestSN int) (err error) {
194 var e2ap *E2ap
195 var e2sm *E2sm
196
197 xapp.Logger.Info("SendRicControlRequest Enter for RanName = %s", aRicHoControlMsg.RicControlGrpcReqPtr.RanName)
198
199 if aRicHoControlMsg.RicControlGrpcReqPtr == nil {
200 return err
201 }
202
203 var lRicControlStyleType int64 = aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.ControlStyle
204 var lRicControlActionID int64 = aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.ControlActionId
205 lUEID := aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID
206
207 lUeIdBuf := []byte(aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
208
209 xapp.Logger.Debug("UEID:%s, lUeIdBuf: %v", aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID, lUeIdBuf)
210
211 var lRicControlHeader []byte = make([]byte, 256) //Check the Size
212 lRicControlHeaderEncoded, err := e2sm.SetRicControlHeader(lRicControlHeader, lUeIdBuf, lRicControlStyleType, lRicControlActionID)
213 if err != nil {
214 xapp.Logger.Error("SetRicControlHeader Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
215 log.Printf("SetRicControlHeader Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
216 return err
217 } else {
218 xapp.Logger.Info("SetRicControlHeader is success: %x", lRicControlHeaderEncoded)
219 fmt.Fprintf(os.Stderr, "Encoded RIC Control Header PDU:\n")
220 for i := 0; i < len(lRicControlHeaderEncoded); i++ {
221 fmt.Fprintf(os.Stderr, " %02x", lRicControlHeaderEncoded[i])
222 }
223 fmt.Fprintf(os.Stderr, "\n")
224 }
225
226 var lTargetPrimaryCell int64 = RIC_CONTROL_TARGET_PRIMARY_CELL
227 var lTargetCell int64 = RIC_CONTROL_TARGET_CELL
228 var lNrCGIOrECGI int64 = RIC_CONTROL_CGI_TYPE
229
230 lNrOrEUtraCellType := aRicHoControlMsg.RicControlGrpcReqPtr.RICControlMessageData.RICControlCellTypeVal
231 lTargetCellVal := aRicHoControlMsg.RicControlGrpcReqPtr.RICControlMessageData.TargetCellID
232 lTargetCellValBuf := []byte(lTargetCellVal)
233
234 var lRicControlMessage []byte = make([]byte, 1024)
235 lRicControlMessageEncoded, err := e2sm.SetRicControlMessage(lRicControlMessage, lTargetPrimaryCell, lTargetCell, lNrCGIOrECGI, int64(lNrOrEUtraCellType), lTargetCellValBuf)
236 if err != nil {
237 xapp.Logger.Error("SetRicControlMessage Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
238 log.Printf("SetRicControlMessage Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
239 return err
240 } else {
241 xapp.Logger.Debug("SetRicControlMessage is success: %x", lRicControlMessageEncoded)
242 fmt.Fprintf(os.Stderr, "Encoded RIC Control Message PDU:\n")
243 for i := 0; i < len(lRicControlMessageEncoded); i++ {
244 fmt.Fprintf(os.Stderr, " %02x", lRicControlMessageEncoded[i])
245 }
246 fmt.Fprintf(os.Stderr, "\n")
247 }
248
249 lParams := &xapp.RMRParams{}
250 lParams.Mtype = 12040 //RIC_CONTROL_REQ
251 lParams.SubId = -1
252
253 var lRequestorId uint16 = uint16(aRicHoControlMsg.RicControlGrpcReqPtr.RICE2APHeaderData.RICRequestorID)
254 var lFuncId uint16 = uint16(aRicHoControlMsg.RicControlGrpcReqPtr.RICE2APHeaderData.RanFuncId)
255
256 lParams.Payload = make([]byte, 2048)
257 lParams.Payload, err = e2ap.SetRicControlRequestPayload(lParams.Payload, lRequestorId, uint16(aRequestSN), lFuncId,
258 lRicControlHeaderEncoded, lRicControlMessageEncoded)
259 if err != nil {
260 xapp.Logger.Error("SetRicControlRequestPayload Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
261 log.Printf("SetRicControlRequestPayload Failed: %v, UEID:%s", err, aRicHoControlMsg.RicControlGrpcReqPtr.RICControlHeaderData.UEID)
262 return err
263 } else {
264 xapp.Logger.Debug("Encoding RicControlRequestPayload is success. UEID: %s, Payload: %x", lUEID, lParams.Payload)
265 fmt.Fprintf(os.Stderr, "Encoded RIC Control Req PDU:\n")
266 for i := 0; i < len(lParams.Payload); i++ {
267 fmt.Fprintf(os.Stderr, " %02x", lParams.Payload[i])
268 }
269 fmt.Fprintf(os.Stderr, "\n")
270 }
271
272 valEnbId := aRicHoControlMsg.RicControlGrpcReqPtr.E2NodeID
273 valRanName := aRicHoControlMsg.RicControlGrpcReqPtr.RanName
274 valPlmnId := aRicHoControlMsg.RicControlGrpcReqPtr.PlmnID
275 lParams.Meid = &xapp.RMRMeid{PlmnID: valPlmnId, EnbID: valEnbId, RanName: valRanName}
276
277 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)
278
279 xapp.Logger.Debug("Sending RIC Control message to RanName: %s, UEID: %s ", aRicHoControlMsg.RicControlGrpcReqPtr.RanName, lUEID)
280
281 err = gControlData.rmrSend(lParams)
282 if err != nil {
283 xapp.Logger.Error("Failed to send RIC_CONTROL_REQ: %v", err)
284 log.Printf("Failed to send RIC_CONTROL_REQ: %v", err)
285 return err
286 }
287
288 xapp.Logger.Info("Sending RIC Control message to RanName: %s, UEID: %s Success", aRicHoControlMsg.RicControlGrpcReqPtr.RanName, lUEID)
289
290 aRicHoControlMsg.setEventRicControlCreateExpiredTimer(aRequestSN) //TODO check if this is required as we are not expecting Control ACK
291
292 return nil
293}
294
295func HandleControlResponse(params *xapp.RMRParams) (err error) {
296 xapp.Logger.Debug("The SubId in RIC_CONTROL_RESP is %d", params.SubId)
297 log.Printf("The SubId in RIC_CONTROL_RESP is %d", params.SubId)
298
299 return nil
300}
301
302func HandleControlFailure(params *xapp.RMRParams) (err error) {
303
304 xapp.Logger.Debug("The SubId in RIC_CONTROL_FAILURE is %d", params.SubId)
305 log.Printf("The SubId in RIC_CONTROL_FAILURE is %d", params.SubId)
306
307 return nil
308}