Merge "Adding configuration for rmr"
authorsubhash kumar singh <subh.singh@samsung.com>
Fri, 12 May 2023 11:31:06 +0000 (11:31 +0000)
committerGerrit Code Review <gerrit@o-ran-sc.org>
Fri, 12 May 2023 11:31:06 +0000 (11:31 +0000)
config/configuration.go [new file with mode: 0644]
go.mod
pkg/resthooks/resthooks.go
pkg/resthooks/resthooks_test.go
pkg/rmr/rmr.go

diff --git a/config/configuration.go b/config/configuration.go
new file mode 100644 (file)
index 0000000..e67083a
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+==================================================================================
+  Copyright (c) 2023 Samsung
+
+   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 config
+
+import (
+       "os"
+
+       "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
+       "github.com/spf13/viper"
+)
+
+type Configuration struct {
+       LogLevel          string
+       Name              string
+       MaxSize           int
+       ThreadType        int
+       LowLatency        bool
+       FastAck           bool
+       MaxRetryOnFailure int
+       Port              int
+}
+
+func ParseConfiguration() *Configuration {
+       viper.SetConfigType("yaml")
+       viper.SetConfigName("configuration")
+       configFile := os.Getenv("A1_CONFIG_FILE")
+       viper.SetConfigFile(configFile)
+       err := viper.ReadInConfig()
+       if err != nil {
+               a1.Logger.Info("#configuration.ParseConfiguration - failed to read configuration file: %s\n", err)
+       }
+
+       config := Configuration{}
+       config.LogLevel = viper.GetString("log-level")
+       viper.SetDefault("log-level", "info")
+       config.Name = viper.GetString("NAME")
+       viper.SetDefault("NAME", "")
+       config.MaxSize = viper.GetInt("MAX_SIZE")
+       viper.SetDefault("MAX_SIZE", 65534)
+       config.ThreadType = viper.GetInt("THREAD_TYPE")
+       viper.SetDefault("THREAD_TYPE", 0)
+       config.LowLatency = viper.GetBool("LOW_LATENCY")
+       viper.SetDefault("LOW_LATENCY", false)
+       config.FastAck = viper.GetBool("FAST_ACK")
+       viper.SetDefault("FAST_ACK", false)
+       config.MaxRetryOnFailure = viper.GetInt("MAX_RETRY_ON_FAILURE")
+       viper.SetDefault("MAX_RETRY_ON_FAILURE", 1)
+       config.Port = viper.GetInt("PORT")
+       viper.SetDefault("PORT", 4561)
+       return &config
+}
diff --git a/go.mod b/go.mod
index a78b02b..1f58f26 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -44,6 +44,7 @@ require (
        github.com/stretchr/testify v1.6.1
        golang.org/x/net v0.0.0-20210428140749-89ef3d95e781
        gopkg.in/yaml.v2 v2.4.0
+       github.com/spf13/viper v1.7.0
 )
 
 require (
index 49df6f4..79e52fa 100644 (file)
@@ -396,7 +396,7 @@ func (rh *Resthook) CreatePolicyInstance(policyTypeId models.PolicyTypeID, polic
                        a1.Logger.Error("error : %v", err)
                        return err
                }
-               isSent := rh.iRmrSenderInst.RmrSendToXapp(rmrMessage, a1PolicyRequest)
+               isSent := rh.iRmrSenderInst.RmrSendToXapp(rmrMessage, a1PolicyRequest, int(policyTypeId))
                if isSent {
                        a1.Logger.Debug("rmrSendToXapp : message sent")
                } else {
@@ -713,7 +713,7 @@ func (rh *Resthook) DeletePolicyInstance(policyTypeId models.PolicyTypeID, polic
                a1.Logger.Error("error : %v", err1)
                return err1
        }
-       isSent := rh.iRmrSenderInst.RmrSendToXapp(rmrMessage, a1PolicyRequest)
+       isSent := rh.iRmrSenderInst.RmrSendToXapp(rmrMessage, a1PolicyRequest, int(policyTypeId))
        if isSent {
                a1.Logger.Debug("rmrSendToXapp : message sent")
        } else {
@@ -734,7 +734,7 @@ func (rh *Resthook) DataDelivery(httpBody interface{}) error {
                return err
        }
        a1.Logger.Debug("rmrSendToXapp :rmrMessage %+v", rmrMessage)
-       isSent := rh.iRmrSenderInst.RmrSendToXapp(rmrMessage, a1EIDataDelivery)
+       isSent := rh.iRmrSenderInst.RmrSendToXapp(rmrMessage, a1EIDataDelivery, rmr.DefaultSubId)
        if isSent {
                a1.Logger.Debug("rmrSendToXapp : message sent")
        } else {
index 01d11e7..11afe47 100644 (file)
@@ -155,7 +155,7 @@ func TestCreatePolicyTypeInstance(t *testing.T) {
        a1.Logger.Debug("metadatainstancekey   : %+v", metadatainstancekey)
        metadatainstancearr := []interface{}{metadatainstancekey, string(metadata)}
        sdlInst.On("Set", "A1m_ns", metadatainstancearr).Return(nil)
-       rmrSenderInst.On("RmrSendToXapp", "httpBodyString", 20010).Return(true)
+       rmrSenderInst.On("RmrSendToXapp", "httpBodyString", 20010, int(policyTypeId)).Return(true)
 
        errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, instancedata)
 
@@ -303,7 +303,7 @@ func TestDeletePolicyInstance(t *testing.T) {
 
        httpBodyString := `{"operation":"DELETE","payload":"","policy_instance_id":"123456","policy_type_id":"20001"}`
 
-       rmrSenderInst.On("RmrSendToXapp", httpBodyString, 20010).Return(true)
+       rmrSenderInst.On("RmrSendToXapp", httpBodyString, 20010, int(policyTypeId)).Return(true)
 
        errresp := rh.DeletePolicyInstance(policyTypeId, policyInstanceID)
 
@@ -322,7 +322,7 @@ func TestDataDelivery(t *testing.T) {
        json.Unmarshal([]byte(httpBody), &instancedata)
        a1.Logger.Debug("Marshaled data : %+v", (instancedata))
        httpBodyString := `{"ei_job_id":"1","payload":"payload"}`
-       rmrSenderInst.On("RmrSendToXapp", httpBodyString, 20017).Return(true)
+       rmrSenderInst.On("RmrSendToXapp", httpBodyString, 20017, -1).Return(true)
        errresp := rh.DataDelivery(instancedata)
 
        assert.Nil(t, errresp)
@@ -412,12 +412,12 @@ func (s *SdlMock) SetIf(ns string, key string, oldData, newData interface{}) (bo
        return args.Bool(0), args.Error(1)
 }
 
-func (rmr *RmrSenderMock) RmrSendToXapp(httpBodyString string, mtype int) bool {
+func (rmr *RmrSenderMock) RmrSendToXapp(httpBodyString string, mtype int, subid int) bool {
        if httpBodyString == `{"blocking_rate":20,"enforce":true,"trigger_threshold":10,"window_length":20}` {
-               args := rmr.MethodCalled("RmrSendToXapp", httpBodyString, mtype)
+               args := rmr.MethodCalled("RmrSendToXapp", httpBodyString, mtype, subid)
                return args.Bool(0)
        } else if httpBodyString == `{"ei_job_id":"1","payload":"payload"}` {
-               args := rmr.MethodCalled("RmrSendToXapp", httpBodyString, mtype)
+               args := rmr.MethodCalled("RmrSendToXapp", httpBodyString, mtype, subid)
                return args.Bool(0)
        }
        return true
index c0366af..d9ec0f0 100644 (file)
@@ -29,6 +29,7 @@ import (
        "net/http"
        "strconv"
 
+       "gerrit.o-ran-sc.org/r/ric-plt/a1/config"
        "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
        "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
        "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/policy"
@@ -44,6 +45,7 @@ const (
        a1EiQueryAllResp  = 20014
        a1EiCreateJobResp = 20016
        jobCreationData   = `{"ei_job_id": %s.}`
+       DefaultSubId      = -1
 )
 
 type RmrSender struct {
@@ -52,21 +54,22 @@ type RmrSender struct {
 }
 
 type IRmrSender interface {
-       RmrSendToXapp(httpBodyString string, messagetype int) bool
+       RmrSendToXapp(httpBodyString string, messagetype int, subid int) bool
 }
 
 func NewRMRSender(policyManager *policy.PolicyManager) IRmrSender {
+       config := config.ParseConfiguration()
        RMRclient := xapp.NewRMRClientWithParams(&xapp.RMRClientParams{
                StatDesc: "",
                RmrData: xapp.PortData{
-                       //TODO: Read configuration from config file
-                       Name:              "",
-                       MaxSize:           65534,
-                       ThreadType:        0,
-                       LowLatency:        false,
-                       FastAck:           false,
-                       MaxRetryOnFailure: 1,
-                       Port:              4561,
+
+                       Name:              config.Name,
+                       MaxSize:           config.MaxSize,
+                       ThreadType:        config.ThreadType,
+                       LowLatency:        config.LowLatency,
+                       FastAck:           config.FastAck,
+                       MaxRetryOnFailure: config.MaxRetryOnFailure,
+                       Port:              config.Port,
                },
        })
 
@@ -99,11 +102,11 @@ func (rmr *RmrSender) GetRicMessageName(id int) (s string) {
        return
 }
 
-func (rmr *RmrSender) RmrSendToXapp(httpBodyString string, messagetype int) bool {
+func (rmr *RmrSender) RmrSendToXapp(httpBodyString string, messagetype int, subid int) bool {
 
        params := &xapp.RMRParams{}
        params.Mtype = messagetype
-       params.SubId = -1
+       params.SubId = subid
        params.Xid = ""
        params.Meid = &xapp.RMRMeid{}
        params.Src = a1SourceName
@@ -164,7 +167,7 @@ func (rmr *RmrSender) Consume(msg *xapp.RMRParams) (err error) {
                                return err1
                        }
                        a1.Logger.Debug("rmrMessage ", rmrMessage)
-                       isSent := rmr.RmrSendToXapp(rmrMessage, a1PolicyRequest)
+                       isSent := rmr.RmrSendToXapp(rmrMessage, a1PolicyRequest, int(policytypeid))
                        if isSent {
                                a1.Logger.Debug("rmrSendToXapp : message sent")
                        } else {
@@ -192,7 +195,7 @@ func (rmr *RmrSender) Consume(msg *xapp.RMRParams) (err error) {
 
                a1.Logger.Debug("response : %+v", string(respByte))
 
-               isSent := rmr.RmrSendToXapp(string(respByte), a1EiQueryAllResp)
+               isSent := rmr.RmrSendToXapp(string(respByte), a1EiQueryAllResp, DefaultSubId)
                if isSent {
                        a1.Logger.Debug("rmrSendToXapp : message sent")
                } else {
@@ -241,7 +244,7 @@ func (rmr *RmrSender) Consume(msg *xapp.RMRParams) (err error) {
                        rmrData := fmt.Sprintf(jobCreationData, jobIdStr)
                        a1.Logger.Debug("rmr_Data to send: ", rmrData)
 
-                       isSent := rmr.RmrSendToXapp(rmrData, a1EiCreateJobResp)
+                       isSent := rmr.RmrSendToXapp(rmrData, a1EiCreateJobResp, DefaultSubId)
                        if isSent {
                                a1.Logger.Debug("rmrSendToXapp : message sent")
                        } else {