Merge branch 'master' into senslab2
[sfa.git] / sfa / rspecs / elements / versions / sfav1Node.py
index 37a786e..26d41a9 100644 (file)
@@ -1,11 +1,10 @@
-
+from sfa.util.sfalogging import logger
 from sfa.util.xml import XpathFilter
 from sfa.util.xml import XpathFilter
-from sfa.util.plxrn import PlXrn
+from sfa.util.plxrn import PlXrn, xrn_to_hostname
 from sfa.util.xrn import Xrn
 from sfa.rspecs.elements.element import Element
 from sfa.rspecs.elements.node import Node
 from sfa.rspecs.elements.sliver import Sliver
 from sfa.util.xrn import Xrn
 from sfa.rspecs.elements.element import Element
 from sfa.rspecs.elements.node import Node
 from sfa.rspecs.elements.sliver import Sliver
-from sfa.rspecs.elements.network import Network 
 from sfa.rspecs.elements.location import Location
 from sfa.rspecs.elements.hardware_type import HardwareType
 from sfa.rspecs.elements.disk_image import DiskImage
 from sfa.rspecs.elements.location import Location
 from sfa.rspecs.elements.hardware_type import HardwareType
 from sfa.rspecs.elements.disk_image import DiskImage
@@ -20,19 +19,19 @@ class SFAv1Node:
 
     @staticmethod
     def add_nodes(xml, nodes):
 
     @staticmethod
     def add_nodes(xml, nodes):
-        network_elems = Element.get_elements(xml, '//network', fields=['name'])
+        network_elems = xml.xpath('//network')
         if len(network_elems) > 0:
             network_elem = network_elems[0]
         elif len(nodes) > 0 and nodes[0].get('component_manager_id'):
         if len(network_elems) > 0:
             network_elem = network_elems[0]
         elif len(nodes) > 0 and nodes[0].get('component_manager_id'):
-            network_urn = nodes[0]['component_manager_id']    
-            network_elems = Element.add_elements(xml, 'network', {'name': Xrn(network_urn).get_hrn()})
-            network_elem = network_elems[0]
+            network_urn = nodes[0]['component_manager_id']
+            network_elem = xml.add_element('network', name = Xrn(network_urn).get_hrn())     
+        else:
+            network_elem = xml
 
         node_elems = []       
         for node in nodes:
             node_fields = ['component_manager_id', 'component_id', 'boot_state']
 
         node_elems = []       
         for node in nodes:
             node_fields = ['component_manager_id', 'component_id', 'boot_state']
-            elems = Element.add_elements(network_elem, 'node', node, node_fields)
-            node_elem = elems[0]  
+            node_elem = network_elem.add_instance('node', node, node_fields)
             node_elems.append(node_elem)
 
             # determine network hrn
             node_elems.append(node_elem)
 
             # determine network hrn
@@ -42,23 +41,30 @@ class SFAv1Node:
 
             # set component_name attribute and  hostname element
             if 'component_id' in node and node['component_id']:
 
             # set component_name attribute and  hostname element
             if 'component_id' in node and node['component_id']:
-                xrn = Xrn(node['component_id'])
-                node_elem.set('component_name', xrn.get_leaf())
-                hostname_tag = node_elem.add_element('hostname')
-                hostname_tag.set_text(xrn.get_leaf())
+                component_name = xrn_to_hostname(node['component_id'])
+                node_elem.set('component_name', component_name)
+                hostname_elem = node_elem.add_element('hostname')
+                hostname_elem.set_text(component_name)
 
             # set site id
             if 'authority_id' in node and node['authority_id']:
                 node_elem.set('site_id', node['authority_id'])
 
 
             # set site id
             if 'authority_id' in node and node['authority_id']:
                 node_elem.set('site_id', node['authority_id'])
 
-            location_elems = Element.add_elements(node_elem, 'location', node.get('location', []), Location.fields)
-            interface_elems = Element.add_elements(node_elem, 'interface', node.get('interfaces', []), Interface.fields)
+            # add locaiton
+            location = node.get('location')
+            if location:
+                node_elem.add_instance('location', location, Location.fields)
+
+            for interface in node.get('interfaces', []):
+                node_elem.add_instance('interface', interface, ['component_id', 'client_id', 'ipv4']) 
             
             #if 'bw_unallocated' in node and node['bw_unallocated']:
             #    bw_unallocated = etree.SubElement(node_elem, 'bw_unallocated', units='kbps').text = str(int(node['bw_unallocated'])/1000)
 
             PGv2Services.add_services(node_elem, node.get('services', []))
             
             #if 'bw_unallocated' in node and node['bw_unallocated']:
             #    bw_unallocated = etree.SubElement(node_elem, 'bw_unallocated', units='kbps').text = str(int(node['bw_unallocated'])/1000)
 
             PGv2Services.add_services(node_elem, node.get('services', []))
-            SFAv1PLTag.add_pl_tags(node_elem, node.get('tags', [])) 
+            for tag in node.get('tags', []):
+                tag_elem = node_elem.add_element(tag['tagname'])
+                tag_elem.set_text(tag['value'])
             SFAv1Sliver.add_slivers(node_elem, node.get('slivers', []))
 
     @staticmethod 
             SFAv1Sliver.add_slivers(node_elem, node.get('slivers', []))
 
     @staticmethod 
@@ -71,7 +77,7 @@ class SFAv1Node:
                 sliver = {}
             elif 'component_id' in sliver and sliver['component_id']:
                 filter['component_id'] = '*%s*' % sliver['component_id']
                 sliver = {}
             elif 'component_id' in sliver and sliver['component_id']:
                 filter['component_id'] = '*%s*' % sliver['component_id']
-            if not fliter:
+            if not filter:
                 continue 
             nodes = SFAv1Node.get_nodes(xml, filter)
             if not nodes:
                 continue 
             nodes = SFAv1Node.get_nodes(xml, filter)
             if not nodes:
@@ -96,9 +102,9 @@ class SFAv1Node:
 
     @staticmethod
     def get_nodes_with_slivers(xml):
 
     @staticmethod
     def get_nodes_with_slivers(xml):
-        xpath = '//node/sliver | //default:node/default:sliver' % (XpathFilter.xpath(filter), XpathFilter.xpath(filter))
+        xpath = '//node[count(sliver)>0] | //default:node[count(default:sliver)>0]' 
         node_elems = xml.xpath(xpath)
         node_elems = xml.xpath(xpath)
-        return SFAv1Node.get_nodes_objs(node_elems)
+        return SFAv1Node.get_node_objs(node_elems)
 
 
     @staticmethod
 
 
     @staticmethod
@@ -108,16 +114,31 @@ class SFAv1Node:
             node = Node(node_elem.attrib, node_elem)
             if 'site_id' in node_elem.attrib:
                 node['authority_id'] = node_elem.attrib['site_id']
             node = Node(node_elem.attrib, node_elem)
             if 'site_id' in node_elem.attrib:
                 node['authority_id'] = node_elem.attrib['site_id']
-            location_objs = Element.get_elements(node_elem, './default:location | ./location', Location)
-            if len(location_objs) > 0:
-                node['location'] = location_objs[0]
-            bwlimit_objs = Element.get_elements(node_elem, './default:bw_limit | ./bw_limit', BWlimit)
-            if len(bwlimit_objs) > 0:
-                node['bwlimit'] = bwlimit_objs[0]
-            node['interfaces'] = Element.get_elements(node_elem, './default:interface | ./interface', Interface)
+            # get location
+            location_elems = node_elem.xpath('./default:location | ./location')
+            locations = [loc_elem.get_instance(Location) for loc_elem in location_elems]  
+            if len(locations) > 0:
+                node['location'] = locations[0]
+            # get bwlimit
+            bwlimit_elems = node_elem.xpath('./default:bw_limit | ./bw_limit')
+            bwlimits = [bwlimit_elem.get_instance(BWlimit) for bwlimit_elem in bwlimit_elems]
+            if len(bwlimits) > 0:
+                node['bwlimit'] = bwlimits[0]
+            # get interfaces
+            iface_elems = node_elem.xpath('./default:interface | ./interface')
+            ifaces = [iface_elem.get_instance(Interface) for iface_elem in iface_elems]
+            node['interfaces'] = ifaces
+            # get services
             node['services'] = PGv2Services.get_services(node_elem) 
             node['services'] = PGv2Services.get_services(node_elem) 
+            # get slivers
             node['slivers'] = SFAv1Sliver.get_slivers(node_elem)
             node['slivers'] = SFAv1Sliver.get_slivers(node_elem)
-            node['tags'] =  SFAv1PLTag.get_pl_tags(node_elem, ignore=Node.fields.keys())
+            # get tags
+            node['tags'] =  SFAv1PLTag.get_pl_tags(node_elem, ignore=Node.fields)
+
+            parent = node_elem.getparent()
+            if (parent != None) and (parent.tag=="network") and ("name" in parent.attrib):
+                node['network_name'] = parent.attrib['name']
+
             nodes.append(node)
         return nodes            
             nodes.append(node)
         return nodes            
-            
+