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"/>'
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']}
54 self.namespaces = namespaces
57 self.parse_rspec(rspec, self.namespaces)
64 self.xml.set('type', self.type)
66 def get_network(self):
68 nodes = self.xml.xpath('//rspecv2:node[@component_manager_uuid][1]', namespaces=self.namespaces)
70 network = nodes[0].get('component_manager_uuid')
73 def get_networks(self):
74 networks = self.xml.xpath('//rspecv2:node[@component_manager_uuid]/@component_manager_uuid', namespaces=self.namespaces)
77 def get_node_element(self, hostname, network=None):
78 nodes = self.xml.xpath('//rspecv2:node[@component_id[contains(., "%s")]] | node[@component_id[contains(., "%s")]]' % (hostname, hostname), namespaces=self.namespaces)
79 if isinstance(nodes,list) and nodes:
84 def get_node_elements(self, network=None):
85 nodes = self.xml.xpath('//rspecv2:node | //node', namespaces=self.namespaces)
88 def get_nodes(self, network=None):
89 xpath = '//rspecv2:node[@component_name]/@component_id | //node[@component_name]/@component_id'
90 nodes = self.xml.xpath(xpath, namespaces=self.namespaces)
91 nodes = [xrn_to_hostname(node) for node in nodes]
94 def get_nodes_with_slivers(self, network=None):
96 nodes = self.xml.xpath('//rspecv2:node[@component_manager_id="%s"][sliver_type]/@component_id' % network, namespaces=self.namespaces)
98 nodes = self.xml.xpath('//rspecv2:node[rspecv2:sliver_type]/@component_id', namespaces=self.namespaces)
99 nodes = [xrn_to_hostname(node) for node in nodes]
102 def get_nodes_without_slivers(self, network=None):
105 def get_sliver_attributes(self, hostname, network=None):
106 node = self.get_node_element(hostname, network)
107 sliver = node.xpath('//rspecv2:sliver_type', namespaces=self.namespaces)
108 if sliver is not None and isinstance(sliver, list):
110 return self.attributes_list(sliver)
112 def get_slice_attributes(self, network=None):
113 slice_attributes = []
114 nodes_with_slivers = self.get_nodes_with_slivers(network)
115 # TODO: default sliver attributes in the PG rspec?
116 default_ns_prefix = self.namespaces['rspecv2']
117 for node in nodes_with_slivers:
118 sliver_attributes = self.get_sliver_attributes(node, network)
119 for sliver_attribute in sliver_attributes:
120 name=str(sliver_attribute[0])
121 value=str(sliver_attribute[1])
122 # we currently only suppor the <initscript> and <flack> attributes
124 attribute = {'name': 'flack_info', 'value': str(sliver_attribute[2]), 'node_id': node}
125 slice_attributes.append(attribute)
126 elif 'initscript' in name:
127 attribute = {'name': 'initscript', 'value': value, 'node_id': node}
128 slice_attributes.append(attribute)
130 return slice_attributes
132 def attributes_list(self, elem):
136 opts.append((e.tag, e.text, e.attrib))
139 def get_default_sliver_attributes(self, network=None):
142 def add_default_sliver_attribute(self, name, value, network=None):
145 def add_nodes(self, nodes, check_for_dupes=False):
146 if not isinstance(nodes, list):
150 if check_for_dupes and \
151 self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, namespaces=self.namespaces):
152 # node already exists
155 node_tag = etree.SubElement(self.xml, 'node', exclusive='false')
156 if 'network_urn' in node:
157 node_tag.set('component_manager_id', node['network_urn'])
159 node_tag.set('component_id', node['urn'])
160 if 'hostname' in node:
161 node_tag.set('component_name', node['hostname'])
162 # TODO: should replace plab-pc with pc model
163 node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='plab-pc')
164 node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='pc')
165 available_tag = etree.SubElement(node_tag, 'available', now='true')
166 sliver_type_tag = etree.SubElement(node_tag, 'sliver_type', name='plab-vnode')
167 # protogeni uses the <sliver_type> tag to identify the types of
168 # vms available at the node.
169 # only add location tag if longitude and latitude are not null
171 longitude = node['site'].get('longitude', None)
172 latitude = node['site'].get('latitude', None)
173 if longitude and latitude:
174 location_tag = etree.SubElement(node_tag, 'location', country="us", \
175 longitude=str(longitude), latitude=str(latitude))
178 def add_slivers(self, slivers, sliver_urn=None, no_dupes=False):
180 # all nodes hould already be present in the rspec. Remove all
181 # nodes that done have slivers
182 slivers = self._process_slivers(slivers)
183 sliver_hosts = [sliver['hostname'] for sliver in slivers]
184 nodes = self.get_node_elements()
186 urn = node.get('component_id')
187 hostname = xrn_to_hostname(urn)
188 if hostname not in sliver_hosts:
189 parent = node.getparent()
192 node.set('client_id', hostname)
194 node.set('sliver_id', sliver_urn)
195 from sfa.util.sfalogging import logger
197 for sliver in slivers:
199 logger.info(sliver['hostname'])
200 node_elem = self.get_node_element(sliver['hostname'])
201 sliver_elem = node_elem.xpath('//rspecv2:sliver_type | //sliver_type', namespaces=self.namespaces)
202 if isinstance(sliver_elem, list) and sliver_elem:
203 sliver_elem = sliver_elem[0]
204 for tag in sliver['tags']:
205 if tag['name'] == 'flack_info':
206 etree.SubElement(sliver_elem, 'flack:info', attrib=eval(tag['value']))
209 def add_default_sliver_attribute(self, name, value, network=None):
212 def add_interfaces(self, interfaces, no_dupes=False):
215 def add_links(self, links, no_dupes=False):
219 def merge(self, in_rspec):
221 Merge contents for specified rspec with current rspec
224 # just copy over all the child elements under the root element
225 tree = etree.parse(StringIO(in_rspec))
226 root = tree.getroot()
227 for child in root.getchildren():
228 self.xml.append(child)
231 # remove unncecessary elements, attributes
232 if self.type in ['request', 'manifest']:
233 # remove 'available' element from remaining node elements
234 self.remove_element('//rspecv2:available | //available')
236 if __name__ == '__main__':