X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=a1-go%2Fpkg%2Fresthooks%2Fresthooks.go;h=8ed8a08d87cec2e4c88f0320e424fc94a5fb4ff2;hb=f1e014de1ceeb2dd1c88f29c2eb3663d2cd90d2a;hp=ea02562c1fdc9af5fe00d2210fc3955690d43414;hpb=05a0f585da0bbf0c931d5667692807c512bb3238;p=ric-plt%2Fa1.git diff --git a/a1-go/pkg/resthooks/resthooks.go b/a1-go/pkg/resthooks/resthooks.go index ea02562..8ed8a08 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" ) @@ -38,6 +39,8 @@ const ( a1PolicyPrefix = "a1.policy_type." a1MediatorNs = "A1m_ns" a1InstancePrefix = "a1.policy_instance." + a1InstanceMetadataPrefix = "a1.policy_inst_metadata." + a1HandlerPrefix = "a1.policy_handler." ) var typeAlreadyError = errors.New("Policy Type already exists") @@ -180,6 +183,8 @@ func (rh *Resthook) CreatePolicyType(policyTypeId models.PolicyTypeID, httpreque return nil } + + func toStringKeys(val interface{}) (interface{}, error) { var err error switch val := val.(type) { @@ -301,6 +306,31 @@ func (rh *Resthook) StorePolicyInstance(policyTypeId models.PolicyTypeID, policy return operation, nil } +func (rh *Resthook) storePolicyInstanceMetadata(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID) (bool, error) { + + creation_timestamp := time.Now() + instanceMetadataKey := a1InstanceMetadataPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + + a1.Logger.Debug("key : %+v", instanceMetadataKey) + + var metadatajson []interface{} + metadatajson = append(metadatajson, map[string]string{"created_at": creation_timestamp.Format("2006-01-02 15:04:05"), "has_been_deleted": "False"}) + metadata, _ := json.Marshal(metadatajson) + + a1.Logger.Debug("policyinstanceMetaData to create : %+v", string(metadata)) + + err := rh.db.Set(a1MediatorNs, instanceMetadataKey, string(metadata)) + + if err != nil { + a1.Logger.Error("error :%+v", err) + return false, err + } + + a1.Logger.Debug("Policy Instance Meta Data created at :%+v", creation_timestamp) + + return true, 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,12 +347,21 @@ 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 } a1.Logger.Debug("policy instance :%+v", operation) + 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") + } } else { a1.Logger.Error("%+v", invalidJsonSchema) return invalidJsonSchema @@ -377,18 +416,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 +437,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 }