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
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'])
20 class IotlabPosition(Element):
21 fields = ['posx', 'posy','posz']
23 class IotlabLocation(Location):
24 fields = list(Location.fields)
25 fields.extend (['site'])
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.
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]
45 iotlab_network_dict = {}
46 iotlab_network_dict['login'] = ldap_username
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']))
57 def add_nodes(xml, nodes, rspec_content_type=None):
58 """Adds the nodes to the xml.
60 Adds the nodes as well as dedicated iotlab fields to the node xml
63 :param xml: the xml being constructed.
65 :param nodes: list of node dict
67 :returns: a list of node elements.
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())
83 logger.debug("iotlabv1Node \t add_nodes nodes %s \r\n "%(nodes[0]))
85 #Then add nodes items to the network item in the xml
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)
93 #Set the attibutes of this node element
94 for attribute in node:
96 if attribute is 'component_id':
97 component_name = node['component_name']
98 node_elem.set('component_name', component_name)
100 # set hardware types, extend fields to add Iotlab's architecture
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)
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']
118 node_elem.add_instance('granularity', \
119 granularity, granularity.fields)
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', \
128 available_elem = node_elem.add_element('available', \
132 if attribute is 'position':
133 node_elem.add_instance('position', node['position'], \
134 IotlabPosition.fields)
136 #PGv2Services.add_services(node_elem, node.get('services', []))
138 if attribute is 'slivers':
139 slivers = node.get('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']})
150 Iotlabv1Sliver.add_slivers(node_elem, slivers)
152 # add sliver tag in Request Rspec
153 if rspec_content_type == "request":
154 node_elem.add_instance('sliver', '', [])
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)
168 def get_nodes_with_slivers(xml, sliver_filter={}):
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)
178 def get_node_objs(node_elems):
180 for node_elem in node_elems:
181 node = NodeElement(node_elem.attrib, node_elem)
183 if 'component_id' in node_elem.attrib:
184 node['authority_id'] = \
185 Xrn(node_elem.attrib['component_id']).get_authority_urn()
188 hardware_type_elems = node_elem.xpath('./default:hardware_type | \
190 node['hardware_types'] = [hw_type.get_instance(HardwareType) \
191 for hw_type in hardware_type_elems]
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]
202 iface_elems = node_elem.xpath('./default:interface | ./interface')
203 node['interfaces'] = [iface_elem.get_instance(Interface) \
204 for iface_elem in iface_elems]
207 position_elems = node_elem.xpath('./default:position | ./position')
209 position_elem = position_elems[0]
210 node['position'] = position_elem.get_instance(IotlabPosition)
213 #node['services'] = PGv2Services.get_services(node_elem)
216 node['slivers'] = Iotlabv1Sliver.get_slivers(node_elem)
217 available_elems = node_elem.xpath('./default: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'
223 node['boot_state'] = 'disabled'
225 logger.debug("SLABV1NODE \tget_nodes_objs \
231 def add_slivers(xml, slivers):
232 logger.debug("Iotlabv1NODE \tadd_slivers ")
234 for sliver in slivers:
236 if isinstance(sliver, str):
237 filter_sliver['component_id'] = '*%s*' % 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:
243 nodes = Iotlabv1Node.get_nodes(xml, filter_sliver)
247 Iotlabv1Sliver.add_slivers(node, sliver)
250 def remove_slivers(xml, hostnames):
251 for hostname in hostnames:
252 nodes = Iotlabv1Node.get_nodes(xml, \
253 {'component_id': '*%s*' % hostname})
255 slivers = Iotlabv1Sliver.get_slivers(node.element)
256 for sliver in slivers:
257 node.element.remove(sliver.element)