Commit 2f4526b8 authored by Carlos Manso's avatar Carlos Manso
Browse files

Implementation of PolicyService.


Lacks events.

Signed-off-by: default avatarmansoca <carlos.manso@cttc.es>
parent fa708e26
Loading
Loading
Loading
Loading
+45 −0
Original line number 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 logging
LOGGER = logging.getLogger(__name__)

def json_policy_rule_id(policy_rule_uuid: str):
    result = {'uuid': {'uuid': policy_rule_uuid}}
    return result


def json_policy_rule(policy_rule_uuid: str, policy_rule_type: str):
    result = {}
    basic = {
        "policyRuleId": json_policy_rule_id(policy_rule_uuid),
        "priority": 1,
        "conditionList": [],
        "booleanOperator": bool(),
        "actionList": []
    }

    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"}},
        }

    else:
        result["device"] = {}
        result["device"]["policyRuleBasic"] = basic

    return result
+37 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ from common.proto.context_pb2 import (
    Slice, SliceEvent, SliceId, SliceIdList, SliceList,
    Topology, TopologyEvent, TopologyId, TopologyIdList, TopologyList)
from common.proto.context_pb2_grpc import ContextServiceStub
from common.proto.context_policy_pb2_grpc import ContextPolicyServiceStub
from common.proto.policy_pb2 import (PolicyRuleIdList, PolicyRuleId, PolicyRuleList, PolicyRule)

LOGGER = logging.getLogger(__name__)
MAX_RETRIES = 15
@@ -42,17 +44,52 @@ class ContextClient:
        LOGGER.debug('Creating channel to {:s}...'.format(str(self.endpoint)))
        self.channel = None
        self.stub = None
        self.policy_stub = None
        self.connect()
        LOGGER.debug('Channel created')

    def connect(self):
        self.channel = grpc.insecure_channel(self.endpoint)
        self.stub = ContextServiceStub(self.channel)
        self.policy_stub = ContextPolicyServiceStub(self.channel)

    def close(self):
        if self.channel is not None: self.channel.close()
        self.channel = None
        self.stub = None
        self.policy_stub = None


    @RETRY_DECORATOR
    def ListPolicyRuleIds(self, request: Empty) -> PolicyRuleIdList:
        LOGGER.debug('ListPolicyRuleIds request: {:s}'.format(grpc_message_to_json_string(request)))
        response = self.policy_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.policy_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.info('GetPolicyRule request: {:s}'.format(grpc_message_to_json_string(request)))
        response = self.policy_stub.GetPolicyRule(request)
        LOGGER.info('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.policy_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.policy_stub.RemovePolicyRule(request)
        LOGGER.debug('RemovePolicyRule result: {:s}'.format(grpc_message_to_json_string(response)))
        return response

    @RETRY_DECORATOR
    def ListContextIds(self, request: Empty) -> ContextIdList:
+35 −0
Original line number 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 logging
import json
from typing import Dict, List, Optional, Tuple
from common.orm.fields.PrimaryKeyField import PrimaryKeyField
from common.orm.fields.StringField import StringField
from common.orm.model.Model import Model

LOGGER = logging.getLogger(__name__)


class PolicyRuleModel(Model):
    pk = PrimaryKeyField()
    value = StringField(required=True, allow_empty=False)

    def dump_id(self) -> Dict:
        return {
            "uuid": {"uuid": self.pk}
        }

    def dump(self) -> Dict:
        return json.loads(self.value)
+3 −1
Original line number Diff line number Diff line
@@ -19,7 +19,9 @@ TOPIC_DEVICE = 'device'
TOPIC_LINK       = 'link'
TOPIC_SERVICE    = 'service'
TOPIC_SLICE      = 'slice'
TOPIC_POLICY     = 'policy'

TOPICS = {TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_TOPOLOGY, TOPIC_DEVICE, TOPIC_LINK, TOPIC_SERVICE, TOPIC_SLICE}
TOPICS = {TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_TOPOLOGY, TOPIC_DEVICE, TOPIC_LINK, TOPIC_SERVICE, TOPIC_SLICE,
          TOPIC_POLICY}

CONSUME_TIMEOUT = 0.5 # seconds
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ from common.Settings import get_service_port_grpc
from common.message_broker.MessageBroker import MessageBroker
from common.orm.Database import Database
from common.proto.context_pb2_grpc import add_ContextServiceServicer_to_server
from common.proto.context_policy_pb2_grpc import add_ContextPolicyServiceServicer_to_server
from common.tools.service.GenericGrpcService import GenericGrpcService
from .ContextServiceServicerImpl import ContextServiceServicerImpl

@@ -31,3 +32,4 @@ class ContextService(GenericGrpcService):

    def install_servicers(self):
        add_ContextServiceServicer_to_server(self.context_servicer, self.server)
        add_ContextPolicyServiceServicer_to_server(self.context_servicer, self.server)
Loading