Skip to content
Snippets Groups Projects
Commit 6d7d39ed authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Added basic REST-API-based NBI to Context service and associated unitary tests.

parent 79757081
No related branches found
No related tags found
1 merge request!54Release 2.0.0
...@@ -5,3 +5,4 @@ prometheus-client ...@@ -5,3 +5,4 @@ prometheus-client
pytest pytest
pytest-benchmark pytest-benchmark
redis redis
requests
...@@ -45,7 +45,8 @@ def main(): ...@@ -45,7 +45,8 @@ def main():
grpc_service.start() grpc_service.start()
rest_server = Server(port=restapi_service_port, base_url=restapi_base_url) 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() rest_server.start()
# Wait for Ctrl+C or termination signal # Wait for Ctrl+C or termination signal
......
from flask.json import jsonify from flask.json import jsonify
from flask_restful import Resource 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 from common.database.api.context.Constants import DEFAULT_CONTEXT_ID
class Context(Resource): class Context(Resource):
def __init__(self, database : Database) -> None:
super().__init__()
self.database = database
def get(self): def get(self):
database = get_database() with self.database:
return jsonify(database.context(DEFAULT_CONTEXT_ID).dump()) return jsonify(self.database.context(DEFAULT_CONTEXT_ID).dump())
import copy, grpc, logging, pytest import copy, grpc, logging, pytest, requests, time
from google.protobuf.json_format import MessageToDict from google.protobuf.json_format import MessageToDict
from common.database.Factory import get_database, DatabaseEngineEnum from common.database.Factory import get_database, DatabaseEngineEnum
from common.database.api.Database import Database from common.database.api.Database import Database
from common.database.api.context.Constants import DEFAULT_CONTEXT_ID, DEFAULT_TOPOLOGY_ID from common.database.api.context.Constants import DEFAULT_CONTEXT_ID, DEFAULT_TOPOLOGY_ID
from common.database.tests.script import populate_example 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.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.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 = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG) LOGGER.setLevel(logging.DEBUG)
...@@ -35,14 +40,25 @@ def context_database(): ...@@ -35,14 +40,25 @@ def context_database():
@pytest.fixture(scope='session') @pytest.fixture(scope='session')
def context_service(context_database : Database): def context_service(context_database : Database):
_service = ContextService( _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() _service.start()
yield _service yield _service
_service.stop() _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') @pytest.fixture(scope='session')
def context_client(context_service): 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 yield _client
_client.close() _client.close()
...@@ -227,3 +243,16 @@ def test_get_topology_completed_2(context_client : ContextClient): ...@@ -227,3 +243,16 @@ def test_get_topology_completed_2(context_client : ContextClient):
validate_topology(topology) validate_topology(topology)
validate_topology_has_devices(topology) validate_topology_has_devices(topology)
validate_topology_has_links(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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment