Commit afeea9f9 authored by Carlos Manso's avatar Carlos Manso
Browse files

update

parent 8b07c76a
Loading
Loading
Loading
Loading
+85 −89
Original line number Diff line number Diff line
@@ -15,7 +15,9 @@
import copy
from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method
from common.proto.e2eorchestrator_pb2 import E2EOrchestratorRequest, E2EOrchestratorReply
from common.proto.context_pb2 import Empty, Connection, ContextId, EndPointId, Link, LinkId, TopologyDetails, TopologyId, Device, Topology, Context, Service, ServiceStatus, DeviceId, ServiceTypeEnum, ServiceStatusEnum 
from common.proto.context_pb2 import (
    Empty, Connection, EndPointId, Link, TopologyDetails, Topology, Context, Service, ServiceTypeEnum,
    ServiceStatusEnum)
from common.proto.e2eorchestrator_pb2_grpc import E2EOrchestratorServiceServicer
from context.client.ContextClient import ContextClient
from service.client.ServiceClient import ServiceClient
@@ -34,7 +36,7 @@ from common.Constants import DEFAULT_CONTEXT_NAME


LOGGER = logging.getLogger(__name__)
logging.getLogger("websockets").propagate = False
logging.getLogger("websockets").propagate = True

METRICS_POOL = MetricsPool("E2EOrchestrator", "RPC")

@@ -48,9 +50,53 @@ EXT_PORT = "8762"
OWN_HOST = "e2e-orchestratorservice.tfs-e2e.svc.cluster.local"
OWN_PORT = "8761"

ALL_HOSTS = "0.0.0.0"

def _event_received(websocket):
    for message in websocket:
class SubscriptionServer(Thread):
    def __init__(self):
        Thread.__init__(self)
            
    def run(self):
        url = "ws://" + EXT_HOST + ":" + EXT_PORT
        request = VNTSubscriptionRequest()
        request.host = OWN_HOST
        request.port = OWN_PORT
        try: 
            LOGGER.debug("Trying to connect to {}".format(url))
            websocket = connect(url)
        except Exception as ex:
            LOGGER.error('Error connecting to {}'.format(url))
        else:
            with websocket:
                LOGGER.debug("Connected to {}".format(url))
                send = grpc_message_to_json_string(request)
                websocket.send(send)
                LOGGER.debug("Sent: {}".format(send))
                try:
                    message = websocket.recv()
                    LOGGER.debug("Received message from WebSocket: {}".format(message))
                except Exception as ex:
                    LOGGER.info('Exception receiving from WebSocket: {}'.format(ex))

            self._events_server()


    def _events_server(self):
        all_hosts = "0.0.0.0"

        try:
            server = serve(self._event_received, all_hosts, int(OWN_PORT))
        except Exception as ex:
            LOGGER.error('Error starting server on {}:{}'.format(all_hosts, OWN_PORT))
            LOGGER.error('Exception!: {}'.format(ex))
        else:
            with server:
                LOGGER.info("Running events server...: {}:{}".format(all_hosts, OWN_PORT))
                server.serve_forever()


    def _event_received(self, connection):
        for message in connection:
            message_json = json.loads(message)

            if 'link_id' in message_json:
@@ -84,7 +130,7 @@ def _event_received(websocket):
                service.service_endpoint_ids.append(copy.deepcopy(z_ep_id))

                service_client.UpdateService(service)
            websocket.send(grpc_message_to_json_string(link))
                connection.send(grpc_message_to_json_string(link))

            else:
                topology_details = TopologyDetails(**message_json)
@@ -105,64 +151,14 @@ def _event_received(websocket):




def requestSubscription():
    url = "ws://" + EXT_HOST + ":" + EXT_PORT
    request = VNTSubscriptionRequest()
    request.host = OWN_HOST
    request.port = OWN_PORT
    LOGGER.debug("Connecting to {}".format(url))
    try: 
        websocket = connect(url)
    except Exception as ex:
        LOGGER.error('Error connecting to {}'.format(url))
    else:
        with websocket:
            LOGGER.debug("Connected to {}".format(url))
            send = grpc_message_to_json_string(request)
            websocket.send(send)
            try:
                message = websocket.recv()
                LOGGER.debug("Received message from WebSocket: {}".format(message))
            except Exception as ex:
                LOGGER.error('Exception receiving from WebSocket: {}'.format(ex))

        events_server()
        LOGGER.info('Subscription requested')


def events_server():
    all_hosts = "0.0.0.0"

    try:
        server = serve(_event_received, all_hosts, int(OWN_PORT))
    except Exception as ex:
        LOGGER.error('Error starting server on {}:{}'.format(all_hosts, OWN_PORT))
        LOGGER.error('Exception!: {}'.format(ex))
    else:
        with server:
            LOGGER.info("Running events server...: {}:{}".format(all_hosts, OWN_PORT))
            server.serve_forever()
            LOGGER.info("Exiting events server...")




class SubscriptionServer():

class E2EOrchestratorServiceServicerImpl(E2EOrchestratorServiceServicer):
    def __init__(self):
        LOGGER.debug("Creating Servicer...")
        LOGGER.debug("Servicer Created")

        try:
            LOGGER.debug("Requesting subscription")
            subscription_thread = Thread(target=requestSubscription)
            subscription_thread.start()


            # import_optical()

            sub_server = SubscriptionServer()
            sub_server.start()
            LOGGER.debug("Servicer Created")

        except Exception as ex:
            LOGGER.info("Exception!: {}".format(ex))