3 from sfa.util.plxrn import PlXrn
4 from sfa.util.xrn import Xrn
5 from sfa.rspecs.elements.node import Node
6 from sfa.rspecs.elements.sliver import Sliver
7 from sfa.rspecs.elements.network import Network
8 from sfa.rspecs.elements.location import Location
9 from sfa.rspecs.elements.hardware_type import HardwareType
10 from sfa.rspecs.elements.disk_image import DiskImage
11 from sfa.rspecs.elements.interface import Interface
12 from sfa.rspecs.elements.bwlimit import BWlimit
13 from sfa.rspecs.elements.pltag import PLTag
14 from sfa.rspecs.rspec_elements import RSpecElement, RSpecElements
15 from sfa.rspecs.elements.versions.sfav1Network import SFAv1Network
16 from sfa.rspecs.elements.versions.pgv2Services import PGv2Services
21 'node': RSpecElement(RSpecElements.NODE, '//default:node | //node'),
22 'sliver': RSpecElement(RSpecElements.SLIVER, './default:sliver | ./sliver'),
23 'interface': RSpecElement(RSpecElements.INTERFACE, './default:interface | ./interface'),
24 'location': RSpecElement(RSpecElements.LOCATION, './default:location | ./location'),
25 'bw_limit': RSpecElement(RSpecElements.BWLIMIT, './default:bw_limit | ./bw_limit'),
29 def add_nodes(xml, nodes):
30 network_elems = SFAv1Network.get_networks(xml)
31 if len(network_elems) > 0:
32 network_elem = network_elems[0]
33 elif len(nodes) > 0 and nodes[0].get('component_manager_id'):
34 network_elem = SFAv1Network.add_network(xml.root, {'name': nodes[0]['component_manager_id']})
39 node_elem = etree.SubElement(network_elem, 'node')
40 node_elems.append(node_elem)
42 if 'component_manager_id' in node and node['component_manager_id']:
43 node_elem.set('component_manager_id', node['component_manager_id'])
44 network = Xrn(node['component_manager_id']).get_hrn()
45 if 'component_id' in node and node['component_id']:
46 node_elem.set('component_id', node['component_id'])
47 xrn = Xrn(node['component_id'])
48 node_elem.set('component_name', xrn.get_leaf())
49 hostname_tag = etree.SubElement(node_elem, 'hostname').text = xrn.get_leaf()
50 if 'authority_id' in node and node['authority_id']:
51 node_elem.set('site_id', node['authority_id'])
52 if 'boot_state' in node and node['boot_state']:
53 node_elem.set('boot_state', node['boot_state'])
54 if 'location' in node and node['location']:
55 location_elem = etree.SubElement(node_elem, 'location')
56 for field in Location.fields:
57 if field in node['location'] and node['location'][field]:
58 location_elem.set(field, node['location'][field])
59 if 'interfaces' in node and node['interfaces']:
61 for interface in node['interfaces']:
62 if 'bwlimit' in interface and interface['bwlimit']:
63 bwlimit = etree.SubElement(node_elem, 'bw_limit', units='kbps').text = str(interface['bwlimit']/1000)
64 comp_id = PlXrn(auth=network, interface='node%s:eth%s' % (interface['node_id'], i)).get_urn()
65 ipaddr = interface['ipv4']
66 interface_elem = etree.SubElement(node_elem, 'interface', component_id=comp_id, ipv4=ipaddr)
68 if 'bw_unallocated' in node and node['bw_unallocated']:
69 bw_unallocated = etree.SubElement(node_elem, 'bw_unallocated', units='kbps').text = str(int(node['bw_unallocated'])/1000)
71 if node.get('services'):
72 PGv2Services.add_services(node_elem, node.get('services'))
75 for tag in node['tags']:
76 # expose this hard wired list of tags, plus the ones that are marked 'sfa' in their category
77 if tag['name'] in ['fcdistro', 'arch']:
78 tag_element = etree.SubElement(node_elem, tag['name']).text=tag['value']
80 if node.get('slivers'):
81 for sliver in node['slivers']:
82 sliver_elem = etree.SubElement(node_elem, 'sliver')
83 if sliver.get('sliver_id'):
84 sliver_id_leaf = Xrn(sliver.get('sliver_id')).get_leaf()
85 sliver_id_parts = sliver_id_leaf.split(':')
86 name = sliver_id_parts[0]
87 sliver_elem.set('name', name)
90 def add_slivers(xml, slivers):
96 node_elems = xml.xpath(SFAv1Node.elements['node'].path)
97 for node_elem in node_elems:
98 node = Node(node_elem.attrib, node_elem)
99 if 'site_id' in node_elem.attrib:
100 node['authority_id'] = node_elem.attrib['site_id']
101 if 'authority_id' in node_elem.attrib:
102 node['authority_id'] = node_elem.attrib['authority_id']
105 location_elems = node_elem.xpath(SFAv1Node.elements['location'].path, xml.namespaces)
106 if len(location_elems) > 0:
107 node['location'] = Location(location_elems[0].attrib, location_elems[0])
110 bwlimit_elems = node_elem.xpath(SFAv1Node.elements['bw_limit'].path, xml.namespaces)
111 if len(bwlimit_elems) > 0:
112 bwlimit = BWlimit(bwlimit_elems[0].attrib, bwlimit_elems[0])
113 node['bwlimit'] = bwlimit
116 interface_elems = node_elem.xpath(SFAv1Node.elements['interface'].path, xml.namespaces)
117 node['interfaces'] = []
118 for interface_elem in interface_elems:
119 node['interfaces'].append(Interface(interface_elem.attrib, interface_elem))
122 sliver_elems = node_elem.xpath(SFAv1Node.elements['sliver'].path, xml.namespaces)
124 for sliver_elem in sliver_elems:
125 node['slivers'].append(Sliver(sliver_elem.attrib, sliver_elem))
129 for child in node_elem.iterchildren():
130 if child.tag not in SFAv1Node.elements:
131 tag = PLTag({'name': child.tag, 'value': child.text}, child)
132 node['tags'].append(tag)
137 def get_nodes_with_slivers(xml):
138 nodes = SFAv1Node.get_nodes(xml)
139 nodes_with_slivers = [node for node in nodes if node['slivers']]
140 return nodes_with_slivers