X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Frspecs%2Fversions%2Fpgv2.py;h=01a31550bc4a0ef7be5ec427ce24624271582b09;hb=797c6e7020483cdb9d7df41d9d8050c3033f016c;hp=ceff971bda43615098140653eb3d2be80b75a048;hpb=69fb221c274eb0b6e9f6ff6f895e5e6f90b17230;p=sfa.git diff --git a/sfa/rspecs/versions/pgv2.py b/sfa/rspecs/versions/pgv2.py index ceff971b..01a31550 100644 --- a/sfa/rspecs/versions/pgv2.py +++ b/sfa/rspecs/versions/pgv2.py @@ -1,13 +1,14 @@ from copy import deepcopy from StringIO import StringIO -from sfa.util.xrn import urn_to_sliver_id -from sfa.util.plxrn import hostname_to_urn, xrn_to_hostname -from sfa.rspecs.baseversion import BaseVersion +from sfa.util.xrn import Xrn +from sfa.rspecs.version import RSpecVersion from sfa.rspecs.elements.versions.pgv2Link import PGv2Link from sfa.rspecs.elements.versions.pgv2Node import PGv2Node from sfa.rspecs.elements.versions.pgv2SliverType import PGv2SliverType - -class PGv2(BaseVersion): +from sfa.rspecs.elements.versions.pgv2Lease import PGv2Lease +from sfa.util.sfalogging import logger + +class PGv2(RSpecVersion): type = 'ProtoGENI' content_type = 'ad' version = '2' @@ -16,22 +17,28 @@ class PGv2(BaseVersion): extensions = { 'flack': "http://www.protogeni.net/resources/rspec/ext/flack/1", 'planetlab': "http://www.planet-lab.org/resources/sfa/ext/planetlab/1", + 'plos': "http://www.planet-lab.org/resources/sfa/ext/plos/1", } namespaces = dict(extensions.items() + [('default', namespace)]) - # Networks - def get_network(self): - network = None - nodes = self.xml.xpath('//default:node[@component_manager_id][1]', namespaces=self.namespaces) - if nodes: - network = nodes[0].get('component_manager_id') - return network - + # Networks def get_networks(self): - networks = self.xml.xpath('//default:node[@component_manager_id]/@component_manager_id', namespaces=self.namespaces) - return set(networks) + network_names = set() + nodes = self.xml.xpath('//default:node[@component_manager_id] | //node[@component_manager_id]', + namespaces=self.namespaces) + for node in nodes: + if 'component_manager_id' in node.attrib: + network_urn = node.get('component_manager_id') + if network_urn.startswith("urn:"): + network_hrn = Xrn(network_urn).get_hrn() + else: + # some component_manager_ids are hrns instead of urns?? + network_hrn = network_urn + network_names.add(network_hrn) + network_names = list(network_names) + networks = [{"name": x} for x in network_names] + return networks - # Nodes def get_nodes(self, filter=None): @@ -40,8 +47,8 @@ class PGv2(BaseVersion): def get_nodes_with_slivers(self): return PGv2Node.get_nodes_with_slivers(self.xml) - def add_nodes(self, nodes, check_for_dupes=False): - return PGv2Node.add_nodes(self.xml, nodes) + def add_nodes(self, nodes, check_for_dupes=False, rspec_content_type=None): + return PGv2Node.add_nodes(self.xml, nodes, rspec_content_type) def merge_node(self, source_node_tag): # this is untested @@ -49,16 +56,18 @@ class PGv2(BaseVersion): # Slivers - def get_sliver_attributes(self, hostname, network=None): - nodes = self.get_nodes({'component_id': '*%s*' %hostname}) - attribs = [] - if nodes is not None and isinstance(nodes, list) and len(nodes) > 0: + def get_sliver_attributes(self, component_id, network=None): + nodes = self.get_nodes({'component_id': '*%s*' %component_id}) + try: node = nodes[0] sliver = node.xpath('./default:sliver_type', namespaces=self.namespaces) if sliver is not None and isinstance(sliver, list) and len(sliver) > 0: sliver = sliver[0] - #attribs = self.attributes_list(sliver) - return attribs + return self.attributes_list(sliver) + else: + return [] + except: + return [] def get_slice_attributes(self, network=None): slice_attributes = [] @@ -66,7 +75,7 @@ class PGv2(BaseVersion): # TODO: default sliver attributes in the PG rspec? default_ns_prefix = self.namespaces['default'] for node in nodes_with_slivers: - sliver_attributes = self.get_sliver_attributes(node, network) + sliver_attributes = self.get_sliver_attributes(node['component_id'], network) for sliver_attribute in sliver_attributes: name=str(sliver_attribute[0]) text =str(sliver_attribute[1]) @@ -98,7 +107,8 @@ class PGv2(BaseVersion): def add_default_sliver_attribute(self, name, value, network=None): pass - def add_slivers(self, hostnames, attributes=[], sliver_urn=None, append=False): + def add_slivers(self, hostnames, attributes=None, sliver_urn=None, append=False): + if attributes is None: attributes=[] # all nodes hould already be present in the rspec. Remove all # nodes that done have slivers for hostname in hostnames: @@ -116,10 +126,18 @@ class PGv2(BaseVersion): if not requested_sliver_type: continue - sliver = {'name': requested_sliver_type, + sliver = {'type': requested_sliver_type, 'pl_tags': attributes} - # remove existing sliver_type tags + # remove available element + for available_elem in node_elem.xpath('./default:available | ./available'): + node_elem.remove(available_elem) + + # remove interface elements + for interface_elem in node_elem.xpath('./default:interface | ./interface'): + node_elem.remove(interface_elem) + + # remove existing sliver_type elements for sliver_type in node_elem.get('slivers', []): node_elem.element.remove(sliver_type.element) @@ -131,7 +149,7 @@ class PGv2(BaseVersion): # set the sliver id #slice_id = sliver_info.get('slice_id', -1) #node_id = sliver_info.get('node_id', -1) - #sliver_id = urn_to_sliver_id(sliver_urn, slice_id, node_id) + #sliver_id = Xrn(xrn=sliver_urn, type='slice', id=str(node_id)).get_urn() #node_elem.set('sliver_id', sliver_id) # add the sliver type elemnt @@ -161,6 +179,22 @@ class PGv2(BaseVersion): def add_link_requests(self, link_tuples, append=False): PGv2Link.add_link_requests(self.xml.root, link_tuples, append) + # Leases + + def get_leases(self, filter=None): + return PGv2Lease.get_leases(self.xml, filter) + + def add_leases(self, leases, network = None, no_dupes=False): + PGv2Lease.add_leases(self.xml, leases) + + # Spectrum + + def get_channels(self, filter=None): + return [] + + def add_channels(self, channels, network = None, no_dupes=False): + pass + # Utility def merge(self, in_rspec): @@ -169,12 +203,31 @@ class PGv2(BaseVersion): """ from sfa.rspecs.rspec import RSpec # just copy over all the child elements under the root element - if isinstance(in_rspec, RSpec): - in_rspec = in_rspec.toxml() + if isinstance(in_rspec, basestring): + in_rspec = RSpec(in_rspec) + + nodes = in_rspec.version.get_nodes() + # protogeni rspecs need to advertise the availabel sliver types + main_nodes = [] + for node in nodes: + if not node['component_name']: + # this node element is part of a lease + continue + if not node.has_key('sliver') or not node['sliver']: + node['sliver'] = {'name': 'plab-vserver'} + main_nodes.append(node) + self.add_nodes(main_nodes) + self.add_links(in_rspec.version.get_links()) + + # Leases + leases = in_rspec.version.get_leases() + self.add_leases(leases) + # + #rspec = RSpec(in_rspec) + #for child in rspec.xml.iterchildren(): + # self.xml.root.append(child) + - rspec = RSpec(in_rspec) - for child in rspec.xml.iterchildren(): - self.xml.root.append(child) def cleanup(self): # remove unncecessary elements, attributes @@ -186,19 +239,19 @@ class PGv2Ad(PGv2): enabled = True content_type = 'ad' schema = 'http://www.protogeni.net/resources/rspec/2/ad.xsd' - template = '' + template = '' class PGv2Request(PGv2): enabled = True content_type = 'request' schema = 'http://www.protogeni.net/resources/rspec/2/request.xsd' - template = '' + template = '' class PGv2Manifest(PGv2): enabled = True content_type = 'manifest' schema = 'http://www.protogeni.net/resources/rspec/2/manifest.xsd' - template = '' + template = ''