0154214d6e107cf3cc66e3542a8fcea614eeba80
[sfa.git] / sfa / rspecs / elements / versions / nitosv1Node.py
1 from sfa.util.sfalogging import logger
2 from sfa.util.xml import XpathFilter
3 from sfa.util.xrn import Xrn
4
5 from sfa.rspecs.elements.element import Element
6 from sfa.rspecs.elements.node import Node
7 from sfa.rspecs.elements.sliver import Sliver
8 from sfa.rspecs.elements.location import Location
9 from sfa.rspecs.elements.position_3d import Position3D
10 from sfa.rspecs.elements.hardware_type import HardwareType
11 from sfa.rspecs.elements.disk_image import DiskImage
12 from sfa.rspecs.elements.interface import Interface
13 from sfa.rspecs.elements.bwlimit import BWlimit
14 from sfa.rspecs.elements.pltag import PLTag
15 from sfa.rspecs.elements.versions.nitosv1Sliver import NITOSv1Sliver
16 from sfa.rspecs.elements.versions.nitosv1PLTag import NITOSv1PLTag
17 from sfa.rspecs.elements.versions.pgv2Services import PGv2Services
18
19
20 class NITOSv1Node:
21
22     @staticmethod
23     def add_nodes(xml, nodes):
24         network_elems = xml.xpath('//network')
25         if len(network_elems) > 0:
26             network_elem = network_elems[0]
27         elif len(nodes) > 0 and nodes[0].get('component_manager_id'):
28             network_urn = nodes[0]['component_manager_id']
29             network_elem = xml.add_element('network', name = Xrn(network_urn).get_hrn())
30         else:
31             network_elem = xml
32
33         node_elems = []       
34         for node in nodes:
35             node_fields = ['component_manager_id', 'component_id', 'boot_state']
36             node_elem = network_elem.add_instance('node', node, node_fields)
37             node_elems.append(node_elem)
38
39             # determine network hrn
40             network_hrn = None 
41             if 'component_manager_id' in node and node['component_manager_id']:
42                 network_hrn = Xrn(node['component_manager_id']).get_hrn()
43
44             # set component_name attribute and  hostname element
45             if 'component_id' in node and node['component_id']:
46                 component_name = Xrn(xrn=node['component_id']).get_leaf()
47                 node_elem.set('component_name', component_name)
48                 hostname_elem = node_elem.add_element('hostname')
49                 hostname_elem.set_text(component_name)
50
51             # set site id
52             if 'authority_id' in node and node['authority_id']:
53                 node_elem.set('site_id', node['authority_id'])
54
55             # add locaiton
56             location = node.get('location')
57             if location:
58                 node_elem.add_instance('location', location, Location.fields)
59
60             # add 3D Position of the node
61             position_3d = node.get('position_3d')
62             if position_3d:
63                 node_elem.add_instance('position_3d', position_3d, Position3D.fields)
64
65             # all nitos nodes are exculsive
66             exclusive_elem = node_elem.add_element('exclusive')
67             exclusive_elem.set_text('TRUE')
68
69             # add granularity of the reservation system
70             granularity = node.get('granularity')['grain']
71             if granularity:
72                 #node_elem.add_instance('granularity', granularity, granularity.fields)
73                 granularity_elem = node_elem.add_element('granularity')
74                 granularity_elem.set_text(str(granularity))
75             # add hardware type
76             #hardware_type = node.get('hardware_type')
77             #if hardware_type:
78             #    node_elem.add_instance('hardware_type', hardware_type)
79             hardware_type_elem = node_elem.add_element('hardware_type')
80             hardware_type_elem.set_text(node.get('hardware_type'))
81
82
83             if isinstance(node.get('interfaces'), list):
84                 for interface in node.get('interfaces', []):
85                     node_elem.add_instance('interface', interface, ['component_id', 'client_id', 'ipv4']) 
86             
87             #if 'bw_unallocated' in node and node['bw_unallocated']:
88             #    bw_unallocated = etree.SubElement(node_elem, 'bw_unallocated', units='kbps').text = str(int(node['bw_unallocated'])/1000)
89
90             PGv2Services.add_services(node_elem, node.get('services', []))
91             tags = node.get('tags', [])
92             if tags:
93                 for tag in tags:
94                     tag_elem = node_elem.add_element(tag['tagname'])
95                     tag_elem.set_text(tag['value'])
96             NITOSv1Sliver.add_slivers(node_elem, node.get('slivers', []))
97
98     @staticmethod 
99     def add_slivers(xml, slivers):
100         component_ids = []
101         for sliver in slivers:
102             filter = {}
103             if isinstance(sliver, str):
104                 filter['component_id'] = '*%s*' % sliver
105                 sliver = {}
106             elif 'component_id' in sliver and sliver['component_id']:
107                 filter['component_id'] = '*%s*' % sliver['component_id']
108             if not filter:
109                 continue 
110             nodes = NITOSv1Node.get_nodes(xml, filter)
111             if not nodes:
112                 continue
113             node = nodes[0]
114             NITOSv1Sliver.add_slivers(node, sliver)
115
116     @staticmethod
117     def remove_slivers(xml, hostnames):
118         for hostname in hostnames:
119             nodes = NITOSv1Node.get_nodes(xml, {'component_id': '*%s*' % hostname})
120             for node in nodes:
121                 slivers = NITOSv1Sliver.get_slivers(node.element)
122                 for sliver in slivers:
123                     node.element.remove(sliver.element)
124         
125     @staticmethod
126     def get_nodes(xml, filter={}):
127         xpath = '//node%s | //default:node%s' % (XpathFilter.xpath(filter), XpathFilter.xpath(filter))
128         node_elems = xml.xpath(xpath)
129         return NITOSv1Node.get_node_objs(node_elems)
130
131     @staticmethod
132     def get_nodes_with_slivers(xml):
133         xpath = '//node[count(sliver)>0] | //default:node[count(default:sliver)>0]' 
134         node_elems = xml.xpath(xpath)
135         return NITOSv1Node.get_node_objs(node_elems)
136
137
138     @staticmethod
139     def get_node_objs(node_elems):
140         nodes = []    
141         for node_elem in node_elems:
142             node = Node(node_elem.attrib, node_elem)
143             if 'site_id' in node_elem.attrib:
144                 node['authority_id'] = node_elem.attrib['site_id']
145             # get location
146             location_elems = node_elem.xpath('./default:location | ./location')
147             locations = [loc_elem.get_instance(Location) for loc_elem in location_elems]  
148             if len(locations) > 0:
149                 node['location'] = locations[0]
150             # get bwlimit
151             bwlimit_elems = node_elem.xpath('./default:bw_limit | ./bw_limit')
152             bwlimits = [bwlimit_elem.get_instance(BWlimit) for bwlimit_elem in bwlimit_elems]
153             if len(bwlimits) > 0:
154                 node['bwlimit'] = bwlimits[0]
155             # get interfaces
156             iface_elems = node_elem.xpath('./default:interface | ./interface')
157             ifaces = [iface_elem.get_instance(Interface) for iface_elem in iface_elems]
158             node['interfaces'] = ifaces
159             # get services
160             node['services'] = PGv2Services.get_services(node_elem) 
161             # get slivers
162             node['slivers'] = NITOSv1Sliver.get_slivers(node_elem)
163             # get tags
164             node['tags'] =  NITOSv1PLTag.get_pl_tags(node_elem, ignore=Node.fields+["hardware_type"])
165             # get hardware types
166             hardware_type_elems = node_elem.xpath('./default:hardware_type | ./hardware_type')
167             node['hardware_types'] = [hw_type.get_instance(HardwareType) for hw_type in hardware_type_elems]
168
169             # temporary... play nice with old slice manager rspec
170             if not node['component_name']:
171                 hostname_elem = node_elem.find("hostname")
172                 if hostname_elem != None:
173                     node['component_name'] = hostname_elem.text
174
175             nodes.append(node)
176         return nodes            
177