X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=a1-go%2Fpkg%2Fresthooks%2Fresthooks.go;h=79dba939345f31a826de191265209466e0538fea;hb=1a41ce75dbf508ae4d85c2dc4a562f36da04bb65;hp=5e704be4ea359d83fdef0bbf6254400fc68a7bad;hpb=44f5c8a90e9aabb096d8f6100e49f34eb4f89b01;p=ric-plt%2Fa1.git diff --git a/a1-go/pkg/resthooks/resthooks.go b/a1-go/pkg/resthooks/resthooks.go index 5e704be..79dba93 100644 --- a/a1-go/pkg/resthooks/resthooks.go +++ b/a1-go/pkg/resthooks/resthooks.go @@ -26,19 +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/sdlgo" - + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" "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." ) var typeAlreadyError = errors.New("Policy Type already exists") @@ -70,12 +73,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, } } @@ -116,7 +120,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) @@ -240,7 +244,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) @@ -302,6 +306,30 @@ 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") @@ -316,16 +344,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) + if errmetadata != nil { + a1.Logger.Error("error :%+v", errmetadata) + return errmetadata + } + if iscreated { + 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 @@ -380,15 +426,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 policyTypeInstances, err } a1.Logger.Debug("keys : %+v", keys) typekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." @@ -401,10 +447,10 @@ 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 }