<commit_before>"""
OpenNSA NSI Service -> Backend adaptor (router).

Author: <NAME> <<EMAIL>>
Copyright: NORDUnet (2011)
"""

import random

from zope.interface import implements

from twisted.python import log
from twisted.internet import defer

from opennsa.interface import NSIServiceInterface
from opennsa import nsa, error, topology, proxy, connection



class NSIService:

    implements(NSIServiceInterface)

    def __init__(self, network, backend, topology_file, client):
        self.network = network
        self.backend = backend

        self.topology = topology.parseGOLETopology(topology_file)

        # get own nsa from topology
        self.nsa = self.topology.getNetwork(self.network).nsa
        self.proxy = proxy.NSIProxy(client, self.nsa, self.topology)

        self.connections = {} # persistence, ha!
        self.reservations = {} # outstanding reservations

    # utility functionality

    def getConnection(self, requester_nsa, connection_id):

        conn = self.connections.get(requester_nsa, {}).get(connection_id, None)
        if conn is None:
            raise error.NoSuchConnectionError('No connection with id %s for NSA with address %s' % (connection_id, requester_nsa.address))
        else:
            return conn


    # command functionality

    def reservation(self, requester_nsa, provider_nsa, session_security_attr, global_reservation_id, description, connection_id, service_parameters):

        def setupSubConnection(source_stp, dest_stp, conn):

            assert source_stp.network == dest_stp.network

            # should check for local network
            if source_stp.network == self.network:
                local_conn = connection.LocalConnection(conn, source_stp.endpoint, dest_stp.endpoint, backend=self.backend)
                assert conn.local_connection is None
                conn.local_connection = local_conn
            else:
                sub_conn_id = 'int-ccid' + ''.join( [ str(int(random.random() * 10)) for _ in range(4) ] )
                # FIXME should be setup with NSA context, not network
                sub_conn = connection.SubConnection(conn, sub_conn_id, source_stp.network, source_stp, dest_stp, proxy=self.proxy)
                conn.sub_connections.append(sub_conn)
                self.reservations[sub_conn_id] = sub_conn

            return conn

        # --

        if connection_id in self.connections.get(requester_nsa, {}):
            raise error.ReserveError('Reservation with connection id %s already exists' % connection_id)

        source_stp = service_parameters.source_stp
        dest_stp   = service_parameters.dest_stp

        conn = connection.Connection(requester_nsa, connection_id, source_stp, dest_stp, global_reservation_id, description)

        self.connections.setdefault(requester_nsa, {})[conn.connection_id] = conn

        # figure out nature of request
<commit_msg>use uuids for creating sub connection ids<commit_after>"""
OpenNSA NSI Service -> Backend adaptor (router).

Author: <NAME> <<EMAIL>>
Copyright: NORDUnet (2011)
"""

import uuid

from zope.interface import implements

from twisted.python import log
from twisted.internet import defer

from opennsa.interface import NSIServiceInterface
from opennsa import nsa, error, topology, proxy, connection



class NSIService:

    implements(NSIServiceInterface)

    def __init__(self, network, backend, topology_file, client):
        self.network = network
        self.backend = backend

        self.topology = topology.parseGOLETopology(topology_file)

        # get own nsa from topology
        self.nsa = self.topology.getNetwork(self.network).nsa
        self.proxy = proxy.NSIProxy(client, self.nsa, self.topology)

        self.connections = {} # persistence, ha!
        self.reservations = {} # outstanding reservations

    # utility functionality

    def getConnection(self, requester_nsa, connection_id):

        conn = self.connections.get(requester_nsa, {}).get(connection_id, None)
        if conn is None:
            raise error.NoSuchConnectionError('No connection with id %s for NSA with address %s' % (connection_id, requester_nsa.address))
        else:
            return conn


    # command functionality

    def reservation(self, requester_nsa, provider_nsa, session_security_attr, global_reservation_id, description, connection_id, service_parameters):

        def setupSubConnection(source_stp, dest_stp, conn):

            assert source_stp.network == dest_stp.network

            # should check for local network
            if source_stp.network == self.network:
                local_conn = connection.LocalConnection(conn, source_stp.endpoint, dest_stp.endpoint, backend=self.backend)
                assert conn.local_connection is None
                conn.local_connection = local_conn
            else:
                sub_conn_id = 'urn:uuid:' + str(uuid.uuid1())
                # FIXME should be setup with NSA context, not network
                sub_conn = connection.SubConnection(conn, sub_conn_id, source_stp.network, source_stp, dest_stp, proxy=self.proxy)
                conn.sub_connections.append(sub_conn)
                self.reservations[sub_conn_id] = sub_conn

            return conn

        # --

        if connection_id in self.connections.get(requester_nsa, {}):
            raise error.ReserveError('Reservation with connection id %s already exists' % connection_id)

        source_stp = service_parameters.source_stp
        dest_stp   = service_parameters.dest_stp

        conn = connection.Connection(requester_nsa, connection_id, source_stp, dest_stp, global_reservation_id, description)

        self.connections.setdefault(requester_nsa, {})[conn.connection_id] = conn

        # figure out nature of request

{'fraction_non_alphanumeric': 0.08153020598926779, 'fraction_numerical': 0.0020772027003635105, 'mean_word_length': 2.976600137646249, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2603366', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1586, 'n_words': 401}