# Path: opennsa/state.py
# LOG_SYSTEM = 'opennsa.state'
# RESERVE_START           = 'ReserveStart'
# RESERVE_CHECKING        = 'ReserveChecking'
# RESERVE_HELD            = 'ReserveHeld'
# RESERVE_COMMITTING      = 'ReserveComitting'
# RESERVE_FAILED          = 'ReserveFailed'
# RESERVE_ABORTING        = 'ReserveAborting'
# RESERVE_TIMEOUT         = 'ReserveTimeout'      # Only UPA
# RELEASED                = 'Released'
# PROVISIONING            = 'Provisioning'
# PROVISIONED             = 'Provisioned'
# RELEASING               = 'Releasing'
# CREATED                 = 'Created'
# FAILED                  = 'Failed'
# PASSED_ENDTIME          = 'PassedEndTime'
# TERMINATING             = 'Terminating'
# TERMINATED              = 'Terminated'
# RESERVE_TRANSITIONS = {
#     RESERVE_START       : [ RESERVE_CHECKING                                       ],
#     RESERVE_CHECKING    : [ RESERVE_HELD,       RESERVE_FAILED                     ],
#     RESERVE_HELD        : [ RESERVE_COMMITTING, RESERVE_ABORTING, RESERVE_TIMEOUT  ],
#     RESERVE_COMMITTING  : [ RESERVE_START                                          ],
#     RESERVE_FAILED      : [ RESERVE_ABORTING                                       ],
#     RESERVE_TIMEOUT     : [ RESERVE_ABORTING                                       ],
#     RESERVE_ABORTING    : [ RESERVE_START                                          ]
# }
# PROVISION_TRANSITIONS = {
#     RELEASED        : [ PROVISIONING  ],
#     PROVISIONING    : [ PROVISIONING,   PROVISIONED   ],
#     PROVISIONED     : [ RELEASING     ],
#     RELEASING       : [ RELEASING,      RELEASED      ]
# }
# LIFECYCLE_TRANSITIONS = {
#     CREATED         : [ FAILED, PASSED_ENDTIME, TERMINATING, TERMINATED ],
#     FAILED          : [ TERMINATING ],
#     PASSED_ENDTIME  : [ TERMINATING ],
#     TERMINATING     : [ TERMINATING, TERMINATED ],
#     TERMINATED      : []
# }
# SUBSCRIPTIONS = {}
# def subscribe(connection_id, f):
# def desubscribe(connection_id, f):
# def saveNotify(conn):
#     def notify(conn):
# def _switchState(transition_schema, old_state, new_state):
# def reserveChecking(conn):
# def reserveHeld(conn):
# def reserveFailed(conn):
# def reserveCommit(conn):
# def reserveAbort(conn):
# def reserveTimeout(conn):
# def reserved(conn):
# def reserveMultiSwitch(conn, *states):
# def provisioning(conn):
# def provisioned(conn):
# def releasing(conn):
# def released(conn):
# def passedEndtime(conn):
# def failed(conn):
# def terminating(conn):
# def terminated(conn):
# 
# 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 datetime
import psycopg2
from twisted.internet import defer
from twisted.trial import unittest
from opennsa import state
from opennsa.backends.common import genericbackend
from . import db






class DatabaseTest(unittest.TestCase):

    def setUp(self):
        db.setupDatabase()


    @defer.inlineCallbacks
    def testReverseStartEndTimeConstraint(self):

        now = datetime.datetime.utcnow()
        start_time = now - datetime.timedelta(seconds=10)
        end_time   = now - datetime.timedelta(seconds=1000)

        conn = genericbackend.GenericBackendConnections(
            connection_id='conn-123',
            revision=0,
            global_reservation_id='gid-123',
            description='test',
            requester_nsa='req-nsa',
            reserve_time=now,

# Path: opennsa/state.py
# LOG_SYSTEM = 'opennsa.state'
# RESERVE_START           = 'ReserveStart'
# RESERVE_CHECKING        = 'ReserveChecking'
# RESERVE_HELD            = 'ReserveHeld'
# RESERVE_COMMITTING      = 'ReserveComitting'
# RESERVE_FAILED          = 'ReserveFailed'
# RESERVE_ABORTING        = 'ReserveAborting'
# RESERVE_TIMEOUT         = 'ReserveTimeout'      # Only UPA
# RELEASED                = 'Released'
# PROVISIONING            = 'Provisioning'
# PROVISIONED             = 'Provisioned'
# RELEASING               = 'Releasing'
# CREATED                 = 'Created'
# FAILED                  = 'Failed'
# PASSED_ENDTIME          = 'PassedEndTime'
# TERMINATING             = 'Terminating'
# TERMINATED              = 'Terminated'
# RESERVE_TRANSITIONS = {
#     RESERVE_START       : [ RESERVE_CHECKING                                       ],
#     RESERVE_CHECKING    : [ RESERVE_HELD,       RESERVE_FAILED                     ],
#     RESERVE_HELD        : [ RESERVE_COMMITTING, RESERVE_ABORTING, RESERVE_TIMEOUT  ],
#     RESERVE_COMMITTING  : [ RESERVE_START                                          ],
#     RESERVE_FAILED      : [ RESERVE_ABORTING                                       ],
#     RESERVE_TIMEOUT     : [ RESERVE_ABORTING                                       ],
#     RESERVE_ABORTING    : [ RESERVE_START                                          ]
# }
# PROVISION_TRANSITIONS = {
#     RELEASED        : [ PROVISIONING  ],
#     PROVISIONING    : [ PROVISIONING,   PROVISIONED   ],
#     PROVISIONED     : [ RELEASING     ],
#     RELEASING       : [ RELEASING,      RELEASED      ]
# }
# LIFECYCLE_TRANSITIONS = {
#     CREATED         : [ FAILED, PASSED_ENDTIME, TERMINATING, TERMINATED ],
#     FAILED          : [ TERMINATING ],
#     PASSED_ENDTIME  : [ TERMINATING ],
#     TERMINATING     : [ TERMINATING, TERMINATED ],
#     TERMINATED      : []
# }
# SUBSCRIPTIONS = {}
# def subscribe(connection_id, f):
# def desubscribe(connection_id, f):
# def saveNotify(conn):
#     def notify(conn):
# def _switchState(transition_schema, old_state, new_state):
# def reserveChecking(conn):
# def reserveHeld(conn):
# def reserveFailed(conn):
# def reserveCommit(conn):
# def reserveAbort(conn):
# def reserveTimeout(conn):
# def reserved(conn):
# def reserveMultiSwitch(conn, *states):
# def provisioning(conn):
# def provisioned(conn):
# def releasing(conn):
# def released(conn):
# def passedEndtime(conn):
# def failed(conn):
# def terminating(conn):
# def terminated(conn):
# 
# 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: test/test_database.py
import datetime
import psycopg2
from twisted.internet import defer
from twisted.trial import unittest
from opennsa import state
from opennsa.backends.common import genericbackend
from . import db







class DatabaseTest(unittest.TestCase):

    def setUp(self):
        db.setupDatabase()


    @defer.inlineCallbacks
    def testReverseStartEndTimeConstraint(self):

        now = datetime.datetime.utcnow()
        start_time = now - datetime.timedelta(seconds=10)
        end_time   = now - datetime.timedelta(seconds=1000)

        conn = genericbackend.GenericBackendConnections(
            connection_id='conn-123',
            revision=0,
            global_reservation_id='gid-123',
            description='test',
            requester_nsa='req-nsa',
            reserve_time=now,

            reservation_state=state.RESERVE_START,