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

Service component:

- Enhanced generic SettingsHandler with a method to dump config rules stored.
- Extended L2NMEmulated Service Handler to support per-endpoint settings provided as device initialization parameters
parent a6e8d850
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ import anytree, json, logging
from typing import Any, List, Optional, Tuple, Union
from common.proto.context_pb2 import ConfigActionEnum, ConfigRule, Device, EndPoint, ServiceConfig
from common.tools.grpc.Tools import grpc_message_to_json, grpc_message_to_json_string
from service.service.service_handler_api.AnyTreeTools import TreeNode, delete_subnode, get_subnode, set_subnode_value
from .AnyTreeTools import TreeNode, delete_subnode, dump_subtree, get_subnode, set_subnode_value

LOGGER = logging.getLogger(__name__)

@@ -86,3 +86,6 @@ class SettingsHandler:
            MSG = 'Unsupported Action({:s}) in ConfigRule({:s})'
            LOGGER.warning(MSG.format(str(action), grpc_message_to_json_string(config_rule)))
            return

    def dump_config_rules(self) -> List[Tuple[Any, Any]]:
        return dump_subtree(self.__config)
+44 −2
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
import json, logging
from typing import Any, List, Optional, Tuple, Union
from common.method_wrappers.Decorator import MetricsPool, metered_subclass_method
from common.proto.context_pb2 import ConfigRule, DeviceId, Service
from common.proto.context_pb2 import ConfigActionEnum, ConfigRule, DeviceId, Service
from common.tools.object_factory.Device import json_device_id
from common.type_checkers.Checkers import chk_type
from service.service.service_handler_api.Tools import get_device_endpoint_uuids, get_endpoint_matching
@@ -52,9 +52,30 @@ class L2NMEmulatedServiceHandler(_ServiceHandler):
                device_uuid, endpoint_uuid = get_device_endpoint_uuids(endpoint)

                device_obj = self.__task_executor.get_device(DeviceId(**json_device_id(device_uuid)))
                device_name = device_obj.name

                for config_rule in device_obj.device_config.config_rules:
                    raw_data = SettingsHandler._config_rule_to_raw(config_rule)
                    if raw_data is None: continue
                    action, key_or_path, value = raw_data
                    if action != ConfigActionEnum.CONFIGACTION_SET: continue
                    if not key_or_path.startswith('/endpoints/endpoint['): continue
                    if not key_or_path.endswith(']/settings'): continue
                    key_or_path = key_or_path.replace('/endpoints/', '/device[{:s}]/'.format(device_name))
                    LOGGER.debug('Setting key_or_path={:s} value={:s}'.format(str(key_or_path), str(value)))
                    self.__settings_handler.set(key_or_path, value)

                service_config_rules = self.__settings_handler.dump_config_rules()
                LOGGER.debug('service_config_rules={:s}'.format(str(service_config_rules)))

                endpoint_obj = get_endpoint_matching(device_obj, endpoint_uuid)
                endpoint_settings = self.__settings_handler.get_endpoint_settings(device_obj, endpoint_obj)
                endpoint_name = endpoint_obj.name
                endpoint_settings = self.__settings_handler.get_endpoint_settings(device_obj, endpoint_obj)
                MSG = 'device_uuid={:s} device_name={:s} endpoint_uuid={:s} endpoint_name={:s} endpoint_settings={:s}'
                str_endpoint_settings = str(None) if endpoint_settings is None else str(endpoint_settings.value)
                LOGGER.debug(MSG.format(
                    str(device_uuid), str(device_name), str(endpoint_uuid), str(endpoint_name), str_endpoint_settings
                ))

                json_config_rules = setup_config_rules(
                    service_uuid, connection_uuid, device_uuid, endpoint_uuid, endpoint_name,
@@ -89,9 +110,30 @@ class L2NMEmulatedServiceHandler(_ServiceHandler):
                device_uuid, endpoint_uuid = get_device_endpoint_uuids(endpoint)

                device_obj = self.__task_executor.get_device(DeviceId(**json_device_id(device_uuid)))
                device_name = device_obj.name

                for config_rule in device_obj.device_config.config_rules:
                    raw_data = SettingsHandler._config_rule_to_raw(config_rule)
                    if raw_data is None: continue
                    action, key_or_path, value = raw_data
                    if action != ConfigActionEnum.CONFIGACTION_SET: continue
                    if not key_or_path.startswith('/endpoints/endpoint['): continue
                    if not key_or_path.endswith(']/settings'): continue
                    key_or_path = key_or_path.replace('/endpoints/', '/device[{:s}]/'.format(device_name))
                    LOGGER.debug('Setting key_or_path={:s} value={:s}'.format(str(key_or_path), str(value)))
                    self.__settings_handler.set(key_or_path, value)

                service_config_rules = self.__settings_handler.dump_config_rules()
                LOGGER.debug('service_config_rules={:s}'.format(str(service_config_rules)))

                endpoint_obj = get_endpoint_matching(device_obj, endpoint_uuid)
                endpoint_settings = self.__settings_handler.get_endpoint_settings(device_obj, endpoint_obj)
                endpoint_name = endpoint_obj.name
                MSG = 'device_uuid={:s} device_name={:s} endpoint_uuid={:s} endpoint_name={:s} endpoint_settings={:s}'
                str_endpoint_settings = str(None) if endpoint_settings is None else str(endpoint_settings.value)
                LOGGER.debug(MSG.format(
                    str(device_uuid), str(device_name), str(endpoint_uuid), str(endpoint_name), str_endpoint_settings
                ))

                json_config_rules = teardown_config_rules(
                    service_uuid, connection_uuid, device_uuid, endpoint_uuid, endpoint_name,