Commit 1796019d authored by Pablo Armingol's avatar Pablo Armingol
Browse files

Code cleanup

parent 2092ad05
Loading
Loading
Loading
Loading
+24 −20
Original line number Diff line number Diff line
@@ -16,8 +16,9 @@ import json, logging
from flask import request
from flask.json import jsonify
from flask_restful import Resource
from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME
from common.proto.context_pb2 import ContextId, Empty
from common.tools.context_queries.Topology import get_topology_details
from common.tools.object_factory.Context import json_context_id
from context.client.ContextClient import ContextClient
from nbi.service.rest_server.nbi_plugins.tools.Authentication import HTTP_AUTH
from nbi.service.rest_server.nbi_plugins.tools.HttpStatusCodes import HTTP_OK, HTTP_SERVERERROR
@@ -25,35 +26,38 @@ from .YangHandler import YangHandler

LOGGER = logging.getLogger(__name__)

TE_TOPOLOGY_NAMES = [
    'providerId-10-clientId-0-topologyId-1',
]

class Networks(Resource):
    @HTTP_AUTH.login_required
    def get(self):
        LOGGER.info('Request: {:s}'.format(str(request)))
        topology_id = ''
        LOGGER.info(f'Request: {request}')
        try:
            context_client = ContextClient()
            topology_details = get_topology_details(
                context_client, DEFAULT_TOPOLOGY_NAME)
            yang_handler = YangHandler()
            network_list_reply = []

            contexts = context_client.ListContexts(Empty()).contexts
            context_names = [context.name for context in contexts]
            LOGGER.info(f'Contexts detected: {context_names}')

            for context_name in context_names:
                topologies = context_client.ListTopologies(ContextId(**json_context_id(context_name))).topologies
                topology_names = [topology.name for topology in topologies]
                LOGGER.info(f'Topologies detected for context {context_name}: {topology_names}')

                for topology_name in topology_names:
                    topology_details = get_topology_details(context_client, topology_name, context_name)
                    if topology_details is None:
                MSG = 'Topology({:s}/{:s}) not found'
                raise Exception(MSG.format(DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME))
                        raise Exception(f'Topology({context_name}/{topology_name}) not found')
                    
            network_list_reply = []
            yang_handler = YangHandler()
            for te_topology_name in TE_TOPOLOGY_NAMES:
                network_reply = yang_handler.compose_network(te_topology_name, topology_details)
                    network_reply = yang_handler.compose_network(topology_name, topology_details)
                    network_list_reply.append(network_reply)

            yang_handler.destroy()
            response = jsonify(network_list_reply)

            response.status_code = HTTP_OK
        except Exception as e: # pylint: disable=broad-except
            LOGGER.exception('Something went wrong Retrieving Topology({:s})'.format(str(topology_id)))

        except Exception as e:
            LOGGER.exception(f'Error retrieving topologies: {e}')
            response = jsonify({'error': str(e)})
            response.status_code = HTTP_SERVERERROR
        return response
+0 −6
Original line number Diff line number Diff line
@@ -36,10 +36,6 @@ class YangHandler:
            LOGGER.info('Loading module: {:s}'.format(str(yang_module_name)))
            self._yang_context.load_module(yang_module_name).feature_enable_all()
    
    def get_endpoint_name(self, device: str) -> None:
        context_client = ContextClient()
        device = context_client.GetDevice(DeviceId(**json_device_id(device)))
            
    def compose_network(self, te_topology_name : str, topology_details : TopologyDetails) -> None:
        networks = self._yang_context.create_data_path('/ietf-network:networks')
        network = networks.create_path('network[network-id="{:s}"]'.format(te_topology_name))
@@ -103,14 +99,12 @@ class YangHandler:
        links = network.create_path('ietf-network-topology:link[link-id="{:s}"]'.format(link_name))
        links.create_path('link-id', link_name)
        src_endpoint_id = link_specs.link_endpoint_ids[0]
        LOGGER.info('SRC: {:s}'.format(str(src_endpoint_id)))

        source = links.create_path('source')
        source.create_path('source-node', name_mappings.get_device_name(src_endpoint_id.device_id))
        source.create_path('source-tp', name_mappings.get_endpoint_name(src_endpoint_id))

        dst_endpoint_id = link_specs.link_endpoint_ids[-1]
        LOGGER.info('DST: {:s}'.format(str(dst_endpoint_id)))

        destination = links.create_path('destination')
        destination.create_path('dest-node', name_mappings.get_device_name(dst_endpoint_id.device_id))