RIC-997: ErrorIndication handling in e2mgr
[ric-plt/e2mgr.git] / E2Manager / rmrCgo / rmrCgoUtils.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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 rmrCgo
22
23 // #cgo LDFLAGS: -L/usr/local/lib -lrmr_si
24 // #include <rmr/rmr.h>
25 // #include <stdlib.h>
26 import "C"
27 import (
28         "e2mgr/logger"
29         "bytes"
30         "encoding/binary"
31         "strings"
32         "unsafe"
33 )
34
35 func convertToMBuf(logger *logger.Logger, m *C.rmr_mbuf_t) *MBuf {
36         payloadArr := C.GoBytes(unsafe.Pointer(m.payload),C.int(m.len))
37         xActionArr := C.GoBytes(unsafe.Pointer(m.xaction),C.int(RMR_MAX_XACTION_LEN))
38
39         // Trim padding (space and 0)
40         xActionStr :=  strings.TrimRight(string(xActionArr),"\040\000")
41         xActionArr = []byte(xActionStr)
42
43         mbuf := &MBuf{
44                 MType: int(m.mtype),
45                 Len:   int(m.len),
46                 Payload: &payloadArr,
47                 XAction: &xActionArr,
48                 msgSrc: C.CBytes(make([]byte, RMR_MAX_SRC_LEN)),
49         }
50
51         C.rmr_get_src(m, (*C.uchar)(mbuf.msgSrc)) // Capture message source
52
53         meidBuf := make([]byte, RMR_MAX_MEID_LEN)
54         if meidCstr := C.rmr_get_meid(m, (*C.uchar)(unsafe.Pointer(&meidBuf[0]))); meidCstr != nil {
55                 mbuf.Meid =     strings.TrimRight(string(meidBuf), "\000")
56         }
57
58         return mbuf
59 }
60
61 func (ctx *Context) getAllocatedCRmrMBuf(logger *logger.Logger, mBuf *MBuf, maxMsgSize int) (cMBuf *C.rmr_mbuf_t) {
62         var xActionBuf [RMR_MAX_XACTION_LEN]byte
63         var meidBuf[RMR_MAX_MEID_LEN]byte
64
65         cMBuf = C.rmr_alloc_msg(ctx.RmrCtx, C.int(maxMsgSize))
66         if cMBuf == nil {
67                 return nil
68         }
69         cMBuf.mtype = C.int(mBuf.MType)
70         cMBuf.len = C.int(mBuf.Len)
71
72         payloadLen := len(*mBuf.Payload)
73         xActionLen := len(*mBuf.XAction)
74
75         //Add padding
76         copy(xActionBuf[:], *mBuf.XAction)
77         for i:= xActionLen; i < RMR_MAX_XACTION_LEN; i++{
78                 xActionBuf[i] = '\040' //space
79         }
80
81         // Add padding
82         copy(meidBuf[:], mBuf.Meid)
83         for i:= len(mBuf.Meid); i < RMR_MAX_MEID_LEN; i++{
84                 meidBuf[i] = 0
85         }
86
87         payloadArr := (*[1 << 30]byte)(unsafe.Pointer(cMBuf.payload))[:payloadLen:payloadLen]
88         xActionArr := (*[1 << 30]byte)(unsafe.Pointer(cMBuf.xaction))[:RMR_MAX_XACTION_LEN:RMR_MAX_XACTION_LEN]
89
90         err := binary.Read(bytes.NewReader(*mBuf.Payload), binary.LittleEndian, payloadArr)
91         if err != nil {
92                 ctx.Logger.Errorf(
93                         "#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to read payload to allocated RMR message buffer")
94         }
95         err = binary.Read(bytes.NewReader(xActionBuf[:]), binary.LittleEndian, xActionArr)
96         if err != nil {
97                 ctx.Logger.Errorf(
98                         "#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to read xAction data to allocated RMR message buffer")
99         }
100         len := C.rmr_bytes2meid(cMBuf,  (*C.uchar)(unsafe.Pointer(&meidBuf[0])), C.int(RMR_MAX_MEID_LEN))
101         if int(len) != RMR_MAX_MEID_LEN {
102                 ctx.Logger.Errorf(
103                         "#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to copy meid data to allocated RMR message buffer")
104         }
105         return cMBuf
106 }
107
108 //TODO: change to assert or return error
109 func (ctx *Context) checkContextInitialized() {
110         if ctx.RmrCtx == nil {
111                 /*if ctx.Logger != nil {
112                         ctx.Logger.DPanicf("#rmrCgoUtils.checkContextInitialized - The RMR router has not been initialized")
113                 }*/
114                 panic("#rmrCgoUtils.checkContextInitialized - The RMR router has not been initialized. To initialize router please call Init() method")
115         }
116 }