1 from sfa.util.xrn import Xrn, get_leaf
2 from sfa.util.xml import XpathFilter
4 from sfa.rspecs.elements.node import NodeElement
5 from sfa.rspecs.elements.sliver import Sliver
6 from sfa.rspecs.elements.location import Location
7 from sfa.rspecs.elements.hardware_type import HardwareType
8 from sfa.rspecs.elements.disk_image import DiskImage
9 from sfa.rspecs.elements.interface import Interface
10 from sfa.rspecs.elements.bwlimit import BWlimit
11 from sfa.rspecs.elements.pltag import PLTag
12 from sfa.rspecs.elements.versions.pgv2Services import PGv2Services
13 from sfa.rspecs.elements.versions.pgv2SliverType import PGv2SliverType
14 from sfa.rspecs.elements.versions.pgv2Interface import PGv2Interface
15 from sfa.rspecs.elements.versions.sfav1PLTag import SFAv1PLTag
16 from sfa.rspecs.elements.granularity import Granularity
17 from sfa.rspecs.elements.attribute import Attribute
23 def add_nodes(xml, nodes, rspec_content_type=None):
26 node_fields = ['component_manager_id', 'component_id',
27 'client_id', 'sliver_id', 'exclusive']
28 node_elem = xml.add_instance('node', node, node_fields)
29 node_elems.append(node_elem)
31 if node.get('component_id'):
32 component_name = Xrn.unescape(
33 get_leaf(Xrn(node['component_id']).get_hrn()))
34 node_elem.set('component_name', component_name)
36 if node.get('hardware_types'):
37 for hardware_type in node.get('hardware_types', []):
38 node_elem.add_instance(
39 'hardware_type', hardware_type, HardwareType.fields)
41 if node.get('location'):
42 node_elem.add_instance(
43 'location', node['location'], Location.fields)
46 if node.get('exclusive') == "true":
47 granularity = node.get('granularity')
48 node_elem.add_instance(
49 'granularity', granularity, granularity.fields)
51 PGv2Interface.add_interfaces(node_elem, node.get('interfaces'))
52 # if node.get('interfaces'):
53 # for interface in node.get('interfaces', []):
54 # node_elem.add_instance('interface', interface, ['component_id', 'client_id'])
55 # set available element
56 if node.get('available'):
57 available_elem = node_elem.add_element(
58 'available', now=node['available'])
60 PGv2Services.add_services(node_elem, node.get('services', []))
62 slivers = node.get('slivers', [])
64 # we must still advertise the available sliver types
65 if node.get('sliver_type'):
66 slivers = Sliver({'type': node['sliver_type']})
69 slivers = Sliver({'type': 'plab-vserver'})
70 # we must also advertise the available initscripts
72 if node.get('pl_initscripts'):
73 for initscript in node.get('pl_initscripts', []):
74 slivers['tags'].append(
75 {'name': 'initscript', 'value': initscript['name']})
76 PGv2SliverType.add_slivers(node_elem, slivers)
78 # advertise the node tags
79 tags = node.get('tags', [])
82 tag['name'] = tag.pop('tagname')
83 node_elem.add_instance('{%s}attribute' % xml.namespaces[
84 'planetlab'], tag, ['name', 'value'])
86 # add sliver tag in Request Rspec
87 # if rspec_content_type == "request":
88 # node_elem.add_instance('sliver', '', [])
93 def get_nodes(xml, filter=None):
96 xpath = '//node%s | //default:node%s' % (
97 XpathFilter.xpath(filter), XpathFilter.xpath(filter))
98 node_elems = xml.xpath(xpath)
99 return PGv2Node.get_node_objs(node_elems)
102 def get_nodes_with_slivers(xml, filter=None):
105 xpath = '//node[count(sliver_type)>0] | //default:node[count(default:sliver_type) > 0]'
106 node_elems = xml.xpath(xpath)
107 return PGv2Node.get_node_objs(node_elems)
110 def get_node_objs(node_elems):
112 for node_elem in node_elems:
113 node = NodeElement(node_elem.attrib, node_elem)
115 if 'component_id' in node_elem.attrib:
116 node['authority_id'] = Xrn(
117 node_elem.attrib['component_id']).get_authority_urn()
120 hardware_type_elems = node_elem.xpath(
121 './default:hardware_type | ./hardware_type')
122 node['hardware_types'] = [dict(hw_type.get_instance(
123 HardwareType)) for hw_type in hardware_type_elems]
126 location_elems = node_elem.xpath('./default:location | ./location')
127 locations = [dict(location_elem.get_instance(Location))
128 for location_elem in location_elems]
129 if len(locations) > 0:
130 node['location'] = locations[0]
133 granularity_elems = node_elem.xpath(
134 './default:granularity | ./granularity')
135 if len(granularity_elems) > 0:
136 node['granularity'] = granularity_elems[
137 0].get_instance(Granularity)
140 iface_elems = node_elem.xpath('./default:interface | ./interface')
141 node['interfaces'] = [dict(iface_elem.get_instance(
142 Interface)) for iface_elem in iface_elems]
145 node['services'] = PGv2Services.get_services(node_elem)
148 node['slivers'] = PGv2SliverType.get_slivers(node_elem)
151 available_elems = node_elem.xpath(
152 './default:available | ./available')
153 if len(available_elems) > 0 and 'now' in available_elems[0].attrib:
154 if available_elems[0].attrib.get('now', '').lower() == 'true':
155 node['boot_state'] = 'boot'
157 node['boot_state'] = 'disabled'
161 node['pl_initscripts'] = []
162 initscript_elems = node_elem.xpath(
163 './default:sliver_type/planetlab:initscript | ./sliver_type/initscript')
164 if len(initscript_elems) > 0:
165 for initscript_elem in initscript_elems:
166 if 'name' in initscript_elem.attrib:
167 node['pl_initscripts'].append(
168 dict(initscript_elem.attrib))
174 tag_elems = node_elem.xpath(
175 './planetlab:attribute | ./attribute')
177 if len(tag_elems) > 0:
178 for tag_elem in tag_elems:
179 tag = dict(tag_elem.get_instance(Attribute))
180 tag['tagname'] = tag.pop('name')
181 node['tags'].append(tag)
188 def add_slivers(xml, slivers):
190 for sliver in slivers:
192 if isinstance(sliver, str):
193 filter['component_id'] = '*%s*' % sliver
195 elif 'component_id' in sliver and sliver['component_id']:
196 filter['component_id'] = '*%s*' % sliver['component_id']
199 nodes = PGv2Node.get_nodes(xml, filter)
203 PGv2SliverType.add_slivers(node, sliver)
206 def remove_slivers(xml, hostnames):
207 for hostname in hostnames:
208 nodes = PGv2Node.get_nodes(
209 xml, {'component_id': '*%s*' % hostname})
211 slivers = PGv2SliverType.get_slivers(node.element)
212 for sliver in slivers:
213 node.element.remove(sliver.element)
214 if __name__ == '__main__':
215 from sfa.rspecs.rspec import RSpec
217 r = RSpec('/tmp/emulab.rspec')
218 r2 = RSpec(version='ProtoGENI')
219 nodes = PGv2Node.get_nodes(r.xml)
220 PGv2Node.add_nodes(r2.xml.root, nodes)