diff --git a/src/service/service/service_handler_api/SettingsHandler.py b/src/service/service/service_handler_api/SettingsHandler.py index 85dd3a12851bf8c5ba697180fe00d0467e7a76b5..a58db00cea142f68eca9778e126bb3dbf8351470 100644 --- a/src/service/service/service_handler_api/SettingsHandler.py +++ b/src/service/service/service_handler_api/SettingsHandler.py @@ -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) diff --git a/src/service/service/service_handlers/l2nm_emulated/L2NMEmulatedServiceHandler.py b/src/service/service/service_handlers/l2nm_emulated/L2NMEmulatedServiceHandler.py index 416c10f72fe2199ce241c4d527d9c58ce93d2b44..8bd16442233c8cf23fcb43b452f8de97ac9cab4c 100644 --- a/src/service/service/service_handlers/l2nm_emulated/L2NMEmulatedServiceHandler.py +++ b/src/service/service/service_handlers/l2nm_emulated/L2NMEmulatedServiceHandler.py @@ -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,