Error handling in get all policy instance api

Error handling in get all policy instance api

Signed-off-by: naman.gupta <naman.gupta@samsung.com>
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 @@
 
 	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 @@
 	"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 @@
 	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 @@
 	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 @@
 	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 @@
 		}
 	}
 
+	if len(policyTypeInstances)==0{
+		a1.Logger.Debug("policy instance Not Present  ")
+	}
+
 	a1.Logger.Debug("return : %+v", policyTypeInstances)
-	return policyTypeInstances
+	return policyTypeInstances ,nil
 }