even safer handling of expiration types across
[sfa.git] / sfa / managers / aggregate_manager_pl.py
index 557e325..0dd236c 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+
 
 import datetime
 import time
@@ -26,20 +26,20 @@ from sfa.plc.slices import *
 from sfa.util.version import version_core
 from sfa.rspecs.rspec_version import RSpecVersion
 from sfa.rspecs.sfa_rspec import sfa_rspec_version
-from sfa.rspecs.pg_rspec import pg_rspec_version
+from sfa.rspecs.pg_rspec import pg_rspec_ad_version, pg_rspec_request_version
 from sfa.rspecs.rspec_parser import parse_rspec 
 from sfa.util.sfatime import utcparse
 from sfa.util.callids import Callids
 
 def GetVersion(api):
     xrn=Xrn(api.hrn)
-
-    supported_rspecs = [dict(pg_rspec_version), dict(sfa_rspec_version)]
+    request_rspec_versions = [dict(pg_rspec_request_version), dict(sfa_rspec_version)]
+    ad_rspec_versions = [dict(pg_rspec_ad_version), dict(sfa_rspec_version)]
     version_more = {'interface':'aggregate',
                     'testbed':'myplc',
                     'hrn':xrn.get_hrn(),
-                    'request_rspec_versions': supported_rspecs,
-                    'ad_rspec_versions': supported_rspecs,
+                    'request_rspec_versions': request_rspec_versions,
+                    'ad_rspec_versions': ad_rspec_versions,
                     'default_ad_rspec': dict(sfa_rspec_version)
                     }
     return version_core(version_more)
@@ -79,6 +79,7 @@ def __get_registry_objects(slice_xrn, creds, users):
 
         slice = {}
         
+        # get_expiration always returns a normalized datetime - no need to utcparse
         extime = Credential(string=creds[0]).get_expiration()
         # If the expiration time is > 60 days from now, set the expiration time to 60 days from now
         if extime > datetime.datetime.utcnow() + datetime.timedelta(days=60):
@@ -118,7 +119,7 @@ def SliverStatus(api, slice_xrn, creds, call_id):
     
     slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids','person_ids','name','expires'])
     if len(slices) == 0:        
-        raise Exception("Slice %s not found (used %s as slicename internally)" % slice_xrn, slicename)
+        raise Exception("Slice %s not found (used %s as slicename internally)" % (slice_xrn, slicename))
     slice = slices[0]
     
     # report about the local nodes only
@@ -195,15 +196,47 @@ def CreateSliver(api, slice_xrn, creds, rspec_string, users, call_id):
     # add nodes from rspec
     added_nodes = list(set(requested_slivers).difference(current_slivers))
 
+    # get sliver attributes
+    requested_slice_attributes = rspec.get_slice_attributes()
+    removed_slice_attributes = []
+    existing_slice_attributes = []    
+    for slice_tag in api.plshell.GetSliceTags(api.plauth, {'slice_id': slice['slice_id']}):
+        attribute_found=False
+        for requested_attribute in requested_slice_attributes:
+            if requested_attribute['name'] == slice_tag['tagname'] and \
+               requested_attribute['value'] == slice_tag['value']:
+                attribute_found=True
+
+        if not attribute_found: 
+            removed_slice_attributes.append(slice_tag)
+        else:
+            existing_slice_attributes.append(slice_tag)  
+         
+    #api.logger.debug("requested slice attributes: %s" % str(requested_slice_attributes))
+    #api.logger.debug("removed slice attributes: %s" % str(removed_slice_attributes))
+    #api.logger.debug("existing slice attributes: %s" % str(existing_slice_attributes))
     try:
         if peer:
             api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
 
         api.plshell.AddSliceToNodes(api.plauth, slice['name'], added_nodes) 
         api.plshell.DeleteSliceFromNodes(api.plauth, slice['name'], deleted_nodes)
-
-        # TODO: update slice tags
-        #network.updateSliceTags()
+        # remove stale attributes
+        for attribute in removed_slice_attributes:
+            try:
+                api.plshell.DeleteSliceTag(api.plauth, attribute['slice_tag_id'])
+            except Exception, e:
+                api.logger.warn('Failed to remove sliver attribute. name: %s, value: %s, node_id: %s\nCause:%s'\
+                                % (name, value,  node_id, str(e)))
+
+        # add requested_attributes
+        for attribute in requested_slice_attributes:
+            try:
+                name, value, node_id = attribute['name'], attribute['value'], attribute.get('node_id', None)
+                api.plshell.AddSliceTag(api.plauth, slice['name'], name, value, node_id)
+            except Exception, e:
+                api.logger.warn('Failed to add sliver attribute. name: %s, value: %s, node_id: %s\nCause:%s'\
+                                % (name, value,  node_id, str(e)))
 
     finally:
         if peer:
@@ -283,8 +316,8 @@ def DeleteSliver(api, xrn, creds, call_id):
     return 1
 
 # xxx Thierry : caching at the aggregate level sounds wrong...
-caching=True
-#caching=False
+#caching=True
+caching=False
 def ListSlices(api, creds, call_id):
     if Callids().already_handled(call_id): return []
     # look in cache first
@@ -313,7 +346,11 @@ def ListResources(api, creds, options,call_id):
     # get the rspec's return format from options
     rspec_version = RSpecVersion(options.get('rspec_version'))
     version_string = "rspec_%s" % (rspec_version.get_version_name())
-    
+
+    #panos adding the info option to the caching key (can be improved)
+    if options.get('info'):
+       version_string = version_string + "_"+options.get('info', 'default')
+
     # look in cache first
     if caching and api.cache and not xrn:
         rspec = api.cache.get(version_string)
@@ -321,9 +358,12 @@ def ListResources(api, creds, options,call_id):
             api.logger.info("aggregate.ListResources: returning cached value for hrn %s"%hrn)
             return rspec 
 
-    aggregate = Aggregate(api)
+    #aggregate = Aggregate(api)
+    #panos: passing user-defined options
+    #print "manager options = ",options
+    aggregate = Aggregate(api, options)
 
-    rspec =  aggregate.get_rspec(slice_xrn=xrn, version=rspec_version, type='advertisement')
+    rspec =  aggregate.get_rspec(slice_xrn=xrn, version=rspec_version)
 
     # cache the result
     if caching and api.cache and not xrn: