renew returns a list of structs
[sfa.git] / sfa / planetlab / pldriver.py
index df4bb5f..138e254 100644 (file)
@@ -1,7 +1,8 @@
 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
@@ -575,7 +576,9 @@ class PlDriver (Driver):
     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)
@@ -614,28 +617,13 @@ class PlDriver (Driver):
         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)
 
@@ -665,16 +653,13 @@ class PlDriver (Driver):
         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'] 
@@ -696,18 +681,12 @@ class PlDriver (Driver):
  
             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()
+            # 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:
@@ -731,22 +710,29 @@ class PlDriver (Driver):
         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={}):