From 6d7d39ed21cc2e8312cf9aee289fcdf8875e8131 Mon Sep 17 00:00:00 2001 From: Lluis Gifre <lluis.gifre@cttc.es> Date: Fri, 13 Aug 2021 17:30:54 +0200 Subject: [PATCH] Added basic REST-API-based NBI to Context service and associated unitary tests. --- src/context/requirements.in | 1 + src/context/service/__main__.py | 3 +- .../service/rest_server/resources/Context.py | 10 +++-- src/context/tests/test_unitary.py | 43 ++++++++++++++++--- 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/context/requirements.in b/src/context/requirements.in index 398b8d3d8..e80c645fc 100644 --- a/src/context/requirements.in +++ b/src/context/requirements.in @@ -5,3 +5,4 @@ prometheus-client pytest pytest-benchmark redis +requests diff --git a/src/context/service/__main__.py b/src/context/service/__main__.py index 0bcdb8056..e0a988868 100644 --- a/src/context/service/__main__.py +++ b/src/context/service/__main__.py @@ -45,7 +45,8 @@ def main(): grpc_service.start() rest_server = Server(port=restapi_service_port, base_url=restapi_base_url) - rest_server.add_resource(Context, '/restconf/config/context', endpoint='api.context') + rest_server.add_resource( + Context, '/restconf/config/context', endpoint='api.context', resource_class_args=(database,)) rest_server.start() # Wait for Ctrl+C or termination signal diff --git a/src/context/service/rest_server/resources/Context.py b/src/context/service/rest_server/resources/Context.py index 421ad4a2a..293ff24ed 100644 --- a/src/context/service/rest_server/resources/Context.py +++ b/src/context/service/rest_server/resources/Context.py @@ -1,9 +1,13 @@ from flask.json import jsonify from flask_restful import Resource -from common.database.Factory import get_database +from common.database.api.Database import Database from common.database.api.context.Constants import DEFAULT_CONTEXT_ID class Context(Resource): + def __init__(self, database : Database) -> None: + super().__init__() + self.database = database + def get(self): - database = get_database() - return jsonify(database.context(DEFAULT_CONTEXT_ID).dump()) + with self.database: + return jsonify(self.database.context(DEFAULT_CONTEXT_ID).dump()) diff --git a/src/context/tests/test_unitary.py b/src/context/tests/test_unitary.py index 4827488a7..b3a22c60d 100644 --- a/src/context/tests/test_unitary.py +++ b/src/context/tests/test_unitary.py @@ -1,16 +1,21 @@ -import copy, grpc, logging, pytest +import copy, grpc, logging, pytest, requests, time from google.protobuf.json_format import MessageToDict from common.database.Factory import get_database, DatabaseEngineEnum from common.database.api.Database import Database from common.database.api.context.Constants import DEFAULT_CONTEXT_ID, DEFAULT_TOPOLOGY_ID from common.database.tests.script import populate_example -from common.tests.Assertions import validate_empty, validate_link_id, validate_topology, validate_topology_has_devices, validate_topology_has_links, validate_topology_is_empty +from common.tests.Assertions import validate_empty, validate_link_id, validate_topology, validate_topology_has_devices,\ + validate_topology_has_links, validate_topology_is_empty from context.client.ContextClient import ContextClient -from context.proto.context_pb2 import Empty, Link, LinkId +from context.proto.context_pb2 import Empty, Link, LinkId, Topology from context.service.ContextService import ContextService -from context.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD +from context.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, RESTAPI_SERVICE_PORT, \ + RESTAPI_BASE_URL +from context.service.rest_server.Server import Server +from context.service.rest_server.resources.Context import Context -port = 10000 + GRPC_SERVICE_PORT # avoid privileged ports +grpc_port = 10000 + GRPC_SERVICE_PORT # avoid privileged ports +restapi_port = 10000 + RESTAPI_SERVICE_PORT # avoid privileged ports LOGGER = logging.getLogger(__name__) LOGGER.setLevel(logging.DEBUG) @@ -35,14 +40,25 @@ def context_database(): @pytest.fixture(scope='session') def context_service(context_database : Database): _service = ContextService( - context_database, port=port, max_workers=GRPC_MAX_WORKERS, grace_period=GRPC_GRACE_PERIOD) + context_database, port=grpc_port, max_workers=GRPC_MAX_WORKERS, grace_period=GRPC_GRACE_PERIOD) _service.start() yield _service _service.stop() +@pytest.fixture(scope='session') +def context_service_rest(context_database : Database): + _rest_server = Server(port=restapi_port, base_url=RESTAPI_BASE_URL) + _rest_server.add_resource( + Context, '/restconf/config/context', endpoint='api.context', resource_class_args=(context_database,)) + _rest_server.start() + time.sleep(1) # bring time for the server to start + yield _rest_server + _rest_server.shutdown() + _rest_server.join() + @pytest.fixture(scope='session') def context_client(context_service): - _client = ContextClient(address='127.0.0.1', port=port) + _client = ContextClient(address='127.0.0.1', port=grpc_port) yield _client _client.close() @@ -227,3 +243,16 @@ def test_get_topology_completed_2(context_client : ContextClient): validate_topology(topology) validate_topology_has_devices(topology) validate_topology_has_links(topology) + +def test_get_topology_completed_rest_api(context_service_rest : Server): + # should work + request_url = 'http://127.0.0.1:{}{}/restconf/config/context'.format(restapi_port, RESTAPI_BASE_URL) + reply = requests.get(request_url) + json_reply = reply.json() + topology = MessageToDict( + Topology(**json_reply['topologies'][0]), + including_default_value_fields=True, preserving_proto_field_name=True, + use_integers_for_enums=False) + validate_topology(topology) + validate_topology_has_devices(topology) + validate_topology_has_links(topology) -- GitLab