import datetime
#
from sfa.util.faults import MissingSfaInfo, UnknownSfaType, \
- RecordNotFound, SfaNotImplemented, SliverDoesNotExist, SearchFailed
+ RecordNotFound, SfaNotImplemented, SliverDoesNotExist, SearchFailed, \
+ UnsupportedOperation
from sfa.util.sfalogging import logger
from sfa.util.defaultdict import defaultdict
from sfa.util.sfatime import utcparse, datetime_to_string, datetime_to_epoch
def status (self, urns, options={}):
aggregate = PlAggregate(self)
desc = aggregate.describe(urns)
- return desc['geni_slivers']
+ status = {'geni_urn': desc['geni_urn'],
+ 'geni_slivers': desc['geni_slivers']}
+ return status
def allocate (self, urn, rspec_string, options={}):
xrn = Xrn(urn)
nodes = slices.verify_slice_nodes(slice, requested_slivers, peer)
# update all sliver allocation states setting then to geni_allocated
- sliver_state_updated = {}
+ sliver_ids = []
for node in nodes:
sliver_hrn = '%s.%s-%s' % (self.hrn, slice['slice_id'], node['node_id'])
sliver_id = Xrn(sliver_hrn, type='sliver').urn
- sliver_state_updated[sliver_id] = False
-
- constraint = SliverAllocation.sliver_id.in_(sliver_state_updated.keys())
- cur_sliver_allocations = dbsession.query(SliverAllocation).filter(constraint)
- for sliver_allocation in cur_sliver_allocations:
- sliver_allocation.allocation_state = 'geni_allocated'
- sliver_state_updated[sliver_allocation.sliver_id] = True
- dbsession.commit()
-
- # Some states may not have been updated becuase no sliver allocation state record
- # exists for the sliver. Insert new allocation records for these slivers and set
- # it to geni_allocated.
- for (sliver_id, state_updated) in sliver_state_updated.items():
- if state_updated == False:
- record = SliverAllocation(sliver_id=sliver_id, allocation_state='geni_allocated')
- dbsession.add(record)
- dbsession.commit()
-
+ sliver_ids.append(sliver_id)
+ SliverAllocation.set_allocations(sliver_ids, 'geni_allocated')
+
# add/remove links links
slices.verify_slice_links(slice, rspec.version.get_link_requests(), nodes)
aggregate = PlAggregate(self)
slivers = aggregate.get_slivers(urns)
sliver_ids = [sliver['sliver_id'] for sliver in slivers]
- constraint = SliverAllocation.sliver_id.in_(sliver_ids)
- cur_sliver_allocations = dbsession.query(SliverAllocation).filter(constraint)
- for sliver_allocation in cur_sliver_allocations:
- sliver_allocation.allocation_state = 'geni_provisioned'
- dbsession.commit()
+ SliverAllocation.set_allocations(sliver_ids, 'geni_provisioned')
return self.describe(urns, None, options=options)
def delete(self, urns, options={}):
-
+ # collect sliver ids so we can update sliver allocation states after
+ # we remove the slivers.
aggregate = PlAggregate(self)
slivers = aggregate.get_slivers(urns)
- slice_id = slivers[0]['slice_id']
- node_ids = []
- sliver_ids = []
- for sliver in slivers:
- node_ids.append(sliver['node_id'])
- sliver_ids.append(sliver['sliver_id'])
-
- # determine if this is a peer slice
- # xxx I wonder if this would not need to use PlSlices.get_peer instead
- # in which case plc.peers could be deprecated as this here
- # is the only/last call to this last method in plc.peers
- slice_hrn = PlXrn(auth=self.hrn, slicename=slivers[0]['name']).get_hrn()
- peer = peers.get_peer(self, slice_hrn)
- try:
- if peer:
- self.shell.UnBindObjectFromPeer('slice', slice_id, peer)
-
- self.shell.DeleteSliceFromNodes(slice_id, node_ids)
-
- # update slivera allocation states
- constraint = SliverAllocation.sliver_id.in_(sliver_ids)
- cur_sliver_allocations = dbsession.query(SliverAllocation).filter(constraint)
- for sliver_allocation in cur_sliver_allocations:
- sliver_allocation.allocation_state = 'geni_unallocated'
- dbsession.commit()
- finally:
- if peer:
- self.shell.BindObjectToPeer('slice', slice_id, peer, slice['peer_slice_id'])
-
-
+ if slivers:
+ slice_id = slivers[0]['slice_id']
+ node_ids = []
+ sliver_ids = []
+ for sliver in slivers:
+ node_ids.append(sliver['node_id'])
+ sliver_ids.append(sliver['sliver_id'])
+
+ # determine if this is a peer slice
+ # xxx I wonder if this would not need to use PlSlices.get_peer instead
+ # in which case plc.peers could be deprecated as this here
+ # is the only/last call to this last method in plc.peers
+ slice_hrn = PlXrn(auth=self.hrn, slicename=slivers[0]['name']).get_hrn()
+ peer = peers.get_peer(self, slice_hrn)
+ try:
+ if peer:
+ self.shell.UnBindObjectFromPeer('slice', slice_id, peer)
+
+ self.shell.DeleteSliceFromNodes(slice_id, node_ids)
+
+ # delete sliver allocation states
+ SliverAllocation.delete_allocations(sliver_ids)
+ finally:
+ if peer:
+ self.shell.BindObjectToPeer('slice', slice_id, peer, slice['peer_slice_id'])
# prepare return struct
geni_slivers = []
- for node_id in node_ids:
- sliver_hrn = '%s.%s-%s' % (self.hrn, slice_id, node_id)
+ for sliver in slivers:
geni_slivers.append(
- {'geni_sliver_urn': Xrn(sliver_hrn, type='sliver').urn,
+ {'geni_sliver_urn': sliver['sliver_id'],
'geni_allocation_status': 'geni_unallocated',
- 'geni_expires': datetime_to_string(utcparse(slivers[0]['expires']))})
+ 'geni_expires': datetime_to_string(utcparse(sliver['expires']))})
return geni_slivers
def renew (self, urns, expiration_time, options={}):
slice = slices[0]
requested_time = utcparse(expiration_time)
record = {'expires': int(datetime_to_epoch(requested_time))}
- try:
- self.shell.UpdateSlice(slice['slice_id'], record)
- return True
- except:
- return False
+ self.shell.UpdateSlice(slice['slice_id'], record)
+ description = self.describe(urns, None, options)
+ return description['geni_slivers']
+
def perform_operational_action (self, urns, action, options={}):
# MyPLC doesn't support operational actions. Lets pretend like it
# supports start, but reject everything else.
action = action.lower()
- if action == 'geni_start':
- pass
- else:
+ if action not in ['geni_start']:
raise UnsupportedOperation(action)
+
+ # fault if sliver is not full allocated (operational status is geni_pending_allocation)
description = self.describe(urns, None, options)
- return description['geni_slivers']
+ for sliver in description['geni_slivers']:
+ if sliver['geni_operational_status'] == 'geni_pending_allocation':
+ raise UnsupportedOperation(action, "Sliver must be fully allocated (operational status is not geni_pending_allocation)")
+ #
+ # Perform Operational Action Here
+ #
+
+ geni_slivers = self.describe(urns, None, options)['geni_slivers']
+ return geni_slivers
# set the 'enabled' tag to 0
def shutdown (self, xrn, options={}):