Slice manager converts PG advertisement RSpec to SFA RSpec
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Mon, 2 May 2011 02:31:53 +0000 (22:31 -0400)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Mon, 2 May 2011 02:31:53 +0000 (22:31 -0400)
sfa/managers/slice_manager_pl.py
sfa/rspecs/pg_rspec.py
sfa/rspecs/pg_rspec_converter.py
sfa/rspecs/rspec_parser.py

index 254c817..24b6e89 100644 (file)
@@ -15,7 +15,10 @@ from sfa.util.rspec import *
 from sfa.util.specdict import *
 from sfa.util.faults import *
 from sfa.util.record import SfaRecord
+from sfa.rspecs.pg_rspec import PGRSpec
 from sfa.rspecs.sfa_rspec import SfaRSpec
+from sfa.rspecs.pg_rspec_converter import PGRSpecConverter
+from sfa.rspecs.rspec_parser import parse_rspec    
 from sfa.util.policy import Policy
 from sfa.util.prefixTree import prefixTree
 from sfa.util.sfaticket import *
@@ -380,13 +383,23 @@ def ListResources(api, creds, options, call_id):
         #threads.run(server.get_resources, cred, xrn, origin_hrn)
                     
     results = threads.get_results()
+    #results.append(open('/root/protogeni.rspec', 'r').read())
     rspec = SfaRSpec()
     for result in results:
-        rspec.merge(result)
+        try:
+            tmp_rspec = parse_rspec(result)
+            if isinstance(tmp_rspec, SfaRSpec):
+                rspec.merge(result)
+            elif isinstance(tmp_rspec, PGRSpec):
+                rspec.merge(PGRSpecConverter.to_sfa_rspec(result))
+            else:
+                api.logger.info("SM.ListResources: invalid aggregate rspec")                        
+        except:
+            api.logger.info("SM.ListResources: Failed to merge aggregate rspec")
 
     # cache the result
     if caching and api.cache and not xrn:
-        api.cache.add(version_string, rspec)
+        api.cache.add(version_string, rspec.toxml())
  
     return rspec.toxml()
 
index 569c342..bc8d19f 100755 (executable)
@@ -23,7 +23,7 @@ class PGRSpec(RSpec):
 
     def get_network(self):
         network = None 
-        nodes = self.xml.xpath('//rspecv2:node[@component_manager_uuid][1]', self.namespaces)
+        nodes = self.xml.xpath('//rspecv2:node[@component_manager_uuid][1]', namespaces=self.namespaces)
         if nodes:
             network  = nodes[0].get('component_manager_uuid')
         return network
@@ -33,17 +33,17 @@ class PGRSpec(RSpec):
         return set(networks)
 
     def get_node_elements(self):
-        nodes = self.xml.xpath('//rspecv2:node', self.namespaces)
+        nodes = self.xml.xpath('//rspecv2:node', namespaces=self.namespaces)
         return nodes
 
     def get_nodes(self, network=None):
-        return self.xml.xpath('//rspecv2:node[@component_uuid]/@component_uuid', self.namespaces) 
+        return self.xml.xpath('//rspecv2:node[@component_uuid]/@component_uuid', namespaces=self.namespaces) 
 
     def get_nodes_with_slivers(self, network=None):
         if network:
-            return self.xml.xpath('//node[@component_manager_uuid="%s"][sliver_type]/@component_uuid' % network, self.namespaces)
+            return self.xml.xpath('//node[@component_manager_uuid="%s"][sliver_type]/@component_uuid' % network, namespaces=self.namespaces)
         else:
-            return self.xml.xpath('//node[sliver_type]/@component_uuid' % network, self.namespaces)
+            return self.xml.xpath('//node[sliver_type]/@component_uuid' % network, namespaces=self.namespaces)
 
     def get_nodes_without_slivers(self, network=None):
         pass
@@ -54,16 +54,27 @@ class PGRSpec(RSpec):
         for node in nodes:
             urn = ""
             if check_for_dupes and \
-              self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, self.namespaces):
+              self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, namespaces=self.namespaces):
                 # node already exists
                 continue
                 
             node_tag = etree.SubElement(self.xml, 'node')
+            if 'network' in node:
+                node_tag.set('component_manager_id', network)
+            if 'urn' in node:
+                node_tag.set('component_id', node['urn'])
+            if 'hostname' in node:
+                node_tag.set('name', node['hostname'])
             node_type_tag = etree.SubElement(node_tag, 'node_type', type_name='pcvm', type_slots='100')
             available_tag = etree.SubElement(node_tag, 'available').text = 'true'
             exclusive_tag = etree.SubElement(node_tag, 'exclusive').text = 'false'
-            location_tag = etree.SubElement(node_tag, 'location')
-            interface_tag = etree.SubElement(node_tag, 'interface')
+            location_tag = etree.SubElement(node_tag, 'location', location="US")
+            if 'site' in node:
+                if 'longitude' in node['site']:
+                    location_tag.set('longitude', site['longitude'])
+                if 'latitude' in node['site']:
+                    location_tag.set('latitude', site['latitude'])
+            #if 'interfaces' in node:
             
 
     def add_slivers(self, slivers, check_for_dupes=False): 
index 7ec6803..aa68546 100755 (executable)
@@ -34,16 +34,12 @@ class PGRSpecConverter:
 
     @staticmethod
     def to_sfa_node(site, node, i=0):
-        cm_urn = node.get('component_manager_uuid')
-        c_name = node.get('component_name')
-        c_urn = node.get('component_uuid')
-        c_hrn, _ = urn_to_hrn(c_urn)
+        urn = node.get('component_uuid')
+        hrn, _ = urn_to_hrn(urn)
+        hostname = Xrn.urn_split(urn)[-1]
         node_tag = etree.SubElement(site, "node")
-        node_tag.set("component_manager_uuid", cm_urn)
-        node_tag.set("component_name", c_name)
-        node_tag.set("component_uuid", c_urn)
-        hostname_tag = etree.SubElement(node_tag, "hostname").text = c_hrn
-        urn_tag = etree.SubElement(node_tag, "urn").text = c_hrn
+        hostname_tag = etree.SubElement(node_tag, "hostname").text = hostname
+        urn_tag = etree.SubElement(node_tag, "urn").text = urn
         for child in node.getchildren():
             node_tag.append(transform(child).getroot())      
 
@@ -51,16 +47,13 @@ class PGRSpecConverter:
     def to_sfa_network(pg_rspec, xml): 
         network_urn = pg_rspec.get_network()
         network,  _ = urn_to_hrn(network_urn)
-        nodes = pg_rspec.get_nodes()
+        nodes = pg_rspec.get_node_elements()
         network_tag = etree.SubElement(xml, "network")
         network_tag.set("name", network)
         network_tag.set("id", network)
-        site_tag = etree.SubElement(network_tag, "Site")
-        site_tag.set("id", network)
-        name = etree.SubElement(site_tag, "name").text = network
         i = 0
         for node in nodes:
-           PGRSpecConverter.to_sfa_node(site_tag, node, i)
+           PGRSpecConverter.to_sfa_node(network_tag, node, i)
         
     @staticmethod
     def to_sfa_rspec(rspec):
index c3dde65..91f58e5 100755 (executable)
@@ -3,7 +3,6 @@ from sfa.rspecs.sfa_rspec import SfaRSpec
 from sfa.rspecs.pg_rspec import PGRSpec
 from sfa.rspecs.rspec import RSpec
 from lxml import etree 
-from 
 
 def parse_rspec(in_rspec):
     rspec = RSpec(rspec=in_rspec)