rspec.version.add_nodes take an extra arg to handle Request RSpec
[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, rspec_content_type=None):
58         """Adds the nodes to the xml.
59
60         Adds the nodes as well as dedicated iotlab fields to the node xml
61         element.
62
63         :param xml: the xml being constructed.
64         :type xml: xml
65         :param nodes: list of node dict
66         :type nodes: list
67         :returns: a list of node elements.
68         :rtype: list
69
70         """
71 >>>>>>> f7d277c... rspec.version.add_nodes take an extra arg to handle Request RSpec
72         #Add network item in the xml
73         network_elems = xml.xpath('//network')
74         if len(network_elems) > 0:
75             network_elem = network_elems[0]
76         elif len(nodes) > 0 and nodes[0].get('component_manager_id'):
77             network_urn = nodes[0]['component_manager_id']
78             network_elem = xml.add_element('network', \
79                                         name = Xrn(network_urn).get_hrn())
80         else:
81             network_elem = xml
82
83         logger.debug("iotlabv1Node \t add_nodes  nodes %s \r\n "%(nodes[0]))
84         node_elems = []
85         #Then add nodes items to the network item in the xml
86         for node in nodes:
87             #Attach this node to the network element
88             node_fields = ['component_manager_id', 'component_id', 'exclusive',\
89                                                     'boot_state', 'mobile']
90             node_elem = network_elem.add_instance('node', node, node_fields)
91             node_elems.append(node_elem)
92
93             #Set the attibutes of this node element
94             for attribute in node:
95             # set component name
96                 if attribute is 'component_id':
97                     component_name = node['component_name']
98                     node_elem.set('component_name', component_name)
99
100             # set hardware types, extend fields to add Iotlab's architecture
101             #and radio type
102
103                 if attribute is 'hardware_types':
104                     for hardware_type in node.get('hardware_types', []):
105                         fields = HardwareType.fields
106                         fields.extend(['archi','radio'])
107                         node_elem.add_instance('hardware_types', node, fields)
108
109             # set location
110                 if attribute is 'location':
111                     node_elem.add_instance('location', node['location'], \
112                                                         IotlabLocation.fields)
113              # add granularity of the reservation system
114              #TODO put the granularity in network instead SA 18/07/12
115                 if attribute is 'granularity' :
116                     granularity = node['granularity']
117                     if granularity:
118                         node_elem.add_instance('granularity', \
119                                     granularity, granularity.fields)
120
121
122             # set available element
123                 if attribute is 'boot_state':
124                     if node.get('boot_state').lower() == 'alive':
125                         available_elem = node_elem.add_element('available', \
126                                                                     now='true')
127                     else:
128                         available_elem = node_elem.add_element('available', \
129                                                                 now='false')
130
131             #set position
132                 if attribute is 'position':
133                     node_elem.add_instance('position', node['position'], \
134                                                         IotlabPosition.fields)
135             ## add services
136             #PGv2Services.add_services(node_elem, node.get('services', []))
137             # add slivers
138                 if attribute is 'slivers':
139                     slivers = node.get('slivers', [])
140                     if not slivers:
141                     # we must still advertise the available sliver types
142                         slivers = Sliver({'type': 'iotlab-node'})
143                     # we must also advertise the available initscripts
144                     #slivers['tags'] = []
145                     #if node.get('pl_initscripts'):
146                         #for initscript in node.get('pl_initscripts', []):
147                             #slivers['tags'].append({'name': 'initscript', \
148                                                     #'value': initscript['name']})
149
150                     Iotlabv1Sliver.add_slivers(node_elem, slivers)
151             
152             # add sliver tag in Request Rspec
153             if rspec_content_type == "request":
154                 node_elem.add_instance('sliver', '', [])
155
156         return node_elems
157
158
159
160     @staticmethod
161     def get_nodes(xml, filter={}):
162         xpath = '//node%s | //default:node%s' % (XpathFilter.xpath(filter), \
163                                                     XpathFilter.xpath(filter))
164         node_elems = xml.xpath(xpath)
165         return Iotlabv1Node.get_node_objs(node_elems)
166
167     @staticmethod
168     def get_nodes_with_slivers(xml, sliver_filter={}):
169
170         xpath = '//node[count(sliver)>0] | \
171                                 //default:node[count(default:sliver) > 0]'
172         node_elems = xml.xpath(xpath)
173         logger.debug("SLABV1NODE \tget_nodes_with_slivers  \
174                                 node_elems %s"%(node_elems))
175         return Iotlabv1Node.get_node_objs(node_elems)
176
177     @staticmethod
178     def get_node_objs(node_elems):
179         nodes = []
180         for node_elem in node_elems:
181             node = NodeElement(node_elem.attrib, node_elem)
182             nodes.append(node)
183             if 'component_id' in node_elem.attrib:
184                 node['authority_id'] = \
185                     Xrn(node_elem.attrib['component_id']).get_authority_urn()
186
187             # get hardware types
188             hardware_type_elems = node_elem.xpath('./default:hardware_type | \
189                                                             ./hardware_type')
190             node['hardware_types'] = [hw_type.get_instance(HardwareType) \
191                                             for hw_type in hardware_type_elems]
192
193             # get location
194             location_elems = node_elem.xpath('./default:location | ./location')
195             locations = [location_elem.get_instance(Location) \
196                                             for location_elem in location_elems]
197             if len(locations) > 0:
198                 node['location'] = locations[0]
199
200
201             # get interfaces
202             iface_elems = node_elem.xpath('./default:interface | ./interface')
203             node['interfaces'] = [iface_elem.get_instance(Interface) \
204                                             for iface_elem in iface_elems]
205
206             # get position
207             position_elems = node_elem.xpath('./default:position | ./position')
208             if position_elems:
209                 position_elem = position_elems[0]
210                 node['position'] = position_elem.get_instance(IotlabPosition)
211
212             # get services
213             #node['services'] = PGv2Services.get_services(node_elem)
214
215             # get slivers
216             node['slivers'] = Iotlabv1Sliver.get_slivers(node_elem)
217             available_elems = node_elem.xpath('./default:available | \
218                                                                 ./available')
219             if len(available_elems) > 0 and 'name' in available_elems[0].attrib:
220                 if available_elems[0].attrib.get('now', '').lower() == 'true':
221                     node['boot_state'] = 'boot'
222                 else:
223                     node['boot_state'] = 'disabled'
224
225         logger.debug("SLABV1NODE \tget_nodes_objs  \
226                                 #nodes %s"%(nodes))
227         return nodes
228
229
230     @staticmethod
231     def add_slivers(xml, slivers):
232         logger.debug("Iotlabv1NODE \tadd_slivers ")
233         component_ids = []
234         for sliver in slivers:
235             filter_sliver = {}
236             if isinstance(sliver, str):
237                 filter_sliver['component_id'] = '*%s*' % sliver
238                 sliver = {}
239             elif 'component_id' in sliver and sliver['component_id']:
240                 filter_sliver['component_id'] = '*%s*' % sliver['component_id']
241             if not filter_sliver:
242                 continue
243             nodes = Iotlabv1Node.get_nodes(xml, filter_sliver)
244             if not nodes:
245                 continue
246             node = nodes[0]
247             Iotlabv1Sliver.add_slivers(node, sliver)
248
249     @staticmethod
250     def remove_slivers(xml, hostnames):
251         for hostname in hostnames:
252             nodes = Iotlabv1Node.get_nodes(xml, \
253                                     {'component_id': '*%s*' % hostname})
254             for node in nodes:
255                 slivers = Iotlabv1Sliver.get_slivers(node.element)
256                 for sliver in slivers:
257                     node.element.remove(sliver.element)
258
259
260