First commit
This commit is contained in:
106
setup/e2mgr/tools/xappmock/rmr/rmrCgoApi.go
Normal file
106
setup/e2mgr/tools/xappmock/rmr/rmrCgoApi.go
Normal file
@@ -0,0 +1,106 @@
|
||||
//
|
||||
// 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 rmr
|
||||
|
||||
// #cgo LDFLAGS: -L/usr/local/lib -lrmr_nng -lnng
|
||||
// #include <rmr/rmr.h>
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func (*Context) Init(port string, maxMsgSize int, maxRetries int, flags int) *Messenger {
|
||||
pp := C.CString(port)
|
||||
defer C.free(unsafe.Pointer(pp))
|
||||
ctx := NewContext(maxMsgSize, maxRetries, flags, C.rmr_init(pp, C.int(maxMsgSize), C.int(flags)))
|
||||
start := time.Now()
|
||||
for !ctx.IsReady() {
|
||||
time.Sleep(time.Second)
|
||||
if time.Since(start) >= time.Minute {
|
||||
start = time.Now()
|
||||
}
|
||||
}
|
||||
// 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(0))
|
||||
r := Messenger(ctx)
|
||||
return &r
|
||||
}
|
||||
|
||||
func (ctx *Context) SendMsg(msg *MBuf) (*MBuf, error) {
|
||||
|
||||
allocatedCMBuf, err := ctx.getAllocatedCRmrMBuf(msg, ctx.MaxMsgSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if state := allocatedCMBuf.state; 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)
|
||||
}
|
||||
defer C.rmr_free_msg(allocatedCMBuf)
|
||||
|
||||
for i := 0; i < ctx.MaxRetries; i++ {
|
||||
currCMBuf := C.rmr_send_msg(ctx.RmrCtx, allocatedCMBuf)
|
||||
if state := currCMBuf.state; state != RMR_OK {
|
||||
if state != RMR_ERR_RETRY {
|
||||
errorMessage := fmt.Sprintf("#rmrCgoApi.SendMsg - Failed to send message. state: %v - %s", state, states[int(state)])
|
||||
return nil, errors.New(errorMessage)
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
return convertToMBuf(currCMBuf)
|
||||
}
|
||||
|
||||
return nil, errors.New(fmt.Sprintf("#rmrCgoApi.SendMsg - Too many retries"))
|
||||
}
|
||||
|
||||
func (ctx *Context) RecvMsg() (*MBuf, error) {
|
||||
allocatedCMBuf, err := C.rmr_alloc_msg(ctx.RmrCtx, C.int(ctx.MaxMsgSize))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if state := allocatedCMBuf.state; 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)
|
||||
}
|
||||
defer C.rmr_free_msg(allocatedCMBuf)
|
||||
|
||||
currCMBuf := C.rmr_rcv_msg(ctx.RmrCtx, allocatedCMBuf)
|
||||
if state := currCMBuf.state; state != RMR_OK {
|
||||
errorMessage := fmt.Sprintf("#rmrCgoApi.RecvMsg - Failed to receive message. state: %v - %s", state, states[int(state)])
|
||||
return nil, errors.New(errorMessage)
|
||||
}
|
||||
|
||||
return convertToMBuf(currCMBuf)
|
||||
}
|
||||
|
||||
func (ctx *Context) IsReady() bool {
|
||||
return int(C.rmr_ready(ctx.RmrCtx)) != 0
|
||||
}
|
||||
|
||||
func (ctx *Context) Close() {
|
||||
C.rmr_close(ctx.RmrCtx)
|
||||
}
|
124
setup/e2mgr/tools/xappmock/rmr/rmrCgoTypes.go
Normal file
124
setup/e2mgr/tools/xappmock/rmr/rmrCgoTypes.go
Normal file
@@ -0,0 +1,124 @@
|
||||
//
|
||||
// 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 rmr
|
||||
|
||||
// #cgo LDFLAGS: -L/usr/local/lib -lrmr_nng -lnng
|
||||
// #include <rmr/rmr.h>
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func NewMBuf(mType int, len int, payload []byte, xAction []byte) *MBuf {
|
||||
return &MBuf{
|
||||
MType: mType,
|
||||
Len: len,
|
||||
Payload: payload,
|
||||
XAction: xAction,
|
||||
}
|
||||
}
|
||||
|
||||
func NewContext(maxMsgSize int, maxRetries, flags int, ctx unsafe.Pointer) *Context {
|
||||
return &Context{
|
||||
MaxMsgSize: maxMsgSize,
|
||||
MaxRetries: maxRetries,
|
||||
Flags: flags,
|
||||
RmrCtx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
RMR_MAX_XACTION_LEN = int(C.RMR_MAX_XID)
|
||||
RMR_MAX_MSG_SIZE = int(C.RMR_MAX_RCV_BYTES)
|
||||
RMR_MAX_MEID_LEN = int(C.RMR_MAX_MEID)
|
||||
|
||||
//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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
type Context struct {
|
||||
MaxMsgSize int
|
||||
MaxRetries int
|
||||
Flags int
|
||||
RmrCtx unsafe.Pointer
|
||||
}
|
||||
|
||||
type Messenger interface {
|
||||
Init(port string, maxMsgSize int, maxRetries int, flags int) *Messenger
|
||||
SendMsg(msg *MBuf) (*MBuf, error)
|
||||
RecvMsg() (*MBuf, error)
|
||||
IsReady() bool
|
||||
Close()
|
||||
}
|
||||
type Config struct {
|
||||
Port int
|
||||
MaxMsgSize int
|
||||
MaxRetries int
|
||||
Flags int
|
||||
}
|
119
setup/e2mgr/tools/xappmock/rmr/rmrCgoUtils.go
Normal file
119
setup/e2mgr/tools/xappmock/rmr/rmrCgoUtils.go
Normal file
@@ -0,0 +1,119 @@
|
||||
//
|
||||
// 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 rmr
|
||||
|
||||
// #cgo LDFLAGS: -L/usr/local/lib -lrmr_nng -lnng
|
||||
// #include <rmr/rmr.h>
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
/*
|
||||
Allocates an mBuf and initialize it with the content of C.rmr_mbuf_t.
|
||||
The xAction field is assigned a a value without trailing spaces.
|
||||
*/
|
||||
func convertToMBuf(m *C.rmr_mbuf_t) (*MBuf, error) {
|
||||
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: (*[]byte)(unsafe.Pointer(m.payload)),
|
||||
Payload: payloadArr,
|
||||
//XAction: (*[]byte)(unsafe.Pointer(m.xaction)),
|
||||
XAction: xActionArr,
|
||||
}
|
||||
|
||||
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, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Allocates an C.rmr_mbuf_t and initialize it with the content of mBuf.
|
||||
The xAction field is padded with trailing spaces upto capacity
|
||||
*/
|
||||
func (ctx *Context) getAllocatedCRmrMBuf(mBuf *MBuf, maxMsgSize int) (cMBuf *C.rmr_mbuf_t, rc error) {
|
||||
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)
|
||||
|
||||
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 {
|
||||
return nil, errors.New(fmt.Sprintf("#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to read payload to allocated RMR message buffer, %s", err))
|
||||
}
|
||||
err = binary.Read(bytes.NewReader(xActionBuf[:]), binary.LittleEndian, xActionArr)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to read xAction data to allocated RMR message buffer, %s", err))
|
||||
}
|
||||
|
||||
len := C.rmr_bytes2meid(cMBuf, (*C.uchar)(unsafe.Pointer(&meidBuf[0])), C.int(RMR_MAX_XACTION_LEN))
|
||||
if int(len) != RMR_MAX_MEID_LEN {
|
||||
return nil, errors.New(
|
||||
"#rmrCgoUtils.getAllocatedCRmrMBuf - Failed to copy meid data to allocated RMR message buffer")
|
||||
}
|
||||
return cMBuf, nil
|
||||
}
|
||||
|
||||
func MessageIdToUint(id string) (msgId uint64, err error) {
|
||||
if len(id) == 0 {
|
||||
msgId, err = 0, nil
|
||||
} else {
|
||||
msgId, err = strconv.ParseUint(id, 10, 16)
|
||||
}
|
||||
return
|
||||
}
|
51
setup/e2mgr/tools/xappmock/rmr/rmrEndPoint.go
Normal file
51
setup/e2mgr/tools/xappmock/rmr/rmrEndPoint.go
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// 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 rmr
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// RmrService holds an instance of RMR messenger as well as its configuration
|
||||
type Service struct {
|
||||
messenger *Messenger
|
||||
}
|
||||
|
||||
// NewRmrService instantiates a new Rmr service instance
|
||||
func NewService(rmrConfig Config, messenger Messenger) *Service {
|
||||
return &Service{
|
||||
messenger: messenger.Init("tcp:"+strconv.Itoa(rmrConfig.Port), rmrConfig.MaxMsgSize, rmrConfig.MaxRetries, rmrConfig.Flags),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Service) SendMessage(messageType int, ranName string, msg []byte, transactionId []byte) (*MBuf, error) {
|
||||
mbuf := NewMBuf(messageType, len(msg), msg, transactionId)
|
||||
mbuf.Meid = ranName
|
||||
return (*r.messenger).SendMsg(mbuf)
|
||||
}
|
||||
|
||||
func (r *Service) RecvMessage() (*MBuf, error) {
|
||||
return (*r.messenger).RecvMsg()
|
||||
}
|
||||
|
||||
func (r *Service) CloseContext() {
|
||||
(*r.messenger).Close()
|
||||
|
||||
}
|
Reference in New Issue
Block a user