Merge branch 'master' of ssh://git.planet-lab.org/git/sfa
[sfa.git] / sfa / managers / slice_manager_pl.py
index 6be29bf..1077dcf 100644 (file)
@@ -20,7 +20,8 @@ from sfa.rspecs.sfa_rspec import SfaRSpec
 from sfa.rspecs.rspec_converter import RSpecConverter
 from sfa.rspecs.rspec_parser import parse_rspec    
 from sfa.rspecs.rspec_version import RSpecVersion
-from sfa.rspecs.pl_rspec_version import supported_rspecs
+from sfa.rspecs.sfa_rspec import sfa_rspec_version
+from sfa.rspecs.pg_rspec import pg_rspec_ad_version, pg_rspec_request_version   
 from sfa.util.policy import Policy
 from sfa.util.prefixTree import prefixTree
 from sfa.util.sfaticket import *
@@ -45,11 +46,16 @@ def GetVersion(api):
     peers =dict ([ (peername,get_serverproxy_url(v)) for (peername,v) in api.aggregates.iteritems() 
                    if peername != api.hrn])
     xrn=Xrn (api.hrn)
+    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':'slicemgr',
                     'hrn' : xrn.get_hrn(),
                     'urn' : xrn.get_urn(),
-                    'peers': peers,}
-    version_more.update(supported_rspecs)     
+                    'peers': peers,
+                    'request_rspec_versions': request_rspec_versions,
+                    'ad_rspec_versions': ad_rspec_versions,
+                    'default_ad_rspec': dict(sfa_rspec_version)
+                    }
     sm_version=version_core(version_more)
     # local aggregate if present needs to have localhost resolved
     if api.hrn in api.aggregates:
@@ -59,11 +65,20 @@ def GetVersion(api):
 
 def CreateSliver(api, xrn, creds, rspec_str, users, call_id):
 
-    def _CreateSliver(server, xrn, credentail, rspec, users, call_id):
-            # should check the cache first
-            # get aggregate version
-            version = server.GetVersion()
-            if 'sfa' not in version and 'geni_api' in version:
+    def _CreateSliver(aggregate, xrn, credential, rspec, users, call_id):
+            # Need to call GetVersion at an aggregate to determine the supported 
+            # rspec type/format beofre calling CreateSliver at an Aggregate. 
+            # The Aggregate's verion info is cached 
+            server = api.aggregates[aggregate]
+            # get cached aggregate version
+            aggregate_version_key = 'version_'+ aggregate
+            aggregate_version = api.cache.get(aggregate_version_key)
+            if not aggregate_version:
+                # get current aggregate version anc cache it for 24 hours
+                aggregate_version = server.GetVersion()
+                api.cache.add(aggregate_version_key, aggregate_version, 60 * 60 * 24)
+                
+            if 'sfa' not in aggregate_version and 'geni_api' in aggregate_version:
                 # sfa aggregtes support both sfa and pg rspecs, no need to convert
                 # if aggregate supports sfa rspecs. othewise convert to pg rspec
                 rspec = RSpecConverter.to_pg_rspec(rspec)
@@ -98,8 +113,7 @@ def CreateSliver(api, xrn, creds, rspec_str, users, call_id):
             continue
             
         # Just send entire RSpec to each aggregate
-        server = api.aggregates[aggregate]
-        threads.run(_CreateSliver, server, xrn, credential, rspec.toxml(), users, call_id)
+        threads.run(_CreateSliver, aggregate, xrn, credential, rspec.toxml(), users, call_id)
             
     results = threads.get_results()
     rspec = SfaRSpec()
@@ -346,7 +360,7 @@ def ListResources(api, creds, options, call_id):
     (hrn, type) = urn_to_hrn(xrn)
 
     # get the rspec's return format from options
-    rspec_version = RSpecVersion(options.get('rspec_version', 'SFA 1'))
+    rspec_version = RSpecVersion(options.get('rspec_version'))
     version_string = "rspec_%s" % (rspec_version.get_version_name())
 
     # look in cache first
@@ -376,17 +390,15 @@ def ListResources(api, creds, options, call_id):
         threads.run(server.ListResources, credential, my_opts, call_id)
                     
     results = threads.get_results()
-    #results.append(open('/root/protogeni.rspec', 'r').read())
-    rspec = SfaRSpec()
+    rspec_version = RSpecVersion(my_opts.get('rspec_version'))
+    if rspec_version['type'] == pg_rspec_ad_version['type']:
+        rspec = PGRSpec()
+    else:
+        rspec = SfaRSpec()
+
     for result in results:
         try:
-            tmp_rspec = parse_rspec(result)
-            if isinstance(tmp_rspec, SfaRSpec):
-                rspec.merge(result)
-            elif isinstance(tmp_rspec, PGRSpec):
-                rspec.merge(RSpecConverter.to_sfa_rspec(result))
-            else:
-                api.logger.info("SM.ListResources: invalid aggregate rspec")                        
+            rspec.merge(result)
         except:
             api.logger.info("SM.ListResources: Failed to merge aggregate rspec")
 
@@ -420,19 +432,13 @@ def SliverStatus(api, slice_xrn, creds, call_id):
 
     # mmh, it is expected that all results carry the same urn
     overall['geni_urn'] = results[0]['geni_urn']
-
-    # consolidate geni_status - simple model using max on a total order
-    states = [ 'ready', 'configuring', 'failed', 'unknown' ]
-    # hash name to index
-    shash = dict ( zip ( states, range(len(states)) ) )
-    def combine_status (x,y):
-        return shash [ max (shash(x),shash(y)) ]
-    overall['geni_status'] = reduce (combine_status, [ result['geni_status'] for result in results], 'ready' )
-
-    # {'ready':0,'configuring':1,'failed':2,'unknown':3}
+    overall['pl_login'] = results[0]['pl_login']
     # append all geni_resources
     overall['geni_resources'] = \
         reduce (lambda x,y: x+y, [ result['geni_resources'] for result in results] , [])
+    overall['status'] = 'unknown'
+    if overall['geni_resources']:
+        overall['status'] = 'ready'
 
     return overall