no longer use sfa.util.storage.SimpleStorage for caching list of instantiated slices...
[sfa.git] / sfa / managers / slice_manager_pl.py
index 2f23fe5..5676416 100644 (file)
@@ -20,13 +20,11 @@ from sfa.util.prefixTree import prefixTree
 from sfa.util.rspec import *
 from sfa.util.sfaticket import *
 from sfa.util.debug import log
-from sfa.server.registry import Registries
-from sfa.server.aggregate import Aggregates
 import sfa.plc.peers as peers
 
 def delete_slice(api, xrn, origin_hrn=None):
     credential = api.getCredential()
-    aggregates = Aggregates(api)
+    aggregates = api.aggregates
     for aggregate in aggregates:
         success = False
         # request hash is optional so lets try the call without it
@@ -41,8 +39,10 @@ def delete_slice(api, xrn, origin_hrn=None):
 def create_slice(api, xrn, rspec, origin_hrn=None):
     hrn, type = urn_to_hrn(xrn)
 
-    # Validate the RSpec against PlanetLab's schema
-    schema = "/var/www/html/schemas/pl.rng"
+    # Validate the RSpec against PlanetLab's schema --disabled for now
+    # The schema used here needs to aggregate the PL and VINI schemas
+    # schema = "/var/www/html/schemas/pl.rng"
+    schema = None
     if schema:
         try:
             tree = etree.parse(StringIO(rspec))
@@ -58,7 +58,7 @@ def create_slice(api, xrn, rspec, origin_hrn=None):
             message = "%s (line %s)" % (error.message, error.line)
             raise InvalidRSpec(message)
 
-    aggs = Aggregates(api)
+    aggs = api.aggregates
     cred = api.getCredential()                                                 
     for agg in aggs:
         if agg not in [api.auth.client_cred.get_gid_caller().get_hrn()]:      
@@ -89,7 +89,7 @@ def get_ticket(api, xrn, rspec, origin_hrn=None):
         rspecs[net_hrn] = temp_rspec.toxml() 
     
     # send the rspec to the appropiate aggregate/sm
-    aggregates = Aggregates(api)
+    aggregates = api.aggregates
     credential = api.getCredential()
     tickets = {}
     for net_hrn in rspecs:
@@ -185,18 +185,32 @@ def reset_slice(api, xrn):
     return 1
 
 def get_slices(api):
-    # XX just import the legacy module and excute that until
-    # we transition the code to this module
-    from sfa.plc.slices import Slices
-    slices = Slices(api)
-    slices.refresh()
-    return [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slices['hrn']]
-     
+    # look in cache first
+    if api.cache:
+        slices = api.cache.get('slices')
+        if slices:
+            return slices    
+
+    # fetch from aggregates
+    slices = []
+    credential = api.getCredential()
+    for aggregate in api.aggregates:
+        try:
+            tmp_slices = api.aggregates[aggregate].get_slices(credential)
+            slices.extend(tmp_slices)
+        except:
+            print >> log, "%s" % (traceback.format_exc())
+            print >> log, "Error calling slices at aggregate %(aggregate)s" % locals()
+
+    # cache the result
+    api.cache.add('slices', slices)
+    return slices
 def get_rspec(api, xrn=None, origin_hrn=None):
     hrn, type = urn_to_hrn(xrn)
     rspec = None
 
-    aggs = Aggregates(api)
+    aggs = api.aggregates
     cred = api.getCredential()                                                 
     for agg in aggs:
         if agg not in [api.auth.client_cred.get_gid_caller().get_hrn()]:      
@@ -208,7 +222,7 @@ def get_rspec(api, xrn=None, origin_hrn=None):
                 print >> log, "Error getting resources at aggregate %s" % agg
                 traceback.print_exc(log)
                 print >> log, "%s" % (traceback.format_exc())
-
+                continue
                 
             try:
                 tree = etree.parse(StringIO(agg_rspec))
@@ -217,12 +231,14 @@ def get_rspec(api, xrn=None, origin_hrn=None):
                 raise InvalidRSpec(message)
 
             root = tree.getroot()
-            if root.get("type") in ["Planetlab", "VINI"]:
+            if root.get("type") in ["SFA"]:
                 if rspec == None:
                     rspec = root
                 else:
                     for network in root.iterfind("./network"):
                         rspec.append(deepcopy(network))
+                    for request in root.iterfind("./request"):
+                        rspec.append(deepcopy(request))
 
     return etree.tostring(rspec, xml_declaration=True, pretty_print=True)