Commit 7dd26fcd authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context component:

- pre-merge cleanup
parent f0fc6c2e
Loading
Loading
Loading
Loading
+24 −21
Original line number Original line Diff line number Diff line
@@ -13,33 +13,36 @@
# limitations under the License.
# limitations under the License.


import logging
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):
LOGGER = logging.getLogger(__name__)
    result = {'uuid': {'uuid': policy_rule_uuid}}
    return result


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):
def json_policy_rule(
    result = {}
    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 = {
    basic = {
        "policyRuleId": json_policy_rule_id(policy_rule_uuid),
        'policyRuleId': json_policy_rule_id(policy_rule_uuid),
        "priority": 1,
        'priority': policy_priority,
        "conditionList": [],
        'conditionList': condition_list,
        "booleanOperator": bool(),
        'booleanOperator': boolean_operator,
        "actionList": []
        '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:
    else:
        result["device"] = {}
        policy_rule_type = 'device'
        result["device"]["policyRuleBasic"] = basic
        result[policy_rule_type] = {'policyRuleBasic': basic}


    result[policy_rule_type]['deviceList'] = device_id_list
    return result
    return result

src/policy/client/PolicyClient.py

deleted100644 → 0
+0 −89
Original line number Original line Diff line number Diff line
# 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

src/policy/client/__init__.py

deleted100644 → 0
+0 −14
Original line number Original line Diff line number Diff line
# 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.