ListResources uses the 'rspec_version' field specified in the 'options' struct to...
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Fri, 29 Apr 2011 20:29:03 +0000 (16:29 -0400)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Fri, 29 Apr 2011 20:29:03 +0000 (16:29 -0400)
sfa/client/sfi.py
sfa/managers/aggregate_manager_pl.py
sfa/managers/slice_manager_pl.py
sfa/plc/aggregate.py
sfa/rspecs/pg_rspec.py
sfa/rspecs/rspec.py
sfa/rspecs/rspec_parser.py

index bc6cc60..50b88bc 100755 (executable)
@@ -204,6 +204,8 @@ class Sfi:
                             default="all")
         # display formats
         if command in ("resources"):
+            parser.add_option("-r", "--rspec-version", dest="rspec_version", default="sfa 1",
+                              help="schema type and version of resulting RSpec")
             parser.add_option("-f", "--format", dest="format", type="choice",
                              help="display format ([xml]|dns|ip)", default="xml",
                              choices=("xml", "dns", "ip"))
@@ -818,8 +820,11 @@ class Sfi:
         creds = [cred]
         if opts.delegate:
             delegated_cred = self.delegate_cred(cred, get_authority(self.authority))
-            creds.append(delegated_cred) 
+            creds.append(delegated_cred)
+        if opts.rspec_version:
+            call_options['rspec_version'] = opts.rspec_version 
         result = server.ListResources(creds, call_options,unique_call_id())
+        #result = server.ListResources(creds, call_options)
         format = opts.format
         if opts.file is None:
             display_rspec(result, format)
index d0eabda..437dd15 100644 (file)
@@ -22,6 +22,7 @@ from sfa.plc.api import SfaAPI
 from sfa.plc.aggregate import Aggregate
 from sfa.plc.slices import *
 from sfa.util.version import version_core
+from sfa.rspecs.rspec_version import RSpecVersion 
 from sfa.util.sfatime import utcparse
 from sfa.util.callids import Callids
 
@@ -306,18 +307,12 @@ def ListResources(api, creds, options,call_id):
     (hrn, type) = urn_to_hrn(xrn)
 
     # get the rspec's return format from options
-    try:
-        format_raw = options.get('rspec_version', 'SFA 1')
-        format_split = format_raw.split(' ')
-        format, version = format_split[0].lower(), format_split[1]
-    except:
-        # invalid format. Just continue
-        format, version = 'sfa', '1'
-    format_template = "rsepc_%s_%s"
-
+    rspec_version = RSpecVersion(options.get('rspec_version', 'SFA 1'))
+    version_string = "rspec_%s_%s" % (rspec_version.format, rspec_version.version)
+    
     # look in cache first
     if caching and api.cache and not xrn:
-        rspec = api.cache.get(format_template % (format, version))
+        rspec = api.cache.get(version_string)
         if rspec:
             api.logger.info("aggregate.ListResources: returning cached value for hrn %s"%hrn)
             return rspec 
@@ -326,13 +321,13 @@ def ListResources(api, creds, options,call_id):
 
     if xrn:
         # get this rspec for the specified slice 
-        rspec =  aggregate.get_rspec(slice_xrn=hrn, format=format)
+        rspec =  aggregate.get_rspec(slice_xrn=hrn, version=rspec_version)
     else:
         # generate rspec in both pg and sfa formats
-        rspec = aggregate.get_rspec(format=format)
+        rspec = aggregate.get_rspec(version=rspec_version)
     # cache the result
     if caching and api.cache:
-        api.cache.add(format_template % (format, version), rspec)
+        api.cache.add(version_string, rspec)
 
     return rspec
 
index dc16a05..254c817 100644 (file)
@@ -24,6 +24,7 @@ from sfa.util.threadmanager import ThreadManager
 import sfa.util.xmlrpcprotocol as xmlrpcprotocol     
 import sfa.plc.peers as peers
 from sfa.util.version import version_core
+from sfa.rspecs.rspec_version import RSpecVersion
 from sfa.util.callids import Callids
 
 # we have specialized xmlrpclib.ServerProxy to remember the input url
@@ -339,6 +340,10 @@ def ListResources(api, creds, options, call_id):
     xrn = options.get('geni_slice_urn', '')
     (hrn, type) = urn_to_hrn(xrn)
 
+    # get the rspec's return format from options
+    rspec_version = RSpecVersion(options.get('rspec_version', 'SFA 1'))
+    version_string = "rspec_%s_%s" % (rspec_version.format, rspec_version.version)
+
     # get hrn of the original caller
     origin_hrn = options.get('origin_hrn', None)
     if not origin_hrn:
@@ -349,7 +354,7 @@ def ListResources(api, creds, options, call_id):
     
     # look in cache first 
     if caching and api.cache and not xrn:
-        rspec =  api.cache.get('nodes')
+        rspec =  api.cache.get(version_string)
         if rspec:
             return rspec
 
@@ -381,7 +386,7 @@ def ListResources(api, creds, options, call_id):
 
     # cache the result
     if caching and api.cache and not xrn:
-        api.cache.add('nodes', rspec)
+        api.cache.add(version_string, rspec)
  
     return rspec.toxml()
 
index 8160024..1d123d8 100644 (file)
@@ -61,12 +61,16 @@ class Aggregate:
 
         self.prepared = True  
 
-    def get_rspec(self, slice_xrn=None, format='sfa'):
+    def get_rspec(self, slice_xrn=None, version = None):
         self.prepare()
         rspec = None
-        if format == ['pg']:
-            rspec = PGRSpec()
-        else:
+        if version:
+            format = version.format
+            if format == 'pg':
+                rspec = PGRSpec()
+            else:
+                rspec = SfaRSpec()
+        else: 
             rspec = SfaRSpec()
 
         rspec.add_nodes(self.nodes.values())
index c3bda95..569c342 100755 (executable)
@@ -6,25 +6,20 @@ from sfa.util.xrn import *
 from sfa.util.plxrn import hostname_to_urn
 from sfa.util.config import Config  
 
+
 class PGRSpec(RSpec):
-    xml = None
     header = '<?xml version="1.0"?>\n'
+    template = """
+<rspec xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.protogeni.net/resources/rspec/0.2" xsi:schemaLocation="http://www.protogeni.net/resources/rspec/0.2 http://www.protogeni.net/resources/rspec/0.2/ad.xsd">\n
+</rspec>
+"""
     namespaces = {'rspecv2':'http://www.protogeni.net/resources/rspec/0.2',
                   'xsi': 'http://www.w3.org/2001/XMLSchema-instance'
                  }
     schemas =  {'xsi': 'http://www.protogeni.net/resources/rspec/0.2 http://www.protogeni.net/resources/rspec/0.2/ad.xsd'
             }
     format = 'pg'
-
-    def create(self, type="advertisement"):
-        RSpec.create(self)
-        for namespace in self.namespaces:
-            xmlns = "xmlns"
-            if namespace not in 'rspecv2':
-                xmlns = xmlns + ":" + namespace
-            self.xml.set(xmlns, self.namespaces[namespace])
-        for schema in self.schemas:
-            self.xml.set(schema+":schemaLocation", self.schemas[schema])
+    xml = None
 
     def get_network(self):
         network = None 
@@ -74,6 +69,9 @@ class PGRSpec(RSpec):
     def add_slivers(self, slivers, check_for_dupes=False): 
         pass
 
+    def add_interfaces(self, interfaces, check_for_dupes=False):
+        pass
+
     def add_links(self, links, check_for_dupes=False):
         pass
 
index cc42a87..aeb0281 100755 (executable)
@@ -8,10 +8,14 @@ from sfa.util.config import Config
 from sfa.util.faults import SfaNotImplemented, InvalidRSpec
 
 class RSpec:
-    xml = None
     header = '<?xml version="1.0"?>\n'
+    template = """
+<RSpec>\n 
+</RSpec>
+"""
     namespaces = {}
     config = Config()
+    xml = None
   
     def __init__(self, rspec="", namespaces={}):
         if rspec:
@@ -24,10 +28,10 @@ class RSpec:
         date_format = '%Y-%m-%dT%H:%M:%SZ'
         now = datetime.utcnow()
         generated_ts = now.strftime(date_format)
-        expires_ts = (now + timedelta(minutes=30)).strftime(date_format) 
-        self.xml = etree.Element("rspec", type = type, 
-                                 valid_until=expires_ts,   
-                                 generated=generated_ts)
+        expires_ts = (now + timedelta(hours=1)).strftime(date_format) 
+        self.parse_rspec(self.template, self.namespaces)
+        self.xml.set('valid_until', expires_ts)
+        self.xml.set('generated', generated_ts)
     
     def parse_rspec(self, rspec, namespaces={}):
         parser = etree.XMLParser(remove_blank_text=True)
index 3d96860..c3dde65 100755 (executable)
@@ -3,7 +3,7 @@ from sfa.rspecs.sfa_rspec import SfaRSpec
 from sfa.rspecs.pg_rspec import PGRSpec
 from sfa.rspecs.rspec import RSpec
 from lxml import etree 
-
+from 
 
 def parse_rspec(in_rspec):
     rspec = RSpec(rspec=in_rspec)
@@ -20,8 +20,6 @@ def parse_rspec(in_rspec):
         out_rspec.xml = rspec.xml
     return out_rspec
 
-
-
 if __name__ == '__main__':
     
     print "Parsing SFA RSpec:",