From f2a4ff047199d94add5e2240b952851e5a04445a Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Fri, 29 Apr 2011 16:29:03 -0400 Subject: [PATCH] ListResources uses the 'rspec_version' field specified in the 'options' struct to determine the format of the resulting rspec --- sfa/client/sfi.py | 7 ++++++- sfa/managers/aggregate_manager_pl.py | 21 ++++++++------------- sfa/managers/slice_manager_pl.py | 9 +++++++-- sfa/plc/aggregate.py | 12 ++++++++---- sfa/rspecs/pg_rspec.py | 20 +++++++++----------- sfa/rspecs/rspec.py | 14 +++++++++----- sfa/rspecs/rspec_parser.py | 4 +--- 7 files changed, 48 insertions(+), 39 deletions(-) diff --git a/sfa/client/sfi.py b/sfa/client/sfi.py index bc6cc60f..50b88bc9 100755 --- a/sfa/client/sfi.py +++ b/sfa/client/sfi.py @@ -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) diff --git a/sfa/managers/aggregate_manager_pl.py b/sfa/managers/aggregate_manager_pl.py index d0eabdaa..437dd151 100644 --- a/sfa/managers/aggregate_manager_pl.py +++ b/sfa/managers/aggregate_manager_pl.py @@ -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 diff --git a/sfa/managers/slice_manager_pl.py b/sfa/managers/slice_manager_pl.py index dc16a053..254c817f 100644 --- a/sfa/managers/slice_manager_pl.py +++ b/sfa/managers/slice_manager_pl.py @@ -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() diff --git a/sfa/plc/aggregate.py b/sfa/plc/aggregate.py index 8160024d..1d123d8e 100644 --- a/sfa/plc/aggregate.py +++ b/sfa/plc/aggregate.py @@ -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()) diff --git a/sfa/rspecs/pg_rspec.py b/sfa/rspecs/pg_rspec.py index c3bda951..569c342b 100755 --- a/sfa/rspecs/pg_rspec.py +++ b/sfa/rspecs/pg_rspec.py @@ -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 = '\n' + template = """ +\n + +""" 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 diff --git a/sfa/rspecs/rspec.py b/sfa/rspecs/rspec.py index cc42a878..aeb02819 100755 --- a/sfa/rspecs/rspec.py +++ b/sfa/rspecs/rspec.py @@ -8,10 +8,14 @@ from sfa.util.config import Config from sfa.util.faults import SfaNotImplemented, InvalidRSpec class RSpec: - xml = None header = '\n' + template = """ +\n + +""" 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) diff --git a/sfa/rspecs/rspec_parser.py b/sfa/rspecs/rspec_parser.py index 3d96860b..c3dde657 100755 --- a/sfa/rspecs/rspec_parser.py +++ b/sfa/rspecs/rspec_parser.py @@ -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:", -- 2.43.0