From 6579f78d0b544ffdf9cbf4049eb47a64758f5a71 Mon Sep 17 00:00:00 2001 From: "naman.gupta" Date: Mon, 2 May 2022 15:34:31 +0530 Subject: [PATCH] Get Policy Type Instance Api for Get Policy Type Instance Signed-off-by: naman.gupta Issue-ID: RIC-831 Change-Id: I649dd94059263244645e7a8236ad058c3f0bf86e --- a1-go/pkg/restful/restful.go | 11 +++++++++ a1-go/pkg/resthooks/resthooks.go | 46 +++++++++++++++++++++++++++++++++++ a1-go/pkg/resthooks/resthooks_test.go | 27 ++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/a1-go/pkg/restful/restful.go b/a1-go/pkg/restful/restful.go index b494dab..deb2470 100644 --- a/a1-go/pkg/restful/restful.go +++ b/a1-go/pkg/restful/restful.go @@ -84,6 +84,17 @@ func (r *Restful) setupHandler() *operations.A1API { }) + api.A1MediatorA1ControllerGetPolicyInstanceHandler = a1_mediator.A1ControllerGetPolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyInstanceParams) middleware.Responder { + a1.Logger.Debug("handler for get policy instance from policytypeID") + if resp, err := r.rh.GetPolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err == nil { + return a1_mediator.NewA1ControllerGetPolicyInstanceOK().WithPayload(resp) + } + if r.rh.IsPolicyInstanceNotFound(err) { + return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound + } + return a1_mediator.NewA1ControllerGetPolicyInstanceServiceUnavailable + }) + return api } diff --git a/a1-go/pkg/resthooks/resthooks.go b/a1-go/pkg/resthooks/resthooks.go index 43011a4..c09fb14 100644 --- a/a1-go/pkg/resthooks/resthooks.go +++ b/a1-go/pkg/resthooks/resthooks.go @@ -330,3 +330,49 @@ func (rh *Resthook) CreatePolicyInstance(policyTypeId models.PolicyTypeID, polic return nil } + +func (rh *Resthook) GetPolicyInstance(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID) (interface{}, error) { + a1.Logger.Debug("GetPolicyInstance1") + + var keys [1]string + + typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + keys[0] = typekey + + a1.Logger.Debug("key1 : %+v", typekey) + + valmap, err := rh.db.Get(a1MediatorNs, keys[:]) + if len(valmap) == 0 { + a1.Logger.Debug("policy type Not Present for policyid : %v", policyTypeId) + return "{}", policyTypeNotFoundError + } + + if err != nil { + a1.Logger.Error("error in retrieving policy type. err: %v", err) + return "{}", err + } + + if valmap[typekey] == nil { + a1.Logger.Debug("policy type Not Present for policyid : %v", policyTypeId) + return "{}", policyTypeNotFoundError + } + + a1.Logger.Debug("keysmap : %+v", valmap[typekey]) + + instancekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + a1.Logger.Debug("key2 : %+v", instancekey) + keys[0] = instancekey + instanceMap, err := rh.db.Get(a1MediatorNs, keys[:]) + if err != nil { + a1.Logger.Error("policy instance error : %v", err) + } + a1.Logger.Debug("policyinstancetype map : %+v", instanceMap) + + if instanceMap[instancekey] == nil { + a1.Logger.Debug("policy instance Not Present for policyinstaneid : %v", policyInstanceID) + return "{}", policyInstanceNotFoundError + } + + valStr := fmt.Sprint(instanceMap[instancekey]) + return valStr, nil +} diff --git a/a1-go/pkg/resthooks/resthooks_test.go b/a1-go/pkg/resthooks/resthooks_test.go index cef23ea..50a7fe2 100644 --- a/a1-go/pkg/resthooks/resthooks_test.go +++ b/a1-go/pkg/resthooks/resthooks_test.go @@ -134,6 +134,33 @@ func TestCreatePolicyTypeInstance(t *testing.T) { sdlInst.AssertExpectations(t) } +func TestGetPolicyInstance(t *testing.T) { + + var policyTypeId models.PolicyTypeID + policyTypeId = 20001 + var policyInstanceID models.PolicyInstanceID + policyInstanceID = "123456" + httpBody := `{ + "enforce":true, + "window_length":20, + "blocking_rate":20, + "trigger_threshold":10 + }` + instancekey := a1PolicyPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) + a1.Logger.Debug("httpBody String : %+v", httpBody) + a1.Logger.Debug("key : %+v", instancekey) + var keys [1]string + keys[0] = instancekey + //Setup Expectations + sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody, nil) + + resp := rh.GetPolicyInstance(policyTypeId, policyInstanceID) + a1.Logger.Error("resp : %+v", resp) + assert.NotNil(t, resp) + + sdlInst.AssertExpectations(t) +} + type SdlMock struct { mock.Mock } -- 2.16.6