Commit 80586f7c authored by Konstantinos Poulakakis's avatar Konstantinos Poulakakis
Browse files

Remove booleanOperator and conditionList variable from the policy AddService...

Remove booleanOperator and conditionList variable from the policy AddService endpoint. Fix necessary test for those changes.
parent e3f502c3
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package policy;
import "context.proto";
import "policy_condition.proto";
import "policy_action.proto";
import "monitoring.proto"; // to be migrated to: "kpi_manager.proto"

service PolicyService {
  rpc PolicyAddService (PolicyRuleService) returns (PolicyRuleState) {}
@@ -58,11 +59,10 @@ message PolicyRuleBasic {
  PolicyRuleId policyRuleId = 1;
  PolicyRuleState policyRuleState = 2; //policy.proto:58:12: Explicit 'optional' labels are disallowed in the Proto3 syntax. To define 'optional' fields in Proto3, simply remove the 'optional' label, as fields are 'optional' by default.
  uint32 priority = 3;
  monitoring.KpiId    kpiId       = 4;  // to be migrated to: "kpi_manager.KpiId"

  // Event-Condition-Action (ECA) model
  repeated PolicyRuleCondition conditionList = 4;  // When these policy conditions are met, an event is automatically thrown
  BooleanOperator booleanOperator = 5;             // Evaluation operator to be used
  repeated PolicyRuleAction actionList = 6;        // One or more actions should be applied
  repeated PolicyRuleAction actionList = 5;        // One or more actions should be applied
}

// Service-oriented policy rule
+99 −0
Original line number Diff line number Diff line
# Copyright 2022-2024 ETSI SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
#
# 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
#
#     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.

import grpc , logging
from common.proto.analytics_frontend_pb2 import AnalyzerId
from common.proto.policy_pb2 import PolicyRuleState
from common.proto.automation_pb2 import ZSMCreateRequest, ZSMService

from analytics.frontend.client.AnalyticsFrontendClient import AnalyticsFrontendClient
from automation.client.PolicyClient import PolicyClient
from context.client.ContextClient import ContextClient
from automation.service.zsm_handler_api._ZSMHandler import _ZSMHandler
from common.proto.policy_condition_pb2 import PolicyRuleCondition

LOGGER = logging.getLogger(__name__)

class P4INTZSMPlugin(_ZSMHandler):
    def __init__(self):
        LOGGER.info('Init P4INTZSMPlugin')

    def zsmCreate(self,request : ZSMCreateRequest, context : grpc.ServicerContext):
        # check that service does not exist
        context_client = ContextClient()
        policy_client = PolicyClient()
        analytics_frontend_client = AnalyticsFrontendClient()

        # Verify the input target service ID
        try:
            target_service_id = context_client.GetService(request.target_service_id)
        except grpc.RpcError as ex:
            if ex.code() != grpc.StatusCode.NOT_FOUND: raise  # pylint: disable=no-member
            LOGGER.exception('Unable to get target service({:s})'.format(str(target_service_id)))
            context_client.close()
            return None

        # Verify the input telemetry service ID
        try:
            telemetry_service_id = context_client.GetService(request.telemetry_service_id)
        except grpc.RpcError as ex:
            if ex.code() != grpc.StatusCode.NOT_FOUND: raise  # pylint: disable=no-member
            LOGGER.exception('Unable to get telemetry service({:s})'.format(str(telemetry_service_id)))
            context_client.close()
            return None

        # Start an analyzer
        try:
            analyzer_id_lat: AnalyzerId = analytics_frontend_client.StartAnalyzer(request.analyzer) # type: ignore
            LOGGER.info('analyzer_id_lat({:s})'.format(str(analyzer_id_lat)))
        except grpc.RpcError as ex:
            if ex.code() != grpc.StatusCode.NOT_FOUND: raise  # pylint: disable=no-member
            LOGGER.exception('Unable to start analyzer({:s})'.format(str(request.analyzer)))
            context_client.close()
            analytics_frontend_client.close()
            return None

        # Create a policy
        try:
            LOGGER.info('policy({:s})'.format(str(request.policy)))
            # PolicyRuleCondition
            policyRuleCondition = PolicyRuleCondition()
            # policyRuleCondition.kpiId.kpi_id.uuid = request.analyzer.output_kpi_ids[0].kpi_id.uuid
            # policyRuleCondition.numericalOperator = 5
            # policyRuleCondition.kpiValue.floatVal = 300
            # request.policy.policyRuleBasic.conditionList.append(policyRuleCondition)
            LOGGER.info('policy after({:s})'.format(str(request.policy)))

            policy_rule_state: PolicyRuleState = policy_client.PolicyAddService(request.policy) # type: ignore
            LOGGER.info('policy_rule_state({:s})'.format(str(policy_rule_state)))
        except grpc.RpcError as ex:
            if ex.code() != grpc.StatusCode.NOT_FOUND: raise  # pylint: disable=no-member
            LOGGER.exception('Unable to create policy({:s})'.format(str(request.policy)))
            context_client.close()
            policy_client.close()
            return None

        context_client.close()
        analytics_frontend_client.close()
        policy_client.close()
        return ZSMService()

    def zsmDelete(self):
        LOGGER.info('zsmDelete method')

    def zsmGetById(self):
        LOGGER.info('zsmGetById method')

    def zsmGetByService(self):
        LOGGER.info('zsmGetByService method')
+0 −2
Original line number Diff line number Diff line
@@ -79,8 +79,6 @@ def policyrule_set(db_engine : Engine, messagebroker : MessageBroker, request :

    json_policyrule_basic = grpc_message_to_json(policyrule_basic)
    policyrule_eca_data = json.dumps({
        'conditionList': json_policyrule_basic.get('conditionList', []),
        'booleanOperator': json_policyrule_basic['booleanOperator'],
        'actionList': json_policyrule_basic.get('actionList', []),
    }, sort_keys=True)

+5 −19
Original line number Diff line number Diff line
@@ -1960,23 +1960,18 @@ public class Serializer {
        final var policyRuleId = policyRuleBasic.getPolicyRuleId();
        final var policyRuleState = policyRuleBasic.getPolicyRuleState();
        final var priority = policyRuleBasic.getPriority();
        final var policyRuleConditions = policyRuleBasic.getPolicyRuleConditions();
        final var booleanOperator = policyRuleBasic.getBooleanOperator();
        final var kpiId = policyRuleBasic.getKpiId();
        final var policyRuleActions = policyRuleBasic.getPolicyRuleActions();

        final var serializedPolicyRuleId = serializePolicyRuleId(policyRuleId);
        final var serializedPolicyRuleState = serialize(policyRuleState);
        final var serializedPolicyRuleConditions =
                policyRuleConditions.stream().map(this::serialize).collect(Collectors.toList());
        final var serializedBooleanOperator = serialize(booleanOperator);
        final var serializedPolicyRuleActions =
                policyRuleActions.stream().map(this::serialize).collect(Collectors.toList());

        builder.setPolicyRuleId(serializedPolicyRuleId);
        builder.setPolicyRuleState(serializedPolicyRuleState);
        builder.setPriority(priority);
        builder.addAllConditionList(serializedPolicyRuleConditions);
        builder.setBooleanOperator(serializedBooleanOperator);
        builder.setKpiId(KpiId.newBuilder().setKpiId(Uuid.newBuilder().setUuid("kpiId").build()));
        builder.addAllActionList(serializedPolicyRuleActions);

        return builder.build();
@@ -1986,25 +1981,16 @@ public class Serializer {
        final var serializedPolicyRuleId = serializedPolicyRuleBasic.getPolicyRuleId();
        final var serializedPolicyRuleState = serializedPolicyRuleBasic.getPolicyRuleState();
        final var priority = serializedPolicyRuleBasic.getPriority();
        final var serializedPolicyRuleConditions = serializedPolicyRuleBasic.getConditionListList();
        final var serializedBooleanOperator = serializedPolicyRuleBasic.getBooleanOperator();
        final var serializedPolicyRuleActions = serializedPolicyRuleBasic.getActionListList();
        final var serializedKpiId = serializedPolicyRuleBasic.getKpiId();

        final var policyRuleId = deserialize(serializedPolicyRuleId);
        final var kpiId = deserialize(serializedKpiId);
        final var policyRuleState = deserialize(serializedPolicyRuleState);
        final var policyRuleConditions =
                serializedPolicyRuleConditions.stream().map(this::deserialize).collect(Collectors.toList());
        final var booleanOperator = deserialize(serializedBooleanOperator);
        final var policyRuleActions =
                serializedPolicyRuleActions.stream().map(this::deserialize).collect(Collectors.toList());

        return new PolicyRuleBasic(
                policyRuleId,
                policyRuleState,
                priority,
                policyRuleConditions,
                booleanOperator,
                policyRuleActions);
        return new PolicyRuleBasic(policyRuleId, kpiId, policyRuleState, priority, policyRuleActions);
    }

    public Policy.PolicyRuleService serialize(PolicyRuleService policyRuleService) {
+0 −13
Original line number Diff line number Diff line
@@ -70,17 +70,6 @@ public class AddPolicyDeviceImpl {
        final var policyRuleTypeDevice = new PolicyRuleTypeDevice(policyRuleDevice);
        final var policyRule = new PolicyRule(policyRuleTypeDevice);

        final var alarmDescriptorList = commonPolicyServiceImpl.createAlarmDescriptorList(policyRule);
        if (alarmDescriptorList.isEmpty()) {
            var policyRuleState =
                    new PolicyRuleState(
                            PolicyRuleStateEnum.POLICY_FAILED,
                            String.format(
                                    "Invalid PolicyRuleConditions in PolicyRule with ID: %s",
                                    policyRuleBasic.getPolicyRuleId()));
            return Uni.createFrom().item(policyRuleState);
        }

        return contextService
                .setPolicyRule(policyRule)
                .onFailure()
@@ -88,8 +77,6 @@ public class AddPolicyDeviceImpl {
                .onItem()
                .transform(
                        policyId -> {
                            commonAlarmService.startMonitoringBasedOnAlarmDescriptors(
                                    policyId, policyRuleDevice, alarmDescriptorList);
                            return VALIDATED_POLICYRULE_STATE;
                        });
    }
Loading