Skip to content

Commit

Permalink
Update EMS to use container RR instead of service call
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Foster committed Oct 23, 2012
1 parent c804e6b commit e30afd5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
72 changes: 36 additions & 36 deletions ion/services/coi/exchange_management_service.py
Expand Up @@ -28,13 +28,13 @@ def create_exchange_space(self, exchange_space=None, org_id=''):
self.assert_condition(exchange_space and org_id, "Arguments not set")

#First make sure that Org with the org_id exists, otherwise bail
org = self.clients.resource_registry.read(org_id)
org = self.container.resource_registry.read(org_id)
if not org:
raise NotFound("Org %s does not exist" % org_id)

exchange_space_id,rev = self.clients.resource_registry.create(exchange_space)
exchange_space_id,rev = self.container.resource_registry.create(exchange_space)

aid = self.clients.resource_registry.create_association(org_id, PRED.hasExchangeSpace, exchange_space_id)
aid = self.container.resource_registry.create_association(org_id, PRED.hasExchangeSpace, exchange_space_id)

# Now do the work

Expand All @@ -55,7 +55,7 @@ def update_exchange_space(self, exchange_space=None):
@throws NotFound object with specified id does not exist
@throws Conflict object not based on latest persisted object version
"""
self.clients.resource_registry.update(exchange_space)
self.container.resource_registry.update(exchange_space)

def read_exchange_space(self, exchange_space_id=''):
"""Returns an Exchange Space resource for the provided exchange space id.
Expand All @@ -64,7 +64,7 @@ def read_exchange_space(self, exchange_space_id=''):
@retval exchange_space ExchangeSpace
@throws NotFound object with specified id does not exist
"""
exchange_space = self.clients.resource_registry.read(exchange_space_id)
exchange_space = self.container.resource_registry.read(exchange_space_id)
if not exchange_space:
raise NotFound("Exchange Space %s does not exist" % exchange_space_id)
return exchange_space
Expand All @@ -76,27 +76,27 @@ def delete_exchange_space(self, exchange_space_id=''):
@param exchange_space_id str
@throws NotFound object with specified id does not exist
"""
exchange_space = self.clients.resource_registry.read(exchange_space_id)
exchange_space = self.container.resource_registry.read(exchange_space_id)
if not exchange_space:
raise NotFound("Exchange Space %s does not exist" % exchange_space_id)

# remove association between itself and org
_, assocs = self.clients.resource_registry.find_subjects(RT.Org, PRED.hasExchangeSpace, exchange_space_id, id_only=True)
_, assocs = self.container.resource_registry.find_subjects(RT.Org, PRED.hasExchangeSpace, exchange_space_id, id_only=True)
for assoc in assocs:
self.clients.resource_registry.delete_association(assoc._id)
self.container.resource_registry.delete_association(assoc._id)

# delete assocs to XNs
_, assocs = self.clients.resource_registry.find_objects(exchange_space_id, PRED.hasExchangeName, RT.ExchangeName, id_only=True)
_, assocs = self.container.resource_registry.find_objects(exchange_space_id, PRED.hasExchangeName, RT.ExchangeName, id_only=True)
for assoc in assocs:
self.clients.resource_registry.delete_association(assoc._id)
self.container.resource_registry.delete_association(assoc._id)

# delete assocs to XPs
_, assocs = self.clients.resource_registry.find_objects(exchange_space_id, PRED.hasExchangePoint, RT.ExchangePoint, id_only=True)
_, assocs = self.container.resource_registry.find_objects(exchange_space_id, PRED.hasExchangePoint, RT.ExchangePoint, id_only=True)
for assoc in assocs:
self.clients.resource_registry.delete_association(assoc._id)
self.container.resource_registry.delete_association(assoc._id)

# delete XS now
self.clients.resource_registry.delete(exchange_space_id)
self.container.resource_registry.delete(exchange_space_id)

# call container API to delete @TODO this is clunky
xs = exchange.ExchangeSpace(self.container.ex_manager, self.container.ex_manager._priviledged_transport, exchange_space.name)
Expand Down Expand Up @@ -124,7 +124,7 @@ def declare_exchange_name(self, exchange_name=None, exchange_space_id=''):
if not exchange_name.xn_type in typemap:
raise BadRequest("Unknown exchange name type: %s" % exchange_name.xn_type)

xns, assocs = self.clients.resource_registry.find_objects(subject=exchange_space_id, predicate=PRED.hasExchangeName, id_only=False)
xns, assocs = self.container.resource_registry.find_objects(subject=exchange_space_id, predicate=PRED.hasExchangeName, id_only=False)
exchange_name_id = None
for xn in xns:
if xn.name == exchange_name.name and xn.xn_type == exchange_name.xn_type:
Expand All @@ -135,9 +135,9 @@ def declare_exchange_name(self, exchange_name=None, exchange_space_id=''):

exchange_space = self.read_exchange_space(exchange_space_id)
if not exchange_name_id:
exchange_name_id,rev = self.clients.resource_registry.create(exchange_name)
exchange_name_id,rev = self.container.resource_registry.create(exchange_name)

aid = self.clients.resource_registry.create_association(exchange_space_id, PRED.hasExchangeName, exchange_name_id)
aid = self.container.resource_registry.create_association(exchange_space_id, PRED.hasExchangeName, exchange_name_id)

# call container API
xs = exchange.ExchangeSpace(self.container.ex_manager, self.container.ex_manager._priviledged_transport, exchange_space.name)
Expand All @@ -155,26 +155,26 @@ def undeclare_exchange_name(self, canonical_name='', exchange_space_id=''):
"""
# @TODO: currently we are using the exchange_name's id as the canonical name
# and exchange_space_id is unused?
exchange_name = self.clients.resource_registry.read(canonical_name)
exchange_name = self.container.resource_registry.read(canonical_name)
if not exchange_name:
raise NotFound("Exchange Name with id %s does not exist" % canonical_name)

exchange_name_id = exchange_name._id # yes, this should be same, but let's make it look cleaner

# get associated XS first
exchange_space_list, assoc_list = self.clients.resource_registry.find_subjects(RT.ExchangeSpace, PRED.hasExchangeName, exchange_name_id)
exchange_space_list, assoc_list = self.container.resource_registry.find_subjects(RT.ExchangeSpace, PRED.hasExchangeName, exchange_name_id)
if not len(exchange_space_list) == 1:
raise NotFound("Associated Exchange Space to Exchange Name %s does not exist" % exchange_name_id)

exchange_space = exchange_space_list[0]

# remove association between itself and XS
_, assocs = self.clients.resource_registry.find_subjects(RT.ExchangeSpace, PRED.hasExchangeName, exchange_name_id, id_only=True)
_, assocs = self.container.resource_registry.find_subjects(RT.ExchangeSpace, PRED.hasExchangeName, exchange_name_id, id_only=True)
for assoc in assocs:
self.clients.resource_registry.delete_association(assoc._id)
self.container.resource_registry.delete_association(assoc._id)

# remove XN
self.clients.resource_registry.delete(exchange_name_id)
self.container.resource_registry.delete(exchange_name_id)

# call container API
xs = exchange.ExchangeSpace(self.container.ex_manager, self.container.ex_manager._priviledged_transport, exchange_space.name)
Expand All @@ -198,7 +198,7 @@ def create_exchange_point(self, exchange_point=None, exchange_space_id=''):
@throws BadRequest if object passed has _id or _rev attribute
"""

xs_xps, assocs = self.clients.resource_registry.find_objects(subject=exchange_space_id, predicate=PRED.hasExchangePoint, id_only=False)
xs_xps, assocs = self.container.resource_registry.find_objects(subject=exchange_space_id, predicate=PRED.hasExchangePoint, id_only=False)
exchange_point_id = None
for xs_xp in xs_xps:
if xs_xp.name == exchange_point.name and xs_xp.topology_type == exchange_point.topology_type:
Expand All @@ -207,9 +207,9 @@ def create_exchange_point(self, exchange_point=None, exchange_space_id=''):

exchange_space = self.read_exchange_space(exchange_space_id)
if not exchange_point_id:
exchange_point_id, _ver = self.clients.resource_registry.create(exchange_point)
exchange_point_id, _ver = self.container.resource_registry.create(exchange_point)

self.clients.resource_registry.create_association(exchange_space_id, PRED.hasExchangePoint, exchange_point_id)
self.container.resource_registry.create_association(exchange_space_id, PRED.hasExchangePoint, exchange_point_id)

# call container API
xs = exchange.ExchangeSpace(self.container.ex_manager, self.container.ex_manager._priviledged_transport, exchange_space.name)
Expand All @@ -226,7 +226,7 @@ def update_exchange_point(self, exchange_point=None):
@throws NotFound object with specified id does not exist
@throws Conflict object not based on latest persisted object version
"""
self.clients.resource_registry.update(exchange_point)
self.container.resource_registry.update(exchange_point)


def read_exchange_point(self, exchange_point_id=''):
Expand All @@ -236,7 +236,7 @@ def read_exchange_point(self, exchange_point_id=''):
@retval exchange_point ExchangePoint
@throws NotFound object with specified id does not exist
"""
exchange_point = self.clients.resource_registry.read(exchange_point_id)
exchange_point = self.container.resource_registry.read(exchange_point_id)
if not exchange_point:
raise NotFound("Exchange Point %s does not exist" % exchange_point_id)
return exchange_point
Expand All @@ -247,23 +247,23 @@ def delete_exchange_point(self, exchange_point_id=''):
@param exchange_point_id str
@throws NotFound object with specified id does not exist
"""
exchange_point = self.clients.resource_registry.read(exchange_point_id)
exchange_point = self.container.resource_registry.read(exchange_point_id)
if not exchange_point:
raise NotFound("Exchange Point %s does not exist" % exchange_point_id)

# get associated XS first
exchange_space_list, assoc_list = self.clients.resource_registry.find_subjects(RT.ExchangeSpace, PRED.hasExchangePoint, exchange_point_id)
exchange_space_list, assoc_list = self.container.resource_registry.find_subjects(RT.ExchangeSpace, PRED.hasExchangePoint, exchange_point_id)
if not len(exchange_space_list) == 1:
raise NotFound("Associated Exchange Space to Exchange Point %s does not exist" % exchange_point_id)

exchange_space = exchange_space_list[0]

# delete association to XS
for assoc in assoc_list:
self.clients.resource_registry.delete_association(assoc._id)
self.container.resource_registry.delete_association(assoc._id)

# delete from RR
self.clients.resource_registry.delete(exchange_point_id)
self.container.resource_registry.delete(exchange_point_id)

# call container API
xs = exchange.ExchangeSpace(self.container.ex_manager, self.container.ex_manager._priviledged_transport, exchange_space.name)
Expand All @@ -286,8 +286,8 @@ def create_exchange_broker(self, exchange_broker=None):
@retval exchange_broker_id str
@throws BadRequest if object passed has _id or _rev attribute
"""
exchange_point_id, _ver = self.clients.resource_registry.create(exchange_point)
return exchange_point_id
exchange_broker_id, _ver = self.container.resource_registry.create(exchange_broker)
return exchange_broker_id

def update_exchange_broker(self, exchange_broker=None):
"""Updates an existing exchange broker resource.
Expand All @@ -297,7 +297,7 @@ def update_exchange_broker(self, exchange_broker=None):
@throws NotFound object with specified id does not exist
@throws Conflict object not based on latest persisted object version
"""
self.clients.resource_registry.update(exchange_broker)
self.container.resource_registry.update(exchange_broker)

def read_exchange_broker(self, exchange_broker_id=''):
"""Returns an existing exchange broker resource.
Expand All @@ -306,7 +306,7 @@ def read_exchange_broker(self, exchange_broker_id=''):
@retval exchange_broker ExchangeBroker
@throws NotFound object with specified id does not exist
"""
exchange_broker = self.clients.resource_registry.read(exchange_broker_id)
exchange_broker = self.container.resource_registry.read(exchange_broker_id)
if not exchange_broker:
raise NotFound("Exchange Broker %s does not exist" % exchange_broker_id)
return exchange_broker
Expand All @@ -317,10 +317,10 @@ def delete_exchange_broker(self, exchange_broker_id=''):
@param exchange_broker_id str
@throws NotFound object with specified id does not exist
"""
exchange_broker = self.clients.resource_registry.read(exchange_broker_id)
exchange_broker = self.container.resource_registry.read(exchange_broker_id)
if not exchange_broker:
raise NotFound("Exchange Broker %s does not exist" % exchange_broker_id)
self.clients.resource_registry.delete(exchange_broker)
self.container.resource_registry.delete(exchange_broker)

def find_exchange_broker(self, filters=None):
"""Returns a list of exchange broker resources for the provided resource filter.
Expand Down
34 changes: 20 additions & 14 deletions ion/services/coi/test/test_exchange_management_service.py
Expand Up @@ -28,28 +28,34 @@
class TestExchangeManagementService(PyonTestCase):

def setUp(self):

self.container = Mock()

mock_clients = self._create_service_mock('exchange_management')

self.exchange_management_service = ExchangeManagementService()
self.exchange_management_service.clients = mock_clients

self.exchange_management_service.container = Mock()
self.exchange_management_service.container = self.container

# Rename to save some typing
self.mock_create = mock_clients.resource_registry.create
self.mock_read = mock_clients.resource_registry.read
self.mock_update = mock_clients.resource_registry.update
self.mock_delete = mock_clients.resource_registry.delete
self.mock_create_association = mock_clients.resource_registry.create_association
self.mock_delete_association = mock_clients.resource_registry.delete_association
self.mock_find_objects = mock_clients.resource_registry.find_objects
self.mock_find_resources = mock_clients.resource_registry.find_resources
self.mock_find_subjects = mock_clients.resource_registry.find_subjects
self.mock_create = self.container.resource_registry.create
self.mock_read = self.container.resource_registry.read
self.mock_update = self.container.resource_registry.update
self.mock_delete = self.container.resource_registry.delete
self.mock_create_association = self.container.resource_registry.create_association
self.mock_delete_association = self.container.resource_registry.delete_association
self.mock_find_objects = self.container.resource_registry.find_objects
self.mock_find_resources = self.container.resource_registry.find_resources
self.mock_find_subjects = self.container.resource_registry.find_subjects

# Exchange Space
self.exchange_space = Mock()
self.exchange_space.name = "Foo"

# fixup for direct RR access
self.container.resource_registry.create.return_value = (sentinel.id, sentinel.rev)
self.container.resource_registry.find_subjects.return_value = (sentinel.id, [])

def test_create_exchange_space(self):
self.mock_create.return_value = ['111', 1]
Expand All @@ -72,7 +78,7 @@ def test_read_and_update_exchange_space(self):
exchange_space = self.exchange_management_service.read_exchange_space('111')

assert exchange_space is self.mock_read.return_value
self.mock_read.assert_called_once_with('111', '')
self.mock_read.assert_called_once_with('111')

exchange_space.name = 'Bar'

Expand All @@ -89,7 +95,7 @@ def test_delete_exchange_space(self):

self.exchange_management_service.delete_exchange_space('111')

self.mock_read.assert_called_once_with('111', '')
self.mock_read.assert_called_once_with('111')
self.mock_delete.assert_called_once_with('111')

def test_read_exchange_space_not_found(self):
Expand All @@ -101,7 +107,7 @@ def test_read_exchange_space_not_found(self):

ex = cm.exception
self.assertEqual(ex.message, 'Exchange Space bad does not exist')
self.mock_read.assert_called_once_with('bad', '')
self.mock_read.assert_called_once_with('bad')

def test_delete_exchange_space_not_found(self):
self.mock_read.return_value = None
Expand All @@ -112,7 +118,7 @@ def test_delete_exchange_space_not_found(self):

ex = cm.exception
self.assertEqual(ex.message, 'Exchange Space bad does not exist')
self.mock_read.assert_called_once_with('bad', '')
self.mock_read.assert_called_once_with('bad')


@attr('INT', group='coi')
Expand Down

0 comments on commit e30afd5

Please sign in to comment.