diff --git a/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py b/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py index ce60bdea3a7ab08b8dc24dd2e7c2efe4ecf81ae0..f360e318127706b4b4c8fdc4130dfdfc0ba711c0 100644 --- a/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py +++ b/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py @@ -15,33 +15,71 @@ import json from flask.json import jsonify from flask_restful import Resource, request +from werkzeug.exceptions import BadRequest from common.proto.context_pb2 import Empty from common.tools.grpc.Tools import grpc_message_to_json from context.client.ContextClient import ContextClient +from device.client.DeviceClient import DeviceClient from service.client.ServiceClient import ServiceClient +from slice.client.SliceClient import SliceClient 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_service, grpc_slice_id, grpc_topology_id) + format_grpc_to_json, grpc_connection_id, grpc_context, grpc_context_id, grpc_device, + grpc_device_id, grpc_link, grpc_link_id, grpc_policy_rule_id, + grpc_service_id, grpc_service, grpc_slice, grpc_slice_id, grpc_topology, grpc_topology_id +) class _Resource(Resource): def __init__(self) -> None: super().__init__() - self.client = ContextClient() + self.context_client = ContextClient() + self.device_client = DeviceClient() self.service_client = ServiceClient() + self.slice_client = SliceClient() class ContextIds(_Resource): def get(self): - return format_grpc_to_json(self.client.ListContextIds(Empty())) + return format_grpc_to_json(self.context_client.ListContextIds(Empty())) class Contexts(_Resource): def get(self): - return format_grpc_to_json(self.client.ListContexts(Empty())) + return format_grpc_to_json(self.context_client.ListContexts(Empty())) + + def post(self): + json_requests = request.get_json() + if 'contexts' in json_requests: + json_requests = json_requests['contexts'] + return [ + format_grpc_to_json(self.context_client.SetContext(grpc_context(context))) + for context in json_requests + ] + +class Context(_Resource): + def get(self, context_uuid : str): + return format_grpc_to_json(self.context_client.GetContext(grpc_context_id(context_uuid))) + + def put(self, context_uuid : str): + context = request.get_json() + if context_uuid != context['context_id']['context_uuid']['uuid']: + raise BadRequest('Mismatching context_uuid') + return format_grpc_to_json(self.context_client.SetContext(grpc_context(context))) + + def delete(self, context_uuid : str): + return format_grpc_to_json(self.context_client.RemoveContext(grpc_context_id(context_uuid))) class DummyContexts(_Resource): def get(self): - contexts = grpc_message_to_json(self.client.ListContexts(Empty()), use_integers_for_enums=True)['contexts'] - devices = grpc_message_to_json(self.client.ListDevices(Empty()), use_integers_for_enums=True)['devices'] - links = grpc_message_to_json(self.client.ListLinks(Empty()), use_integers_for_enums=True)['links'] + contexts = grpc_message_to_json( + self.context_client.ListContexts(Empty()), + use_integers_for_enums=True + )['contexts'] + devices = grpc_message_to_json( + self.context_client.ListDevices(Empty()), + use_integers_for_enums=True + )['devices'] + links = grpc_message_to_json( + self.context_client.ListLinks(Empty()), + use_integers_for_enums=True + )['links'] topologies = list() slices = list() @@ -53,17 +91,17 @@ class DummyContexts(_Resource): context_id = grpc_context_id(context_uuid) topologies.extend(grpc_message_to_json( - self.client.ListTopologies(context_id), + self.context_client.ListTopologies(context_id), use_integers_for_enums=True )['topologies']) slices.extend(grpc_message_to_json( - self.client.ListSlices(context_id), + self.context_client.ListSlices(context_id), use_integers_for_enums=True )['slices']) context_services = grpc_message_to_json( - self.client.ListServices(context_id), + self.context_client.ListServices(context_id), use_integers_for_enums=True )['services'] services.extend(context_services) @@ -72,7 +110,7 @@ class DummyContexts(_Resource): service_uuid = service['service_id']['service_uuid']['uuid'] service_id = grpc_service_id(context_uuid, service_uuid) connections.extend(grpc_message_to_json( - self.client.ListConnections(service_id), + self.context_client.ListConnections(service_id), use_integers_for_enums=True )['connections']) @@ -97,115 +135,191 @@ class DummyContexts(_Resource): if len(connections) > 0: dummy_context['connections'] = connections return jsonify(dummy_context) -class Context(_Resource): - def get(self, context_uuid : str): - return format_grpc_to_json(self.client.GetContext(grpc_context_id(context_uuid))) - class TopologyIds(_Resource): def get(self, context_uuid : str): - return format_grpc_to_json(self.client.ListTopologyIds(grpc_context_id(context_uuid))) + return format_grpc_to_json(self.context_client.ListTopologyIds(grpc_context_id(context_uuid))) class Topologies(_Resource): def get(self, context_uuid : str): - return format_grpc_to_json(self.client.ListTopologies(grpc_context_id(context_uuid))) + return format_grpc_to_json(self.context_client.ListTopologies(grpc_context_id(context_uuid))) + + def post(self, context_uuid : str): + json_requests = request.get_json() + if 'topologies' in json_requests: + json_requests = json_requests['topologies'] + for topology in json_requests: + if context_uuid != topology['topology_id']['context_id']['context_uuid']['uuid']: + raise BadRequest('Mismatching context_uuid') + return [ + format_grpc_to_json(self.context_client.SetTopology(grpc_topology(**topology))) + for topology in json_requests + ] class Topology(_Resource): def get(self, context_uuid : str, topology_uuid : str): - return format_grpc_to_json(self.client.GetTopology(grpc_topology_id(context_uuid, topology_uuid))) + return format_grpc_to_json(self.context_client.GetTopology(grpc_topology_id(context_uuid, topology_uuid))) + + def put(self, context_uuid : str, topology_uuid : str): + topology = request.get_json() + if context_uuid != topology['topology_id']['context_id']['context_uuid']['uuid']: + raise BadRequest('Mismatching context_uuid') + if topology_uuid != topology['topology_id']['topology_uuid']['uuid']: + raise BadRequest('Mismatching topology_uuid') + return format_grpc_to_json(self.context_client.SetTopology(grpc_topology(topology))) + + def delete(self, context_uuid : str, topology_uuid : str): + return format_grpc_to_json(self.context_client.RemoveTopology(grpc_topology_id(context_uuid, topology_uuid))) class ServiceIds(_Resource): def get(self, context_uuid : str): - return format_grpc_to_json(self.client.ListServiceIds(grpc_context_id(context_uuid))) + return format_grpc_to_json(self.context_client.ListServiceIds(grpc_context_id(context_uuid))) class Services(_Resource): def get(self, context_uuid : str): - return format_grpc_to_json(self.client.ListServices(grpc_context_id(context_uuid))) + return format_grpc_to_json(self.context_client.ListServices(grpc_context_id(context_uuid))) + + def post(self, context_uuid : str): + json_requests = request.get_json() + if 'services' in json_requests: + json_requests = json_requests['services'] + for service in json_requests: + if context_uuid != service['service_id']['context_id']['context_uuid']['uuid']: + raise BadRequest('Mismatching context_uuid') + return [ + format_grpc_to_json(self.service_client.CreateService(grpc_service(**service))) + for service in json_requests + ] class Service(_Resource): def get(self, context_uuid : str, service_uuid : str): - return format_grpc_to_json(self.client.GetService(grpc_service_id(context_uuid, service_uuid))) - - def post(self, context_uuid : str, service_uuid : str): # pylint: disable=unused-argument - service = request.get_json()['services'][0] - return format_grpc_to_json(self.service_client.CreateService(grpc_service( - service_uuid = service['service_id']['service_uuid']['uuid'], - service_type = service['service_type'], - context_uuid = service['service_id']['context_id']['context_uuid']['uuid'], - ))) - - def put(self, context_uuid : str, service_uuid : str): # pylint: disable=unused-argument - service = request.get_json()['services'][0] - return format_grpc_to_json(self.service_client.UpdateService(grpc_service( - service_uuid = service['service_id']['service_uuid']['uuid'], - service_type = service['service_type'], - context_uuid = service['service_id']['context_id']['context_uuid']['uuid'], - status = service['service_status']['service_status'], - endpoint_ids = service['service_endpoint_ids'], - constraints = service['service_constraints'], - config_rules = service['service_config']['config_rules'] - ))) + return format_grpc_to_json(self.context_client.GetService(grpc_service_id(context_uuid, service_uuid))) + + def put(self, context_uuid : str, service_uuid : str): + service = request.get_json() + if context_uuid != service['service_id']['context_id']['context_uuid']['uuid']: + raise BadRequest('Mismatching context_uuid') + if service_uuid != service['service_id']['service_uuid']['uuid']: + raise BadRequest('Mismatching service_uuid') + return format_grpc_to_json(self.service_client.UpdateService(grpc_service(service))) def delete(self, context_uuid : str, service_uuid : str): - return format_grpc_to_json(self.service_client.DeleteService(grpc_service_id( - context_uuid, service_uuid, - ))) + return format_grpc_to_json(self.service_client.DeleteService(grpc_service_id(context_uuid, service_uuid))) class SliceIds(_Resource): def get(self, context_uuid : str): - return format_grpc_to_json(self.client.ListSliceIds(grpc_context_id(context_uuid))) + return format_grpc_to_json(self.context_client.ListSliceIds(grpc_context_id(context_uuid))) class Slices(_Resource): def get(self, context_uuid : str): - return format_grpc_to_json(self.client.ListSlices(grpc_context_id(context_uuid))) + return format_grpc_to_json(self.context_client.ListSlices(grpc_context_id(context_uuid))) + + def post(self, context_uuid : str): + json_requests = request.get_json() + if 'slices' in json_requests: + json_requests = json_requests['slices'] + for slice_ in json_requests: + if context_uuid != slice_['slice_id']['context_id']['context_uuid']['uuid']: + raise BadRequest('Mismatching context_uuid') + return [ + format_grpc_to_json(self.slice_client.CreateSlice(grpc_slice(**slice_))) + for slice_ in json_requests + ] class Slice(_Resource): def get(self, context_uuid : str, slice_uuid : str): - return format_grpc_to_json(self.client.GetSlice(grpc_slice_id(context_uuid, slice_uuid))) + return format_grpc_to_json(self.context_client.GetSlice(grpc_slice_id(context_uuid, slice_uuid))) + + def put(self, context_uuid : str, slice_uuid : str): + slice_ = request.get_json() + if context_uuid != slice_['slice_id']['context_id']['context_uuid']['uuid']: + raise BadRequest('Mismatching context_uuid') + if slice_uuid != slice_['slice_id']['slice_uuid']['uuid']: + raise BadRequest('Mismatching slice_uuid') + return format_grpc_to_json(self.slice_client.UpdateSlice(grpc_slice(slice_))) + + def delete(self, context_uuid : str, slice_uuid : str): + return format_grpc_to_json(self.slice_client.DeleteSlice(grpc_slice_id(context_uuid, slice_uuid))) class DeviceIds(_Resource): def get(self): - return format_grpc_to_json(self.client.ListDeviceIds(Empty())) + return format_grpc_to_json(self.context_client.ListDeviceIds(Empty())) class Devices(_Resource): def get(self): - return format_grpc_to_json(self.client.ListDevices(Empty())) + return format_grpc_to_json(self.context_client.ListDevices(Empty())) + + def post(self): + json_requests = request.get_json() + if 'devices' in json_requests: + json_requests = json_requests['devices'] + return [ + format_grpc_to_json(self.device_client.AddDevice(grpc_device(device))) + for device in json_requests + ] class Device(_Resource): def get(self, device_uuid : str): - return format_grpc_to_json(self.client.GetDevice(grpc_device_id(device_uuid))) + return format_grpc_to_json(self.context_client.GetDevice(grpc_device_id(device_uuid))) + + def put(self, device_uuid : str): + device = request.get_json() + if device_uuid != device['device_id']['device_uuid']['uuid']: + raise BadRequest('Mismatching device_uuid') + return format_grpc_to_json(self.device_client.ConfigureDevice(grpc_device(device))) + + def delete(self, device_uuid : str): + return format_grpc_to_json(self.device_client.DeleteDevice(grpc_device_id(device_uuid))) class LinkIds(_Resource): def get(self): - return format_grpc_to_json(self.client.ListLinkIds(Empty())) + return format_grpc_to_json(self.context_client.ListLinkIds(Empty())) class Links(_Resource): def get(self): - return format_grpc_to_json(self.client.ListLinks(Empty())) + return format_grpc_to_json(self.context_client.ListLinks(Empty())) + + def post(self): + json_requests = request.get_json() + if 'links' in json_requests: + json_requests = json_requests['links'] + return [ + format_grpc_to_json(self.context_client.SetLink(grpc_link(link))) + for link in json_requests + ] class Link(_Resource): def get(self, link_uuid : str): - return format_grpc_to_json(self.client.GetLink(grpc_link_id(link_uuid))) + return format_grpc_to_json(self.context_client.GetLink(grpc_link_id(link_uuid))) + + def put(self, link_uuid : str): + link = request.get_json() + if link_uuid != link['link_id']['link_uuid']['uuid']: + raise BadRequest('Mismatching link_uuid') + return format_grpc_to_json(self.context_client.SetLink(grpc_link(link))) + + def delete(self, link_uuid : str): + return format_grpc_to_json(self.context_client.RemoveLink(grpc_link_id(link_uuid))) class ConnectionIds(_Resource): def get(self, context_uuid : str, service_uuid : str): - return format_grpc_to_json(self.client.ListConnectionIds(grpc_service_id(context_uuid, service_uuid))) + return format_grpc_to_json(self.context_client.ListConnectionIds(grpc_service_id(context_uuid, service_uuid))) class Connections(_Resource): def get(self, context_uuid : str, service_uuid : str): - return format_grpc_to_json(self.client.ListConnections(grpc_service_id(context_uuid, service_uuid))) + return format_grpc_to_json(self.context_client.ListConnections(grpc_service_id(context_uuid, service_uuid))) class Connection(_Resource): def get(self, connection_uuid : str): - return format_grpc_to_json(self.client.GetConnection(grpc_connection_id(connection_uuid))) + return format_grpc_to_json(self.context_client.GetConnection(grpc_connection_id(connection_uuid))) class PolicyRuleIds(_Resource): def get(self): - return format_grpc_to_json(self.client.ListPolicyRuleIds(Empty())) + return format_grpc_to_json(self.context_client.ListPolicyRuleIds(Empty())) class PolicyRules(_Resource): def get(self): - return format_grpc_to_json(self.client.ListPolicyRules(Empty())) + return format_grpc_to_json(self.context_client.ListPolicyRules(Empty())) class PolicyRule(_Resource): def get(self, policy_rule_uuid : str): - return format_grpc_to_json(self.client.GetPolicyRule(grpc_policy_rule_id(policy_rule_uuid))) + return format_grpc_to_json(self.context_client.GetPolicyRule(grpc_policy_rule_id(policy_rule_uuid))) diff --git a/src/nbi/service/rest_server/nbi_plugins/tfs_api/Tools.py b/src/nbi/service/rest_server/nbi_plugins/tfs_api/Tools.py index 1f69ffffb8c97a83591ec626920b57f40d032783..bb10ee375f0ecdf7b63459b300dd0ff0fed40615 100644 --- a/src/nbi/service/rest_server/nbi_plugins/tfs_api/Tools.py +++ b/src/nbi/service/rest_server/nbi_plugins/tfs_api/Tools.py @@ -12,21 +12,20 @@ # See the License for the specific language governing permissions and # limitations under the License. +from typing import Dict from flask.json import jsonify from common.proto.context_pb2 import ( - ConnectionId, ContextId, DeviceId, LinkId, ServiceId, SliceId, TopologyId, Service, ServiceStatusEnum + ConnectionId, Context, ContextId, Device, DeviceId, Link, LinkId, + ServiceId, Slice, SliceId, Topology, TopologyId, Service ) -from common.proto.policy_pb2 import PolicyRuleId +from common.proto.policy_pb2 import PolicyRule, 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.ConfigRule import json_config_rule -from common.tools.object_factory.Constraint import json_constraint_custom -from common.tools.object_factory.EndPoint import json_endpoint_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, json_service +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 @@ -40,51 +39,41 @@ def grpc_connection_id(connection_uuid): def grpc_context_id(context_uuid): return ContextId(**json_context_id(context_uuid)) +def grpc_context(json_context : Dict): + return Context(**json_context) + def grpc_device_id(device_uuid): return DeviceId(**json_device_id(device_uuid)) +def grpc_device(json_device : Dict): + return Device(**json_device) + def grpc_link_id(link_uuid): return LinkId(**json_link_id(link_uuid)) +def grpc_link(json_link : Dict): + return Link(**json_link) + def grpc_service_id(context_uuid, service_uuid): return ServiceId(**json_service_id(service_uuid, context_id=json_context_id(context_uuid))) -def grpc_service( - service_uuid, service_type, context_uuid, status=None, endpoint_ids=None, constraints=None, config_rules=None -): - json_context = json_context_id(context_uuid) - json_status = status if status else ServiceStatusEnum.SERVICESTATUS_PLANNED - json_endpoints_ids = [ - json_endpoint_id( - json_device_id(endpoint_id['device_id']['device_uuid']['uuid']), - endpoint_id['endpoint_uuid']['uuid'] - ) - for endpoint_id in endpoint_ids - ] if endpoint_ids else [] - json_constraints = [ - json_constraint_custom( - constraint['custom']['constraint_type'], - constraint['custom']['constraint_value'] - ) - for constraint in constraints - ] if constraints else [] - json_config_rules = [ - json_config_rule( - config_rule['action'], - config_rule['custom']['resource_key'], - config_rule['custom']['resource_value'] - ) - for config_rule in config_rules - ] if config_rules else [] - return Service(**json_service( - service_uuid, service_type, json_context, json_status, - json_endpoints_ids, json_constraints, json_config_rules)) +def grpc_service(json_service : Dict): + return Service(**json_service) def grpc_slice_id(context_uuid, slice_uuid): return SliceId(**json_slice_id(slice_uuid, context_id=json_context_id(context_uuid))) - + +def grpc_slice(json_slice : Dict): + return Slice(**json_slice) + def grpc_topology_id(context_uuid, topology_uuid): return TopologyId(**json_topology_id(topology_uuid, context_id=json_context_id(context_uuid))) +def grpc_topology(json_topology : Dict): + return Topology(**json_topology) + def grpc_policy_rule_id(policy_rule_uuid): return PolicyRuleId(**json_policyrule_id(policy_rule_uuid)) + +def grpc_policy_rule(json_policy_rule : Dict): + return PolicyRule(**json_policy_rule)