First commit

This commit is contained in:
Leonardo Bonati
2021-12-08 20:17:46 +00:00
commit 60dffad583
2923 changed files with 463894 additions and 0 deletions

View File

@@ -0,0 +1,161 @@
//
// Copyright 2019 AT&T Intellectual Property
// Copyright 2019 Nokia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
// platform project (RICP).
package rmrCgo
// #cgo LDFLAGS: -L/usr/local/lib -lrmr_si
// #include <rmr/rmr.h>
// #include <stdlib.h>
import "C"
import (
"fmt"
"github.com/pkg/errors"
"strings"
"time"
"unsafe"
"e2mgr/logger"
)
func (*Context) Init(port string, maxMsgSize int, flags int, logger *logger.Logger) RmrMessenger {
pp := C.CString(port)
defer C.free(unsafe.Pointer(pp))
logger.Debugf("#rmrCgoApi.Init - Going to initiate RMR router")
ctx := NewContext(maxMsgSize, flags, C.rmr_init(pp, C.int(maxMsgSize), C.int(flags)), logger)
start := time.Now()
//TODO use time.Ticker()
for !ctx.IsReady() {
time.Sleep(time.Second)
if time.Since(start) >= time.Minute {
logger.Debugf("#rmrCgoApi.Init - Routing table is not ready")
start = time.Now()
}
}
logger.Infof("#rmrCgoApi.Init - RMR router has been initiated")
// Configure the rmr to make rounds of attempts to send a message before notifying the application that it should retry.
// Each round is about 1000 attempts with a short sleep between each round.
C.rmr_set_stimeout(ctx.RmrCtx, C.int(1000))
r := RmrMessenger(ctx)
return r
}
func (ctx *Context) SendMsg(msg *MBuf, printLogs bool) (*MBuf, error) {
ctx.checkContextInitialized()
ctx.Logger.Debugf("#rmrCgoApi.SendMsg - Going to send message. MBuf: %v", *msg)
allocatedCMBuf := ctx.getAllocatedCRmrMBuf(ctx.Logger, msg, ctx.MaxMsgSize)
state := allocatedCMBuf.state
if state != RMR_OK {
errorMessage := fmt.Sprintf("#rmrCgoApi.SendMsg - Failed to get allocated message. state: %v - %s", state, states[int(state)])
return nil, errors.New(errorMessage)
}
if printLogs {
//TODO: if debug enabled
transactionId := string(*msg.XAction)
tmpTid := strings.TrimSpace(transactionId)
ctx.Logger.Infof("[E2 Manager -> RMR] #rmrCgoApi.SendMsg - Going to send message %v for transaction id: %s", *msg, tmpTid)
}
currCMBuf := C.rmr_send_msg(ctx.RmrCtx, allocatedCMBuf)
defer C.rmr_free_msg(currCMBuf)
state = currCMBuf.state
if state != RMR_OK {
errorMessage := fmt.Sprintf("#rmrCgoApi.SendMsg - Failed to send message. state: %v - %s", state, states[int(state)])
return nil, errors.New(errorMessage)
}
return convertToMBuf(ctx.Logger, currCMBuf), nil
}
func (ctx *Context) WhSendMsg(msg *MBuf, printLogs bool) (*MBuf, error) {
ctx.checkContextInitialized()
ctx.Logger.Debugf("#rmrCgoApi.WhSendMsg - Going to wormhole send message. MBuf: %v", *msg)
whid := C.rmr_wh_open(ctx.RmrCtx, (*C.char)(msg.GetMsgSrc())) // open direct connection, returns wormhole ID
ctx.Logger.Infof("#rmrCgoApi.WhSendMsg - The wormhole id %v has been received", whid)
defer C.rmr_wh_close(ctx.RmrCtx, whid)
allocatedCMBuf := ctx.getAllocatedCRmrMBuf(ctx.Logger, msg, ctx.MaxMsgSize)
state := allocatedCMBuf.state
if state != RMR_OK {
errorMessage := fmt.Sprintf("#rmrCgoApi.WhSendMsg - Failed to get allocated message. state: %v - %s", state, states[int(state)])
return nil, errors.New(errorMessage)
}
if printLogs {
transactionId := string(*msg.XAction)
tmpTid := strings.TrimSpace(transactionId)
ctx.Logger.Infof("[E2 Manager -> RMR] #rmrCgoApi.WhSendMsg - Going to send message %v for transaction id: %s", *msg, tmpTid)
}
currCMBuf := C.rmr_wh_send_msg(ctx.RmrCtx, whid, allocatedCMBuf)
defer C.rmr_free_msg(currCMBuf)
state = currCMBuf.state
if state != RMR_OK {
errorMessage := fmt.Sprintf("#rmrCgoApi.WhSendMsg - Failed to send message. state: %v - %s", state, states[int(state)])
return nil, errors.New(errorMessage)
}
return convertToMBuf(ctx.Logger, currCMBuf), nil
}
func (ctx *Context) RecvMsg() (*MBuf, error) {
ctx.checkContextInitialized()
ctx.Logger.Debugf("#rmrCgoApi.RecvMsg - Going to receive message")
allocatedCMBuf := C.rmr_alloc_msg(ctx.RmrCtx, C.int(ctx.MaxMsgSize))
currCMBuf := C.rmr_rcv_msg(ctx.RmrCtx, allocatedCMBuf)
defer C.rmr_free_msg(currCMBuf)
state := currCMBuf.state
if state != RMR_OK {
errorMessage := fmt.Sprintf("#rmrCgoApi.RecvMsg - Failed to receive message. state: %v - %s", state, states[int(state)])
ctx.Logger.Errorf(errorMessage)
return nil, errors.New(errorMessage)
}
mbuf := convertToMBuf(ctx.Logger, currCMBuf)
if mbuf.MType != E2_TERM_KEEP_ALIVE_RESP {
transactionId := string(*mbuf.XAction)
tmpTid := strings.TrimSpace(transactionId)
ctx.Logger.Infof("[RMR -> E2 Manager] #rmrCgoApi.RecvMsg - message %v has been received for transaction id: %s", *mbuf, tmpTid)
}
return mbuf, nil
}
func (ctx *Context) IsReady() bool {
ctx.Logger.Debugf("#rmrCgoApi.IsReady - Going to check if routing table is initialized")
return int(C.rmr_ready(ctx.RmrCtx)) != 0
}
func (ctx *Context) Close() {
ctx.Logger.Debugf("#rmrCgoApi.Close - Going to close RMR context")
C.rmr_close(ctx.RmrCtx)
time.Sleep(100 * time.Millisecond)
}

View File

@@ -0,0 +1,138 @@
//
// Copyright 2019 AT&T Intellectual Property
// Copyright 2019 Nokia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
// platform project (RICP).
package rmrCgo_test
import (
"bytes"
"e2mgr/logger"
"e2mgr/rmrCgo"
"e2mgr/tests"
"encoding/json"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
"unsafe"
)
var (
log *logger.Logger
msgr rmrCgo.RmrMessenger
)
func TestLogger(t *testing.T) {
log := initLog(t)
data := map[string]interface{}{"messageType": 1001, "ranIp": "10.0.0.3", "ranPort": 879, "ranName": "test1"}
b := new(bytes.Buffer)
_ = json.NewEncoder(b).Encode(data)
req := tests.GetHttpRequest()
boo, _ := ioutil.ReadAll(req.Body)
log.Debugf("#rmr_c_go_api_test.TestLogger - request header: %v\n; request body: %s\n", req.Header, string(boo))
}
func TestNewMBufSuccess(t *testing.T) {
var msgSrc unsafe.Pointer
msg := rmrCgo.NewMBuf(tests.MessageType, len(tests.DummyPayload), "RanName", &tests.DummyPayload, &tests.DummyXAction, msgSrc)
assert.NotNil(t, msg)
assert.NotEmpty(t, msg.Payload)
assert.NotEmpty(t, msg.XAction)
assert.Equal(t, msg.MType, tests.MessageType)
assert.Equal(t, msg.Meid, "RanName")
assert.Equal(t, msg.Len, len(tests.DummyPayload))
}
/*func TestIsReadySuccess(t *testing.T) {
log := initLog(t)
initRmr(tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
if msgr == nil || !msgr.IsReady() {
t.Errorf("#rmr_c_go_api_test.TestIsReadySuccess - The rmr router is not ready")
}
msgr.Close()
}
func TestSendRecvMsgSuccess(t *testing.T) {
log := initLog(t)
initRmr(tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
if msgr == nil || !msgr.IsReady() {
t.Errorf("#rmr_c_go_api_test.TestSendRecvMsgSuccess - The rmr router is not ready")
}
msg := rmrCgo.NewMBuf(1, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
log.Debugf("#rmr_c_go_api_test.TestSendRecvMsgSuccess - Going to send the message: %#v\n", msg)
result, err := msgr.SendMsg(msg, true)
assert.Nil(t, err)
assert.NotNil(t, result)
msgR, err := msgr.RecvMsg()
assert.Nil(t, err)
assert.NotNil(t, msgR)
msgr.Close()
}
func TestSendMsgRmrInvalidMsgNumError(t *testing.T) {
log := initLog(t)
initRmr(tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
if msgr == nil || !msgr.IsReady() {
t.Errorf("#rmr_c_go_api_test.TestSendMsgRmrInvalidMsgNumError - The rmr router is not ready")
}
msg := rmrCgo.NewMBuf(10, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
log.Debugf("#rmr_c_go_api_test.TestSendMsgRmrInvalidMsgNumError - Going to send the message: %#v\n", msg)
result, err := msgr.SendMsg(msg, true)
assert.NotNil(t, err)
assert.Nil(t, result)
msgr.Close()
}
func TestSendMsgRmrInvalidPortError(t *testing.T) {
log := initLog(t)
initRmr("tcp:"+strconv.Itoa(5555), tests.MaxMsgSize, tests.Flags, log)
if msgr == nil || !msgr.IsReady() {
t.Errorf("#rmr_c_go_api_test.TestSendMsgRmrInvalidPortError - The rmr router is not ready")
}
msg := rmrCgo.NewMBuf(1, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
log.Debugf("#rmr_c_go_api_test.TestSendMsgRmrInvalidPortError - Going to send the message: %#v\n", msg)
result, err := msgr.SendMsg(msg, true)
assert.NotNil(t, err)
assert.Nil(t, result)
msgr.Close()
}
func initRmr(port string, maxMsgSize int, flags int, log *logger.Logger) {
var ctx *rmrCgo.Context
msgr = ctx.Init(port, maxMsgSize, flags, log)
}*/
func initLog(t *testing.T) *logger.Logger {
log, err := logger.InitLogger(logger.DebugLevel)
if err != nil {
t.Errorf("#rmr_c_go_api_test.initLog - failed to initialize logger, error: %s", err)
}
return log
}

View File

@@ -0,0 +1,159 @@
//
// Copyright 2019 AT&T Intellectual Property
// Copyright 2019 Nokia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
// platform project (RICP).
package rmrCgo
// #cgo LDFLAGS: -L/usr/local/lib -lrmr_si
// #include <rmr/rmr.h>
// #include <rmr/RIC_message_types.h>
// #include <stdlib.h>
import "C"
import (
"e2mgr/logger"
"fmt"
"unsafe"
)
func NewMBuf(mType int, len int, meid string, payload *[]byte, xAction *[]byte, msgSrc unsafe.Pointer) *MBuf {
return &MBuf{
mType,
len,
meid,
payload,
xAction,
msgSrc,
}
}
func NewContext(maxMsgSize int, flags int, ctx unsafe.Pointer, logger *logger.Logger) *Context {
return &Context{
MaxMsgSize: maxMsgSize,
Flags: flags,
RmrCtx: ctx,
Logger: logger,
}
}
//TODO: consider declaring using its own type
const (
// messages
RIC_X2_SETUP_REQ = C.RIC_X2_SETUP_REQ
RIC_X2_SETUP_RESP = C.RIC_X2_SETUP_RESP
RIC_X2_SETUP_FAILURE = C.RIC_X2_SETUP_FAILURE
RIC_ENDC_X2_SETUP_REQ = C.RIC_ENDC_X2_SETUP_REQ
RIC_ENDC_X2_SETUP_RESP = C.RIC_ENDC_X2_SETUP_RESP
RIC_ENDC_X2_SETUP_FAILURE = C.RIC_ENDC_X2_SETUP_FAILURE
RIC_SCTP_CONNECTION_FAILURE = C.RIC_SCTP_CONNECTION_FAILURE
RIC_ENB_LOAD_INFORMATION = C.RIC_ENB_LOAD_INFORMATION
RIC_ENB_CONF_UPDATE = C.RIC_ENB_CONF_UPDATE
RIC_ENB_CONFIGURATION_UPDATE_ACK = C.RIC_ENB_CONF_UPDATE_ACK
RIC_ENB_CONFIGURATION_UPDATE_FAILURE = C.RIC_ENB_CONF_UPDATE_FAILURE
RIC_ENDC_CONF_UPDATE = C.RIC_ENDC_CONF_UPDATE
RIC_ENDC_CONF_UPDATE_ACK = C.RIC_ENDC_CONF_UPDATE_ACK
RIC_ENDC_CONF_UPDATE_FAILURE = C.RIC_ENDC_CONF_UPDATE_FAILURE
RIC_SCTP_CLEAR_ALL = C.RIC_SCTP_CLEAR_ALL
RIC_X2_RESET_RESP = C.RIC_X2_RESET_RESP
RIC_X2_RESET = C.RIC_X2_RESET
RIC_E2_TERM_INIT = C.E2_TERM_INIT
RAN_CONNECTED = C.RAN_CONNECTED
RAN_RESTARTED = C.RAN_RESTARTED
RAN_RECONFIGURED = C.RAN_RECONFIGURED
E2_TERM_KEEP_ALIVE_REQ = C.E2_TERM_KEEP_ALIVE_REQ
E2_TERM_KEEP_ALIVE_RESP = C.E2_TERM_KEEP_ALIVE_RESP
RIC_E2_SETUP_REQ = C.RIC_E2_SETUP_REQ
RIC_E2_SETUP_RESP = C.RIC_E2_SETUP_RESP
RIC_E2_SETUP_FAILURE = C.RIC_E2_SETUP_FAILURE
)
const (
RMR_MAX_XACTION_LEN = int(C.RMR_MAX_XID)
RMR_MAX_MEID_LEN = int(C.RMR_MAX_MEID)
RMR_MAX_SRC_LEN = int(C.RMR_MAX_SRC)
//states
RMR_OK = C.RMR_OK
RMR_ERR_BADARG = C.RMR_ERR_BADARG
RMR_ERR_NOENDPT = C.RMR_ERR_NOENDPT
RMR_ERR_EMPTY = C.RMR_ERR_EMPTY
RMR_ERR_NOHDR = C.RMR_ERR_NOHDR
RMR_ERR_SENDFAILED = C.RMR_ERR_SENDFAILED
RMR_ERR_CALLFAILED = C.RMR_ERR_CALLFAILED
RMR_ERR_NOWHOPEN = C.RMR_ERR_NOWHOPEN
RMR_ERR_WHID = C.RMR_ERR_WHID
RMR_ERR_OVERFLOW = C.RMR_ERR_OVERFLOW
RMR_ERR_RETRY = C.RMR_ERR_RETRY
RMR_ERR_RCVFAILED = C.RMR_ERR_RCVFAILED
RMR_ERR_TIMEOUT = C.RMR_ERR_TIMEOUT
RMR_ERR_UNSET = C.RMR_ERR_UNSET
RMR_ERR_TRUNC = C.RMR_ERR_TRUNC
RMR_ERR_INITFAILED = C.RMR_ERR_INITFAILED
)
var states = map[int]string{
RMR_OK: "state is good",
RMR_ERR_BADARG: "argument passd to function was unusable",
RMR_ERR_NOENDPT: "send/call could not find an endpoint based on msg type",
RMR_ERR_EMPTY: "msg received had no payload; attempt to send an empty message",
RMR_ERR_NOHDR: "message didn't contain a valid header",
RMR_ERR_SENDFAILED: "send failed; errno has nano reason",
RMR_ERR_CALLFAILED: "unable to send call() message",
RMR_ERR_NOWHOPEN: "no wormholes are open",
RMR_ERR_WHID: "wormhole id was invalid",
RMR_ERR_OVERFLOW: "operation would have busted through a buffer/field size",
RMR_ERR_RETRY: "request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)",
RMR_ERR_RCVFAILED: "receive failed (hard error)",
RMR_ERR_TIMEOUT: "message processing call timed out",
RMR_ERR_UNSET: "the message hasn't been populated with a transport buffer",
RMR_ERR_TRUNC: "received message likely truncated",
RMR_ERR_INITFAILED: "initialisation of something (probably message) failed",
}
type MBuf struct {
MType int
Len int
Meid string //Managed entity id (RAN name)
Payload *[]byte
XAction *[]byte
msgSrc unsafe.Pointer
}
func (m MBuf) String() string {
return fmt.Sprintf("{ MType: %d, Len: %d, Meid: %q, Xaction: %q, Payload: [%x] }", m.MType, m.Len, m.Meid, m.XAction, m.Payload)
}
func (m MBuf) GetMsgSrc() unsafe.Pointer {
return m.msgSrc
}
type Context struct {
MaxMsgSize int
Flags int
RmrCtx unsafe.Pointer
Logger *logger.Logger
}
type RmrMessenger interface {
Init(port string, maxMsgSize int, flags int, logger *logger.Logger) RmrMessenger
SendMsg(msg *MBuf, printLogs bool) (*MBuf, error)
WhSendMsg(msg *MBuf, printLogs bool) (*MBuf, error)
RecvMsg() (*MBuf, error)
IsReady() bool
Close()
}

View File

@@ -0,0 +1,113 @@
//
// Copyright 2019 AT&T Intellectual Property
// Copyright 2019 Nokia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
// platform project (RICP).
package rmrCgo
// #cgo LDFLAGS: -L/usr/local/lib -lrmr_si
// #include <rmr/rmr.h>
// #include <stdlib.h>
import "C"
import (
"e2mgr/logger"
"bytes"
"encoding/binary"
"strings"
"unsafe"
)
func convertToMBuf(logger *logger.Logger, m *C.rmr_mbuf_t) *MBuf {
payloadArr := C.GoBytes(unsafe.Pointer(m.payload),C.int(m.len))
xActionArr := C.GoBytes(unsafe.Pointer(m.xaction),C.int(RMR_MAX_XACTION_LEN))
// Trim padding (space and 0)
xActionStr := strings.TrimRight(string(xActionArr),"\040\000")
xActionArr = []byte(xActionStr)
mbuf := &MBuf{
MType: int(m.mtype),
Len: int(m.len),
Payload: &payloadArr,
XAction: &xActionArr,
msgSrc: C.CBytes(make([]byte, RMR_MAX_SRC_LEN)),
}
C.rmr_get_src(m, (*C.uchar)(mbuf.msgSrc)) // Capture message source
meidBuf := make([]byte, RMR_MAX_MEID_LEN)
if meidCstr := C.rmr_get_meid(m, (*C.uchar)(unsafe.Pointer(&meidBuf[0]))); meidCstr != nil {
mbuf.Meid = strings.TrimRight(string(meidBuf), "\000")
}
return mbuf
}
func (ctx *Context) getAllocatedCRmrMBuf(logger *logger.Logger, mBuf *MBuf, maxMsgSize int) (cMBuf *C.rmr_mbuf_t) {
var xActionBuf [RMR_MAX_XACTION_LEN]byte
var meidBuf[RMR_MAX_MEID_LEN]byte
cMBuf = C.rmr_alloc_msg(ctx.RmrCtx, C.int(maxMsgSize))
cMBuf.mtype = C.int(mBuf.MType)
cMBuf.len = C.int(mBuf.Len)
payloadLen := len(*mBuf.Payload)
xActionLen := len(*mBuf.XAction)
//Add padding
copy(xActionBuf[:], *mBuf.XAction)
for i:= xActionLen; i < RMR_MAX_XACTION_LEN; i++{
xActionBuf[i] = '\040' //space
}
// Add padding
copy(meidBuf[:], mBuf.Meid)
for i:= len(mBuf.Meid); i < RMR_MAX_MEID_LEN; i++{
meidBuf[i] = 0
}
payloadArr := (*[1 << 30]byte)(unsafe.Pointer(cMBuf.payload))[:payloadLen:payloadLen]
xActionArr := (*[1 << 30]byte)(unsafe.Pointer(cMBuf.xaction))[:RMR_MAX_XACTION_LEN:RMR_MAX_XACTION_LEN]
err := binary.Read(bytes.NewReader(*mBuf.Payload), binary.LittleEndian, payloadArr)
if err != nil {
ctx.Logger.Errorf(
"#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to read payload to allocated RMR message buffer")
}
err = binary.Read(bytes.NewReader(xActionBuf[:]), binary.LittleEndian, xActionArr)
if err != nil {
ctx.Logger.Errorf(
"#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to read xAction data to allocated RMR message buffer")
}
len := C.rmr_bytes2meid(cMBuf, (*C.uchar)(unsafe.Pointer(&meidBuf[0])), C.int(RMR_MAX_MEID_LEN))
if int(len) != RMR_MAX_MEID_LEN {
ctx.Logger.Errorf(
"#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to copy meid data to allocated RMR message buffer")
}
return cMBuf
}
//TODO: change to assert or return error
func (ctx *Context) checkContextInitialized() {
if ctx.RmrCtx == nil {
if ctx.Logger != nil {
ctx.Logger.DPanicf("#rmrCgoUtils.checkContextInitialized - The RMR router has not been initialized")
}
panic("#rmrCgoUtils.checkContextInitialized - The RMR router has not been initialized. To initialize router please call Init() method")
}
}