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
10 _version = {'type': 'protogeni',
12 'schema': 'http://www.protogeni.net/resources/rspec/2/request.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'
19 pg_rspec_version = RSpecVersion(_version)
23 header = '<?xml version="1.0"?>\n'
24 template = """<rspec xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.protogeni.net/resources/rspec/0.2" xsi:schemaLocation="http://www.protogeni.net/resources/rspec/0.2 http://www.protogeni.net/resources/rspec/0.2/ad.xsd"></rspec>"""
25 version = pg_rspec_version
26 namespaces = {'rspecv2': version['namespace']}
28 def get_network(self):
30 nodes = self.xml.xpath('//rspecv2:node[@component_manager_uuid][1]', namespaces=self.namespaces)
32 network = nodes[0].get('component_manager_uuid')
35 def get_networks(self):
36 networks = self.xml.xpath('//rspecv2:node[@component_manager_uuid]/@component_manager_uuid', namespaces=self.namespaces)
39 def get_node_elements(self):
40 nodes = self.xml.xpath('//rspecv2:node', namespaces=self.namespaces)
43 def get_nodes(self, network=None):
44 return self.xml.xpath('//rspecv2:node[@component_name]/@component_name', namespaces=self.namespaces)
46 def get_nodes_with_slivers(self, network=None):
48 return self.xml.xpath('//rspecv2:node[@component_manager_id="%s"][sliver_type]/@component_name' % network, namespaces=self.namespaces)
50 return self.xml.xpath('//rspecv2:node[rspecv2:sliver_type]/@component_name', namespaces=self.namespaces)
52 def get_nodes_without_slivers(self, network=None):
55 def add_nodes(self, nodes, check_for_dupes=False):
56 if not isinstance(nodes, list):
60 if check_for_dupes and \
61 self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, namespaces=self.namespaces):
65 node_tag = etree.SubElement(self.xml, 'node')
66 if 'network_urn' in node:
67 node_tag.set('component_manager_id', node['network_urn'])
69 node_tag.set('component_id', node['urn'])
70 if 'hostname' in node:
71 node_tag.set('component_name', node['hostname'])
72 node_type_tag = etree.SubElement(node_tag, 'node_type', type_name='pcvm', type_slots='100')
73 available_tag = etree.SubElement(node_tag, 'available').text = 'true'
74 exclusive_tag = etree.SubElement(node_tag, 'exclusive').text = 'false'
75 location_tag = etree.SubElement(node_tag, 'location', location="US")
77 if 'longitude' in node['site']:
78 location_tag.set('longitude', str(node['site']['longitude']))
79 if 'latitude' in node['site']:
80 location_tag.set('latitude', str(node['site']['latitude']))
81 #if 'interfaces' in node:
84 def add_slivers(self, slivers, check_for_dupes=False):
85 if not isinstance(slivers, list):
88 nodes_with_slivers = self.get_nodes_with_slivers()
89 for sliver in slivers:
90 hostname = sliver['hostname']
91 if hostname in nodes_with_slivers:
93 nodes = self.xml.xpath('//rspecv2:node[@component_name="%s"] | //node[@component_name="%s"]' % (hostname, hostname), namespaces=self.namespaces)
96 node.set('client_id', hostname)
97 etree.SubElement(node, 'sliver_type', name='planetlab-vnode')
99 def add_interfaces(self, interfaces, check_for_dupes=False):
102 def add_links(self, links, check_for_dupes=False):
106 def merge(self, in_rspec):
108 Merge contents for specified rspec with current rspec
111 # just copy over all the child elements under the root element
112 tree = etree.parse(StringIO(in_rspec))
113 root = tree.getroot()
114 for child in root.getchildren():
115 self.xml.append(child)
119 if __name__ == '__main__':