Commit 2eabf46c authored by Carlos Natalino's avatar Carlos Natalino
Browse files

Initial modifications to harmonize L2NM and L3NM emulated service handlers.

parent 0ccf06c8
Loading
Loading
Loading
Loading
+0 −0

Empty file added.

+28 −23
Original line number Diff line number Diff line
@@ -14,18 +14,14 @@

import anytree, json, logging
from typing import Any, Dict, List, Optional, Tuple, Union
from common.orm.Database import Database
from common.orm.HighLevel import get_object
from common.orm.backend.Tools import key_to_str
from common.proto.context_pb2 import Device
from common.proto.context_pb2 import ConfigActionEnum, Device, Service
from common.tools.object_factory.ConfigRule import json_config_rule_delete, json_config_rule_set
from common.type_checkers.Checkers import chk_length, chk_type
from context.client.ContextClient import ContextClient
from device.client.DeviceClient import DeviceClient
from service.service.database.ConfigModel import ORM_ConfigActionEnum, get_config_rules
from service.service.database.ConfigModel import ORM_ConfigActionEnum
from service.service.database.ContextModel import ContextModel
from service.service.database.DeviceModel import DeviceModel
from service.service.database.ServiceModel import ServiceModel
from service.service.task_scheduler.TaskExecutor import TaskExecutor
from service.service.service_handler_api._ServiceHandler import _ServiceHandler
from service.service.service_handler_api.AnyTreeTools import TreeNode, delete_subnode, get_subnode, set_subnode_value

@@ -33,34 +29,43 @@ LOGGER = logging.getLogger(__name__)

class L3NMEmulatedServiceHandler(_ServiceHandler):
    def __init__(   # pylint: disable=super-init-not-called
        self, db_service : ServiceModel, database : Database, context_client : ContextClient,
        device_client : DeviceClient, **settings
        self, service : Service, task_executor : TaskExecutor, **settings
    ) -> None:
        self.__db_service = db_service
        self.__database = database
        self.__context_client = context_client # pylint: disable=unused-private-member
        self.__device_client = device_client

        self.__db_context : ContextModel = get_object(self.__database, ContextModel, self.__db_service.context_fk)
        str_service_key = key_to_str([self.__db_context.context_uuid, self.__db_service.service_uuid])
        db_config = get_config_rules(self.__database, str_service_key, 'running')
        self.__service = service
        self.__task_executor = task_executor # pylint: disable=unused-private-member
        self.__resolver = anytree.Resolver(pathattr='name')
        self.__config = TreeNode('.')
        for action, resource_key, resource_value in db_config:
            if action == ORM_ConfigActionEnum.SET:
        for config_rule in service.service_config.config_rules:

            action = config_rule.action
            if config_rule.WhichOneof('config_rule') != 'custom': continue
            resource_key = config_rule.custom.resource_key
            resource_value = config_rule.custom.resource_value
            if action == ConfigActionEnum.CONFIGACTION_SET:
                try:
                    resource_value = json.loads(resource_value)
                except: # pylint: disable=bare-except
                    pass
                set_subnode_value(self.__resolver, self.__config, resource_key, resource_value)
            elif action == ORM_ConfigActionEnum.DELETE:
            elif action == ConfigActionEnum.CONFIGACTION_DELETE:
                delete_subnode(self.__resolver, self.__config, resource_key)

    def SetEndpoint(self, endpoints : List[Tuple[str, str, Optional[str]]]) -> List[Union[bool, Exception]]:
            # if config_rule.action == ORM_ConfigActionEnum.SET:
            #     try:
            #         resource_value = json.loads(config_rule.resource_value)
            #     except: # pylint: disable=bare-except
            #         pass
            #     set_subnode_value(self.__resolver, self.__config, config_rule.resource_key, config_rule.resource_value)
            # elif config_rule.action == ORM_ConfigActionEnum.DELETE:
            #     delete_subnode(self.__resolver, self.__config, config_rule.resource_key)

    def SetEndpoint(
        self, endpoints : List[Tuple[str, str, Optional[str]]], connection_uuid : Optional[str] = None
    ) -> List[Union[bool, Exception]]:
        chk_type('endpoints', endpoints, list)
        if len(endpoints) == 0: return []

        service_uuid              = self.__db_service.service_uuid
        service_uuid              = self.__service.service_id.service_uuid.uuid
        service_short_uuid        = service_uuid.split('-')[-1]
        network_instance_name     = '{:s}-NetInst'.format(service_short_uuid)
        network_interface_desc    = '{:s}-NetIf'.format(service_uuid)
@@ -206,7 +211,7 @@ class L3NMEmulatedServiceHandler(_ServiceHandler):
        chk_type('endpoints', endpoints, list)
        if len(endpoints) == 0: return []

        service_uuid              = self.__db_service.service_uuid
        service_uuid              = self.__service.service_id.service_uuid.uuid
        service_short_uuid        = service_uuid.split('-')[-1]
        network_instance_name     = '{:s}-NetInst'.format(service_short_uuid)