Commit d162c7d6 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Merge branch 'feat/policy-refactor' into 'develop'

(UBI) Instruct policy to observe a specific KPI ID for alarms

See merge request !396
parents ff14c8ca a579a6d0
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
+10 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ from analytics.frontend.client.AnalyticsFrontendClient import AnalyticsFrontendC
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__)

@@ -65,6 +66,15 @@ class P4INTZSMPlugin(_ZSMHandler):

        # 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:
+3 −7
Original line number Diff line number Diff line
@@ -14,7 +14,6 @@

import logging
from typing import Dict, List, Optional
from common.proto.policy_condition_pb2 import BooleanOperator
from common.proto.policy_pb2 import PolicyRuleStateEnum

LOGGER = logging.getLogger(__name__)
@@ -23,11 +22,9 @@ def json_policyrule_id(policyrule_uuid : str) -> Dict:
    return {'uuid': {'uuid': policyrule_uuid}}

def json_policyrule(
    policyrule_uuid : str, policy_priority : int = 1,
    policyrule_uuid : str, policy_priority : int = 1, policy_kpi_id : str = '',
    policy_state : PolicyRuleStateEnum = PolicyRuleStateEnum.POLICY_UNDEFINED, policy_state_message : str = '',
    boolean_operator : BooleanOperator = BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_AND,
    condition_list : List[Dict] = [], action_list : List[Dict] = [],
    service_id : Optional[Dict] = None, device_id_list : List[Dict] = []
    action_list : List[Dict] = [], service_id : Optional[Dict] = None, device_id_list : List[Dict] = []
) -> Dict:
    basic = {
        'policyRuleId': json_policyrule_id(policyrule_uuid),
@@ -36,8 +33,7 @@ def json_policyrule(
            'policyRuleStateMessage': policy_state_message,
        },
        'priority': policy_priority,
        'conditionList': condition_list,
        'booleanOperator': boolean_operator,
        'kpiId': {'kpi_id': {'uuid': policy_kpi_id}},
        'actionList': action_list,
    }

+3 −3
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)

@@ -92,6 +90,7 @@ def policyrule_set(db_engine : Engine, messagebroker : MessageBroker, request :
        'policyrule_state'    : policyrule_state,
        'policyrule_state_msg': policyrule_state_msg,
        'policyrule_priority' : policyrule_basic.priority,
        'policyrule_kpi_id'   : policyrule_basic.kpiId.kpi_id.uuid,
        'policyrule_eca_data' : policyrule_eca_data,
        'created_at'          : now,
        'updated_at'          : now,
@@ -121,6 +120,7 @@ def policyrule_set(db_engine : Engine, messagebroker : MessageBroker, request :
                policyrule_state     = stmt.excluded.policyrule_state,
                policyrule_state_msg = stmt.excluded.policyrule_state_msg,
                policyrule_priority  = stmt.excluded.policyrule_priority,
                policyrule_kpi_id    = stmt.excluded.policyrule_kpi_id,
                policyrule_eca_data  = stmt.excluded.policyrule_eca_data,
                updated_at           = stmt.excluded.updated_at,
            )
+2 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ class PolicyRuleModel(_Base):
    policyrule_state        = Column(Enum(ORM_PolicyRuleStateEnum), nullable=False)
    policyrule_state_msg    = Column(String, nullable=False)
    policyrule_priority     = Column(Integer, nullable=False)
    policyrule_kpi_id       = Column(String, nullable=False)
    policyrule_service_uuid = Column(ForeignKey('service.service_uuid', ondelete='RESTRICT'), nullable=True, index=True)
    policyrule_eca_data     = Column(String, nullable=False)
    created_at              = Column(DateTime, nullable=False)
@@ -58,6 +59,7 @@ class PolicyRuleModel(_Base):
                'policyRuleStateMessage': self.policyrule_state_msg,
            },
            'priority': self.policyrule_priority,
            'kpiId': {'kpi_id': {'uuid': self.policyrule_kpi_id}},
        })
        result = {
            'policyRuleBasic': policyrule_basic,
Loading