# Path: opennsa/error.py
# LOG_SYSTEM = 'error'
# NSI_ERROR_CODE_TABLE = {
#     '00100' : PayloadError,
#     '00101' : MissingParameterError,
#     '00200' : ConnectionError,
#     '00201' : InvalidTransitionError,
#     '00202' : ConnectionExistsError,
#     '00203' : ConnectionNonExistentError,
#     '00204' : ConnectionGoneError,
#     '00205' : ConnectionCreateError,
#     '00300' : SecurityError,
#     '00301' : AuthenticationFailure,
#     '00302' : UnauthorizedError,
#     '00400' : TopologyError,
#     '00401' : UnknownSTPError,                      # compat
#     '00402' : STPResolutionError,                   # compat
#     '00403' : NoPathFoundError,
#     '00404' : VLANInterchangeNotSupportedError,     # compat
#     '00500' : InternalServerError,
#     '00501' : InternalNRMError,
#     '00600' : ResourceUnavailableError,
#     '00601' : STPUnavailableError,                  # compat
#     '00602' : BandwidthUnavailableError,            # compat
#     '00700' : ServiceError,
#     '00701' : UnknownSTPError,
#     '00702' : STPResolutionError,
#     '00703' : VLANInterchangeNotSupportedError,
#     '00704' : STPUnavailableError,
#     '00705' : BandwidthUnavailableError             # compat
# }
# class CallbackTimeoutError(Exception):
# class NSIError(Exception):
# class UnknownNSIError(NSIError):
# class PayloadError(NSIError):
# class MissingParameterError(PayloadError):
# class UnsupportedParameter(PayloadError):
# class ConnectionError(NSIError):
# class InvalidTransitionError(ConnectionError):
# class ConnectionExistsError(ConnectionError):
# class ConnectionNonExistentError(ConnectionError):
# class ConnectionGoneError(ConnectionError):
# class ConnectionCreateError(ConnectionError):
# class SecurityError(NSIError):
# class AuthenticationFailure(SecurityError):
# class UnauthorizedError(SecurityError):
# class TopologyError(NSIError):
# class NoPathFoundError(NSIError):
# class InternalServerError(NSIError):
# class InternalNRMError(InternalServerError):
# class DownstreamNSAError(InternalServerError):
# class ResourceUnavailableError(NSIError):
# class ServiceError(NSIError): # only use this if nothing else applies
# class UnknownSTPError(ServiceError):
# class STPResolutionError(NSIError):
# class VLANInterchangeNotSupportedError(TopologyError):
# class STPUnavailableError(ServiceError, TopologyError): # need this to be a topology error internally
# class BandwidthUnavailableError(ServiceError): # NSI error name is CAPACITY_UNAVAILABLE
#     def __init__(self, message, nsa_id=None, connection_id=None, variables=None):
# def lookup(error_code):


from zope.interface import implementer
from twisted.python import log, failure
from twisted.internet import reactor, defer
from opennsa import error
from opennsa.interface import INSIProvider
QUERY_RECURSIVE = 'query_recursive'



# In OpenNSA the requester is something that acts as a provider :-)
@implementer(INSIProvider)
class Requester:

    def __init__(self, requester_client, callback_timeout=None):

        self.requester_client = requester_client

        self.callback_timeout = callback_timeout or DEFAULT_CALLBACK_TIMEOUT
        self.calls = {}
        self.notifications = defer.DeferredQueue()


    def addCall(self, provider_nsa, correlation_id, action):

        key = (provider_nsa, correlation_id)
        assert key not in self.calls, 'Cannot have multiple calls with same NSA / correlationId'

        d = defer.Deferred()
        call = reactor.callLater(self.callback_timeout, self.callbackTimeout, provider_nsa, correlation_id, action)
        self.calls[key] = (action, d, call)
        return d


    def callbackTimeout(self, provider_nsa, correlation_id, action):


# Path: opennsa/error.py
# LOG_SYSTEM = 'error'
# NSI_ERROR_CODE_TABLE = {
#     '00100' : PayloadError,
#     '00101' : MissingParameterError,
#     '00200' : ConnectionError,
#     '00201' : InvalidTransitionError,
#     '00202' : ConnectionExistsError,
#     '00203' : ConnectionNonExistentError,
#     '00204' : ConnectionGoneError,
#     '00205' : ConnectionCreateError,
#     '00300' : SecurityError,
#     '00301' : AuthenticationFailure,
#     '00302' : UnauthorizedError,
#     '00400' : TopologyError,
#     '00401' : UnknownSTPError,                      # compat
#     '00402' : STPResolutionError,                   # compat
#     '00403' : NoPathFoundError,
#     '00404' : VLANInterchangeNotSupportedError,     # compat
#     '00500' : InternalServerError,
#     '00501' : InternalNRMError,
#     '00600' : ResourceUnavailableError,
#     '00601' : STPUnavailableError,                  # compat
#     '00602' : BandwidthUnavailableError,            # compat
#     '00700' : ServiceError,
#     '00701' : UnknownSTPError,
#     '00702' : STPResolutionError,
#     '00703' : VLANInterchangeNotSupportedError,
#     '00704' : STPUnavailableError,
#     '00705' : BandwidthUnavailableError             # compat
# }
# class CallbackTimeoutError(Exception):
# class NSIError(Exception):
# class UnknownNSIError(NSIError):
# class PayloadError(NSIError):
# class MissingParameterError(PayloadError):
# class UnsupportedParameter(PayloadError):
# class ConnectionError(NSIError):
# class InvalidTransitionError(ConnectionError):
# class ConnectionExistsError(ConnectionError):
# class ConnectionNonExistentError(ConnectionError):
# class ConnectionGoneError(ConnectionError):
# class ConnectionCreateError(ConnectionError):
# class SecurityError(NSIError):
# class AuthenticationFailure(SecurityError):
# class UnauthorizedError(SecurityError):
# class TopologyError(NSIError):
# class NoPathFoundError(NSIError):
# class InternalServerError(NSIError):
# class InternalNRMError(InternalServerError):
# class DownstreamNSAError(InternalServerError):
# class ResourceUnavailableError(NSIError):
# class ServiceError(NSIError): # only use this if nothing else applies
# class UnknownSTPError(ServiceError):
# class STPResolutionError(NSIError):
# class VLANInterchangeNotSupportedError(TopologyError):
# class STPUnavailableError(ServiceError, TopologyError): # need this to be a topology error internally
# class BandwidthUnavailableError(ServiceError): # NSI error name is CAPACITY_UNAVAILABLE
#     def __init__(self, message, nsa_id=None, connection_id=None, variables=None):
# def lookup(error_code):


# Path: opennsa/protocols/nsi2/requester.py
from zope.interface import implementer
from twisted.python import log, failure
from twisted.internet import reactor, defer
from opennsa import error
from opennsa.interface import INSIProvider

QUERY_RECURSIVE = 'query_recursive'



# In OpenNSA the requester is something that acts as a provider :-)
@implementer(INSIProvider)
class Requester:

    def __init__(self, requester_client, callback_timeout=None):

        self.requester_client = requester_client

        self.callback_timeout = callback_timeout or DEFAULT_CALLBACK_TIMEOUT
        self.calls = {}
        self.notifications = defer.DeferredQueue()


    def addCall(self, provider_nsa, correlation_id, action):

        key = (provider_nsa, correlation_id)
        assert key not in self.calls, 'Cannot have multiple calls with same NSA / correlationId'

        d = defer.Deferred()
        call = reactor.callLater(self.callback_timeout, self.callbackTimeout, provider_nsa, correlation_id, action)
        self.calls[key] = (action, d, call)
        return d


    def callbackTimeout(self, provider_nsa, correlation_id, action):


        err = error.CallbackTimeoutError('Callback for call %s/%s from %s timed out.' % (correlation_id, action, provider_nsa))