diff --git a/src/common/Constants.py b/src/common/Constants.py index c0b4cbf0511884148de34fdd891a256796d7d26a..a7bf198a7204677ed3669fc28a2c3528a5936425 100644 --- a/src/common/Constants.py +++ b/src/common/Constants.py @@ -83,7 +83,6 @@ DEFAULT_SERVICE_HTTP_PORTS = { # Default HTTP/REST-API service base URLs DEFAULT_SERVICE_HTTP_BASEURLS = { - ServiceNameEnum.CONTEXT .value : '/api', - ServiceNameEnum.COMPUTE .value : '/restconf/data', + ServiceNameEnum.COMPUTE .value : '/restconf', ServiceNameEnum.WEBUI .value : None, } diff --git a/src/common/tools/descriptor/Loader.py b/src/common/tools/descriptor/Loader.py index fc3b008b4004efe5afc270da65246c4635c777c3..5972d425be5298ec7fcb63bd28b50f3643363ae4 100644 --- a/src/common/tools/descriptor/Loader.py +++ b/src/common/tools/descriptor/Loader.py @@ -31,8 +31,8 @@ # for message,level in compose_notifications(results): # loggers.get(level)(message) -import json -from typing import Dict, List, Optional, Tuple, Union +import concurrent.futures, json, logging, operator +from typing import Any, Dict, List, Optional, Tuple, Union from common.proto.context_pb2 import Connection, Context, Device, Link, Service, Slice, Topology from context.client.ContextClient import ContextClient from device.client.DeviceClient import DeviceClient @@ -43,6 +43,8 @@ from .Tools import ( get_descriptors_add_contexts, get_descriptors_add_services, get_descriptors_add_slices, get_descriptors_add_topologies, split_devices_by_rules) +LOGGER = logging.getLogger(__name__) + ENTITY_TO_TEXT = { # name => singular, plural 'context' : ('Context', 'Contexts' ), @@ -79,7 +81,7 @@ def compose_notifications(results : TypeResults) -> TypeNotificationList: class DescriptorLoader: def __init__( - self, descriptors : Union[str, Dict], + self, descriptors : Union[str, Dict], num_workers : int = 1, context_client : Optional[ContextClient] = None, device_client : Optional[DeviceClient] = None, service_client : Optional[ServiceClient] = None, slice_client : Optional[SliceClient] = None ) -> None: @@ -93,6 +95,8 @@ class DescriptorLoader: self.__slices = self.__descriptors.get('slices' , []) self.__connections = self.__descriptors.get('connections', []) + self.__num_workers = num_workers + self.__contexts_add = None self.__topologies_add = None self.__devices_add = None @@ -242,12 +246,26 @@ class DescriptorLoader: #self.__dev_cli.close() #self.__ctx_cli.close() + @staticmethod + def worker(grpc_method, grpc_class, entity) -> Any: + return grpc_method(grpc_class(**entity)) + def _process_descr(self, entity_name, action_name, grpc_method, grpc_class, entities) -> None: num_ok, error_list = 0, [] - for entity in entities: - try: - grpc_method(grpc_class(**entity)) - num_ok += 1 - except Exception as e: # pylint: disable=broad-except - error_list.append(f'{str(entity)}: {str(e)}') + + with concurrent.futures.ThreadPoolExecutor(max_workers=self.__num_workers) as executor: + future_to_entity = { + executor.submit(DescriptorLoader.worker, grpc_method, grpc_class, entity): (i, entity) + for i,entity in enumerate(entities) + } + + for future in concurrent.futures.as_completed(future_to_entity): + i, entity = future_to_entity[future] + try: + _ = future.result() + num_ok += 1 + except Exception as e: # pylint: disable=broad-except + error_list.append((i, f'{str(entity)}: {str(e)}')) + + error_list = [str_error for _,str_error in sorted(error_list, key=operator.itemgetter(0))] self.__results.append((entity_name, action_name, num_ok, error_list)) diff --git a/src/compute/service/__main__.py b/src/compute/service/__main__.py index 998c4c98f21648f6e186ad91bae90875dac84fab..9705e3187ffff633a4d127855c1c57afcf397e39 100644 --- a/src/compute/service/__main__.py +++ b/src/compute/service/__main__.py @@ -26,7 +26,7 @@ from .rest_server.nbi_plugins.ietf_l2vpn import register_ietf_l2vpn terminate = threading.Event() LOGGER = None -def signal_handler(signal, frame): # pylint: disable=redefined-outer-name +def signal_handler(signal, frame): # pylint: disable=redefined-outer-name, unused-argument LOGGER.warning('Terminate signal received') terminate.set() diff --git a/src/compute/service/rest_server/nbi_plugins/debug_api/Resources.py b/src/compute/service/rest_server/nbi_plugins/debug_api/Resources.py index 0c66254d93220392d44c8393373ba94ddd7b3f93..67ef3dfb0ba1519440b0a22f46935165c8388cb8 100644 --- a/src/compute/service/rest_server/nbi_plugins/debug_api/Resources.py +++ b/src/compute/service/rest_server/nbi_plugins/debug_api/Resources.py @@ -12,48 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -from flask.json import jsonify from flask_restful import Resource -from common.proto.context_pb2 import ConnectionId, ContextId, DeviceId, Empty, LinkId, ServiceId, SliceId, TopologyId -from common.proto.policy_pb2 import PolicyRuleId -from common.tools.grpc.Tools import grpc_message_to_json -from common.tools.object_factory.Connection import json_connection_id -from common.tools.object_factory.Context import json_context_id -from common.tools.object_factory.Device import json_device_id -from common.tools.object_factory.Link import json_link_id -from common.tools.object_factory.PolicyRule import json_policyrule_id -from common.tools.object_factory.Service import json_service_id -from common.tools.object_factory.Slice import json_slice_id -from common.tools.object_factory.Topology import json_topology_id +from common.proto.context_pb2 import Empty from context.client.ContextClient import ContextClient - - -def format_grpc_to_json(grpc_reply): - return jsonify(grpc_message_to_json(grpc_reply)) - -def grpc_connection_id(connection_uuid): - return ConnectionId(**json_connection_id(connection_uuid)) - -def grpc_context_id(context_uuid): - return ContextId(**json_context_id(context_uuid)) - -def grpc_device_id(device_uuid): - return DeviceId(**json_device_id(device_uuid)) - -def grpc_link_id(link_uuid): - return LinkId(**json_link_id(link_uuid)) - -def grpc_service_id(context_uuid, service_uuid): - return ServiceId(**json_service_id(service_uuid, context_id=json_context_id(context_uuid))) - -def grpc_slice_id(context_uuid, slice_uuid): - return SliceId(**json_slice_id(slice_uuid, context_id=json_context_id(context_uuid))) - -def grpc_topology_id(context_uuid, topology_uuid): - return TopologyId(**json_topology_id(topology_uuid, context_id=json_context_id(context_uuid))) - -def grpc_policy_rule_id(policy_rule_uuid): - return PolicyRuleId(**json_policyrule_id(policy_rule_uuid)) +from .Tools import ( + format_grpc_to_json, grpc_connection_id, grpc_context_id, grpc_device_id, grpc_link_id, grpc_policy_rule_id, + grpc_service_id, grpc_slice_id, grpc_topology_id) class _Resource(Resource): diff --git a/src/compute/service/rest_server/nbi_plugins/debug_api/Tools.py b/src/compute/service/rest_server/nbi_plugins/debug_api/Tools.py new file mode 100644 index 0000000000000000000000000000000000000000..f3dff545ba9812ff3f4e13c3da53774af7626014 --- /dev/null +++ b/src/compute/service/rest_server/nbi_plugins/debug_api/Tools.py @@ -0,0 +1,54 @@ +# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +# +# 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. + +from flask.json import jsonify +from common.proto.context_pb2 import ConnectionId, ContextId, DeviceId, LinkId, ServiceId, SliceId, TopologyId +from common.proto.policy_pb2 import PolicyRuleId +from common.tools.grpc.Tools import grpc_message_to_json +from common.tools.object_factory.Connection import json_connection_id +from common.tools.object_factory.Context import json_context_id +from common.tools.object_factory.Device import json_device_id +from common.tools.object_factory.Link import json_link_id +from common.tools.object_factory.PolicyRule import json_policyrule_id +from common.tools.object_factory.Service import json_service_id +from common.tools.object_factory.Slice import json_slice_id +from common.tools.object_factory.Topology import json_topology_id + + +def format_grpc_to_json(grpc_reply): + return jsonify(grpc_message_to_json(grpc_reply)) + +def grpc_connection_id(connection_uuid): + return ConnectionId(**json_connection_id(connection_uuid)) + +def grpc_context_id(context_uuid): + return ContextId(**json_context_id(context_uuid)) + +def grpc_device_id(device_uuid): + return DeviceId(**json_device_id(device_uuid)) + +def grpc_link_id(link_uuid): + return LinkId(**json_link_id(link_uuid)) + +def grpc_service_id(context_uuid, service_uuid): + return ServiceId(**json_service_id(service_uuid, context_id=json_context_id(context_uuid))) + +def grpc_slice_id(context_uuid, slice_uuid): + return SliceId(**json_slice_id(slice_uuid, context_id=json_context_id(context_uuid))) + +def grpc_topology_id(context_uuid, topology_uuid): + return TopologyId(**json_topology_id(topology_uuid, context_id=json_context_id(context_uuid))) + +def grpc_policy_rule_id(policy_rule_uuid): + return PolicyRuleId(**json_policyrule_id(policy_rule_uuid)) diff --git a/src/compute/service/rest_server/nbi_plugins/debug_api/__init__.py b/src/compute/service/rest_server/nbi_plugins/debug_api/__init__.py index d9243cca711a2b2ad00509102ecab5b06c6cc334..d1309353c412a738e2f2238d0bb4fff07765b825 100644 --- a/src/compute/service/rest_server/nbi_plugins/debug_api/__init__.py +++ b/src/compute/service/rest_server/nbi_plugins/debug_api/__init__.py @@ -12,52 +12,48 @@ # See the License for the specific language governing permissions and # limitations under the License. -# RFC 8466 - L2VPN Service Model (L2SM) -# Ref: https://datatracker.ietf.org/doc/html/rfc8466 - from compute.service.rest_server.RestServer import RestServer from .Resources import ( Connection, ConnectionIds, Connections, Context, ContextIds, Contexts, Device, DeviceIds, Devices, Link, LinkIds, Links, PolicyRule, PolicyRuleIds, PolicyRules, Service, ServiceIds, Services, Slice, SliceIds, Slices, Topologies, Topology, TopologyIds) -URL_PREFIX = '/api' +URL_PREFIX = '/debug-api' -# Use 'path' type in Service and Sink because service_uuid and link_uuid might contain char '/' and Flask is unable to -# recognize them in 'string' type. +# Use 'path' type since some identifiers might contain char '/' and Flask is unable to recognize them in 'string' type. RESOURCES = [ # (endpoint_name, resource_class, resource_url) ('api.context_ids', ContextIds, '/context_ids'), ('api.contexts', Contexts, '/contexts'), - ('api.context', Context, '/context/<string:context_uuid>'), + ('api.context', Context, '/context/<path:context_uuid>'), - ('api.topology_ids', TopologyIds, '/context/<string:context_uuid>/topology_ids'), - ('api.topologies', Topologies, '/context/<string:context_uuid>/topologies'), - ('api.topology', Topology, '/context/<string:context_uuid>/topology/<string:topology_uuid>'), + ('api.topology_ids', TopologyIds, '/context/<path:context_uuid>/topology_ids'), + ('api.topologies', Topologies, '/context/<path:context_uuid>/topologies'), + ('api.topology', Topology, '/context/<path:context_uuid>/topology/<path:topology_uuid>'), - ('api.service_ids', ServiceIds, '/context/<string:context_uuid>/service_ids'), - ('api.services', Services, '/context/<string:context_uuid>/services'), - ('api.service', Service, '/context/<string:context_uuid>/service/<path:service_uuid>'), + ('api.service_ids', ServiceIds, '/context/<path:context_uuid>/service_ids'), + ('api.services', Services, '/context/<path:context_uuid>/services'), + ('api.service', Service, '/context/<path:context_uuid>/service/<path:service_uuid>'), - ('api.slice_ids', SliceIds, '/context/<string:context_uuid>/slice_ids'), - ('api.slices', Slices, '/context/<string:context_uuid>/slices'), - ('api.slice', Slice, '/context/<string:context_uuid>/slice/<path:slice_uuid>'), + ('api.slice_ids', SliceIds, '/context/<path:context_uuid>/slice_ids'), + ('api.slices', Slices, '/context/<path:context_uuid>/slices'), + ('api.slice', Slice, '/context/<path:context_uuid>/slice/<path:slice_uuid>'), ('api.device_ids', DeviceIds, '/device_ids'), ('api.devices', Devices, '/devices'), - ('api.device', Device, '/device/<string:device_uuid>'), + ('api.device', Device, '/device/<path:device_uuid>'), ('api.link_ids', LinkIds, '/link_ids'), ('api.links', Links, '/links'), ('api.link', Link, '/link/<path:link_uuid>'), - ('api.connection_ids', ConnectionIds, '/context/<string:context_uuid>/service/<path:service_uuid>/connection_ids'), - ('api.connections', Connections, '/context/<string:context_uuid>/service/<path:service_uuid>/connections'), + ('api.connection_ids', ConnectionIds, '/context/<path:context_uuid>/service/<path:service_uuid>/connection_ids'), + ('api.connections', Connections, '/context/<path:context_uuid>/service/<path:service_uuid>/connections'), ('api.connection', Connection, '/connection/<path:connection_uuid>'), ('api.policyrule_ids', PolicyRuleIds, '/policyrule_ids'), ('api.policyrules', PolicyRules, '/policyrules'), - ('api.policyrule', PolicyRule, '/policyrule/<string:policyrule_uuid>'), + ('api.policyrule', PolicyRule, '/policyrule/<path:policyrule_uuid>'), ] def register_debug_api(rest_server : RestServer): diff --git a/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/__init__.py b/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/__init__.py index 1b9027b1feb7c65c6fb3ee6ecdef485e4719a1b5..110c51af5fe0e4cd8e012fd4105712ed176dd12a 100644 --- a/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/__init__.py +++ b/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/__init__.py @@ -21,7 +21,7 @@ from .L2VPN_Services import L2VPN_Services from .L2VPN_Service import L2VPN_Service from .L2VPN_SiteNetworkAccesses import L2VPN_SiteNetworkAccesses -URL_PREFIX = '/ietf-l2vpn-svc:l2vpn-svc' +URL_PREFIX = '/data/ietf-l2vpn-svc:l2vpn-svc' def _add_resource(rest_server : RestServer, resource : Resource, *urls, **kwargs): urls = [(URL_PREFIX + url) for url in urls] diff --git a/src/device/service/DeviceServiceServicerImpl.py b/src/device/service/DeviceServiceServicerImpl.py index 0258380a14df03204a7cb77c5a2d8b39aa3c64cc..be40e64ecd25a5c46c23d5ec0a73a2484b65691d 100644 --- a/src/device/service/DeviceServiceServicerImpl.py +++ b/src/device/service/DeviceServiceServicerImpl.py @@ -109,7 +109,7 @@ class DeviceServiceServicerImpl(DeviceServiceServicer): if device is None: raise NotFoundException('Device', device_uuid, extra_details='loading in ConfigureDevice') - driver : _Driver = self.driver_instance_cache.get(device_uuid) + driver : _Driver = get_driver(self.driver_instance_cache, device) if driver is None: msg = ERROR_MISSING_DRIVER.format(device_uuid=str(device_uuid)) raise OperationFailedException('ConfigureDevice', extra_details=msg) @@ -150,6 +150,11 @@ class DeviceServiceServicerImpl(DeviceServiceServicer): self.mutex_queues.wait_my_turn(device_uuid) try: context_client = ContextClient() + device = get_device(context_client, device_uuid, rw_copy=False) + if device is None: + raise NotFoundException('Device', device_uuid, extra_details='loading in DeleteDevice') + device_uuid = device.device_id.device_uuid.uuid + self.monitoring_loops.remove_device(device_uuid) self.driver_instance_cache.delete(device_uuid) context_client.RemoveDevice(request) @@ -163,7 +168,12 @@ class DeviceServiceServicerImpl(DeviceServiceServicer): self.mutex_queues.wait_my_turn(device_uuid) try: - driver : _Driver = self.driver_instance_cache.get(device_uuid) + context_client = ContextClient() + device = get_device(context_client, device_uuid, rw_copy=False) + if device is None: + raise NotFoundException('Device', device_uuid, extra_details='loading in DeleteDevice') + + driver : _Driver = get_driver(self.driver_instance_cache, device) if driver is None: msg = ERROR_MISSING_DRIVER.format(device_uuid=str(device_uuid)) raise OperationFailedException('GetInitialConfig', extra_details=msg) @@ -197,7 +207,12 @@ class DeviceServiceServicerImpl(DeviceServiceServicer): self.mutex_queues.wait_my_turn(device_uuid) try: - driver : _Driver = self.driver_instance_cache.get(device_uuid) + context_client = ContextClient() + device = get_device(context_client, device_uuid, rw_copy=False) + if device is None: + raise NotFoundException('Device', device_uuid, extra_details='loading in DeleteDevice') + + driver : _Driver = get_driver(self.driver_instance_cache, device) if driver is None: msg = ERROR_MISSING_DRIVER.format(device_uuid=str(device_uuid)) raise OperationFailedException('MonitorDeviceKpi', extra_details=msg) diff --git a/src/webui/service/main/routes.py b/src/webui/service/main/routes.py index 38d13aad562f3e55490952db84ef784f87697739..dcbbf71a6fee6ebd040f14c7d0d2cb07ba9ee085 100644 --- a/src/webui/service/main/routes.py +++ b/src/webui/service/main/routes.py @@ -34,6 +34,8 @@ slice_client = SliceClient() LOGGER = logging.getLogger(__name__) +DESCRIPTOR_LOADER_NUM_WORKERS = 10 + def process_descriptors(descriptors): try: descriptors_file = request.files[descriptors.name] @@ -43,7 +45,7 @@ def process_descriptors(descriptors): flash(f'Unable to load descriptor file: {str(e)}', 'danger') return - descriptor_loader = DescriptorLoader(descriptors) + descriptor_loader = DescriptorLoader(descriptors, num_workers=DESCRIPTOR_LOADER_NUM_WORKERS) results = descriptor_loader.process() for message,level in compose_notifications(results): if level == 'error': LOGGER.warning('ERROR message={:s}'.format(str(message)))