3 from StringIO import StringIO
4 from sfa.rspecs.rspec import RSpec
5 from sfa.util.xrn import *
6 from sfa.util.plxrn import hostname_to_urn, xrn_to_hostname
7 from sfa.util.config import Config
8 from sfa.rspecs.rspec_version import RSpecVersion
10 _ad_version = {'type': 'ProtoGENI',
12 'schema': 'http://www.protogeni.net/resources/rspec/2/ad.xsd',
13 'namespace': 'http://www.protogeni.net/resources/rspec/2',
15 'http://www.protogeni.net/resources/rspec/ext/gre-tunnel/1',
16 'http://www.protogeni.net/resources/rspec/ext/other-ext/3'
20 _request_version = {'type': 'ProtoGENI',
22 'schema': 'http://www.protogeni.net/resources/rspec/2/request.xsd',
23 'namespace': 'http://www.protogeni.net/resources/rspec/2',
25 'http://www.protogeni.net/resources/rspec/ext/gre-tunnel/1',
26 'http://www.protogeni.net/resources/rspec/ext/other-ext/3'
29 pg_rspec_ad_version = RSpecVersion(_ad_version)
30 pg_rspec_request_version = RSpecVersion(_request_version)
34 header = '<?xml version="1.0"?>\n'
35 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" xmlns:planetlab="http://www.planet-lab.org/resources/ext/planetlab/1" />'
37 def __init__(self, rspec="", namespaces={}, type=None):
39 type = 'advertisement'
42 if type == 'advertisement':
43 self.version = pg_rspec_ad_version
46 self.version = pg_rspec_request_version
49 self.template = self.template % {'rspec_type': rspec_type}
52 self.namespaces = {'rspecv2': self.version['namespace'],
53 'flack': 'http://www.protogeni.net/resources/rspec/ext/flack/1',
54 'planetlab': 'http://www.planet-lab.org/resources/ext/planetlab/1' }
57 self.namespaces = namespaces
60 self.parse_rspec(rspec, self.namespaces)
67 self.xml.set('type', self.type)
69 def get_network(self):
71 nodes = self.xml.xpath('//rspecv2:node[@component_manager_id][1]', namespaces=self.namespaces)
73 network = nodes[0].get('component_manager_id')
76 def get_networks(self):
77 networks = self.xml.xpath('//rspecv2:node[@component_manager_id]/@component_manager_id', namespaces=self.namespaces)
80 def get_node_element(self, hostname, network=None):
81 nodes = self.xml.xpath('//rspecv2:node[@component_id[contains(., "%s")]] | node[@component_id[contains(., "%s")]]' % (hostname, hostname), namespaces=self.namespaces)
82 if isinstance(nodes,list) and nodes:
87 def get_node_elements(self, network=None):
88 nodes = self.xml.xpath('//rspecv2:node | //node', namespaces=self.namespaces)
91 def get_nodes(self, network=None):
92 xpath = '//rspecv2:node[@component_name]/@component_id | //node[@component_name]/@component_id'
93 nodes = self.xml.xpath(xpath, namespaces=self.namespaces)
94 nodes = [xrn_to_hostname(node) for node in nodes]
97 def get_nodes_with_slivers(self, network=None):
99 nodes = self.xml.xpath('//rspecv2:node[@component_manager_id="%s"][sliver_type]/@component_id' % network, namespaces=self.namespaces)
101 nodes = self.xml.xpath('//rspecv2:node[rspecv2:sliver_type]/@component_id', namespaces=self.namespaces)
102 nodes = [xrn_to_hostname(node) for node in nodes]
105 def get_nodes_without_slivers(self, network=None):
108 def get_sliver_attributes(self, hostname, network=None):
109 node = self.get_node_element(hostname, network)
110 sliver = node.xpath('./rspecv2:sliver_type', namespaces=self.namespaces)
111 if sliver is not None and isinstance(sliver, list):
113 return self.attributes_list(sliver)
115 def get_slice_attributes(self, network=None):
116 slice_attributes = []
117 nodes_with_slivers = self.get_nodes_with_slivers(network)
118 # TODO: default sliver attributes in the PG rspec?
119 default_ns_prefix = self.namespaces['rspecv2']
120 for node in nodes_with_slivers:
121 sliver_attributes = self.get_sliver_attributes(node, network)
122 for sliver_attribute in sliver_attributes:
123 name=str(sliver_attribute[0])
124 text =str(sliver_attribute[1])
125 attribs = sliver_attribute[2]
126 # we currently only suppor the <initscript> and <flack> attributes
128 attribute = {'name': 'flack_info', 'value': str(attribs), 'node_id': node}
129 slice_attributes.append(attribute)
130 elif 'initscript' in name:
131 if attribs is not None and 'name' in attribs:
132 value = attribs['name']
135 attribute = {'name': 'initscript', 'value': value, 'node_id': node}
136 slice_attributes.append(attribute)
138 return slice_attributes
140 def attributes_list(self, elem):
144 opts.append((e.tag, e.text, e.attrib))
147 def get_default_sliver_attributes(self, network=None):
150 def add_default_sliver_attribute(self, name, value, network=None):
153 def add_nodes(self, nodes, check_for_dupes=False):
154 if not isinstance(nodes, list):
158 if check_for_dupes and \
159 self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, namespaces=self.namespaces):
160 # node already exists
163 node_tag = etree.SubElement(self.xml, 'node', exclusive='false')
164 if 'network_urn' in node:
165 node_tag.set('component_manager_id', node['network_urn'])
167 node_tag.set('component_id', node['urn'])
168 if 'hostname' in node:
169 node_tag.set('component_name', node['hostname'])
170 # TODO: should replace plab-pc with pc model
171 node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='plab-pc')
172 node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='pc')
173 available_tag = etree.SubElement(node_tag, 'available', now='true')
174 sliver_type_tag = etree.SubElement(node_tag, 'sliver_type', name='plab-vnode')
176 pl_initscripts = node.get('pl_initscripts', {})
177 for pl_initscript in pl_initscripts.values():
178 etree.SubElement(sliver_type_tag, '{%s}initscript' % self.namespaces['planetlab'], name=pl_initscript['name'])
180 # protogeni uses the <sliver_type> tag to identify the types of
181 # vms available at the node.
182 # only add location tag if longitude and latitude are not null
184 longitude = node['site'].get('longitude', None)
185 latitude = node['site'].get('latitude', None)
186 if longitude and latitude:
187 location_tag = etree.SubElement(node_tag, 'location', country="us", \
188 longitude=str(longitude), latitude=str(latitude))
191 def add_slivers(self, slivers, sliver_urn=None, no_dupes=False):
193 # all nodes hould already be present in the rspec. Remove all
194 # nodes that done have slivers
195 slivers = self._process_slivers(slivers)
197 for sliver in slivers:
198 slivers_dict[sliver['hostname']] = sliver
199 nodes = self.get_node_elements()
201 urn = node.get('component_id')
202 hostname = xrn_to_hostname(urn)
203 if hostname not in slivers_dict:
204 parent = node.getparent()
207 sliver_info = slivers_dict[hostname]
208 node.set('client_id', hostname)
210 slice_id = sliver_info.get('slice_id', -1)
211 node_id = sliver_info.get('node_id', -1)
212 sliver_id = urn_to_sliver_id(sliver_urn, slice_id, node_id)
213 node.set('sliver_id', sliver_id)
215 # remove existing sliver_type tags,it needs to be recreated
216 sliver_elem = node.xpath('./rspecv2:sliver_type | ./sliver_type', namespaces=self.namespaces)
217 if sliver_elem and isinstance(sliver_elem, list):
218 sliver_elem = sliver_elem[0]
219 node.remove(sliver_elem)
221 sliver_elem = etree.SubElement(node, 'sliver_type', name='plab-vnode')
222 for tag in sliver_info['tags']:
223 if tag['tagname'] == 'flack_info':
224 e = etree.SubElement(sliver_elem, '{%s}info' % self.namespaces['flack'], attrib=eval(tag['value']))
225 elif tag['tagname'] == 'initscript':
226 e = etree.SubElement(sliver_elem, '{%s}initscript' % self.namespaces['planetlab'], attrib={'name': tag['value']})
230 def add_default_sliver_attribute(self, name, value, network=None):
233 def add_interfaces(self, interfaces, no_dupes=False):
236 def add_links(self, links, no_dupes=False):
240 def merge(self, in_rspec):
242 Merge contents for specified rspec with current rspec
245 # just copy over all the child elements under the root element
246 tree = etree.parse(StringIO(in_rspec))
247 root = tree.getroot()
248 for child in root.getchildren():
249 self.xml.append(child)
252 # remove unncecessary elements, attributes
253 if self.type in ['request', 'manifest']:
254 # remove 'available' element from remaining node elements
255 self.remove_element('//rspecv2:available | //available')
257 if __name__ == '__main__':