python3 - 2to3 + miscell obvious tweaks
[sfa.git] / sfa / rspecs / versions / pgv2.py
index fbd8a39..17a18e0 100644 (file)
@@ -1,12 +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'
@@ -18,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')
@@ -45,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 = []
@@ -71,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 <initscript> and <flack> attributes
-                if  'info' in name:
-                    attribute = {'name': 'flack_info', 'value': str(attribs), 'node_id': node}
+                # we currently only suppor the <initscript> and <flack>
+                # 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
@@ -103,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:
@@ -111,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)
@@ -147,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:
@@ -158,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
 
@@ -166,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)
@@ -179,9 +194,17 @@ class PGv2(RSpecVersion):
     def get_leases(self, filter=None):
         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
 
     def merge(self, in_rspec):
@@ -190,7 +213,7 @@ 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()
@@ -200,21 +223,19 @@ class PGv2(RSpecVersion):
             if not node['component_name']:
                 # this node element is part of a lease
                 continue
-            if not node.has_key('sliver') or not node['sliver']:
+            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():
+        # for child in rspec.xml.iterchildren():
         #    self.xml.root.append(child)
-        
-        
 
     def cleanup(self):
         # remove unncecessary elements, attributes
@@ -222,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 = '<rspec type="advertisement" xmlns="http://www.protogeni.net/resources/rspec/2" xmlns:flack="http://www.protogeni.net/resources/rspec/ext/flack/1" xmlns:plos="http://www.planet-lab.org/resources/sfa/ext/plos/1" xmlns:planetlab="http://www.planet-lab.org/resources/sfa/ext/planetlab/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.protogeni.net/resources/rspec/2 http://www.protogeni.net/resources/rspec/2/ad.xsd http://www.planet-lab.org/resources/sfa/ext/planetlab/1 http://www.planet-lab.org/resources/sfa/ext/planetlab/1/planetlab.xsd http://www.planet-lab.org/resources/sfa/ext/plos/1 http://www.planet-lab.org/resources/sfa/ext/plos/1/plos.xsd"/>'
 
+
 class PGv2Request(PGv2):
     enabled = True
     content_type = 'request'
     schema = 'http://www.protogeni.net/resources/rspec/2/request.xsd'
     template = '<rspec type="request" xmlns="http://www.protogeni.net/resources/rspec/2" xmlns:flack="http://www.protogeni.net/resources/rspec/ext/flack/1" xmlns:plos="http://www.planet-lab.org/resources/sfa/ext/plos/1" xmlns:planetlab="http://www.planet-lab.org/resources/sfa/ext/planetlab/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.protogeni.net/resources/rspec/2 http://www.protogeni.net/resources/rspec/2/request.xsd http://www.planet-lab.org/resources/sfa/ext/planetlab/1 http://www.planet-lab.org/resources/sfa/ext/planetlab/1/planetlab.xsd http://www.planet-lab.org/resources/sfa/ext/plos/1 http://www.planet-lab.org/resources/sfa/ext/plos/1/plos.xsd"/>'
 
+
 class PGv2Manifest(PGv2):
     enabled = True
     content_type = 'manifest'
     schema = 'http://www.protogeni.net/resources/rspec/2/manifest.xsd'
     template = '<rspec type="manifest" xmlns="http://www.protogeni.net/resources/rspec/2" xmlns:plos="http://www.planet-lab.org/resources/sfa/ext/plos/1" xmlns:flack="http://www.protogeni.net/resources/rspec/ext/flack/1" xmlns:planetlab="http://www.planet-lab.org/resources/sfa/ext/planetlab/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.protogeni.net/resources/rspec/2 http://www.protogeni.net/resources/rspec/2/manifest.xsd http://www.planet-lab.org/resources/sfa/ext/planetlab/1 http://www.planet-lab.org/resources/sfa/ext/planetlab/1/planetlab.xsd http://www.planet-lab.org/resources/sfa/ext/plos/1 http://www.planet-lab.org/resources/sfa/ext/plos/1/plos.xsd"/>'
 
-     
-
 
 if __name__ == '__main__':
     from sfa.rspecs.rspec import RSpec
@@ -249,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))