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