Nitos: channels now have component_id (urn), which is used to address them into Lease...
[sfa.git] / sfa / rspecs / versions / iotlabv1.py
1 from copy import deepcopy
2
3
4 from sfa.rspecs.version import RSpecVersion
5 import sys
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
9
10
11 from sfa.rspecs.elements.versions.sfav1Lease import SFAv1Lease
12
13 from sfa.util.sfalogging import logger
14
15
16 class Iotlabv1(RSpecVersion):
17     """
18     Defines Iotlab style RSpec and associated methods to parse and create a
19     valid Iotlab XML Rspec.
20     """
21     #enabled = True
22     type = 'Iotlab'
23     content_type = 'ad'
24     version = '1'
25     #template = '<RSpec type="%s"></RSpec>' % type
26
27     schema = 'http://senslab.info/resources/rspec/1/ad.xsd'
28     namespace = 'http://www.geni.net/resources/rspec/3'
29     extensions = {
30         'flack': "http://www.protogeni.net/resources/rspec/ext/flack/1",
31         'planetlab': "http://www.planet-lab.org/resources/sfa/ext/planetlab/1",
32     }
33     namespaces = dict(extensions.items() + [('default', namespace)])
34     elements = []
35
36     # Network
37     def get_networks(self):
38         #WARNING Added //default:network to the xpath
39         #otherwise network element not detected 16/07/12 SA
40
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]
44         return networks
45
46     def add_network(self, network):
47         network_tags = self.xml.xpath('//network[@name="%s"]' % network)
48         if not network_tags:
49             network_tag = self.xml.add_element('network', name=network)
50         else:
51             network_tag = network_tags[0]
52         return network_tag
53
54     # Nodes
55     def get_nodes(self, filter=None):
56         return Iotlabv1Node.get_nodes(self.xml, filter)
57
58     def get_nodes_with_slivers(self):
59         return Iotlabv1Node.get_nodes_with_slivers(self.xml)
60
61     def get_slice_timeslot(self ):
62         return Iotlabv1Timeslot.get_slice_timeslot(self.xml)
63
64     def add_connection_information(self, ldap_username, sites_set):
65         return Iotlabv1Node.add_connection_information(self.xml,ldap_username, sites_set)
66
67     def add_nodes(self, nodes, check_for_dupes=False):
68         return Iotlabv1Node.add_nodes(self.xml,nodes )
69
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
74             #return
75         network_tag = self.add_network(network)
76         network_tag.append(deepcopy(source_node_tag))
77
78     # Slivers
79
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})
83         attribs = []
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:
86             node = nodes[0]
87         #if node :
88             #sliver = node.xpath('./default:sliver | ./sliver')
89             #sliver = node.xpath('./default:sliver', namespaces=self.namespaces)
90             sliver = node['slivers']
91
92             if sliver is not None and isinstance(sliver, list) and len(sliver) > 0:
93                 sliver = sliver[0]
94                 attribs = sliver
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)
97         return attribs
98
99     def get_slice_attributes(self, network=None):
100
101         slice_attributes = []
102
103         nodes_with_slivers = self.get_nodes_with_slivers()
104
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
114                 #if  'info' in name:
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']
121                     else:
122                         value = text
123                     attribute = {'name': 'initscript', 'value': value, 'node_id': node}
124                     slice_attributes.append(attribute)
125
126
127         return slice_attributes
128
129     def attributes_list(self, elem):
130         opts = []
131         if elem is not None:
132             for e in elem:
133                 opts.append((e.tag, str(e.text).strip(), e.attrib))
134         return opts
135
136     def get_default_sliver_attributes(self, network=None):
137         return []
138
139     def add_default_sliver_attribute(self, name, value, network=None):
140         pass
141
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})
148             if not node_elems:
149                 continue
150             node_elem = node_elems[0]
151
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']
160
161             if not requested_sliver_type:
162                 continue
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)
169
170             # remove interface elements
171             for interface_elem in node_elem.xpath('./default:interface | ./interface'):
172                 node_elem.remove(interface_elem)
173
174             # remove existing sliver_type elements
175             for sliver_type in node_elem.get('slivers', []):
176                 node_elem.element.remove(sliver_type.element)
177
178             # set the client id
179             node_elem.element.set('client_id', hostname)
180             if sliver_urn:
181                 pass
182                 # TODO
183                 # set the sliver id
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)
188
189             # add the sliver type elemnt
190             Iotlabv1Sliver.add_slivers(node_elem.element, sliver)
191             #Iotlabv1SliverType.add_slivers(node_elem.element, sliver)
192
193         # remove all nodes without slivers
194         if not append:
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)
199
200     def remove_slivers(self, slivers, network=None, no_dupes=False):
201         Iotlabv1Node.remove_slivers(self.xml, slivers)
202
203
204     # Utility
205
206     def merge(self, in_rspec):
207         """
208         Merge contents for specified rspec with current rspec
209         """
210
211         if not in_rspec:
212             return
213
214         from sfa.rspecs.rspec import RSpec
215
216         if isinstance(in_rspec, RSpec):
217             rspec = in_rspec
218         else:
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)
233
234
235
236
237
238     # Leases
239
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)
243
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)
247
248     def cleanup(self):
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')
253
254
255 class Iotlabv1Ad(Iotlabv1):
256     enabled = True
257     content_type = 'ad'
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"/>'
261
262 class Iotlabv1Request(Iotlabv1):
263     enabled = True
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"/>'
268
269 class Iotlabv1Manifest(Iotlabv1):
270     enabled = True
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"/>'
275
276
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)