From: naman.gupta Date: Wed, 25 May 2022 18:01:47 +0000 (+0530) Subject: Error handling in get all policy instance api X-Git-Tag: 2.5.2~2 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=44f5c8a90e9aabb096d8f6100e49f34eb4f89b01;p=ric-plt%2Fa1.git Error handling in get all policy instance api Error handling in get all policy instance api Signed-off-by: naman.gupta Change-Id: I773abbb07d2c2b719acec81be17580171d1833c1 --- diff --git a/a1-go/pkg/restful/restful.go b/a1-go/pkg/restful/restful.go index 28efaaa..b7e2094 100644 --- a/a1-go/pkg/restful/restful.go +++ b/a1-go/pkg/restful/restful.go @@ -97,7 +97,16 @@ func (r *Restful) setupHandler() *operations.A1API { api.A1MediatorA1ControllerGetAllInstancesForTypeHandler = a1_mediator.A1ControllerGetAllInstancesForTypeHandlerFunc(func(params a1_mediator.A1ControllerGetAllInstancesForTypeParams) middleware.Responder { a1.Logger.Debug("handler for get all policy instance") - return a1_mediator.NewA1ControllerGetPolicyInstanceOK().WithPayload(r.rh.GetAllPolicyInstance(models.PolicyTypeID(params.PolicyTypeID))) + if resp, err := r.rh.GetAllPolicyInstance(models.PolicyTypeID(params.PolicyTypeID)); err == nil { + if resp != nil { + return a1_mediator.NewA1ControllerGetAllInstancesForTypeOK().WithPayload(resp) + } + } + if r.rh.IsPolicyInstanceNotFound(err) { + return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound() + } + return a1_mediator.NewA1ControllerGetAllInstancesForTypeServiceUnavailable() + }) return api diff --git a/a1-go/pkg/resthooks/resthooks.go b/a1-go/pkg/resthooks/resthooks.go index ea02562..5e704be 100644 --- a/a1-go/pkg/resthooks/resthooks.go +++ b/a1-go/pkg/resthooks/resthooks.go @@ -30,6 +30,7 @@ import ( "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" + "github.com/santhosh-tekuri/jsonschema/v5" "gopkg.in/yaml.v2" ) @@ -301,6 +302,7 @@ func (rh *Resthook) StorePolicyInstance(policyTypeId models.PolicyTypeID, policy return operation, nil } + func (rh *Resthook) CreatePolicyInstance(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID, httpBody interface{}) error { a1.Logger.Debug("CreatePolicyInstance function") // validate the PUT against the schema @@ -317,7 +319,8 @@ func (rh *Resthook) CreatePolicyInstance(policyTypeId models.PolicyTypeID, polic httpBodyString := fmt.Sprint((httpBody)) isvalid := validate(httpBodyString, schemaString) if isvalid { - operation, err := rh.StorePolicyInstance(policyTypeId, policyInstanceID, httpBody) + var operation string + operation, err = rh.StorePolicyInstance(policyTypeId, policyInstanceID, httpBody) if err != nil { a1.Logger.Error("error :%+v", err) return err @@ -377,18 +380,19 @@ func (rh *Resthook) GetPolicyInstance(policyTypeId models.PolicyTypeID, policyIn return valStr, nil } -func (rh *Resthook) GetAllPolicyInstance(policyTypeId models.PolicyTypeID) []models.PolicyInstanceID { +func (rh *Resthook) GetAllPolicyInstance(policyTypeId models.PolicyTypeID) []models.PolicyInstanceID ,error { a1.Logger.Debug("GetAllPolicyInstance") - var policyTypeInstances []models.PolicyInstanceID + var policyTypeInstances = []models.PolicyInstanceID{} keys, err := rh.db.GetAll("A1m_ns") if err != nil { a1.Logger.Error("error in retrieving policy. err: %v", err) - return policyTypeInstances + return policyTypeInstances ,err } a1.Logger.Debug("keys : %+v", keys) typekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + for _, key := range keys { if strings.HasPrefix(strings.TrimLeft(key, " "), typekey) { pti := strings.Split(strings.Trim(key, " "), typekey)[1] @@ -397,6 +401,10 @@ func (rh *Resthook) GetAllPolicyInstance(policyTypeId models.PolicyTypeID) []mod } } + if len(policyTypeInstances)==0{ + a1.Logger.Debug("policy instance Not Present ") + } + a1.Logger.Debug("return : %+v", policyTypeInstances) - return policyTypeInstances + return policyTypeInstances ,nil }