Commit 32072654 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Intermediate backup of Compute:

- Implement WIM REST API for OSM
- Implement IETFL2VPN REST API
- Implement unitary tests for OSM-Compute-Service workflow
parent cb66db4b
Loading
Loading
Loading
Loading
+36 −36
Original line number Diff line number Diff line
@@ -19,46 +19,46 @@ cat $PROJECTDIR/coverage/.coveragerc.template | sed s+~/teraflow/controller+$PRO
#kubectl --namespace tf-dev expose deployment contextservice --port=6379 --type=NodePort --name=redis-tests
#echo "Waiting 10 seconds for Redis to start..."
#sleep 10
export REDIS_SERVICE_HOST=$(kubectl get node kubernetes-master -o 'jsonpath={.status.addresses[?(@.type=="InternalIP")].address}')
export REDIS_SERVICE_PORT=$(kubectl get service redis-tests --namespace tf-dev -o 'jsonpath={.spec.ports[?(@.port==6379)].nodePort}')
#export REDIS_SERVICE_HOST=$(kubectl get node kubernetes-master -o 'jsonpath={.status.addresses[?(@.type=="InternalIP")].address}')
#export REDIS_SERVICE_PORT=$(kubectl get service redis-tests --namespace tf-dev -o 'jsonpath={.spec.ports[?(@.port==6379)].nodePort}')

# First destroy old coverage file
rm -f $COVERAGEFILE

coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    common/orm/tests/test_unitary.py \
    common/message_broker/tests/test_unitary.py \
    common/rpc_method_wrapper/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    centralizedattackdetector/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    context/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    device/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest -s --log-level=INFO --verbose \
    l3_centralizedattackdetector/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest -s --log-level=INFO --verbose \
    l3_distributedattackdetector/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest -s --log-level=INFO --verbose \
    l3_attackmitigator/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    opticalcentralizedattackdetector/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    dbscanserving/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    opticalattackmitigator/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    service/tests/test_unitary.py
#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    common/orm/tests/test_unitary.py \
#    common/message_broker/tests/test_unitary.py \
#    common/rpc_method_wrapper/tests/test_unitary.py
#
#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    centralizedattackdetector/tests/test_unitary.py
#
#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    context/tests/test_unitary.py
#
#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    device/tests/test_unitary.py
#
#coverage run --rcfile=$RCFILE --append -m pytest -s --log-level=INFO --verbose \
#    l3_centralizedattackdetector/tests/test_unitary.py
#
#coverage run --rcfile=$RCFILE --append -m pytest -s --log-level=INFO --verbose \
#    l3_distributedattackdetector/tests/test_unitary.py
#
#coverage run --rcfile=$RCFILE --append -m pytest -s --log-level=INFO --verbose \
#    l3_attackmitigator/tests/test_unitary.py
#
#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    opticalcentralizedattackdetector/tests/test_unitary.py
#
#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    dbscanserving/tests/test_unitary.py
#
#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    opticalattackmitigator/tests/test_unitary.py
#
#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    service/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    compute/tests/test_unitary.py
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ GRPC_GRACE_PERIOD = 60

# REST-API settings
RESTAPI_SERVICE_PORT = 8080
RESTAPI_BASE_URL = '/api'
RESTAPI_BASE_URL = '/restconf/data/ietf-l2vpn-svc:l2vpn-svc'

# Prometheus settings
METRICS_PORT = 9192
+9 −2
Original line number Diff line number Diff line
import logging, threading
from flask import Flask
import logging, threading, time
from flask import Flask, request
from flask_restful import Api
from werkzeug.serving import make_server
from compute.Config import RESTAPI_BASE_URL, RESTAPI_SERVICE_PORT


logging.getLogger('werkzeug').setLevel(logging.WARNING)

BIND_ADDRESS = '0.0.0.0'
LOGGER = logging.getLogger(__name__)

def log_request(response):
    timestamp = time.strftime('[%Y-%b-%d %H:%M]')
    LOGGER.info('%s %s %s %s %s', timestamp, request.remote_addr, request.method, request.full_path, response.status)
    return response

class Server(threading.Thread):
    def __init__(self, host=BIND_ADDRESS, port=RESTAPI_SERVICE_PORT, base_url=RESTAPI_BASE_URL):
        threading.Thread.__init__(self, daemon=True)
@@ -16,6 +22,7 @@ class Server(threading.Thread):
        self.port = port
        self.base_url = base_url
        self.app = Flask(__name__)
        self.app.after_request(log_request)
        self.api = Api(self.app, prefix=self.base_url)

    def add_resource(self, resource, *urls, **kwargs):
+45 −0
Original line number Diff line number Diff line
import grpc, logging
from concurrent import futures
from service.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD
from service.proto.service_pb2_grpc import  add_ServiceServiceServicer_to_server
from .MockServiceServiceServicerImpl import MockServiceServiceServicerImpl

BIND_ADDRESS = '0.0.0.0'
LOGGER = logging.getLogger(__name__)

class MockServiceService:
    def __init__(
        self, address=BIND_ADDRESS, port=GRPC_SERVICE_PORT, max_workers=GRPC_MAX_WORKERS,
        grace_period=GRPC_GRACE_PERIOD):

        self.address = address
        self.port = port
        self.endpoint = None
        self.max_workers = max_workers
        self.grace_period = grace_period
        self.service_servicer = None
        self.pool = None
        self.server = None

    def start(self):
        self.endpoint = '{:s}:{:s}'.format(str(self.address), str(self.port))
        LOGGER.info('Starting Service (tentative endpoint: {:s}, max_workers: {:s})...'.format(
            str(self.endpoint), str(self.max_workers)))

        self.pool = futures.ThreadPoolExecutor(max_workers=self.max_workers)
        self.server = grpc.server(self.pool) # , interceptors=(tracer_interceptor,))

        self.service_servicer = MockServiceServiceServicerImpl()
        add_ServiceServiceServicer_to_server(self.service_servicer, self.server)

        port = self.server.add_insecure_port(self.endpoint)
        self.endpoint = '{:s}:{:s}'.format(str(self.address), str(port))
        LOGGER.info('Listening on {:s}...'.format(str(self.endpoint)))
        self.server.start()

        LOGGER.debug('Service started')

    def stop(self):
        LOGGER.debug('Stopping service (grace period {:s} seconds)...'.format(str(self.grace_period)))
        self.server.stop(self.grace_period)
        LOGGER.debug('Service stopped')
+28 −0
Original line number Diff line number Diff line
import grpc, json, logging
from google.protobuf.json_format import MessageToDict
from service.proto.context_pb2 import ConnectionList, Empty, Service, ServiceId
from service.proto.service_pb2_grpc import ServiceServiceServicer

LOGGER = logging.getLogger(__name__)

def grpc_message_to_json_string(message):
    return json.dumps(MessageToDict(
        message, including_default_value_fields=True, preserving_proto_field_name=True, use_integers_for_enums=False),
        sort_keys=True)

class MockServiceServiceServicerImpl(ServiceServiceServicer):
    def CreateService(self, request : Service, context : grpc.ServicerContext) -> ServiceId:
        LOGGER.info('[CreateService] request={:s}'.format(grpc_message_to_json_string(request)))
        return request.service_id

    def UpdateService(self, request : Service, context : grpc.ServicerContext) -> ServiceId:
        LOGGER.info('[UpdateService] request={:s}'.format(grpc_message_to_json_string(request)))
        return request.service_id

    def DeleteService(self, request : ServiceId, context : grpc.ServicerContext) -> Empty:
        LOGGER.info('[DeleteService] request={:s}'.format(grpc_message_to_json_string(request)))
        return Empty()

    def GetConnectionList(self, request : ServiceId, context : grpc.ServicerContext) -> ConnectionList:
        LOGGER.info('[GetConnectionList] request={:s}'.format(grpc_message_to_json_string(request)))
        return ConnectionList()
Loading