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