X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Frspecs%2Fversions%2Fpgv2.py;h=17a18e08359e405db6eb66af59695ae93e3746ef;hb=4a9e6751f9f396f463932133b9d62fc925a99ef6;hp=f8759d8e26093eff9aaa6140da8bc6ac482a2bb4;hpb=7c6754da189699add4e360c5f590e2e64fd181c5;p=sfa.git diff --git a/sfa/rspecs/versions/pgv2.py b/sfa/rspecs/versions/pgv2.py index f8759d8e..17a18e08 100644 --- a/sfa/rspecs/versions/pgv2.py +++ b/sfa/rspecs/versions/pgv2.py @@ -1,11 +1,16 @@ + + from copy import deepcopy -from StringIO import StringIO 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 - +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' @@ -17,12 +22,13 @@ class PGv2(RSpecVersion): '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 def get_networks(self): network_names = set() - nodes = self.xml.xpath('//default:node[@component_manager_id] | //node[@component_manager_id]', namespaces=self.namespaces) + 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') @@ -44,25 +50,28 @@ class PGv2(RSpecVersion): 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 self.xml.root.append(deepcopy(source_node_tag)) # 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) + 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 = [] @@ -70,21 +79,25 @@ class PGv2(RSpecVersion): # 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]) + name = str(sliver_attribute[0]) + text = str(sliver_attribute[1]) attribs = sliver_attribute[2] - # we currently only suppor the and attributes - if 'info' in name: - attribute = {'name': 'flack_info', 'value': str(attribs), 'node_id': node} + # we currently only suppor the and + # attributes + if 'info' in name: + attribute = {'name': 'flack_info', + 'value': str(attribs), 'node_id': node} slice_attributes.append(attribute) elif 'initscript' in name: if attribs is not None and 'name' in attribs: value = attribs['name'] else: value = text - attribute = {'name': 'initscript', 'value': value, 'node_id': node} + attribute = {'name': 'initscript', + 'value': value, 'node_id': node} slice_attributes.append(attribute) return slice_attributes @@ -102,7 +115,9 @@ class PGv2(RSpecVersion): 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: @@ -110,27 +125,28 @@ class PGv2(RSpecVersion): if not node_elems: continue node_elem = node_elems[0] - + # determine sliver types for this node - valid_sliver_types = ['emulab-openvz', 'raw-pc', 'plab-vserver', 'plab-vnode'] + valid_sliver_types = ['emulab-openvz', + 'raw-pc', 'plab-vserver', 'plab-vnode'] requested_sliver_type = None for sliver_type in node_elem.get('slivers', []): if sliver_type.get('type') in valid_sliver_types: requested_sliver_type = sliver_type['type'] - + if not requested_sliver_type: continue sliver = {'type': requested_sliver_type, - 'pl_tags': attributes} + 'pl_tags': attributes} # 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) @@ -146,8 +162,8 @@ class PGv2(RSpecVersion): #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 - PGv2SliverType.add_slivers(node_elem.element, sliver) + # add the sliver type elemnt + PGv2SliverType.add_slivers(node_elem.element, sliver) # remove all nodes without slivers if not append: @@ -157,7 +173,7 @@ class PGv2(RSpecVersion): parent.remove(node_elem.element) def remove_slivers(self, slivers, network=None, no_dupes=False): - PGv2Node.remove_slivers(self.xml, slivers) + PGv2Node.remove_slivers(self.xml, slivers) # Links @@ -165,7 +181,7 @@ class PGv2(RSpecVersion): return PGv2Link.get_links(self.xml) def get_link_requests(self): - return PGv2Link.get_link_requests(self.xml) + return PGv2Link.get_link_requests(self.xml) def add_links(self, links): PGv2Link.add_links(self.xml.root, links) @@ -176,9 +192,17 @@ class PGv2(RSpecVersion): # Leases def get_leases(self, filter=None): - pass + return PGv2Lease.get_leases(self.xml, filter) - def add_leases(self, leases, network = None, no_dupes=False): + 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 @@ -189,24 +213,29 @@ class PGv2(RSpecVersion): """ from sfa.rspecs.rspec import RSpec # just copy over all the child elements under the root element - if isinstance(in_rspec, basestring): + 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.has_key('sliver') or not node['sliver']: + 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'} - - self.add_nodes(nodes) + 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(): + # for child in rspec.xml.iterchildren(): # self.xml.root.append(child) - - def cleanup(self): # remove unncecessary elements, attributes @@ -214,26 +243,27 @@ class PGv2(RSpecVersion): # remove 'available' element from remaining node elements self.xml.remove_element('//default:available | //available') + class PGv2Ad(PGv2): enabled = True content_type = 'ad' schema = 'http://www.protogeni.net/resources/rspec/2/ad.xsd' template = '' + class PGv2Request(PGv2): enabled = True content_type = 'request' schema = 'http://www.protogeni.net/resources/rspec/2/request.xsd' template = '' + class PGv2Manifest(PGv2): enabled = True content_type = 'manifest' schema = 'http://www.protogeni.net/resources/rspec/2/manifest.xsd' template = '' - - if __name__ == '__main__': from sfa.rspecs.rspec import RSpec @@ -241,4 +271,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))