Delete Policy Type Api
[ric-plt/a1.git] / a1-go / pkg / resthooks / resthooks.go
index 8ed8a08..8d30b3d 100644 (file)
@@ -26,21 +26,22 @@ import (
        "fmt"
        "strconv"
        "strings"
+       "time"
 
        "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/rmr"
        "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
-
        "github.com/santhosh-tekuri/jsonschema/v5"
        "gopkg.in/yaml.v2"
 )
 
 const (
-       a1PolicyPrefix   = "a1.policy_type."
-       a1MediatorNs     = "A1m_ns"
-       a1InstancePrefix = "a1.policy_instance."
+       a1PolicyPrefix           = "a1.policy_type."
+       a1MediatorNs             = "A1m_ns"
+       a1InstancePrefix         = "a1.policy_instance."
        a1InstanceMetadataPrefix = "a1.policy_inst_metadata."
-       a1HandlerPrefix = "a1.policy_handler."
+       a1HandlerPrefix          = "a1.policy_handler."
 )
 
 var typeAlreadyError = errors.New("Policy Type already exists")
@@ -49,6 +50,11 @@ var typeMismatchError = errors.New("Policytype Mismatch")
 var invalidJsonSchema = errors.New("Invalid Json ")
 var policyInstanceNotFoundError = errors.New("Policy Instance Not Found")
 var policyTypeNotFoundError = errors.New("Policy Type Not Found")
+var policyTypeCanNotBeDeletedError = errors.New("tried to delete a type that isn't empty")
+
+func (rh *Resthook) CanPolicyTypeBeDeleted(err error) bool {
+       return err == policyTypeCanNotBeDeletedError
+}
 
 func (rh *Resthook) IsPolicyTypePresent(err error) bool {
        return err == policyTypeNotFoundError
@@ -72,12 +78,13 @@ func (rh *Resthook) IsValidJson(err error) bool {
        return err == invalidJsonSchema
 }
 func NewResthook() *Resthook {
-       return createResthook(sdlgo.NewSyncStorage())
+       return createResthook(sdlgo.NewSyncStorage(), rmr.NewRMRSender())
 }
 
-func createResthook(sdlInst iSdl) *Resthook {
+func createResthook(sdlInst iSdl, rmrSenderInst rmr.IRmrSender) *Resthook {
        return &Resthook{
-               db: sdlInst,
+               db:             sdlInst,
+               iRmrSenderInst: rmrSenderInst,
        }
 }
 
@@ -118,7 +125,7 @@ func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.Poli
 
        valmap, err := rh.db.Get(a1MediatorNs, keys[:])
 
-       a1.Logger.Debug("policytype map : ", valmap)
+       a1.Logger.Debug("policytype map : %+v", valmap)
 
        if len(valmap) == 0 {
                a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId)
@@ -183,8 +190,6 @@ func (rh *Resthook) CreatePolicyType(policyTypeId models.PolicyTypeID, httpreque
        return nil
 }
 
-
-
 func toStringKeys(val interface{}) (interface{}, error) {
        var err error
        switch val := val.(type) {
@@ -244,7 +249,7 @@ func validate(httpBodyString string, schemaString string) bool {
        return true
 }
 
-func (rh *Resthook) StorePolicyInstance(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID, httpBody interface{}) (string, error) {
+func (rh *Resthook) storePolicyInstance(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID, httpBody interface{}) (string, error) {
        var keys [1]string
        operation := "CREATE"
        typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
@@ -344,24 +349,34 @@ func (rh *Resthook) CreatePolicyInstance(policyTypeId models.PolicyTypeID, polic
        a1.Logger.Debug("schema to validate %+v", string(schemaStr))
        a1.Logger.Debug("httpbody to validate %+v", httpBody)
        schemaString := fmt.Sprint(string(schemaStr))
-       httpBodyString := fmt.Sprint((httpBody))
+       httpBodyMarshal, err := json.Marshal(httpBody)
+       httpBodyString := string((httpBodyMarshal))
+       a1.Logger.Debug("schema to validate sprint  %+v", (schemaString))
+       a1.Logger.Debug("httpbody to validate sprint %+v", httpBodyString)
        isvalid := validate(httpBodyString, schemaString)
        if isvalid {
                var operation string
-               operation, err = rh.StorePolicyInstance(policyTypeId, policyInstanceID, httpBody)
+               operation, err = rh.storePolicyInstance(policyTypeId, policyInstanceID, httpBody)
                if err != nil {
                        a1.Logger.Error("error :%+v", err)
                        return err
                }
                a1.Logger.Debug("policy instance :%+v", operation)
-               iscreated,errmetadata := rh.StorePolicyInstanceMetadata(policyTypeId, policyInstanceID)
+               iscreated, errmetadata := rh.storePolicyInstanceMetadata(policyTypeId, policyInstanceID)
                if errmetadata != nil {
                        a1.Logger.Error("error :%+v", errmetadata)
                        return errmetadata
                }
                if iscreated {
-               a1.Logger.Debug("policy instance metadata created")
+                       a1.Logger.Debug("policy instance metadata created")
+               }
+               isSent := rh.iRmrSenderInst.RmrSendToXapp(httpBodyString)
+               if isSent {
+                       a1.Logger.Debug("rmrSendToXapp : message sent")
+               } else {
+                       a1.Logger.Debug("rmrSendToXapp : message not sent")
                }
+
        } else {
                a1.Logger.Error("%+v", invalidJsonSchema)
                return invalidJsonSchema
@@ -416,15 +431,15 @@ func (rh *Resthook) GetPolicyInstance(policyTypeId models.PolicyTypeID, policyIn
        return valStr, nil
 }
 
-func (rh *Resthook) GetAllPolicyInstance(policyTypeId models.PolicyTypeID) []models.PolicyInstanceID ,error {
+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 ,err
+               return policyTypeInstanceserr
        }
        a1.Logger.Debug("keys : %+v", keys)
        typekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "."
@@ -437,10 +452,35 @@ func (rh *Resthook) GetAllPolicyInstance(policyTypeId models.PolicyTypeID) []mod
                }
        }
 
-       if len(policyTypeInstances)==0{
+       if len(policyTypeInstances) == 0 {
                a1.Logger.Debug("policy instance Not Present  ")
        }
 
        a1.Logger.Debug("return : %+v", policyTypeInstances)
-       return policyTypeInstances ,nil
+       return policyTypeInstances, nil
+}
+
+func (rh *Resthook) DeletePolicyType(policyTypeId models.PolicyTypeID) error {
+       a1.Logger.Debug("DeletePolicyType")
+
+       policyinstances, err := rh.GetAllPolicyInstance(policyTypeId)
+       if err != nil {
+               a1.Logger.Error("error in retrieving policy. err: %v", err)
+               return err
+       }
+
+       var keys [1]string
+       key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
+       keys[0] = key
+       if len(policyinstances) == 0 {
+               err := rh.db.Remove(a1MediatorNs, keys[:])
+               if err != nil {
+                       a1.Logger.Error("error in deleting policy type err: %v", err)
+                       return err
+               }
+       } else {
+               a1.Logger.Error("tried to delete a type that isn't empty")
+               return policyTypeCanNotBeDeletedError
+       }
+       return nil
 }