# Path: opennsa/constants.py
# URN_OGF_PREFIX      = 'urn:ogf:network:'
# CS2_SERVICE_TYPE    = 'application/vnd.org.ogf.nsi.cs.v2+soap'
# NML_SERVICE_TYPE    = 'application/vnd.ogf.nsi.topology.v2+xml'
# CS2_REQUESTER       = 'application/vnd.ogf.nsi.cs.v2.requester+soap'
# CS2_PROVIDER        = 'application/vnd.ogf.nsi.cs.v2.provider+soap'
# OPENNSA_REST        = 'application/vnd.net.nordu.opennsa+rest'
# BIDIRECTIONAL       = 'Bidirectional'
# EVTS_AGOLE          = 'http://services.ogf.org/nsi/2013/07/descriptions/EVTS.A-GOLE'
# ETHERNET            = 'ethernet' # bidirectional implied
# ETHERNET_VLAN       = 'vlan'
# MPLS                = 'mpls'
# RESTRICTTRANSIT     = 'restricttransit'
# ETHERNET_NS         = 'http://schemas.ogf.org/nml/2012/10/ethernet'
# NML_ETHERNET_VLAN   = '%s#vlan' % ETHERNET_NS
# NML_MPLS            = 'http://nsi.nordu.net/nml#mpls' # pretty much just made up
# FEATURE_AGGREGATOR  = 'vnd.ogf.nsi.cs.v2.role.aggregator'
# FEATURE_UPA         = 'vnd.ogf.nsi.cs.v2.role.uPA'
# REQUIRE_USER        = 'requireuser'
# REQUIRE_TRACE       = 'requiretrace'
# AGGREGATOR          = 'aggregator'
# ALLOW_HAIRPIN       = 'allowhairpin'
# 
# Path: opennsa/backends/common/genericbackend.py
# class GenericBackendConnections(DBObject):
# class GenericBackend(service.Service):
#     TPC_TIMEOUT = 120 # seconds
#     def __init__(self, network, nrm_ports, connection_manager, parent_requester, log_system, minimum_duration=60):
#     def startService(self):
#     def stopService(self):
#     def getNotificationId(self):
#     def buildSchedule(self):
#     def _getConnection(self, connection_id, requester_nsa):
#     def _authorize(self, source_port, destination_port, header, request_info, start_time=None, end_time=None):
#     def logStateUpdate(self, conn, state_msg):
#     def reserve(self, header, connection_id, global_reservation_id, description, criteria, request_info=None):
#     def reserveCommit(self, header, connection_id, request_info=None):
#     def reserveAbort(self, header, connection_id, request_info=None):
#     def provision(self, header, connection_id, request_info=None):
#     def release(self, header, connection_id, request_info=None):
#     def terminate(self, header, connection_id, request_info=None):
#     def querySummary(self, header, connection_ids=None, global_reservation_ids=None, request_info=None):
#     def queryRecursive(self, header, connection_ids, global_reservation_ids, request_info=None):
#     def _query(self, header, connection_ids, global_reservation_ids, request_info=None):
#     def queryNotification(self, header, connection_id, start_notification=None, end_notification=None):
#     def _doReserve(self, conn, correlation_id):
#     def _doReserveTimeout(self, conn):
#     def _doReserveRollback(self, conn):
#     def _doActivate(self, conn):
#     def _doTeardown(self, conn):
#     def _doEndtime(self, conn):
#     def _doFreeResource(self, conn):


import string
import random
from twisted.python import log
from twisted.internet import defer
from opennsa import constants as cnt
from opennsa.backends.common import genericbackend

    def __init__(self, log_system, port_map):
        self.log_system = log_system
        self.port_map   = port_map


    def getResource(self, port, label):
        lv = '' if label is None else str(label.labelValue())
        return self.port_map[port] + ':' + lv


    def getTarget(self, port, label):
        lv = '' if label is None else str(label.labelValue())
        return self.port_map[port] + '#' + lv


    def canConnect(self, source_port, dest_port, source_label, dest_label):
        if source_label is None and dest_label is None:
            return True
        elif source_label is None or dest_label is None:
            return False
        else:
            return source_label.type_ == dest_label.type_


    def createConnectionId(self, source_target, dest_target):
        return 'DUD-' + ''.join( [ random.choice(string.hexdigits[:16]) for _ in range(8) ] )


    def canSwapLabel(self, label_type):

# Path: opennsa/constants.py
# URN_OGF_PREFIX      = 'urn:ogf:network:'
# CS2_SERVICE_TYPE    = 'application/vnd.org.ogf.nsi.cs.v2+soap'
# NML_SERVICE_TYPE    = 'application/vnd.ogf.nsi.topology.v2+xml'
# CS2_REQUESTER       = 'application/vnd.ogf.nsi.cs.v2.requester+soap'
# CS2_PROVIDER        = 'application/vnd.ogf.nsi.cs.v2.provider+soap'
# OPENNSA_REST        = 'application/vnd.net.nordu.opennsa+rest'
# BIDIRECTIONAL       = 'Bidirectional'
# EVTS_AGOLE          = 'http://services.ogf.org/nsi/2013/07/descriptions/EVTS.A-GOLE'
# ETHERNET            = 'ethernet' # bidirectional implied
# ETHERNET_VLAN       = 'vlan'
# MPLS                = 'mpls'
# RESTRICTTRANSIT     = 'restricttransit'
# ETHERNET_NS         = 'http://schemas.ogf.org/nml/2012/10/ethernet'
# NML_ETHERNET_VLAN   = '%s#vlan' % ETHERNET_NS
# NML_MPLS            = 'http://nsi.nordu.net/nml#mpls' # pretty much just made up
# FEATURE_AGGREGATOR  = 'vnd.ogf.nsi.cs.v2.role.aggregator'
# FEATURE_UPA         = 'vnd.ogf.nsi.cs.v2.role.uPA'
# REQUIRE_USER        = 'requireuser'
# REQUIRE_TRACE       = 'requiretrace'
# AGGREGATOR          = 'aggregator'
# ALLOW_HAIRPIN       = 'allowhairpin'
# 
# Path: opennsa/backends/common/genericbackend.py
# class GenericBackendConnections(DBObject):
# class GenericBackend(service.Service):
#     TPC_TIMEOUT = 120 # seconds
#     def __init__(self, network, nrm_ports, connection_manager, parent_requester, log_system, minimum_duration=60):
#     def startService(self):
#     def stopService(self):
#     def getNotificationId(self):
#     def buildSchedule(self):
#     def _getConnection(self, connection_id, requester_nsa):
#     def _authorize(self, source_port, destination_port, header, request_info, start_time=None, end_time=None):
#     def logStateUpdate(self, conn, state_msg):
#     def reserve(self, header, connection_id, global_reservation_id, description, criteria, request_info=None):
#     def reserveCommit(self, header, connection_id, request_info=None):
#     def reserveAbort(self, header, connection_id, request_info=None):
#     def provision(self, header, connection_id, request_info=None):
#     def release(self, header, connection_id, request_info=None):
#     def terminate(self, header, connection_id, request_info=None):
#     def querySummary(self, header, connection_ids=None, global_reservation_ids=None, request_info=None):
#     def queryRecursive(self, header, connection_ids, global_reservation_ids, request_info=None):
#     def _query(self, header, connection_ids, global_reservation_ids, request_info=None):
#     def queryNotification(self, header, connection_id, start_notification=None, end_notification=None):
#     def _doReserve(self, conn, correlation_id):
#     def _doReserveTimeout(self, conn):
#     def _doReserveRollback(self, conn):
#     def _doActivate(self, conn):
#     def _doTeardown(self, conn):
#     def _doEndtime(self, conn):
#     def _doFreeResource(self, conn):


# Path: opennsa/backends/dud.py
import string
import random
from twisted.python import log
from twisted.internet import defer
from opennsa import constants as cnt
from opennsa.backends.common import genericbackend


    def __init__(self, log_system, port_map):
        self.log_system = log_system
        self.port_map   = port_map


    def getResource(self, port, label):
        lv = '' if label is None else str(label.labelValue())
        return self.port_map[port] + ':' + lv


    def getTarget(self, port, label):
        lv = '' if label is None else str(label.labelValue())
        return self.port_map[port] + '#' + lv


    def canConnect(self, source_port, dest_port, source_label, dest_label):
        if source_label is None and dest_label is None:
            return True
        elif source_label is None or dest_label is None:
            return False
        else:
            return source_label.type_ == dest_label.type_


    def createConnectionId(self, source_target, dest_target):
        return 'DUD-' + ''.join( [ random.choice(string.hexdigits[:16]) for _ in range(8) ] )


    def canSwapLabel(self, label_type):

        return label_type == cnt.ETHERNET_VLAN