Skip to content
__init__.py 2.59 KiB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
# Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
Carlos Manso's avatar
Carlos Manso committed
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from websockets.sync.server import serve
Carlos Manso's avatar
Carlos Manso committed
from common.proto.vnt_manager_pb2 import VNTSubscriptionRequest
Carlos Manso's avatar
Carlos Manso committed
from common.Settings import get_setting
Carlos Manso's avatar
Carlos Manso committed
from context.client.ContextClient import ContextClient
from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME
from common.tools.object_factory.Topology import json_topology_id
from common.tools.object_factory.Context import json_context_id
from common.proto.context_pb2 import ContextId, TopologyId
import json
import os
from vnt_manager.client.VNTManagerClient import VNTManagerClient

JSON_ADMIN_CONTEXT_ID = json_context_id(DEFAULT_CONTEXT_NAME)
ADMIN_CONTEXT_ID = ContextId(**JSON_ADMIN_CONTEXT_ID)
ADMIN_TOPOLOGY_ID = TopologyId(**json_topology_id(DEFAULT_TOPOLOGY_NAME, context_id=JSON_ADMIN_CONTEXT_ID))

vnt_manager_client: VNTManagerClient =  VNTManagerClient()
context_client:     ContextClient =     ContextClient()   

Carlos Manso's avatar
Carlos Manso committed
ALL_HOSTS = "0.0.0.0"
WS_E2E_PORT = int(get_setting('WS_E2E_PORT', default='8762'))
Carlos Manso's avatar
Carlos Manso committed

LOGGER = logging.getLogger(__name__)


def register_context_subscription():
Carlos Manso's avatar
Carlos Manso committed
    with serve(subcript_to_vnt_manager, ALL_HOSTS, WS_E2E_PORT, logger=LOGGER) as server:
        LOGGER.info("Running subscription server...: {}:{}".format(ALL_HOSTS, str(WS_E2E_PORT)))
Carlos Manso's avatar
Carlos Manso committed
        server.serve_forever()
        LOGGER.info("Exiting subscription server...")


def subcript_to_vnt_manager(websocket):
    for message in websocket:
Carlos Manso's avatar
Carlos Manso committed
        LOGGER.debug("Message received: {}".format(message))
Carlos Manso's avatar
Carlos Manso committed
        message_json = json.loads(message)
        request = VNTSubscriptionRequest()
        request.host = message_json['host']
        request.port = message_json['port']
Carlos Manso's avatar
Carlos Manso committed
        LOGGER.debug("Received gRPC from ws: {}".format(request))
Carlos Manso's avatar
Carlos Manso committed

        try:
            vntm_reply = vnt_manager_client.VNTSubscript(request)
Carlos Manso's avatar
Carlos Manso committed
            LOGGER.debug("Received gRPC from vntm: {}".format(vntm_reply))
Carlos Manso's avatar
Carlos Manso committed
        except Exception as e:
            LOGGER.error('Could not subscript to VTNManager: {}'.format(e))

Carlos Manso's avatar
Carlos Manso committed
        websocket.send(vntm_reply.subscription)