remove unnecessary statement
[sfa.git] / sfa / rspecs / pg_rspec.py
index e2653ad..82d2d8b 100755 (executable)
@@ -3,7 +3,7 @@ from lxml import etree
 from StringIO import StringIO
 from sfa.rspecs.rspec import RSpec 
 from sfa.util.xrn import *
-from sfa.util.plxrn import hostname_to_urn
+from sfa.util.plxrn import hostname_to_urn, xrn_to_hostname
 from sfa.util.config import Config 
 from sfa.rspecs.rspec_version import RSpecVersion 
 
@@ -32,7 +32,7 @@ pg_rspec_request_version = RSpecVersion(_request_version)
 class PGRSpec(RSpec):
     xml = None
     header = '<?xml version="1.0"?>\n'
-    template = """<rspec xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.protogeni.net/resources/rspec/2" xsi:schemaLocation="http://www.protogeni.net/resources/rspec/2 http://www.protogeni.net/resources/rspec/2/%(rspec_type)s.xsd"></rspec>"""
+    template = '<rspec xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.protogeni.net/resources/rspec/2" xsi:schemaLocation="http://www.protogeni.net/resources/rspec/2 http://www.protogeni.net/resources/rspec/2/%(rspec_type)s.xsd" xmlns:flack="http://www.protogeni.net/resources/rspec/ext/flack/1" />'
 
     def __init__(self, rspec="", namespaces={}, type=None):
         if not type:
@@ -49,7 +49,9 @@ class PGRSpec(RSpec):
         self.template = self.template % {'rspec_type': rspec_type}
 
         if not namespaces:
-            self.namespaces = {'rspecv2': self.version['namespace']}
+            self.namespaces = {'rspecv2': self.version['namespace'],
+                               'flack': 'http://www.protogeni.net/resources/rspec/ext/flack/1' }
+
         else:
             self.namespaces = namespaces 
 
@@ -62,7 +64,7 @@ class PGRSpec(RSpec):
         RSpec.create(self)
         if self.type:
             self.xml.set('type', self.type) 
-        
+       
     def get_network(self):
         network = None 
         nodes = self.xml.xpath('//rspecv2:node[@component_manager_uuid][1]', namespaces=self.namespaces)
@@ -74,20 +76,72 @@ class PGRSpec(RSpec):
         networks = self.xml.xpath('//rspecv2:node[@component_manager_uuid]/@component_manager_uuid', namespaces=self.namespaces)
         return set(networks)
 
-    def get_node_elements(self):
-        nodes = self.xml.xpath('//rspecv2:node', namespaces=self.namespaces)
+    def get_node_element(self, hostname, network=None):
+        nodes = self.xml.xpath('//rspecv2:node[@component_id[contains(., "%s")]] | node[@component_id[contains(., "%s")]]' % (hostname, hostname), namespaces=self.namespaces)
+        if isinstance(nodes,list) and nodes:
+            return nodes[0]
+        else:
+            return None
+
+    def get_node_elements(self, network=None):
+        nodes = self.xml.xpath('//rspecv2:node | //node', namespaces=self.namespaces)
         return nodes
 
     def get_nodes(self, network=None):
-        return self.xml.xpath('//rspecv2:node[@component_name]/@component_name', namespaces=self.namespaces) 
+        xpath = '//rspecv2:node[@component_name]/@component_id | //node[@component_name]/@component_id'
+        nodes = self.xml.xpath(xpath, namespaces=self.namespaces)
+        nodes = [xrn_to_hostname(node) for node in nodes]
+        return nodes 
 
     def get_nodes_with_slivers(self, network=None):
         if network:
-            return self.xml.xpath('//rspecv2:node[@component_manager_id="%s"][sliver_type]/@component_name' % network, namespaces=self.namespaces)
+            nodes = self.xml.xpath('//rspecv2:node[@component_manager_id="%s"][sliver_type]/@component_id' % network, namespaces=self.namespaces)
         else:
-            return self.xml.xpath('//rspecv2:node[rspecv2:sliver_type]/@component_name', namespaces=self.namespaces)
+            nodes = self.xml.xpath('//rspecv2:node[rspecv2:sliver_type]/@component_id', namespaces=self.namespaces)
+        nodes = [xrn_to_hostname(node) for node in nodes]
+        return nodes
 
     def get_nodes_without_slivers(self, network=None):
+        return []
+
+    def get_sliver_attributes(self, hostname, network=None):
+        node = self.get_node_element(hostname, network)
+        sliver = node.xpath('//rspecv2:sliver_type', namespaces=self.namespaces)
+        if sliver is not None and isinstance(sliver, list):
+            sliver = sliver[0]
+        return self.attributes_list(sliver)
+   
+    def get_slice_attributes(self, network=None):
+        slice_attributes = []
+        nodes_with_slivers = self.get_nodes_with_slivers(network)
+        # TODO: default sliver attributes in the PG rspec?
+        default_ns_prefix = self.namespaces['rspecv2']
+        for node in nodes_with_slivers:
+            sliver_attributes = self.get_sliver_attributes(node, network)
+            for sliver_attribute in sliver_attributes:
+                name=str(sliver_attribute[0]) 
+                value=str(sliver_attribute[1])
+                # we currently only suppor the <initscript> and <flack> attributes 
+                if  'info' in name:
+                    attribute = {'name': 'flack_info', 'value': str(sliver_attribute[2]), 'node_id': node}
+                    slice_attributes.append(attribute) 
+                elif 'initscript' in name: 
+                    attribute = {'name': 'initscript', 'value': value, 'node_id': node}
+                    slice_attributes.append(attribute) 
+
+        return slice_attributes
+
+    def attributes_list(self, elem):
+        opts = []
+        if elem is not None:
+            for e in elem:
+                opts.append((e.tag, e.text, e.attrib))
+        return opts
+
+    def get_default_sliver_attributes(self, network=None):
+        return []
+
+    def add_default_sliver_attribute(self, name, value, network=None):
         pass
 
     def add_nodes(self, nodes, check_for_dupes=False):
@@ -111,34 +165,56 @@ class PGRSpec(RSpec):
             node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='plab-pc')
             node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='pc')
             available_tag = etree.SubElement(node_tag, 'available', now='true')
-            location_tag = etree.SubElement(node_tag, 'location', country="us")
+            sliver_type_tag = etree.SubElement(node_tag, 'sliver_type', name='plab-vnode')
+            # protogeni uses the <sliver_type> tag to identify the types of
+            # vms available at the node. 
+            # only add location tag if longitude and latitude are not null
             if 'site' in node:
-                if 'longitude' in node['site']:
-                    location_tag.set('longitude', str(node['site']['longitude']))
-                if 'latitude' in node['site']:
-                    location_tag.set('latitude', str(node['site']['latitude']))
-            #if 'interfaces' in node:
-            
-
-    def add_slivers(self, slivers, check_for_dupes=False): 
-        if not isinstance(slivers, list):
-            slivers = [slivers]
-
-        nodes_with_slivers = self.get_nodes_with_slivers()
+                longitude = node['site'].get('longitude', None)
+                latitude = node['site'].get('latitude', None)
+                if longitude and latitude:
+                    location_tag = etree.SubElement(node_tag, 'location', country="us", \
+                                                    longitude=str(longitude), latitude=str(latitude))
+
+
+    def add_slivers(self, slivers, sliver_urn=None, no_dupes=False): 
+
+        # all nodes hould already be present in the rspec. Remove all 
+        # nodes that done have slivers
+        slivers = self._process_slivers(slivers)
+        slivers_dict = {}
         for sliver in slivers:
-            hostname = sliver['hostname']
-            if hostname in nodes_with_slivers:
-                continue
-            nodes = self.xml.xpath('//rspecv2:node[@component_name="%s"] | //node[@component_name="%s"]' % (hostname, hostname), namespaces=self.namespaces)
-            if nodes:
-                node = nodes[0]
+            slivers_dict[sliver['hostname']] = sliver
+        nodes = self.get_node_elements()
+        for node in nodes:
+            urn = node.get('component_id')
+            hostname = xrn_to_hostname(urn)
+            if hostname not in slivers_dict:
+                parent = node.getparent()
+                parent.remove(node)
+            else:
+                sliver_info = slivers_dict[hostname]
                 node.set('client_id', hostname)
-                etree.SubElement(node, 'sliver_type', name='plab-vnode')
+                if sliver_urn:
+                    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)
+                    node.set('sliver_id', sliver_id)
+                sliver_elem = node.xpath('//rspecv2:sliver_type | //sliver_type', namespaces=self.namespaces)
+                if sliver_elem and isinstance(sliver_elem, list):
+                    sliver_elem = sliver_elem[0]
+                    for tag in sliver_info['tags']:
+                        if tag['tagname'] == 'flack_info':
+                            e = etree.SubElement(sliver_elem, '{%s}info' % self.namespaces['flack'], attrib=eval(tag['value']))
+                              
+     
+    def add_default_sliver_attribute(self, name, value, network=None):
+        pass
 
-    def add_interfaces(self, interfaces, check_for_dupes=False):
+    def add_interfaces(self, interfaces, no_dupes=False):
         pass
 
-    def add_links(self, links, check_for_dupes=False):
+    def add_links(self, links, no_dupes=False):
         pass
 
 
@@ -153,7 +229,11 @@ class PGRSpec(RSpec):
         for child in root.getchildren():
             self.xml.append(child)
                   
-    
+    def cleanup(self):
+        # remove unncecessary elements, attributes
+        if self.type in ['request', 'manifest']:
+            # remove 'available' element from remaining node elements
+            self.remove_element('//rspecv2:available | //available')
 
 if __name__ == '__main__':
     rspec = PGRSpec()