"""
NRM backends which just logs actions performed.

Author: Henrik Thostrup Jensen <htj@nordu.net>
Copyright: NORDUnet (2011)
"""

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 DUDNSIBackend(network_name, nrm_ports, parent_requester, configuration):

    name = 'DUD NRM %s' % network_name
    nrm_map  = dict( [ (p.name, p) for p in nrm_ports ] ) # for the generic backend
    port_map = dict( [ (p.name, p.interface) for p in nrm_ports ] ) # for the nrm backend

    cm = DUDConnectionManager(name, port_map)
    return genericbackend.GenericBackend(network_name, nrm_map, cm, parent_requester, name, minimum_duration=1)



class DUDConnectionManager:

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


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


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


    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
        #return False


    def setupLink(self, connection_id, source_target, dest_target, bandwidth):
        log.msg('Link %s -> %s up' % (source_target, dest_target), system=self.log_system)
        return defer.succeed(None)
        #from opennsa import error
        #return defer.fail(error.InternalNRMError('Link setup failed'))


    def teardownLink(self, connection_id, source_target, dest_target, bandwidth):
        log.msg('Link %s -> %s down' % (source_target, dest_target), system=self.log_system)
        return defer.succeed(None)
        #from opennsa import error
        #return defer.fail(error.InternalNRMError('Link teardown failed'))


"""
NRM backends which just logs actions performed.

Author: Henrik Thostrup Jensen <htj@nordu.net>
Copyright: NORDUnet (2011)
"""

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 DUDNSIBackend(network_name, nrm_ports, parent_requester, configuration):

    name = 'DUD NRM %s' % network_name
    nrm_map  = dict( [ (p.name, p) for p in nrm_ports ] ) # for the generic backend
    port_map = dict( [ (p.name, p.interface) for p in nrm_ports ] ) # for the nrm backend

    cm = DUDConnectionManager(name, port_map)
    return genericbackend.GenericBackend(network_name, nrm_map, cm, parent_requester, name, minimum_duration=1)



class DUDConnectionManager:

    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
        #return False


    def setupLink(self, connection_id, source_target, dest_target, bandwidth):
        log.msg('Link %s -> %s up' % (source_target, dest_target), system=self.log_system)
        return defer.succeed(None)
        #from opennsa import error
        #return defer.fail(error.InternalNRMError('Link setup failed'))


    def teardownLink(self, connection_id, source_target, dest_target, bandwidth):
        log.msg('Link %s -> %s down' % (source_target, dest_target), system=self.log_system)
        return defer.succeed(None)
        #from opennsa import error
        #return defer.fail(error.InternalNRMError('Link teardown failed'))


fix resource/target creation in dud resource (thanks to Jan van Oorschot for reporting)