Merge branch 'master' into senslab2
[sfa.git] / sfa / managers / slice_manager.py
index b82ac66..b261fe2 100644 (file)
@@ -12,20 +12,30 @@ from sfa.util.sfalogging import logger
 from sfa.util.xrn import Xrn, urn_to_hrn
 from sfa.util.version import version_core
 from sfa.util.callids import Callids
+from sfa.util.cache import Cache
+
 from sfa.server.threadmanager import ThreadManager
+
 from sfa.rspecs.rspec_converter import RSpecConverter
 from sfa.rspecs.version_manager import VersionManager
 from sfa.rspecs.rspec import RSpec 
+
 from sfa.client.client_helper import sfa_to_pg_users_arg
 from sfa.client.return_value import ReturnValue
 
 class SliceManager:
-    def __init__ (self):
-        # xxx todo should be configurable 
-        # self.caching=False
-        self.caching=True
+
+    # the cache instance is a class member so it survives across incoming requests
+    cache = None
+
+    def __init__ (self, config):
+        self.cache=None
+        if config.SFA_SM_CACHING:
+            if SliceManager.cache is None:
+                SliceManager.cache = Cache()
+            self.cache = SliceManager.cache
         
-    def GetVersion(self, api):
+    def GetVersion(self, api, options):
         # peers explicitly in aggregates.xml
         peers =dict ([ (peername,interface.get_url()) for (peername,interface) in api.aggregates.iteritems()
                        if peername != api.hrn])
@@ -38,15 +48,17 @@ class SliceManager:
             if rspec_version.content_type in ['*', 'request']:
                 request_rspec_versions.append(rspec_version.to_dict())
         xrn=Xrn(api.hrn, 'authority+sa')
-        version_more = {'interface':'slicemgr',
-                        'sfa': 2,
-                        'geni_api': api.config.SFA_AGGREGATE_API_VERSION,
-                        'hrn' : xrn.get_hrn(),
-                        'urn' : xrn.get_urn(),
-                        'peers': peers,
-                        'geni_request_rspec_versions': request_rspec_versions,
-                        'geni_ad_rspec_versions': ad_rspec_versions,
-                    }
+        version_more = {
+            'interface':'slicemgr',
+            'sfa': 2,
+            'geni_api': 2,
+            'geni_api_versions': {'2': 'http://%s:%s' % (api.config.SFA_SM_HOST, api.config.SFA_SM_PORT)},
+            'hrn' : xrn.get_hrn(),
+            'urn' : xrn.get_urn(),
+            'peers': peers,
+            'geni_request_rspec_versions': request_rspec_versions,
+            'geni_ad_rspec_versions': ad_rspec_versions,
+            }
         sm_version=version_core(version_more)
         # local aggregate if present needs to have localhost resolved
         if api.hrn in api.aggregates:
@@ -104,6 +116,7 @@ class SliceManager:
                     forward_options['rspec_version'] = version_manager.get_version('SFA 1').to_dict()
                 else:
                     forward_options['rspec_version'] = version_manager.get_version('ProtoGENI 2').to_dict()
+                    forward_options['geni_rspec_version'] = {'type': 'geni', 'version': '3.0'}
                 rspec = server.ListResources(credential, forward_options)
                 return {"aggregate": aggregate, "rspec": rspec, "elapsed": time.time()-tStart, "status": "success"}
             except Exception, e:
@@ -121,9 +134,11 @@ class SliceManager:
         version_string = "rspec_%s" % (rspec_version)
     
         # look in cache first
-        if self.caching and api.cache and not xrn:
-            rspec =  api.cache.get(version_string)
+        cached_requested = options.get('cached', True)
+        if not xrn and self.cache and cached_requested:
+            rspec =  self.cache.get(version_string)
             if rspec:
+                api.logger.debug("SliceManager.ListResources returns cached advertisement")
                 return rspec
     
         # get the callers hrn
@@ -164,8 +179,9 @@ class SliceManager:
                     api.logger.log_exc("SM.ListResources: Failed to merge aggregate rspec")
     
         # cache the result
-        if self.caching and api.cache and not xrn:
-            api.cache.add(version_string, rspec.toxml())
+        if self.cache and not xrn:
+            api.logger.debug("SliceManager.ListResources caches advertisement")
+            self.cache.add(version_string, rspec.toxml())
     
         return rspec.toxml()
 
@@ -245,18 +261,26 @@ class SliceManager:
         call_id = options.get('call_id')
         if Callids().already_handled(call_id): return True
 
-        def _RenewSliver(server, xrn, creds, expiration_time, options):
-            return server.RenewSliver(xrn, creds, expiration_time, options)
-    
-        (hrn, type) = urn_to_hrn(xrn)
+        def _RenewSliver(aggregate, server, xrn, creds, expiration_time, options):
+            try:
+                result=server.RenewSliver(xrn, creds, expiration_time, options)
+                if type(result)!=dict:
+                    result = {"code": {"geni_code": 0}, value: result}
+                result["aggregate"] = aggregate
+                return result
+            except:
+                logger.log_exc('Something wrong in _RenewSliver with URL %s'%server.url)
+                return {"aggregate": aggregate, "exc_info": traceback.format_exc(), "code": {"geni_code": -1}, "value": False, "output": ""}
+
+        (hrn, urn_type) = urn_to_hrn(xrn)
         # get the callers hrn
         valid_cred = api.auth.checkCredentials(creds, 'renewsliver', hrn)[0]
         caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
-    
+
         # attempt to use delegated credential first
         cred = api.getDelegatedCredential(creds)
         if not cred:
-            cred = api.getCredential()
+            cred = api.getCredential(minimumExpiration=31*86400)
         threads = ThreadManager()
         for aggregate in api.aggregates:
             # prevent infinite loop. Dont send request back to caller
@@ -265,13 +289,24 @@ class SliceManager:
                 continue
             interface = api.aggregates[aggregate]
             server = api.server_proxy(interface, cred)
-            threads.run(_RenewSliver, server, xrn, [cred], expiration_time, options)
-        # 'and' the results
-        results = [ReturnValue.get_value(result) for result in threads.get_results()]
-        return reduce (lambda x,y: x and y, results , True)
-    
+            threads.run(_RenewSliver, aggregate, server, xrn, [cred], expiration_time, options)
+
+        results = threads.get_results()
+
+        geni_code = 0
+        geni_output = ",".join([x.get("output","") for x in results])
+        geni_value = reduce (lambda x,y: x and y, [result.get("value",False) for result in results], True)
+        for agg_result in results:
+            agg_geni_code = agg_result["code"].get("geni_code",0)
+            if agg_geni_code:
+                geni_code = agg_geni_code
+
+        results = {"aggregates": results, "code": {"geni_code": geni_code}, "value": geni_value, "output": geni_output}
+
+        return results
+
     def DeleteSliver(self, api, xrn, creds, options):
-        call_id = options.get('call_id') 
+        call_id = options.get('call_id')
         if Callids().already_handled(call_id): return ""
 
         def _DeleteSliver(server, xrn, creds, options):
@@ -281,7 +316,7 @@ class SliceManager:
         # get the callers hrn
         valid_cred = api.auth.checkCredentials(creds, 'deletesliver', hrn)[0]
         caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
-    
+
         # attempt to use delegated credential first
         cred = api.getDelegatedCredential(creds)
         if not cred:
@@ -346,9 +381,11 @@ class SliceManager:
             return server.ListSlices(creds, options)
 
         # look in cache first
-        if self.caching and api.cache:
-            slices = api.cache.get('slices')
+        # xxx is this really frequent enough that it is worth being cached ?
+        if self.cache:
+            slices = self.cache.get('slices')
             if slices:
+                api.logger.debug("SliceManager.ListSlices returns from cache")
                 return slices
     
         # get the callers hrn
@@ -377,8 +414,9 @@ class SliceManager:
             slices.extend(result)
     
         # cache the result
-        if self.caching and api.cache:
-            api.cache.add('slices', slices)
+        if self.cache:
+            api.logger.debug("SliceManager.ListSlices caches value")
+            self.cache.add('slices', slices)
     
         return slices