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 _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_elements(self):
78 nodes = self.xml.xpath('//rspecv2:node | //node', namespaces=self.namespaces)
81 def get_nodes(self, network=None):
82 xpath = '//rspecv2:node[@component_name]/@component_name | //node[@component_name]/@component_name'
83 return self.xml.xpath(xpath, namespaces=self.namespaces)
85 def get_nodes_with_slivers(self, network=None):
87 return self.xml.xpath('//rspecv2:node[@component_manager_id="%s"][sliver_type]/@component_name' % network, namespaces=self.namespaces)
89 return self.xml.xpath('//rspecv2:node[rspecv2:sliver_type]/@component_name', namespaces=self.namespaces)
91 def get_nodes_without_slivers(self, network=None):
94 def get_slice_attributes(self, network=None):
97 def get_default_sliver_attributes(self, network=None):
100 def add_default_sliver_attribute(self, name, value, network=None):
103 def add_nodes(self, nodes, check_for_dupes=False):
104 if not isinstance(nodes, list):
108 if check_for_dupes and \
109 self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, namespaces=self.namespaces):
110 # node already exists
113 node_tag = etree.SubElement(self.xml, 'node', exclusive='false')
114 if 'network_urn' in node:
115 node_tag.set('component_manager_id', node['network_urn'])
117 node_tag.set('component_id', node['urn'])
118 if 'hostname' in node:
119 node_tag.set('component_name', node['hostname'])
120 # TODO: should replace plab-pc with pc model
121 node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='plab-pc')
122 node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='pc')
123 available_tag = etree.SubElement(node_tag, 'available', now='true')
125 # only add location tag if longitude and latitude are not null
127 longitude = node['site'].get('longitude', None)
128 latitude = node['site'].get('latitude', None)
129 if longitude and latitude:
130 location_tag = etree.SubElement(node_tag, 'location', country="us", \
131 longitude=longitude, latitude=latitude)
134 def add_slivers(self, slivers, sliver_urn=None, no_dupes=False):
135 slivers = self._process_slivers(slivers)
136 nodes_with_slivers = self.get_nodes_with_slivers()
137 for sliver in slivers:
138 hostname = sliver['hostname']
139 if hostname in nodes_with_slivers:
141 nodes = self.xml.xpath('//rspecv2:node[@component_name="%s"] | //node[@component_name="%s"]' % (hostname, hostname), namespaces=self.namespaces)
144 node.set('client_id', hostname)
146 node.set('sliver_id', sliver_urn)
147 etree.SubElement(node, 'sliver_type', name='plab-vnode')
149 def add_default_sliver_attribute(self, name, value, network=None):
152 def add_interfaces(self, interfaces, no_dupes=False):
155 def add_links(self, links, no_dupes=False):
159 def merge(self, in_rspec):
161 Merge contents for specified rspec with current rspec
164 # just copy over all the child elements under the root element
165 tree = etree.parse(StringIO(in_rspec))
166 root = tree.getroot()
167 for child in root.getchildren():
168 self.xml.append(child)
171 # remove unncecessary elements, attributes
172 if self.type in ['request', 'manifest']:
173 # remove nodes without slivers
174 nodes = self.get_node_elements()
177 hostname = node.get('component_name')
178 parent = node.getparent()
179 children = node.getchildren()
180 for child in children:
181 if child.tag.endswith('sliver_type'):
186 # remove 'available' element from remaining node elements
187 self.remove_element('//rspecv2:available | //available')
189 if __name__ == '__main__':