X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fresthooks%2Fresthooks.go;h=34d94b8954c7c6ec8d0122dc7d9ee830d85fa39b;hb=d916d3bfdf78aae92bf54253a20ef7cf73f5b21f;hp=79e52faf29dc8c142ca98ca64a57d006dab15919;hpb=8e5f4be592d00c05ec1e3b2ea455bc39cb023d68;p=ric-plt%2Fa1.git diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go index 79e52fa..34d94b8 100644 --- a/pkg/resthooks/resthooks.go +++ b/pkg/resthooks/resthooks.go @@ -1,21 +1,23 @@ /* ================================================================================== - Copyright (c) 2021 Samsung - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at + Copyright (c) 2021 Samsung - http://www.apache.org/licenses/LICENSE-2.0 + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + This source code is part of the near-RT RIC (RAN Intelligent Controller) + platform project (RICP). - This source code is part of the near-RT RIC (RAN Intelligent Controller) - platform project (RICP). ================================================================================== */ package resthooks @@ -39,13 +41,14 @@ import ( ) const ( - a1PolicyPrefix = "a1.policy_type." - a1MediatorNs = "A1m_ns" - a1InstancePrefix = "a1.policy_instance." - a1InstanceMetadataPrefix = "a1.policy_inst_metadata." - a1HandlerPrefix = "a1.policy_handler." - a1PolicyRequest = 20010 - a1EIDataDelivery = 20017 + a1PolicyPrefix = "a1.policy_type." + a1MediatorNs = "A1m_ns" + a1InstancePrefix = "a1.policy_instance." + a1NotificationDestinationPrefix = "a1.policy_notification_destination." + a1InstanceMetadataPrefix = "a1.policy_inst_metadata." + a1HandlerPrefix = "a1.policy_handler." + a1PolicyRequest = 20010 + a1EIDataDelivery = 20017 ) var typeAlreadyError = errors.New("Policy Type already exists") @@ -65,7 +68,7 @@ func (rh *Resthook) CanPolicyTypeBeDeleted(err error) bool { return err == policyTypeCanNotBeDeletedError } -func (rh *Resthook) IsPolicyTypePresent(err error) bool { +func (rh *Resthook) IsPolicyTypeNotFound(err error) bool { return err == policyTypeNotFoundError } @@ -102,7 +105,11 @@ func createResthook(sdlInst iSdl, rmrSenderInst rmr.IRmrSender) *Resthook { } func (rh *Resthook) GetA1Health() bool { - //TODO : Add database healthcheck + _, err := rh.db.GetAll("A1m_ns") + if err != nil { + a1.Logger.Error("error in connecting to the database. err: %v", err) + return false + } a1.Logger.Debug("A1 is healthy") return true } @@ -131,7 +138,7 @@ func (rh *Resthook) GetAllPolicyType() []models.PolicyTypeID { return policyTypeIDs } -func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.PolicyTypeSchema { +func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) (*models.PolicyTypeSchema, error) { a1.Logger.Debug("GetPolicyType1") var policytypeschema *models.PolicyTypeSchema @@ -148,17 +155,17 @@ func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.Poli if len(valmap) == 0 { a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId) - return policytypeschema + return policytypeschema, policyTypeNotFoundError } if err != nil { a1.Logger.Error("error in retrieving policy type. err: %v", err) - return nil + return nil,policyTypeNotFoundError } if valmap[key] == nil { a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId) - return policytypeschema + return policytypeschema,policyTypeNotFoundError } a1.Logger.Debug("keysmap : %+v", valmap[key]) @@ -171,7 +178,7 @@ func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.Poli valToUnmarshall, err := strconv.Unquote(valkey) if err != nil { a1.Logger.Error("unquote error : %+v", err) - return nil + return nil,policyTypeNotFoundError } a1.Logger.Debug("Policy type for %+v : %+v", key, string(valToUnmarshall)) @@ -181,7 +188,7 @@ func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.Poli a1.Logger.Debug(" Unmarshalled json : %+v", (errunm)) a1.Logger.Debug("Policy type Name : %v", (item.Name)) - return &item + return &item,nil } func (rh *Resthook) CreatePolicyType(policyTypeId models.PolicyTypeID, httprequest models.PolicyTypeSchema) error { @@ -268,7 +275,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{}, notificationDestination string) (string, error) { var keys [1]string operation := "CREATE" typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) @@ -288,7 +295,8 @@ func (rh *Resthook) storePolicyInstance(policyTypeId models.PolicyTypeID, policy // TODO : rmr creation_timestamp := time.Now() // will be needed for rmr to notify the creation of instance instancekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) - keys[0] = typekey + notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + keys[0] = instancekey instanceMap, err := rh.db.Get(a1MediatorNs, keys[:]) if err != nil { a1.Logger.Error("policy type error : %v", err) @@ -301,15 +309,22 @@ func (rh *Resthook) storePolicyInstance(policyTypeId models.PolicyTypeID, policy data, _ := json.Marshal(httpBody) a1.Logger.Debug("Marshaled String : %+v", string(data)) a1.Logger.Debug("key : %+v", instancekey) - success, err1 := rh.db.SetIf(a1MediatorNs, instancekey, instanceMap[instancekey], string(data)) - if err1 != nil { - a1.Logger.Error("error2 :%+v", err1) - return operation, err1 + success, err := rh.db.SetIf(a1MediatorNs, instancekey, instanceMap[instancekey], string(data)) + if err != nil { + a1.Logger.Error("error2 :%+v", err) + return operation, err } if !success { a1.Logger.Debug("Policy instance %+v already exist", policyInstanceID) return operation, InstanceAlreadyError } + + if len(notificationDestination) > 0 { + if err = rh.db.Set(a1MediatorNs, notificationDestinationkey, notificationDestination); err != nil { + a1.Logger.Error("error3 :%+v", err) + return operation, err + } + } } else { data, _ := json.Marshal(httpBody) a1.Logger.Debug("Marshaled String : %+v", string(data)) @@ -320,10 +335,15 @@ func (rh *Resthook) storePolicyInstance(policyTypeId models.PolicyTypeID, policy a1.Logger.Debug("policyinstancetype map : %+v", instance_map[1]) a1.Logger.Debug("policyinstancetype to create : %+v", instance_map) - err1 := rh.db.Set(a1MediatorNs, instancekey, string(data)) - if err1 != nil { - a1.Logger.Error("error1 :%+v", err1) - return operation, err1 + if err = rh.db.Set(a1MediatorNs, instancekey, string(data)); err != nil { + a1.Logger.Error("error4 :%+v", err) + return operation, err + } + if len(notificationDestination) > 0 { + if err := rh.db.Set(a1MediatorNs, notificationDestinationkey, notificationDestination); err != nil { + a1.Logger.Error("error :%+v", err) + return operation, err + } } } a1.Logger.Debug("Policy Instance created ") @@ -355,11 +375,15 @@ func (rh *Resthook) storePolicyInstanceMetadata(policyTypeId models.PolicyTypeID return true, nil } -func (rh *Resthook) CreatePolicyInstance(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID, httpBody interface{}) error { +func (rh *Resthook) CreatePolicyInstance(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID, httpBody interface{}, notificationDestination string) error { a1.Logger.Debug("CreatePolicyInstance function") // validate the PUT against the schema var policyTypeSchema *models.PolicyTypeSchema - policyTypeSchema = rh.GetPolicyType(policyTypeId) + policyTypeSchema, err := rh.GetPolicyType(policyTypeId) + if err != nil { + a1.Logger.Error("error : %+v", err) + return err + } schemaStr, err := json.Marshal(policyTypeSchema.CreateSchema) if err != nil { a1.Logger.Error("Json Marshal error : %+v", err) @@ -375,7 +399,7 @@ func (rh *Resthook) CreatePolicyInstance(policyTypeId models.PolicyTypeID, polic isvalid := validate(httpBodyString, schemaString) if isvalid { var operation string - operation, err = rh.storePolicyInstance(policyTypeId, policyInstanceID, httpBody) + operation, err = rh.storePolicyInstance(policyTypeId, policyInstanceID, httpBody, notificationDestination) if err != nil { a1.Logger.Error("error :%+v", err) return err @@ -589,8 +613,9 @@ func (rh *Resthook) getPolicyInstanceStatus(policyTypeId models.PolicyTypeID, po func (rh *Resthook) GetPolicyInstanceStatus(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID) (*a1_mediator.A1ControllerGetPolicyInstanceStatusOKBody, error) { err := rh.instanceValidity(policyTypeId, policyInstanceID) policyInstanceStatus := a1_mediator.A1ControllerGetPolicyInstanceStatusOKBody{} - policyInstanceStatus.InstanceStatus = "NOT IN EFFECT" - if err != nil && err == policyInstanceNotFoundError || err == policyTypeNotFoundError { + policyInstanceStatus.EnforceStatus = "NOT_ENFORCED" + policyInstanceStatus.EnforceReason = "OTHER_REASON" + if err != nil && (err == policyInstanceNotFoundError || err == policyTypeNotFoundError) { return &policyInstanceStatus, err } metadata, err := rh.getMetaData(policyTypeId, policyInstanceID) @@ -610,13 +635,13 @@ func (rh *Resthook) GetPolicyInstanceStatus(policyTypeId models.PolicyTypeID, po //this error maps to 503 error but can be mapped to 500: internal error return &policyInstanceStatus, err } - resp, err := rh.getPolicyInstanceStatus(policyTypeId, policyInstanceID) - if err != nil || (err == nil && resp == false) { + enforced, err := rh.getPolicyInstanceStatus(policyTypeId, policyInstanceID) + if err != nil || (err == nil && !enforced) { a1.Logger.Error("marshal error : %v", err) return &policyInstanceStatus, err - } else if policyInstanceStatus.HasBeenDeleted == true { - policyInstanceStatus.InstanceStatus = "IN EFFECT" } + policyInstanceStatus.EnforceStatus = "ENFORCED" + policyInstanceStatus.EnforceReason = "" return &policyInstanceStatus, nil } @@ -652,9 +677,22 @@ func (rh *Resthook) deleteInstancedata(policyTypeId models.PolicyTypeID, policyI keys[0] = instancekey err := rh.db.Remove(a1MediatorNs, keys[:]) if err != nil { - a1.Logger.Error("error in deleting policy type err: %v", err) + a1.Logger.Error("error in deleting policy instance err: %v", err) + return err + } + return nil +} + +func (rh *Resthook) deleteNotificationDestination(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID) error { + var keys [1]string + notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) + keys[0] = notificationDestinationkey + err := rh.db.Remove(a1MediatorNs, keys[:]) + if err != nil { + a1.Logger.Error("error in deleting notificationDestination err: %v", err) return err } + return nil } @@ -705,6 +743,8 @@ func (rh *Resthook) DeletePolicyInstance(policyTypeId models.PolicyTypeID, polic rh.deleteInstancedata(policyTypeId, policyInstanceID) + rh.deleteNotificationDestination(policyTypeId, policyInstanceID) + rh.storeDeletedPolicyInstanceMetadata(policyTypeId, policyInstanceID, creation_timestamp.(string)) message := rmr.Message{}