rspecs/iotlab: fixed missing position element during RSpec parsing
[sfa.git] / sfa / rspecs / elements / versions / iotlabv1Node.py
1
2 from sfa.util.xrn import Xrn
3 from sfa.util.xml import XpathFilter
4 from sfa.rspecs.elements.node import NodeElement
5 from sfa.rspecs.elements.sliver import Sliver
6 from sfa.rspecs.elements.location import Location
7 from sfa.rspecs.elements.hardware_type import HardwareType
8 from sfa.rspecs.elements.element import Element
9 from sfa.rspecs.elements.interface import Interface
10 from sfa.rspecs.elements.versions.iotlabv1Sliver import Iotlabv1Sliver
11 from sfa.util.sfalogging import logger
12
13 class IotlabNode(NodeElement):
14     #First get the fields already defined in the class Node
15     fields = list(NodeElement.fields)
16     #Extend it with iotlab's specific fields
17     fields.extend (['archi', 'radio', 'mobile','position'])
18
19
20 class IotlabPosition(Element):
21     fields = ['posx', 'posy','posz']
22
23 class IotlabLocation(Location):
24     fields = list(Location.fields)
25     fields.extend (['site'])
26
27
28
29
30 class Iotlabv1Node:
31
32     @staticmethod
33     def add_connection_information(xml, ldap_username, sites_set):
34         """ Adds login and ssh connection info in the network item in
35         the xml. Does not create the network element, therefore
36         should be used after add_nodes, which creates the network item.
37
38         """
39         logger.debug(" add_connection_information " )
40         #Get network item in the xml
41         network_elems = xml.xpath('//network')
42         if len(network_elems) > 0:
43             network_elem = network_elems[0]
44
45         iotlab_network_dict = {}
46         iotlab_network_dict['login'] = ldap_username
47
48         iotlab_network_dict['ssh'] = \
49             ['ssh ' + ldap_username + '@'+site+'.iotlab.info' \
50             for site in sites_set]
51         network_elem.set('ssh', \
52                 unicode(iotlab_network_dict['ssh']))
53         network_elem.set('login', unicode( iotlab_network_dict['login']))
54
55
56     @staticmethod
57     def add_nodes(xml, nodes):
58         #Add network item in the xml
59         network_elems = xml.xpath('//network')
60         if len(network_elems) > 0:
61             network_elem = network_elems[0]
62         elif len(nodes) > 0 and nodes[0].get('component_manager_id'):
63             network_urn = nodes[0]['component_manager_id']
64             network_elem = xml.add_element('network', \
65                                         name = Xrn(network_urn).get_hrn())
66         else:
67             network_elem = xml
68
69         logger.debug("iotlabv1Node \t add_nodes  nodes %s \r\n "%(nodes[0]))
70         node_elems = []
71         #Then add nodes items to the network item in the xml
72         for node in nodes:
73             #Attach this node to the network element
74             node_fields = ['component_manager_id', 'component_id', 'exclusive',\
75                                                     'boot_state', 'mobile']
76             node_elem = network_elem.add_instance('node', node, node_fields)
77             node_elems.append(node_elem)
78
79             #Set the attibutes of this node element
80             for attribute in node:
81             # set component name
82                 if attribute is 'component_id':
83                     component_name = node['component_name']
84                     node_elem.set('component_name', component_name)
85
86             # set hardware types, extend fields to add Iotlab's architecture
87             #and radio type
88
89                 if attribute is 'hardware_types':
90                     for hardware_type in node.get('hardware_types', []):
91                         fields = HardwareType.fields
92                         fields.extend(['archi','radio'])
93                         node_elem.add_instance('hardware_types', node, fields)
94
95             # set location
96                 if attribute is 'location':
97                     node_elem.add_instance('location', node['location'], \
98                                                         IotlabLocation.fields)
99              # add granularity of the reservation system
100              #TODO put the granularity in network instead SA 18/07/12
101                 if attribute is 'granularity' :
102                     granularity = node['granularity']
103                     if granularity:
104                         node_elem.add_instance('granularity', \
105                                     granularity, granularity.fields)
106
107
108             # set available element
109                 if attribute is 'boot_state':
110                     if node.get('boot_state').lower() == 'alive':
111                         available_elem = node_elem.add_element('available', \
112                                                                     now='true')
113                     else:
114                         available_elem = node_elem.add_element('available', \
115                                                                 now='false')
116
117             #set position
118                 if attribute is 'position':
119                     node_elem.add_instance('position', node['position'], \
120                                                         IotlabPosition.fields)
121             ## add services
122             #PGv2Services.add_services(node_elem, node.get('services', []))
123             # add slivers
124                 if attribute is 'slivers':
125                     slivers = node.get('slivers', [])
126                     if not slivers:
127                     # we must still advertise the available sliver types
128                         slivers = Sliver({'type': 'iotlab-node'})
129                     # we must also advertise the available initscripts
130                     #slivers['tags'] = []
131                     #if node.get('pl_initscripts'):
132                         #for initscript in node.get('pl_initscripts', []):
133                             #slivers['tags'].append({'name': 'initscript', \
134                                                     #'value': initscript['name']})
135
136                     Iotlabv1Sliver.add_slivers(node_elem, slivers)
137         return node_elems
138
139
140
141     @staticmethod
142     def get_nodes(xml, filter={}):
143         xpath = '//node%s | //default:node%s' % (XpathFilter.xpath(filter), \
144                                                     XpathFilter.xpath(filter))
145         node_elems = xml.xpath(xpath)
146         return Iotlabv1Node.get_node_objs(node_elems)
147
148     @staticmethod
149     def get_nodes_with_slivers(xml, sliver_filter={}):
150
151         xpath = '//node[count(sliver)>0] | \
152                                 //default:node[count(default:sliver) > 0]'
153         node_elems = xml.xpath(xpath)
154         logger.debug("SLABV1NODE \tget_nodes_with_slivers  \
155                                 node_elems %s"%(node_elems))
156         return Iotlabv1Node.get_node_objs(node_elems)
157
158     @staticmethod
159     def get_node_objs(node_elems):
160         nodes = []
161         for node_elem in node_elems:
162             node = NodeElement(node_elem.attrib, node_elem)
163             nodes.append(node)
164             if 'component_id' in node_elem.attrib:
165                 node['authority_id'] = \
166                     Xrn(node_elem.attrib['component_id']).get_authority_urn()
167
168             # get hardware types
169             hardware_type_elems = node_elem.xpath('./default:hardware_type | \
170                                                             ./hardware_type')
171             node['hardware_types'] = [hw_type.get_instance(HardwareType) \
172                                             for hw_type in hardware_type_elems]
173
174             # get location
175             location_elems = node_elem.xpath('./default:location | ./location')
176             locations = [location_elem.get_instance(Location) \
177                                             for location_elem in location_elems]
178             if len(locations) > 0:
179                 node['location'] = locations[0]
180
181
182             # get interfaces
183             iface_elems = node_elem.xpath('./default:interface | ./interface')
184             node['interfaces'] = [iface_elem.get_instance(Interface) \
185                                             for iface_elem in iface_elems]
186
187             # get position
188             position_elems = node_elem.xpath('./default:position | ./position')
189             if position_elems:
190                 position_elem = position_elems[0]
191                 node['position'] = position_elem.get_instance(IotlabPosition)
192
193             # get services
194             #node['services'] = PGv2Services.get_services(node_elem)
195
196             # get slivers
197             node['slivers'] = Iotlabv1Sliver.get_slivers(node_elem)
198             available_elems = node_elem.xpath('./default:available | \
199                                                                 ./available')
200             if len(available_elems) > 0 and 'name' in available_elems[0].attrib:
201                 if available_elems[0].attrib.get('now', '').lower() == 'true':
202                     node['boot_state'] = 'boot'
203                 else:
204                     node['boot_state'] = 'disabled'
205
206         logger.debug("SLABV1NODE \tget_nodes_objs  \
207                                 #nodes %s"%(nodes))
208         return nodes
209
210
211     @staticmethod
212     def add_slivers(xml, slivers):
213         logger.debug("Iotlabv1NODE \tadd_slivers ")
214         component_ids = []
215         for sliver in slivers:
216             filter_sliver = {}
217             if isinstance(sliver, str):
218                 filter_sliver['component_id'] = '*%s*' % sliver
219                 sliver = {}
220             elif 'component_id' in sliver and sliver['component_id']:
221                 filter_sliver['component_id'] = '*%s*' % sliver['component_id']
222             if not filter_sliver:
223                 continue
224             nodes = Iotlabv1Node.get_nodes(xml, filter_sliver)
225             if not nodes:
226                 continue
227             node = nodes[0]
228             Iotlabv1Sliver.add_slivers(node, sliver)
229
230     @staticmethod
231     def remove_slivers(xml, hostnames):
232         for hostname in hostnames:
233             nodes = Iotlabv1Node.get_nodes(xml, \
234                                     {'component_id': '*%s*' % hostname})
235             for node in nodes:
236                 slivers = Iotlabv1Sliver.get_slivers(node.element)
237                 for sliver in slivers:
238                     node.element.remove(sliver.element)
239
240
241