X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Frspecs%2Fversions%2Fpgv2.py;h=3e25a376d0031a0b08ef4a4b7cab404598d3d947;hb=031774db6b99361059196ae9b63ecd1f0be9e19a;hp=b336f3d815e12bbe6bf21149ba238bcdf7495ba0;hpb=5daf08641a6363cb0c05d3c92cdf2bf95dd09366;p=sfa.git diff --git a/sfa/rspecs/versions/pgv2.py b/sfa/rspecs/versions/pgv2.py index b336f3d8..3e25a376 100644 --- a/sfa/rspecs/versions/pgv2.py +++ b/sfa/rspecs/versions/pgv2.py @@ -1,13 +1,16 @@ +from __future__ import print_function + from copy import deepcopy -from StringIO import StringIO -from sfa.util.xrn import Xrn, 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 +from sfa.util.py23 import StringType + +class PGv2(RSpecVersion): type = 'ProtoGENI' content_type = 'ad' version = '2' @@ -16,19 +19,27 @@ 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)]) + namespaces = dict(list(extensions.items()) + [('default', namespace)]) - # Networks + # Networks def get_networks(self): - networks = 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: - network_urn = node.get('component_manager_id') - network_hrn = Xrn(network_urn).get_hrn()[0] - networks.add({'name': network_hrn}) - return list(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 @@ -38,8 +49,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 @@ -47,16 +58,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 = [] @@ -64,7 +77,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]) @@ -96,7 +109,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: @@ -114,10 +128,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) @@ -129,7 +151,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 @@ -159,6 +181,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): @@ -167,12 +205,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, StringType): + 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 'sliver' not in node 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 @@ -184,19 +241,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 = '' @@ -207,4 +264,4 @@ if __name__ == '__main__': r = RSpec('/tmp/pg.rspec') r.load_rspec_elements(PGv2.elements) r.namespaces = PGv2.namespaces - print r.get(RSpecElements.NODE) + print(r.get(RSpecElements.NODE))