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
7 from sfa.util.config import Config
8 from sfa.rspecs.rspec_version import RSpecVersion
11 _version = { 'type': 'SFA',
15 sfa_rspec_version = RSpecVersion(_version)
17 class SfaRSpec(RSpec):
19 header = '<?xml version="1.0"?>\n'
20 version = sfa_rspec_version
24 self.xml.set('type', 'SFA')
29 def get_network_elements(self):
30 return self.xml.xpath('//network')
32 def get_networks(self):
33 return self.xml.xpath('//network[@name]/@name')
35 def get_node_element(self, hostname, network=None):
37 names = self.xml.xpath('//network[@name="%s"]//node/hostname' % network)
39 names = self.xml.xpath('//node/hostname')
41 if name.text == hostname:
42 return name.getparent()
45 def get_node_elements(self, network=None):
47 return self.xml.xpath('//network[@name="%s"]//node' % network)
49 return self.xml.xpath('//node')
51 def get_nodes(self, network=None):
53 nodes = self.xml.xpath('//node/hostname/text()')
55 nodes = self.xml.xpath('//network[@name="%s"]//node/hostname/text()' % network)
57 nodes = [node.strip() for node in nodes]
60 def get_nodes_with_slivers(self, network = None):
62 nodes = self.xml.xpath('//network[@name="%s"]//node[sliver]/hostname/text()' % network)
64 nodes = self.xml.xpath('//node[sliver]/hostname/text()')
66 nodes = [node.strip() for node in nodes]
69 def get_nodes_without_slivers(self, network=None):
70 xpath_nodes_without_slivers = '//node[not(sliver)]/hostname/text()'
71 xpath_nodes_without_slivers_in_network = '//network[@name="%s"]//node[not(sliver)]/hostname/text()'
73 return self.xml.xpath('//network[@name="%s"]//node[not(sliver)]/hostname/text()' % network)
75 return self.xml.xpath('//node[not(sliver)]/hostname/text()')
78 def attributes_list(self, elem):
79 # convert a list of attribute tags into list of tuples
80 # (tagnme, text_value)
84 opts.append((e.tag, e.text))
87 def get_default_sliver_attributes(self, network=None):
89 defaults = self.xml.xpath("//network[@name='%s']/sliver_defaults" % network)
91 defaults = self.xml.xpath("//sliver_defaults")
92 if isinstance(defaults, list) and defaults:
93 defaults = defaults[0]
94 return self.attributes_list(defaults)
96 def get_sliver_attributes(self, hostname, network=None):
97 node = self.get_node_element(hostname, network)
98 sliver = node.find("sliver")
99 return self.attributes_list(sliver)
101 def get_slice_attributes(self, network=None):
102 slice_attributes = []
103 nodes_with_slivers = self.get_nodes_with_slivers(network)
104 for default_attribute in self.get_default_sliver_attributes(network):
105 attribute = {'name': str(default_attribute[0]), 'value': str(default_attribute[1]), 'node_id': None}
106 slice_attributes.append(attribute)
107 for node in nodes_with_slivers:
108 sliver_attributes = self.get_sliver_attributes(node, network)
109 for sliver_attribute in sliver_attributes:
110 attribute = {'name': str(sliver_attribute[0]), 'value': str(sliver_attribute[1]), 'node_id': node}
111 slice_attributes.append(attribute)
112 return slice_attributes
114 def get_site_nodes(self, siteid, network=None):
116 nodes = self.xml.xpath('//network[@name="%s"]/site[@id="%s"]/node/hostname/text()' % \
119 nodes = self.xml.xpath('//site[@id="%s"]/node/hostname/text()' % siteid)
122 def get_links(self, network=None):
124 links = self.xml.xpath('//network[@name="%s"]/link' % network)
126 links = self.xml.xpath('//link')
129 (end1, end2) = link.get("endpoints").split()
130 name = link.find("description")
131 linklist.append((name.text,
132 self.get_site_nodes(end1, network),
133 self.get_site_nodes(end2, network)))
136 def get_link(self, fromnode, tonode, network=None):
137 fromsite = fromnode.getparent()
138 tosite = tonode.getparent()
139 fromid = fromsite.get("id")
140 toid = tosite.get("id")
142 query = "//network[@name='%s']" % network + "/link[@endpoints = '%s %s']"
144 query = "//link[@endpoints = '%s %s']"
146 results = self.rspec.xpath(query % (fromid, toid))
148 results = self.rspec.xpath(query % (toid, fromid))
151 def query_links(self, fromnode, tonode, network=None):
152 return get_link(fromnode, tonode, network)
154 def get_vlinks(self, network=None):
157 vlinks = self.xml.xpath("//network[@name='%s']//vlink" % network)
159 vlinks = self.xml.xpath("//vlink")
161 endpoints = vlink.get("endpoints")
162 (end1, end2) = endpoints.split()
164 node1 = self.xml.xpath('//network[@name="%s"]//node[@id="%s"]/hostname/text()' % \
166 node2 = self.xml.xpath('//network[@name="%s"]//node[@id="%s"]/hostname/text()' % \
169 node1 = self.xml.xpath('//node[@id="%s"]/hostname/text()' % end1)[0]
170 node2 = self.xml.xpath('//node[@id="%s"]/hostname/text()' % end2)[0]
171 desc = "%s <--> %s" % (node1, node2)
172 kbps = vlink.find("kbps")
173 vlinklist.append((endpoints, desc, kbps.text))
176 def get_vlink(self, endponts, network=None):
178 query = "//network[@name='%s']//vlink[@endpoints = '%s']" % (network, endpoints)
180 query = "//vlink[@endpoints = '%s']" % (network, endpoints)
181 results = self.rspec.xpath(query)
184 def query_vlinks(self, endpoints, network=None):
185 return get_vlink(endpoints,network)
191 def add_network(self, network):
192 network_tag = etree.SubElement(self.xml, 'network', id=network)
194 def add_nodes(self, nodes, network = None, no_dupes=False):
195 if not isinstance(nodes, list):
199 self.get_node_element(node['hostname']):
200 # node already exists
203 network_tag = self.xml
204 if 'network' in node:
205 network = node['network']
206 network_tags = self.xml.xpath('//network[@name="%s"]' % network)
208 network_tag = etree.SubElement(self.xml, 'network', name=network)
210 network_tag = network_tags[0]
212 node_tag = etree.SubElement(network_tag, 'node')
213 if 'network' in node:
214 node_tag.set('component_manager_id', hrn_to_urn(network, 'authority+sa'))
216 node_tag.set('component_id', node['urn'])
217 if 'site_urn' in node:
218 node_tag.set('site_id', node['site_urn'])
219 if 'node_id' in node:
220 node_tag.set('node_id', 'n'+str(node['node_id']))
221 if 'hostname' in node:
222 hostname_tag = etree.SubElement(node_tag, 'hostname').text = node['hostname']
223 if 'interfaces' in node:
224 for interface in node['interfaces']:
225 if 'bwlimit' in interface and interface['bwlimit']:
226 bwlimit = etree.SubElement(node_tag, 'bw_limit', units='kbps').text = str(interface['bwlimit']/1000)
228 for tag in node['tags']:
229 # expose this hard wired list of tags, plus the ones that are marked 'sfa' in their category
230 if tag['tagname'] in ['fcdistro', 'arch'] or 'sfa' in tag['category'].split('/'):
231 tag_element = etree.SubElement(node_tag, tag['tagname']).text=tag['value']
234 longitude = str(node['site']['longitude'])
235 latitude = str(node['site']['latitude'])
236 location = etree.SubElement(node_tag, 'location', country='unknown', \
237 longitude=longitude, latitude=latitude)
239 def add_interfaces(self, interfaces):
242 def add_links(self, links):
245 def add_slivers(self, slivers, network=None, sliver_urn=None, no_dupes=False):
246 slivers = self._process_slivers(slivers)
247 nodes_with_slivers = self.get_nodes_with_slivers(network)
248 for sliver in slivers:
249 if sliver['hostname'] in nodes_with_slivers:
251 node_elem = self.get_node_element(sliver['hostname'], network)
252 sliver_elem = etree.SubElement(node_elem, 'sliver')
254 for tag in sliver['tags']:
255 etree.SubElement(sliver_elem, tag['tagname']).text = value=tag['value']
257 def remove_slivers(self, slivers, network=None, no_dupes=False):
258 slivers = self._process_slivers(slivers)
259 for sliver in slivers:
260 node_elem = self.get_node_element(sliver['hostname'], network)
261 sliver_elem = node_elem.find('sliver')
262 if sliver_elem != None:
263 node_elem.remove(sliver_elem)
265 def add_default_sliver_attribute(self, name, value, network=None):
267 defaults = self.xml.xpath("//network[@name='%s']/sliver_defaults" % network)
269 defaults = self.xml.xpath("//sliver_defaults" % network)
271 network_tag = self.xml.xpath("//network[@name='%s']" % network)
272 if isinstance(network_tag, list):
273 network_tag = network_tag[0]
274 defaults = self.add_element('sliver_defaults', attrs={}, parent=network_tag)
275 elif isinstance(defaults, list):
276 defaults = defaults[0]
277 self.add_attribute(defaults, name, value)
279 def add_sliver_attribute(self, hostname, name, value, network=None):
280 node = self.get_node_element(hostname, network)
281 sliver = node.find("sliver")
282 self.add_attribute(sliver, name, value)
284 def remove_default_sliver_attribute(self, name, value, network=None):
286 defaults = self.xml.xpath("//network[@name='%s']/sliver_defaults" % network)
288 defaults = self.xml.xpath("//sliver_defaults" % network)
289 self.remove_attribute(defaults, name, value)
291 def remove_sliver_attribute(self, hostname, name, value, network=None):
292 node = self.get_node_element(hostname, network)
293 sliver = node.find("sliver")
294 self.remove_attribute(sliver, name, value)
296 def add_vlink(self, fromhost, tohost, kbps, network=None):
297 fromnode = self.get_node_element(fromhost, network)
298 tonode = self.get_node_element(tohost, network)
299 links = self.get_link(fromnode, tonode, network)
302 vlink = etree.SubElement(link, "vlink")
303 fromid = fromnode.get("id")
304 toid = tonode.get("id")
305 vlink.set("endpoints", "%s %s" % (fromid, toid))
306 self.add_attribute(vlink, "kbps", kbps)
309 def remove_vlink(self, endpoints, network=None):
310 vlinks = self.query_vlinks(endpoints, network)
312 vlink.getparent().remove(vlink)
315 def merge(self, in_rspec):
317 Merge contents for specified rspec with current rspec
320 from sfa.rspecs.rspec_parser import parse_rspec
321 rspec = parse_rspec(in_rspec)
322 if rspec.type.lower() == 'protogeni':
323 from sfa.rspecs.rspec_converter import RSpecConverter
324 in_rspec = RSpecConverter.to_sfa_rspec(in_rspec)
326 # just copy over all networks
327 current_networks = self.get_networks()
328 rspec = SfaRSpec(rspec=in_rspec)
329 networks = rspec.get_network_elements()
330 for network in networks:
331 current_network = network.get('name')
332 if not current_network in current_networks:
333 self.xml.append(network)
334 current_networks.append(current_network)
339 if __name__ == '__main__':
343 'hostname': 'node1.planet-lab.org',
344 'site_urn': 'urn:publicid:IDN+plc+authority+cm',
348 'hostname': 'node2.planet-lab.org',
349 'site_urn': 'urn:publicid:IDN+plc+authority+cm',
353 'hostname': 'node1.planet-lab.eu',
354 'site_urn': 'urn:publicid:IDN+plc+authority+cm',
358 rspec.add_nodes(nodes)