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,48 @@
##############################################################################
#
# Copyright (c) 2019 AT&T Intellectual Property.
#
# 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).
#
FROM nexus3.o-ran-sc.org:10004/bldr-ubuntu16-c-go:2-u16.04-nng as ubuntu
WORKDIR /opt/xappmock
COPY . .
ENV PATH=$PATH:/usr/local/go/bin:/usr/lib/go-1.12/bin
# Install RMr library and dev files
RUN wget --content-disposition https://packagecloud.io/o-ran-sc/staging/packages/debian/stretch/rmr_1.10.0_amd64.deb/download.deb
RUN dpkg -i rmr_1.10.0_amd64.deb
RUN wget --content-disposition https://packagecloud.io/o-ran-sc/staging/packages/debian/stretch/rmr-dev_1.10.0_amd64.deb/download.deb
RUN dpkg -i rmr-dev_1.10.0_amd64.deb
RUN go build main/xappmock.go
FROM ubuntu:16.04
COPY --from=ubuntu /opt/xappmock/xappmock /opt/xappmock/xappmock
COPY --from=ubuntu /opt/xappmock/resources /opt/xappmock/resources
COPY --from=ubuntu /usr/local/lib/librmr_nng.so.1 /usr/local/lib/librmr_nng.so.1
COPY --from=ubuntu /usr/local/lib/libnng.so.1 /usr/local/lib/libnng.so.1
WORKDIR /opt/xappmock
ENV LD_LIBRARY_PATH=/usr/local/lib
ENV RMR_SEED_RT=resources/router.txt
ENV RMR_PORT=5001
#CMD mkdir -p resources/conf exec ./xappmock
CMD mkdir -p resources/conf && exec /bin/bash

View File

@@ -0,0 +1,405 @@
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
// platform project (RICP).
// 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 dispatcher
import (
"context"
"fmt"
"github.com/pkg/errors"
"reflect"
"sync"
"time"
"xappmock/enums"
"xappmock/logger"
"xappmock/models"
"xappmock/rmr"
"xappmock/sender"
)
// Id -> Command
var configuration = make(map[string]*models.JsonCommand)
// Rmr Message Id -> Command
var waitForRmrMessageType = make(map[int]*models.JsonCommand)
func addRmrMessageToWaitFor(rmrMessageToWaitFor string, command models.JsonCommand) error {
rmrMsgId, err := rmr.MessageIdToUint(rmrMessageToWaitFor)
if err != nil {
return errors.New(fmt.Sprintf("invalid rmr message id: %s", rmrMessageToWaitFor))
}
waitForRmrMessageType[int(rmrMsgId)] = &command
return nil
}
type Dispatcher struct {
rmrService *rmr.Service
processResult models.ProcessResult
logger *logger.Logger
jsonSender *sender.JsonSender
}
func (d *Dispatcher) GetProcessResult() models.ProcessResult {
return d.processResult
}
func New(logger *logger.Logger, rmrService *rmr.Service, jsonSender *sender.JsonSender) *Dispatcher {
return &Dispatcher{
rmrService: rmrService,
logger: logger,
jsonSender: jsonSender,
}
}
func (d *Dispatcher) JsonCommandsDecoderCB(cmd models.JsonCommand) error {
if len(cmd.Id) == 0 {
return errors.New(fmt.Sprintf("invalid cmd, no id"))
}
configuration[cmd.Id] = &cmd
return nil
// if len(cmd.ReceiveCommandId) == 0 {
// return nil
// }
//
// return addRmrMessageToWaitFor(cmd.ReceiveCommandId, cmd)
}
func (d *Dispatcher) sendNoRepeat(command models.JsonCommand) error {
if enums.CommandAction(command.Action) == enums.SendRmrMessage && d.processResult.StartTime == nil {
now := time.Now()
d.processResult.StartTime = &now
}
err := d.jsonSender.SendJsonRmrMessage(command, nil, d.rmrService)
if err != nil {
d.logger.Errorf("#Dispatcher.sendNoRepeat - error sending rmr message: %s", err)
d.processResult.Err = err
d.processResult.Stats.SentErrorCount.Inc()
return err
}
d.processResult.Stats.SentCount.Inc()
return nil
}
func (d *Dispatcher) sendWithRepeat(ctx context.Context, command models.JsonCommand) {
if enums.CommandAction(command.Action) == enums.SendRmrMessage && d.processResult.StartTime == nil {
now := time.Now()
d.processResult.StartTime = &now
}
for repeatCount := command.RepeatCount; repeatCount > 0; repeatCount-- {
select {
case <-ctx.Done():
return
default:
}
err := d.jsonSender.SendJsonRmrMessage(command, nil, d.rmrService)
if err != nil {
d.logger.Errorf("#Dispatcher.sendWithRepeat - error sending rmr message: %s", err)
d.processResult.Stats.SentErrorCount.Inc()
continue
}
d.processResult.Stats.SentCount.Inc()
time.Sleep(time.Duration(command.RepeatDelayInMs) * time.Millisecond)
}
}
func getReceiveRmrMessageType(receiveCommandId string) (string, error) {
command, ok := configuration[receiveCommandId]
if !ok {
return "", errors.New(fmt.Sprintf("invalid receive command id: %s", receiveCommandId))
}
if len(command.RmrMessageType) == 0 {
return "", errors.New(fmt.Sprintf("missing RmrMessageType for command id: %s", receiveCommandId))
}
return command.RmrMessageType, nil
}
func (d *Dispatcher) sendHandler(ctx context.Context, sendAndReceiveWg *sync.WaitGroup, command models.JsonCommand) {
defer sendAndReceiveWg.Done()
var listenAndHandleWg sync.WaitGroup
if len(command.ReceiveCommandId) > 0 {
rmrMessageToWaitFor, err := getReceiveRmrMessageType(command.ReceiveCommandId)
if err != nil {
d.processResult.Err = err
return
}
err = addRmrMessageToWaitFor(rmrMessageToWaitFor, command)
if err != nil {
d.processResult.Err = err
return
}
listenAndHandleWg.Add(1)
go d.listenAndHandle(ctx, &listenAndHandleWg, command)
}
if command.RepeatCount == 0 {
err := d.sendNoRepeat(command)
if err != nil {
return
}
} else {
d.sendWithRepeat(ctx, command)
}
if len(command.ReceiveCommandId) > 0 {
listenAndHandleWg.Wait()
}
}
func (d *Dispatcher) receiveHandler(ctx context.Context, sendAndReceiveWg *sync.WaitGroup, command models.JsonCommand) {
defer sendAndReceiveWg.Done()
err := addRmrMessageToWaitFor(command.RmrMessageType, command)
if err != nil {
d.processResult.Err = err
return
}
var listenAndHandleWg sync.WaitGroup
listenAndHandleWg.Add(1) // this is due to the usage of listenAndHandle as a goroutine in the sender case
d.listenAndHandle(ctx, &listenAndHandleWg, command)
}
func getMergedCommand(cmd *models.JsonCommand) (models.JsonCommand, error) {
var command models.JsonCommand
if len(cmd.Id) == 0 {
return command, errors.New(fmt.Sprintf("invalid command, no id"))
}
command = *cmd
conf, ok := configuration[cmd.Id]
if ok {
command = *conf
mergeConfigurationAndCommand(&command, cmd)
}
return command, nil
}
func (d *Dispatcher) ProcessJsonCommand(ctx context.Context, cmd *models.JsonCommand) {
command, err := getMergedCommand(cmd)
if err != nil {
d.processResult.Err = err
return
}
var sendAndReceiveWg sync.WaitGroup
commandAction := enums.CommandAction(command.Action)
switch commandAction {
case enums.SendRmrMessage:
sendAndReceiveWg.Add(1)
go d.sendHandler(ctx, &sendAndReceiveWg, command)
case enums.ReceiveRmrMessage:
sendAndReceiveWg.Add(1)
go d.receiveHandler(ctx, &sendAndReceiveWg, command)
default:
d.processResult = models.ProcessResult{Err: errors.New(fmt.Sprintf("invalid command action %s", command.Action))}
return
}
sendAndReceiveWg.Wait()
}
func getResponseCommand(command models.JsonCommand) (*models.JsonCommand, error) {
responseCommand, ok := configuration[command.SendCommandId]
if !ok {
return nil, errors.New(fmt.Sprintf("invalid SendCommandId %s", command.SendCommandId))
}
return responseCommand, nil
}
func (d *Dispatcher) listenAndHandleNoRepeat(ctx context.Context, command models.JsonCommand) {
for {
select {
case <-ctx.Done():
return
default:
}
mbuf, err := d.rmrService.RecvMessage()
if err != nil {
d.logger.Errorf("#Dispatcher.listenAndHandleNoRepeat - error receiving message: %s", err)
d.processResult.Err = err
d.processResult.Stats.ReceivedErrorCount.Inc()
return
}
if enums.CommandAction(command.Action) == enums.ReceiveRmrMessage && d.processResult.StartTime == nil {
now := time.Now()
d.processResult.StartTime = &now
}
messageInfo := models.NewMessageInfo(mbuf.MType, mbuf.Meid, mbuf.Payload, mbuf.XAction)
_, ok := waitForRmrMessageType[mbuf.MType]
if !ok {
d.logger.Infof("#Dispatcher.listenAndHandleNoRepeat - received unexpected msg: %s", messageInfo)
d.processResult.Stats.ReceivedUnexpectedCount.Inc()
continue
}
d.logger.Infof("#Dispatcher.listenAndHandleNoRepeat - received expected msg: %s", messageInfo)
d.processResult.Stats.ReceivedExpectedCount.Inc()
if len(command.SendCommandId) > 0 {
responseCommand, err := getResponseCommand(command)
if err != nil {
d.processResult.Err = err
return
}
_ = d.sendNoRepeat(*responseCommand)
}
return
}
}
func (d *Dispatcher) listenAndHandleWithRepeat(ctx context.Context, command models.JsonCommand) {
var responseCommand *models.JsonCommand
if len(command.SendCommandId) > 0 {
var err error
responseCommand, err = getResponseCommand(command)
if err != nil {
d.processResult.Err = err
return
}
}
for d.processResult.Stats.ReceivedExpectedCount.Load() < int32(command.RepeatCount) {
select {
case <-ctx.Done():
return
default:
}
mbuf, err := d.rmrService.RecvMessage()
if err != nil {
d.logger.Errorf("#Dispatcher.listenAndHandleWithRepeat - error receiving message: %s", err)
d.processResult.Stats.ReceivedErrorCount.Inc()
continue
}
if enums.CommandAction(command.Action) == enums.ReceiveRmrMessage && d.processResult.StartTime == nil {
now := time.Now()
d.processResult.StartTime = &now
}
messageInfo := models.NewMessageInfo(mbuf.MType, mbuf.Meid, mbuf.Payload, mbuf.XAction)
_, ok := waitForRmrMessageType[mbuf.MType]
if !ok {
d.logger.Infof("#Dispatcher.listenAndHandleWithRepeat - received unexpected msg: %s", messageInfo)
d.processResult.Stats.ReceivedUnexpectedCount.Inc()
continue
}
d.logger.Infof("#Dispatcher.listenAndHandleWithRepeat - received expected msg: %s", messageInfo)
d.processResult.Stats.ReceivedExpectedCount.Inc()
if responseCommand != nil {
_ = d.sendNoRepeat(*responseCommand) // TODO: goroutine? + error handling
}
}
}
func (d *Dispatcher) listenAndHandle(ctx context.Context, listenAndHandleWg *sync.WaitGroup, command models.JsonCommand) {
defer listenAndHandleWg.Done()
if command.RepeatCount == 0 {
d.listenAndHandleNoRepeat(ctx, command)
return
}
d.listenAndHandleWithRepeat(ctx, command)
}
func mergeConfigurationAndCommand(conf *models.JsonCommand, cmd *models.JsonCommand) {
nFields := reflect.Indirect(reflect.ValueOf(cmd)).NumField()
for i := 0; i < nFields; i++ {
if fieldValue := reflect.Indirect(reflect.ValueOf(cmd)).Field(i); fieldValue.IsValid() {
switch fieldValue.Kind() {
case reflect.String:
if fieldValue.Len() > 0 {
reflect.Indirect(reflect.ValueOf(conf)).Field(i).Set(fieldValue)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if fieldValue.Int() != 0 {
reflect.Indirect(reflect.ValueOf(conf)).Field(i).Set(fieldValue)
}
case reflect.Bool:
if fieldValue.Bool() {
reflect.Indirect(reflect.ValueOf(conf)).Field(i).Set(fieldValue)
}
case reflect.Float64, reflect.Float32:
if fieldValue.Float() != 0 {
reflect.Indirect(reflect.ValueOf(conf)).Field(i).Set(fieldValue)
}
default:
reflect.Indirect(reflect.ValueOf(conf)).Field(i).Set(fieldValue)
}
}
}
}

View File

@@ -0,0 +1,28 @@
//
// 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 enums
type CommandAction string
const (
SendRmrMessage CommandAction = "send"
ReceiveRmrMessage CommandAction = "receive"
)

View File

@@ -0,0 +1,59 @@
//
// 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 frontend
import (
"github.com/pkg/errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func ProcessConfigurationFile(resourcesFolder, inputFolder, suffix string, processor func(data []byte) error) error {
cwd, err := os.Getwd()
if err != nil {
return errors.New(err.Error())
}
inputDir := filepath.Join(cwd, resourcesFolder, inputFolder)
files, err := ioutil.ReadDir(inputDir)
if err != nil {
return errors.New(err.Error())
}
for _, file := range files {
if file.Mode().IsRegular() && strings.HasSuffix(strings.ToLower(file.Name()), suffix) {
filespec := filepath.Join(inputDir, file.Name())
data, err := ioutil.ReadFile(filespec)
if err != nil {
return errors.New(err.Error())
}
err = processor(data)
if err != nil {
return err
}
}
}
return nil
}

View File

@@ -0,0 +1,57 @@
//
// 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 frontend
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"xappmock/models"
)
func DecodeJsonCommand(data []byte) (*models.JsonCommand, error) {
dec := json.NewDecoder(bytes.NewReader(data))
var cmd models.JsonCommand
if err := dec.Decode(&cmd); err != nil && err != io.EOF {
return nil, errors.New(err.Error())
}
return &cmd, nil
}
func JsonCommandsDecoder(data []byte, processor func(models.JsonCommand) error) error {
dec := json.NewDecoder(bytes.NewReader(data))
for {
var commands []models.JsonCommand
if err := dec.Decode(&commands); err == io.EOF {
break
} else if err != nil {
return errors.New(err.Error())
}
for i, cmd := range commands {
if err := processor(cmd); err != nil {
return errors.New(fmt.Sprintf("processing error at #%d, %s", i, err))
}
}
}
return nil
}

View File

@@ -0,0 +1,9 @@
module xappmock
go 1.12
require (
github.com/pkg/errors v0.8.1
go.uber.org/atomic v1.5.0
go.uber.org/zap v1.13.0
)

View File

@@ -0,0 +1,57 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.uber.org/atomic v1.5.0 h1:OI5t8sDa1Or+q8AeE+yKeB/SDYioSHAgcVljj9JIETY=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.3.0 h1:sFPn2GLc3poCkfrpIXGhBD2X0CMIo4Q/zSULXrj/+uc=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.13.0 h1:nR6NoDBgAf67s68NhaXbsojM+2gxp3S1hWkHDl27pVU=
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c h1:IGkKhmfzcztjm6gYkykvu/NiS8kaqbCWAEWWAyf8J5U=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=

View File

@@ -0,0 +1,187 @@
//
// 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 logger
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"strings"
"time"
)
type Logger struct {
Logger *zap.Logger
}
// Copied from zap logger
//
// A Level is a logging priority. Higher levels are more important.
type LogLevel int8
const (
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
DebugLevel LogLevel = iota - 1
// InfoLevel is the default logging priority.
InfoLevel
// WarnLevel logs are more important than Info, but don't need individual
// human review.
WarnLevel
// ErrorLevel logs are high-priority. If an application is running smoothly,
// it shouldn't generate any error-level logs.
ErrorLevel
// DPanicLevel logs are particularly important errors. In development the
// logger panics after writing the message.
DPanicLevel
// PanicLevel logs a message, then panics.
PanicLevel
// FatalLevel logs a message, then calls os.Exit(1).
FatalLevel
_minLevel = DebugLevel
_maxLevel = FatalLevel
)
var logLevelTokenToLevel = map[string] LogLevel {
"debug" : DebugLevel,
"info": InfoLevel,
"warn": WarnLevel,
"error": ErrorLevel,
"dpanic": DPanicLevel,
"panic": PanicLevel,
"fatal": FatalLevel,
}
func LogLevelTokenToLevel(level string) (LogLevel, bool) {
if level, ok := logLevelTokenToLevel[strings.TrimSpace(strings.ToLower(level))];ok {
return level, true
}
return _maxLevel+1, false
}
func InitLogger(requested LogLevel) (*Logger, error) {
var logger *zap.Logger
var err error
switch requested {
case DebugLevel:
logger, err = initLoggerByLevel(zapcore.DebugLevel)
case InfoLevel:
logger, err = initLoggerByLevel(zapcore.InfoLevel)
case WarnLevel:
logger, err = initLoggerByLevel(zapcore.WarnLevel)
case ErrorLevel:
logger, err = initLoggerByLevel(zapcore.ErrorLevel)
case DPanicLevel:
logger, err = initLoggerByLevel(zapcore.DPanicLevel)
case PanicLevel:
logger, err = initLoggerByLevel(zapcore.PanicLevel)
case FatalLevel:
logger, err = initLoggerByLevel(zapcore.FatalLevel)
default:
err = fmt.Errorf("Invalid logging Level :%d",requested)
}
if err != nil {
return nil, err
}
return &Logger{Logger:logger}, nil
}
func(l *Logger)Sync() error {
l.Debugf("#logger.Sync - Going to flush buffered log")
return l.Logger.Sync()
}
func (l *Logger)Infof(formatMsg string, a ...interface{}) {
if l.InfoEnabled() {
msg := fmt.Sprintf(formatMsg, a...)
l.Logger.Info(msg, zap.Any("mdc", l.getTimeStampMdc()))
}
}
func (l *Logger)Debugf(formatMsg string, a ...interface{}) {
if l.DebugEnabled(){
msg := fmt.Sprintf(formatMsg, a...)
l.Logger.Debug(msg, zap.Any("mdc", l.getTimeStampMdc()))
}
}
func (l *Logger)Errorf(formatMsg string, a ...interface{}) {
msg := fmt.Sprintf(formatMsg, a...)
l.Logger.Error(msg, zap.Any("mdc", l.getTimeStampMdc()))
}
func (l *Logger)Warnf(formatMsg string, a ...interface{}) {
msg := fmt.Sprintf(formatMsg, a...)
l.Logger.Warn(msg, zap.Any("mdc", l.getTimeStampMdc()))
}
func (l *Logger) getTimeStampMdc() map[string]string {
timeStr := time.Now().Format("2006-01-02 15:04:05.000")
mdc := map[string]string{"time": timeStr}
return mdc
}
func (l *Logger)InfoEnabled()bool{
return l.Logger.Core().Enabled(zap.InfoLevel)
}
func (l *Logger)DebugEnabled()bool{
return l.Logger.Core().Enabled(zap.DebugLevel)
}
func (l *Logger)DPanicf(formatMsg string, a ...interface{}) {
msg := fmt.Sprintf(formatMsg, a...)
l.Logger.DPanic(msg, zap.Any("mdc", l.getTimeStampMdc()))
}
func initLoggerByLevel(l zapcore.Level) (*zap.Logger, error) {
cfg := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(l),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "crit",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "ts",
EncodeTime: epochMillisIntegerTimeEncoder,
CallerKey: "id",
EncodeCaller: xAppMockCallerEncoder,
},
}
return cfg.Build()
}
func xAppMockCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("xAppMock")
}
func epochMillisIntegerTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
nanos := t.UnixNano()
millis := int64(nanos) / int64(time.Millisecond)
enc.AppendInt64(millis)
}

View 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 main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"time"
"xappmock/dispatcher"
"xappmock/frontend"
"xappmock/logger"
"xappmock/rmr"
"xappmock/sender"
)
const (
ENV_RMR_PORT = "RMR_PORT"
RMR_PORT_DEFAULT = 5001
)
var rmrService *rmr.Service
func main() {
logLevel, _ := logger.LogLevelTokenToLevel("info")
logger, err := logger.InitLogger(logLevel)
if err != nil {
fmt.Printf("#app.main - failed to initialize logger, error: %s", err)
os.Exit(1)
}
var rmrContext *rmr.Context
var rmrConfig = rmr.Config{Port: RMR_PORT_DEFAULT, MaxMsgSize: rmr.RMR_MAX_MSG_SIZE, MaxRetries: 10, Flags: 0}
if port, err := strconv.ParseUint(os.Getenv(ENV_RMR_PORT), 10, 16); err == nil {
rmrConfig.Port = int(port)
} else {
logger.Infof("#main - %s: %s, using default (%d).", ENV_RMR_PORT, err, RMR_PORT_DEFAULT)
}
rmrService = rmr.NewService(rmrConfig, rmrContext)
jsonSender := sender.NewJsonSender(logger)
dispatcherDesc := dispatcher.New(logger, rmrService, jsonSender)
/* Load configuration file*/
err = frontend.ProcessConfigurationFile("resources", "conf", ".json",
func(data []byte) error {
return frontend.JsonCommandsDecoder(data, dispatcherDesc.JsonCommandsDecoderCB)
})
if err != nil {
logger.Errorf("#main - processing error: %s", err)
os.Exit(1)
}
logger.Infof("#main - xApp Mock is up and running...")
flag.Parse()
cmd := flag.Arg(0) /*first remaining argument after flags have been processed*/
command, err := frontend.DecodeJsonCommand([]byte(cmd))
if err != nil {
logger.Errorf("#main - command decoding error: %s", err)
rmrService.CloseContext()
logger.Infof("#main - xApp Mock is down")
return
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
oscall := <-c
logger.Infof("system call:%+v", oscall)
cancel()
rmrService.CloseContext()
}()
dispatcherDesc.ProcessJsonCommand(ctx, command)
pr := dispatcherDesc.GetProcessResult()
if pr.Err != nil {
logger.Errorf("#main - command processing Error: %s", pr.Err)
}
if pr.StartTime != nil {
processElapsedTimeInMs := float64(time.Since(*pr.StartTime)) / float64(time.Millisecond)
logger.Infof("#main - processing (sending/receiving) messages took %.2f ms", processElapsedTimeInMs)
}
logger.Infof("#main - process stats: %s", pr.Stats)
rmrService.CloseContext() // TODO: called twice
logger.Infof("#main - xApp Mock is down")
}

View File

@@ -0,0 +1,38 @@
//
// 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 models
type JsonCommand struct {
Id string
RmrMessageType string
SendCommandId string
ReceiveCommandId string
TransactionId string
RanName string
RanIp string
RanPort int
PayloadHeader string
PackedPayload string
Payload string
Action string
RepeatCount int
RepeatDelayInMs int
}

View 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 models
import (
"fmt"
"time"
)
// TODO: message command id / source / dest
type MessageInfo struct {
MessageTimestamp int64 `json:"messageTimestamp"`
MessageType int `json:"messageType"`
Meid string `json:"meid"`
Payload []byte `json:"payload"`
TransactionId string `json:"transactionId"`
}
func NewMessageInfo(messageType int, meid string, payload []byte, transactionId []byte) MessageInfo {
return MessageInfo{
MessageTimestamp: time.Now().Unix(),
MessageType: messageType,
Meid: meid,
Payload: payload,
TransactionId: string(transactionId),
}
}
func (mi MessageInfo) String() string {
return fmt.Sprintf("message timestamp: %d | message type: %d | meid: %s | payload: %x | transaction id: %s",
mi.MessageTimestamp, mi.MessageType, mi.Meid, mi.Payload, mi.TransactionId)
}

View File

@@ -0,0 +1,46 @@
//
// 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 models
import (
"fmt"
"go.uber.org/atomic"
"time"
)
type ProcessStats struct {
SentCount atomic.Int32
SentErrorCount atomic.Int32
ReceivedExpectedCount atomic.Int32
ReceivedUnexpectedCount atomic.Int32
ReceivedErrorCount atomic.Int32
}
type ProcessResult struct {
StartTime *time.Time
Stats ProcessStats
Err error
}
func (ps ProcessStats) String() string {
return fmt.Sprintf("sent messages: %d | send errors: %d | expected received messages: %d | unexpected received messages: %d | receive errors: %d",
ps.SentCount, ps.SentErrorCount, ps.ReceivedExpectedCount, ps.ReceivedUnexpectedCount, ps.ReceivedErrorCount)
}

View File

@@ -0,0 +1,75 @@
[
{
"id": "X2_SETUP_REQUEST",
"rmrMessageType": "10060",
"transactionId": "e2e$",
"payloadHeader": "$ranIp|$ranPort|$ranName|#packedPayload|",
"packedPayload": "0006002a000002001500080013302300fffff000140017000001f700133023fffff0000000133023000000000001"
},
{
"id": "X2_SETUP_RESPONSE",
"rmrMessageType": "10061",
"packedPayload": "2006002a000002001500080002f82900007ab000140017000000630002f8290007ab00102002f829000001000133"
},
{
"id": "RIC_SUBSCRIPTION_REQUEST",
"rmrMessageType": "12010",
"transactionId": "e2e$",
"packedPayload": "00c9002b000003ea7e0005004eec0182ea6300020001ea810015000a0011121300212224264000ea6b000420000000"
},
{
"id": "RIC_SUBSCRIPTION_RESPONSE",
"rmrMessageType": "12011",
"transactionId": "e2e$",
"packedPayload": "20c9001d000003ea7e0005004eec0182ea6300020001ea6c000700ea6d40020000"
},
{
"id": "RIC_SUBSCRIPTION_FAILURE",
"rmrMessageType": "12012",
"transactionId": "e2e$",
"packedPayload": "40c9001f000003ea7e00050000010001ea6300020000ea6e000908ea6f400400014100"
},
{
"id": "RIC_SUBSCRIPTION_DELETE_REQUEST",
"rmrMessageType": "12020",
"transactionId": "e2e$",
"packedPayload": "00ca0012000002ea7e0005004eec0182ea6300020001"
},
{
"id": "RIC_SUBSCRIPTION_DELETE_RESPONSE",
"rmrMessageType": "12021",
"transactionId": "e2e$",
"packedPayload": "20ca0012000002ea7e0005004eec0182ea6300020001"
},
{
"id": "RIC_INDICATION",
"rmrMessageType": "12050"
},
{
"id": "RESOURCE_STATUS_REQUEST",
"rmrMessageType": "10090",
"transactionId": "e2e$",
"packedPayload": "0009003c0000080027000300000e001c00010000260004fe000000001d400d00001f4008000a0b0cabcd8000001e4001000040400100006d4001000091400100"
},
{
"id": "RESOURCE_STATUS_RESPONSE",
"rmrMessageType": "10091",
"transactionId": "e2e$",
"packedPayload": "20090065000003002700030000000028000300004a00414050000042404b4013302302b030a2800043400700020000000a000043400700040000000a000043400700080000000a000043400700200000000a000043400700400000000a000043400700800000000a00"
},
{
"id": "RESOURCE_STATUS_FAILURE",
"rmrMessageType": "10092",
"transactionId": "e2e$",
"packedPayload": "400900320000040027000300000e0028000300000c00054001620044401800004540130002f8290007ab500000434006000000000740"
},
{
"id": "RESOURCE_STATUS_UPDATE",
"rmrMessageType": "10090"
},
{
"id": "LOAD_INFORMATION",
"rmrMessageType": "10020"
}
]

View File

@@ -0,0 +1,29 @@
newrt|start
rte|10060|10.0.2.15:38000
rte|10360|10.0.2.15:38000
rte|10070|10.0.2.15:38000
rte|10071|10.0.2.15:3801
rte|10061|10.0.2.15:3801
rte|10361|10.0.2.15:3801
rte|10062|10.0.2.15:3801
rte|10362|10.0.2.15:3801
rte|1080|10.0.2.15:3801
rte|10020|10.0.2.15:5557
rte|10370|10.0.2.15:3801
rte|10371|10.0.2.15:38000
rte|10372|10.0.2.15:38000
rte|10080|10.0.2.15:3801
rte|10081|10.0.2.15:38000
rte|10082|10.0.2.15:38000
rte|1100|10.0.2.15:3801
rte|1090|10.0.2.15:38000
rte|1200|10.0.2.15:4801
rte|1210|10.0.2.15:4801
rte|1220|10.0.2.15:4801
rte|10090|10.0.2.15:38000
rte|12010|10.0.2.15:38000
rte|12011|10.0.2.15:5555
rte|12020|10.0.2.15:38000
rte|10091|10.0.2.15:4801
rte|10092|10.0.2.15:4801
newrt|end

View File

@@ -0,0 +1 @@
20060043000002001500080002f82900007a8000140030010000630002f8290007ab50102002f8290000010001330000640002f9290007ac50203202f82902f929000002000344

View 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)
}

View 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
}

View 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
}

View 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()
}

View File

@@ -0,0 +1,179 @@
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
// platform project (RICP).
// 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 sender
import (
"fmt"
"github.com/pkg/errors"
"os"
"reflect"
"strconv"
"strings"
"sync/atomic"
"time"
"unicode"
"xappmock/logger"
"xappmock/models"
"xappmock/rmr"
)
var counter uint64
type JsonSender struct {
logger *logger.Logger
}
func NewJsonSender(logger *logger.Logger) *JsonSender {
return &JsonSender{
logger: logger,
}
}
func (s *JsonSender) SendJsonRmrMessage(command models.JsonCommand /*the copy is modified locally*/, xAction *[]byte, r *rmr.Service) error {
var payload []byte
_, err := fmt.Sscanf(command.PackedPayload, "%x", &payload)
if err != nil {
return errors.New(fmt.Sprintf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err))
}
command.PackedPayload = string(payload)
command.TransactionId = expandTransactionId(command.TransactionId)
if len(command.TransactionId) == 0 {
command.TransactionId = string(*xAction)
}
command.PayloadHeader = s.expandPayloadHeader(command.PayloadHeader, &command)
s.logger.Infof("#JsonSender.SendJsonRmrMessage - command payload header: %s", command.PayloadHeader)
rmrMsgId, err := rmr.MessageIdToUint(command.RmrMessageType)
if err != nil {
return errors.New(fmt.Sprintf("invalid rmr message id: %s", command.RmrMessageType))
}
msg := append([]byte(command.PayloadHeader), payload...)
messageInfo := models.NewMessageInfo(int(rmrMsgId), command.RanName, msg, []byte(command.TransactionId))
s.logger.Infof("#JsonSender.SendJsonRmrMessage - going to send message: %s", messageInfo)
_, err = r.SendMessage(int(rmrMsgId), command.RanName, msg, []byte(command.TransactionId))
return err
}
/*
* transactionId (xAction): The value may have a fixed value or $ or <prefix>$.
* $ is replaced by a value generated at runtime (possibly unique per message sent).
* If the tag does not exist, then the mock shall use the value taken from the incoming message.
*/
func expandTransactionId(id string) string {
if len(id) == 1 && id[0] == '$' {
return fmt.Sprintf("%d", incAndGetCounter())
}
if len(id) > 1 && id[len(id)-1] == '$' {
return fmt.Sprintf("%s%d", id[:len(id)-1], incAndGetCounter())
}
return id
}
/*
* payloadHeader: A prefix to combine with the payload that will be the messages payload. The value may include variables of the format $<name> or #<name> where:
* $<name> expands to the value of <name> if it exists or the empty string if not.
* #<name> expands to the length of the value of <name> if it exists or omitted if not.
* The intention is to allow the Mock to construct the payload header required by the setup messages (ranIp|ranPort|ranName|payload len|<payload>).
* Example: “payloadHeader”: “$ranIp|$ranPort|$ranName|#packedPayload|”
*/
func (s *JsonSender) expandPayloadHeader(header string, command *models.JsonCommand) string {
var name strings.Builder
var expandedHeader strings.Builder
r := strings.NewReader(header)
ch, err := r.ReadByte()
for {
if err != nil {
break
}
switch ch {
case '$':
for {
ch, err = r.ReadByte() //on error ch == 0
if unicode.IsDigit(rune(ch)) || unicode.IsLetter(rune(ch)) {
if name.Len() == 0 {
name.WriteByte(byte(unicode.ToUpper(rune(ch))))
} else {
name.WriteByte(ch)
}
} else {
if fieldValue := reflect.Indirect(reflect.ValueOf(command)).FieldByName(name.String()); fieldValue.IsValid() {
switch fieldValue.Kind() {
case reflect.String:
expandedHeader.WriteString(fieldValue.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
expandedHeader.WriteString(strconv.FormatInt(fieldValue.Int(), 10))
case reflect.Bool:
expandedHeader.WriteString(strconv.FormatBool(fieldValue.Bool()))
case reflect.Float64, reflect.Float32:
expandedHeader.WriteString(fmt.Sprintf("%g", fieldValue.Float()))
default:
s.logger.Errorf("#JsonSender.expandPayloadHeader - invalid type for $%s, value must be a string, an int, a bool or a float", name.String())
os.Exit(1)
}
}
name.Reset()
break
}
}
case '#':
for {
ch, err = r.ReadByte() //on error ch == 0
if unicode.IsDigit(rune(ch)) || unicode.IsLetter(rune(ch)) {
if name.Len() == 0 {
name.WriteByte(byte(unicode.ToUpper(rune(ch))))
} else {
name.WriteByte(ch)
}
} else {
if fieldValue := reflect.Indirect(reflect.ValueOf(command)).FieldByName(name.String()); fieldValue.IsValid() {
if fieldValue.Kind() == reflect.String {
expandedHeader.WriteString(strconv.FormatInt(int64(len(fieldValue.String())), 10))
} else {
s.logger.Errorf("#JsonSender.expandPayloadHeader - invalid type for #%s, value must be a string", name.String())
os.Exit(1)
}
}
name.Reset()
break
}
}
default:
if unicode.IsPrint(rune(ch)) {
expandedHeader.WriteByte(ch)
}
ch, err = r.ReadByte()
}
}
return expandedHeader.String()
}
func incAndGetCounter() uint64 {
return atomic.AddUint64(&counter, 1)
}
func init() {
counter = uint64(time.Now().Unix() - 1572000000)
}