Error handling in get all policy instance api 06/8406/3
authornaman.gupta <naman.gupta@samsung.com>
Wed, 25 May 2022 18:01:47 +0000 (23:31 +0530)
committernaman.gupta <naman.gupta@samsung.com>
Mon, 13 Jun 2022 12:57:49 +0000 (18:27 +0530)
Error handling in get all policy instance api

Signed-off-by: naman.gupta <naman.gupta@samsung.com>
Change-Id: I773abbb07d2c2b719acec81be17580171d1833c1

a1-go/pkg/restful/restful.go
a1-go/pkg/resthooks/resthooks.go

index 28efaaa..b7e2094 100644 (file)
@@ -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
index ea02562..5e704be 100644 (file)
@@ -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
 }