From ab3e9ee563b7467bcda8ebbc4187b971690599d2 Mon Sep 17 00:00:00 2001 From: "naman.gupta" Date: Fri, 18 Feb 2022 18:03:36 +0530 Subject: [PATCH] Creation of Policy Type Schema Api for Creation of Policy Type Schema Signed-off-by: naman.gupta Change-Id: I649dd94059263244645e7a8236ad058c3f0df95f --- a1-go/pkg/restful/restful.go | 27 ++++++++++++++---- a1-go/pkg/resthooks/resthooks.go | 54 ++++++++++++++++++++++++++++++----- a1-go/pkg/resthooks/resthooks_test.go | 22 ++++++++------ a1-go/pkg/resthooks/types.go | 1 + 4 files changed, 82 insertions(+), 22 deletions(-) diff --git a/a1-go/pkg/restful/restful.go b/a1-go/pkg/restful/restful.go index 56f2360..843a64c 100644 --- a/a1-go/pkg/restful/restful.go +++ b/a1-go/pkg/restful/restful.go @@ -24,11 +24,12 @@ import ( "log" "os" - "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" - "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi" - "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations" - "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations/a1_mediator" - "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/resthooks" + "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/restapi" + "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations" + "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations/a1_mediator" + "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/resthooks" "github.com/go-openapi/loads" "github.com/go-openapi/runtime/middleware" ) @@ -49,9 +50,23 @@ func (r *Restful) setupHandler() *operations.A1API { api := operations.NewA1API(swaggerSpec) api.A1MediatorA1ControllerGetAllPolicyTypesHandler = a1_mediator.A1ControllerGetAllPolicyTypesHandlerFunc(func(param a1_mediator.A1ControllerGetAllPolicyTypesParams) middleware.Responder { - a1.Logger.Debug("handler for get all all policy type") + a1.Logger.Debug("handler for get all policy type") return a1_mediator.NewA1ControllerGetAllPolicyTypesOK().WithPayload(r.rh.GetAllPolicyType()) }) + + api.A1MediatorA1ControllerCreatePolicyTypeHandler = a1_mediator.A1ControllerCreatePolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerCreatePolicyTypeParams) middleware.Responder { + a1.Logger.Debug("handler for get policy type from policytypeID") + if err = r.rh.CreatePolicyType(models.PolicyTypeID(params.PolicyTypeID), *params.Body); err == nil { + //Increase prometheus counter + return a1_mediator.NewA1ControllerCreatePolicyTypeCreated() + } + if r.rh.IsTypeAlready(err) || r.rh.IsTypeMismatch(err) { + return a1_mediator.NewA1ControllerCreatePolicyTypeBadRequest() + } + return a1_mediator.NewA1ControllerCreatePolicyTypeServiceUnavailable() + + }) + return api } diff --git a/a1-go/pkg/resthooks/resthooks.go b/a1-go/pkg/resthooks/resthooks.go index 5194f37..0a832ff 100644 --- a/a1-go/pkg/resthooks/resthooks.go +++ b/a1-go/pkg/resthooks/resthooks.go @@ -21,14 +21,29 @@ package resthooks import ( + "errors" "strconv" "strings" - "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/a1" + "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models" "gerrit.o-ran-sc.org/r/ric-plt/sdlgo" ) +const ( + a1PolicyPrefix = "a1.policy_type." + a1MediatorNs = "A1m_ns" +) + +var typeAlreadyError = errors.New("Policy Type already exists") +var typeMismatchError = errors.New("Policytype Mismatch") + +func (rh *Resthook) IsTypeAlready(err error) bool { + return err == typeAlreadyError +} +func (rh *Resthook) IsTypeMismatch(err error) bool { + return err == typeMismatchError +} func NewResthook() *Resthook { return createResthook(sdlgo.NewSyncStorage()) } @@ -46,19 +61,44 @@ func (rh *Resthook) GetAllPolicyType() []models.PolicyTypeID { keys, err := rh.db.GetAll("A1m_ns") if err != nil { - a1.Logger.Error("error in retrieving policy. err: %v", err) + a1.Logger.Error("error in retrieving policy. err: %v", err) return policyTypeIDs } - a1.Logger.Debug("keys : %+v", keys) + a1.Logger.Debug("keys : %+v", keys) for _, key := range keys { - if strings.HasPrefix(strings.TrimLeft(key, " "), "a1.policy_type.") { - pti := strings.Split(strings.Trim(key, " "), "a1.policy_type.")[1] + if strings.HasPrefix(strings.TrimLeft(key, " "), a1PolicyPrefix) { + pti := strings.Split(strings.Trim(key, " "), a1PolicyPrefix)[1] ptii, _ := strconv.ParseInt(pti, 10, 64) policyTypeIDs = append(policyTypeIDs, models.PolicyTypeID(ptii)) } } - a1.Logger.Debug("return : %+v", policyTypeIDs) + a1.Logger.Debug("return : %+v", policyTypeIDs) return policyTypeIDs } + +func (rh *Resthook) CreatePolicyType(policyTypeId models.PolicyTypeID, httprequest models.PolicyTypeSchema) error { + a1.Logger.Debug("CreatePolicyType function") + if policyTypeId != models.PolicyTypeID(*httprequest.PolicyTypeID) { + //error message + a1.Logger.Debug("Policytype Mismatch") + return typeMismatchError + } + key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + a1.Logger.Debug("key %+v ", key) + if data, err := httprequest.MarshalBinary(); err == nil { + a1.Logger.Debug("Marshaled String : %+v", string(data)) + success, err1 := rh.db.SetIfNotExists(a1MediatorNs, key, string(data)) + a1.Logger.Info("success:%+v", success) + if err1 != nil { + a1.Logger.Error("error :%+v", err1) + return err1 + } + if !success { + a1.Logger.Debug("Policy type %+v already exist", policyTypeId) + return typeAlreadyError + } + } + return nil +} diff --git a/a1-go/pkg/resthooks/resthooks_test.go b/a1-go/pkg/resthooks/resthooks_test.go index 6b7d2ce..e74ed3a 100644 --- a/a1-go/pkg/resthooks/resthooks_test.go +++ b/a1-go/pkg/resthooks/resthooks_test.go @@ -24,7 +24,7 @@ import ( "os" "testing" - "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" + "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) @@ -35,13 +35,13 @@ var sdlInst *SdlMock func TestMain(m *testing.M) { sdlInst = new(SdlMock) - sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos", - "a1.policy_type.1006001", - "a1.policy_type.20000", - "a1.policy_inst_metadata.1006001.qos", - }, nil) + sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos", + "a1.policy_type.1006001", + "a1.policy_type.20000", + "a1.policy_inst_metadata.1006001.qos", + }, nil) - a1.Init() + a1.Init() rh = createResthook(sdlInst) code := m.Run() os.Exit(code) @@ -57,6 +57,10 @@ type SdlMock struct { } func (s *SdlMock) GetAll(ns string) ([]string, error) { - args := s.MethodCalled("GetAll", ns) - return args.Get(0).([]string), nil + args := s.MethodCalled("GetAll", ns) + return args.Get(0).([]string), nil +} + +func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) { + return true, nil } diff --git a/a1-go/pkg/resthooks/types.go b/a1-go/pkg/resthooks/types.go index 778cb17..f18ca57 100644 --- a/a1-go/pkg/resthooks/types.go +++ b/a1-go/pkg/resthooks/types.go @@ -26,4 +26,5 @@ type Resthook struct { type iSdl interface { GetAll(string) ([]string, error) + SetIfNotExists(ns string, key string, data interface{}) (bool, error) } -- 2.16.6