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