1 from copy import deepcopy
4 from sfa.rspecs.version import RSpecVersion
6 # from sfa.rspecs.elements.versions.iotlabv1Lease import Iotlabv1Lease
7 from sfa.rspecs.elements.versions.iotlabv1Node import Iotlabv1Node
8 from sfa.rspecs.elements.versions.iotlabv1Sliver import Iotlabv1Sliver
11 from sfa.rspecs.elements.versions.sfav1Lease import SFAv1Lease
13 from sfa.util.sfalogging import logger
16 class Iotlabv1(RSpecVersion):
18 Defines Iotlab style RSpec and associated methods to parse and create a
19 valid Iotlab XML Rspec.
25 #template = '<RSpec type="%s"></RSpec>' % type
27 schema = 'http://senslab.info/resources/rspec/1/ad.xsd'
28 namespace = 'http://www.geni.net/resources/rspec/3'
30 'flack': "http://www.protogeni.net/resources/rspec/ext/flack/1",
31 'planetlab': "http://www.planet-lab.org/resources/sfa/ext/planetlab/1",
33 namespaces = dict(extensions.items() + [('default', namespace)])
37 def get_networks(self):
38 #WARNING Added //default:network to the xpath
39 #otherwise network element not detected 16/07/12 SA
41 network_elems = self.xml.xpath('//network | //default:network')
42 networks = [network_elem.get_instance(fields=['name', 'slice']) for \
43 network_elem in network_elems]
46 def add_network(self, network):
47 network_tags = self.xml.xpath('//network[@name="%s"]' % network)
49 network_tag = self.xml.add_element('network', name=network)
51 network_tag = network_tags[0]
55 def get_nodes(self, filter=None):
56 return Iotlabv1Node.get_nodes(self.xml, filter)
58 def get_nodes_with_slivers(self):
59 return Iotlabv1Node.get_nodes_with_slivers(self.xml)
61 def get_slice_timeslot(self ):
62 return Iotlabv1Timeslot.get_slice_timeslot(self.xml)
64 def add_connection_information(self, ldap_username, sites_set):
65 return Iotlabv1Node.add_connection_information(self.xml,ldap_username, sites_set)
67 def add_nodes(self, nodes, check_for_dupes=False):
68 return Iotlabv1Node.add_nodes(self.xml,nodes )
70 def merge_node(self, source_node_tag, network, no_dupes = False):
71 logger.debug("SLABV1 merge_node")
72 #if no_dupes and self.get_node_element(node['hostname']):
73 ## node already exists
75 network_tag = self.add_network(network)
76 network_tag.append(deepcopy(source_node_tag))
80 def get_sliver_attributes(self, hostname, node, network=None):
81 print>>sys.stderr, "\r\n \r\n \r\n \t\t SLABV1.PY get_sliver_attributes hostname %s " %(hostname)
82 nodes = self.get_nodes({'component_id': '*%s*' %hostname})
84 print>>sys.stderr, "\r\n \r\n \r\n \t\t SLABV1.PY get_sliver_attributes-----------------nodes %s " %(nodes)
85 if nodes is not None and isinstance(nodes, list) and len(nodes) > 0:
88 #sliver = node.xpath('./default:sliver | ./sliver')
89 #sliver = node.xpath('./default:sliver', namespaces=self.namespaces)
90 sliver = node['slivers']
92 if sliver is not None and isinstance(sliver, list) and len(sliver) > 0:
95 #attribs = self.attributes_list(sliver)
96 print>>sys.stderr, "\r\n \r\n \r\n \t\t SLABV1.PY get_sliver_attributes----------NN------- sliver %s self.namespaces %s attribs %s " %(sliver, self.namespaces,attribs)
99 def get_slice_attributes(self, network=None):
101 slice_attributes = []
103 nodes_with_slivers = self.get_nodes_with_slivers()
105 # TODO: default sliver attributes in the PG rspec?
106 default_ns_prefix = self.namespaces['default']
107 for node in nodes_with_slivers:
108 sliver_attributes = self.get_sliver_attributes(node['component_id'],node, network)
109 for sliver_attribute in sliver_attributes:
110 name = str(sliver_attribute[0])
111 text = str(sliver_attribute[1])
112 attribs = sliver_attribute[2]
113 # we currently only suppor the <initscript> and <flack> attributes
115 #attribute = {'name': 'flack_info', 'value': str(attribs), 'node_id': node}
116 #slice_attributes.append(attribute)
117 #elif 'initscript' in name:
118 if 'initscript' in name:
119 if attribs is not None and 'name' in attribs:
120 value = attribs['name']
123 attribute = {'name': 'initscript', 'value': value, 'node_id': node}
124 slice_attributes.append(attribute)
127 return slice_attributes
129 def attributes_list(self, elem):
133 opts.append((e.tag, str(e.text).strip(), e.attrib))
136 def get_default_sliver_attributes(self, network=None):
139 def add_default_sliver_attribute(self, name, value, network=None):
142 def add_slivers(self, hostnames, attributes=[], sliver_urn=None, append=False):
143 # all nodes hould already be present in the rspec. Remove all
144 # nodes that done have slivers
145 print>>sys.stderr, "\r\n \r\n \r\n \t\t\t Iotlabv1.PY add_slivers ----->get_node "
146 for hostname in hostnames:
147 node_elems = self.get_nodes({'component_id': '*%s*' % hostname})
150 node_elem = node_elems[0]
152 # determine sliver types for this node
153 #TODO : add_slivers valid type of sliver needs to be changed 13/07/12 SA
154 valid_sliver_types = ['iotlab-node', 'emulab-openvz', 'raw-pc', 'plab-vserver', 'plab-vnode']
155 #valid_sliver_types = ['emulab-openvz', 'raw-pc', 'plab-vserver', 'plab-vnode']
156 requested_sliver_type = None
157 for sliver_type in node_elem.get('slivers', []):
158 if sliver_type.get('type') in valid_sliver_types:
159 requested_sliver_type = sliver_type['type']
161 if not requested_sliver_type:
163 sliver = {'type': requested_sliver_type,
164 'pl_tags': attributes}
165 print>>sys.stderr, "\r\n \r\n \r\n \t\t\t Iotlabv1.PY add_slivers node_elem %s sliver_type %s \r\n \r\n " %(node_elem, sliver_type)
166 # remove available element
167 for available_elem in node_elem.xpath('./default:available | ./available'):
168 node_elem.remove(available_elem)
170 # remove interface elements
171 for interface_elem in node_elem.xpath('./default:interface | ./interface'):
172 node_elem.remove(interface_elem)
174 # remove existing sliver_type elements
175 for sliver_type in node_elem.get('slivers', []):
176 node_elem.element.remove(sliver_type.element)
179 node_elem.element.set('client_id', hostname)
184 #slice_id = sliver_info.get('slice_id', -1)
185 #node_id = sliver_info.get('node_id', -1)
186 #sliver_id = urn_to_sliver_id(sliver_urn, slice_id, node_id)
187 #node_elem.set('sliver_id', sliver_id)
189 # add the sliver type elemnt
190 Iotlabv1Sliver.add_slivers(node_elem.element, sliver)
191 #Iotlabv1SliverType.add_slivers(node_elem.element, sliver)
193 # remove all nodes without slivers
195 for node_elem in self.get_nodes():
196 if not node_elem['client_id']:
197 parent = node_elem.element.getparent()
198 parent.remove(node_elem.element)
200 def remove_slivers(self, slivers, network=None, no_dupes=False):
201 Iotlabv1Node.remove_slivers(self.xml, slivers)
206 def merge(self, in_rspec):
208 Merge contents for specified rspec with current rspec
214 from sfa.rspecs.rspec import RSpec
216 if isinstance(in_rspec, RSpec):
219 rspec = RSpec(in_rspec)
220 if rspec.version.type.lower() == 'protogeni':
221 from sfa.rspecs.rspec_converter import RSpecConverter
222 in_rspec = RSpecConverter.to_sfa_rspec(rspec.toxml())
223 rspec = RSpec(in_rspec)
224 # just copy over all networks
225 #Attention special get_networks using //default:network xpath
226 current_networks = self.get_networks()
227 networks = rspec.version.get_networks()
228 for network in networks:
229 current_network = network.get('name')
230 if current_network and current_network not in current_networks:
231 self.xml.append(network.element)
232 current_networks.append(current_network)
240 def get_leases(self, lease_filter=None):
241 return SFAv1Lease.get_leases(self.xml, lease_filter)
242 #return Iotlabv1Lease.get_leases(self.xml, lease_filter)
244 def add_leases(self, leases, network = None, no_dupes=False):
245 SFAv1Lease.add_leases(self.xml, leases)
246 #Iotlabv1Lease.add_leases(self.xml, leases)
249 # remove unncecessary elements, attributes
250 if self.type in ['request', 'manifest']:
251 # remove 'available' element from remaining node elements
252 self.xml.remove_element('//default:available | //available')
255 class Iotlabv1Ad(Iotlabv1):
258 schema = 'http://senslab.info/resources/rspec/1/ad.xsd'
259 #http://www.geni.net/resources/rspec/3/ad.xsd'
260 template = '<rspec type="advertisement" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://senslab.info/resources/rspec/1" xmlns:flack="http://senslab.info/resources/rspec/ext/flack/1" xmlns:planetlab="http://www.planet-lab.org/resources/sfa/ext/planetlab/1" xsi:schemaLocation="http://senslab.info/resources/rspec/1 http://senslab.info/resources/rspec/1/ad.xsd http://www.planet-lab.org/resources/sfa/ext/planetlab/1 http://www.planet-lab.org/resources/sfa/ext/planetlab/1/planetlab.xsd"/>'
262 class Iotlabv1Request(Iotlabv1):
264 content_type = 'request'
265 schema = 'http://senslab.info/resources/rspec/1/request.xsd'
266 #http://www.geni.net/resources/rspec/3/request.xsd
267 template = '<rspec type="request" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://senslab.info/resources/rspec/1" xmlns:flack="http://senslab.info/resources/rspec/ext/flack/1" xmlns:planetlab="http://www.planet-lab.org/resources/sfa/ext/planetlab/1" xsi:schemaLocation="http://senslab.info/resources/rspec/1 http://senslab.info/resources/rspec/1/request.xsd http://www.planet-lab.org/resources/sfa/ext/planetlab/1 http://www.planet-lab.org/resources/sfa/ext/planetlab/1/planetlab.xsd"/>'
269 class Iotlabv1Manifest(Iotlabv1):
271 content_type = 'manifest'
272 schema = 'http://senslab.info/resources/rspec/1/manifest.xsd'
273 #http://www.geni.net/resources/rspec/3/manifest.xsd
274 template = '<rspec type="manifest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://senslab.info/resources/rspec/1" xmlns:flack="http://senslab.info/resources/rspec/ext/flack/1" xmlns:planetlab="http://www.planet-lab.org/resources/sfa/ext/planetlab/1" xsi:schemaLocation="http://senslab.info/resources/rspec/1 http://senslab.info/resources/rspec/1/manifest.xsd http://www.planet-lab.org/resources/sfa/ext/planetlab/1 http://www.planet-lab.org/resources/sfa/ext/planetlab/1/planetlab.xsd"/>'
277 if __name__ == '__main__':
278 from sfa.rspecs.rspec import RSpec
279 from sfa.rspecs.rspec_elements import *
280 r = RSpec('/tmp/iotlab.rspec')
281 r.load_rspec_elements(Iotlabv1.elements)
282 r.namespaces = Iotlabv1.namespaces
283 print r.get(RSpecElements.NODE)