diff --git a/src/common/tools/object_factory/PolicyRule.py b/src/common/tools/object_factory/PolicyRule.py index 4157a3f0679efa37db72416c1fe2d7726c07355c..8702f931dfffef175ce6c25de24a10de8286effc 100644 --- a/src/common/tools/object_factory/PolicyRule.py +++ b/src/common/tools/object_factory/PolicyRule.py @@ -13,33 +13,36 @@ # limitations under the License. import logging -LOGGER = logging.getLogger(__name__) +from typing import Dict, List, Optional +from common.proto.policy_condition_pb2 import BooleanOperator -def json_policy_rule_id(policy_rule_uuid: str): - result = {'uuid': {'uuid': policy_rule_uuid}} - return result +LOGGER = logging.getLogger(__name__) +def json_policy_rule_id(policy_rule_uuid : str) -> Dict: + return {'uuid': {'uuid': policy_rule_uuid}} -def json_policy_rule(policy_rule_uuid: str, policy_rule_type: str): - result = {} +def json_policy_rule( + policy_rule_uuid : str, policy_priority : int = 1, + 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] = [] +) -> Dict: basic = { - "policyRuleId": json_policy_rule_id(policy_rule_uuid), - "priority": 1, - "conditionList": [], - "booleanOperator": bool(), - "actionList": [] + 'policyRuleId': json_policy_rule_id(policy_rule_uuid), + 'priority': policy_priority, + 'conditionList': condition_list, + 'booleanOperator': boolean_operator, + 'actionList': action_list, } - if policy_rule_type == 'service': - result["service"] = {} - result["service"]["policyRuleBasic"] = basic - result["service"]["serviceId"] = { - "service_uuid": {"uuid": "uuid-service"}, - "context_id": {"context_uuid": {"uuid": "uuid-context"}}, - } - + result = {} + if service_id is not None: + policy_rule_type = 'service' + result[policy_rule_type] = {'policyRuleBasic': basic} + result[policy_rule_type]['serviceId'] = service_id else: - result["device"] = {} - result["device"]["policyRuleBasic"] = basic + policy_rule_type = 'device' + result[policy_rule_type] = {'policyRuleBasic': basic} + result[policy_rule_type]['deviceList'] = device_id_list return result diff --git a/src/policy/client/PolicyClient.py b/src/policy/client/PolicyClient.py deleted file mode 100644 index 2532a35caeccd182e5ffd1812ea99e1c41b95b4c..0000000000000000000000000000000000000000 --- a/src/policy/client/PolicyClient.py +++ /dev/null @@ -1,89 +0,0 @@ -# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) -# -# 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.Constants import ServiceNameEnum -from common.Settings import get_service_host, get_service_port_grpc -from common.tools.client.RetryDecorator import retry, delay_exponential -from common.tools.grpc.Tools import grpc_message_to_json_string -from common.proto.context_pb2 import ( - Connection, ConnectionEvent, ConnectionId, ConnectionIdList, ConnectionList, - Context, ContextEvent, ContextId, ContextIdList, ContextList, - Device, DeviceEvent, DeviceId, DeviceIdList, DeviceList, - Empty, - Link, LinkEvent, LinkId, LinkIdList, LinkList, - Service, ServiceEvent, ServiceId, ServiceIdList, ServiceList, - Slice, SliceEvent, SliceId, SliceIdList, SliceList, - Topology, TopologyEvent, TopologyId, TopologyIdList, TopologyList) -from common.proto.policy_pb2 import (PolicyRuleIdList, PolicyRuleId, PolicyRuleList, PolicyRule) -from common.proto.context_pb2_grpc import ContextServiceStub - -LOGGER = logging.getLogger(__name__) -MAX_RETRIES = 15 -DELAY_FUNCTION = delay_exponential(initial=0.01, increment=2.0, maximum=5.0) -RETRY_DECORATOR = retry(max_retries=MAX_RETRIES, delay_function=DELAY_FUNCTION, prepare_method_name='connect') - -class PolicyClient: - def __init__(self, host=None, port=None): - if not host: - host = get_service_host(ServiceNameEnum.POLICY) - if not port: - port = get_service_port_grpc(ServiceNameEnum.POLICY) - self.endpoint = '{:s}:{:s}'.format(str(host), str(port)) - LOGGER.debug('Creating channel to {:s}...'.format(str(self.endpoint))) - self.channel = None - self.stub = None - self.connect() - LOGGER.debug('Channel created') - - def connect(self): - self.channel = grpc.insecure_channel(self.endpoint) - self.stub = ContextServiceStub(self.channel) - - def close(self): - if self.channel is not None: - self.channel.close() - self.channel = None - self.stub = None - - @RETRY_DECORATOR - def ListPolicyRuleIds(self, request: Empty) -> PolicyRuleIdList: - LOGGER.debug('ListPolicyRuleIds request: {:s}'.format(grpc_message_to_json_string(request))) - response = self.stub.ListPolicyRuleIds(request) - LOGGER.debug('ListPolicyRuleIds result: {:s}'.format(grpc_message_to_json_string(response))) - return response - @RETRY_DECORATOR - def ListPolicyRules(self, request: Empty) -> PolicyRuleList: - LOGGER.debug('ListPolicyRules request: {:s}'.format(grpc_message_to_json_string(request))) - response = self.stub.ListPolicyRules(request) - LOGGER.debug('ListPolicyRules result: {:s}'.format(grpc_message_to_json_string(response))) - return response - @RETRY_DECORATOR - def GetPolicyRule(self, request: PolicyRuleId) -> PolicyRule: - LOGGER.debug('GetPolicyRule request: {:s}'.format(grpc_message_to_json_string(request))) - response = self.stub.GetPolicyRule(request) - LOGGER.debug('GetPolicyRule result: {:s}'.format(grpc_message_to_json_string(response))) - return response - @RETRY_DECORATOR - def SetPolicyRule(self, request: PolicyRule) -> PolicyRuleId: - LOGGER.debug('SetPolicyRule request: {:s}'.format(grpc_message_to_json_string(request))) - response = self.stub.SetPolicyRule(request) - LOGGER.debug('SetPolicyRule result: {:s}'.format(grpc_message_to_json_string(response))) - return response - @RETRY_DECORATOR - def RemovePolicyRule(self, request: PolicyRuleId) -> Empty: - LOGGER.debug('RemovePolicyRule request: {:s}'.format(grpc_message_to_json_string(request))) - response = self.stub.RemovePolicyRule(request) - LOGGER.debug('RemovePolicyRule result: {:s}'.format(grpc_message_to_json_string(response))) - return response diff --git a/src/policy/client/__init__.py b/src/policy/client/__init__.py deleted file mode 100644 index 70a33251242c51f49140e596b8208a19dd5245f7..0000000000000000000000000000000000000000 --- a/src/policy/client/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) -# -# 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. -