the big cleanup: remove unused code relating to openstack/nova
[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 NodeElement
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, rspec_content_type=None):
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(
30                 'network', name=Xrn(network_urn).get_hrn())
31         else:
32             network_elem = xml
33
34         # needs to be improuved to retreive the gateway addr dynamically.
35         gateway_addr = 'nitlab.inf.uth.gr'
36
37         node_elems = []
38         for node in nodes:
39             node_fields = ['component_manager_id',
40                            'component_id', 'boot_state']
41             node_elem = network_elem.add_instance('node', node, node_fields)
42             node_elems.append(node_elem)
43
44             # determine network hrn
45             network_hrn = None
46             if 'component_manager_id' in node and node['component_manager_id']:
47                 network_hrn = Xrn(node['component_manager_id']).get_hrn()
48
49             # set component_name attribute and  hostname element
50             if 'component_id' in node and node['component_id']:
51                 component_name = Xrn(xrn=node['component_id']).get_leaf()
52                 node_elem.set('component_name', component_name)
53                 hostname_elem = node_elem.add_element('hostname')
54                 hostname_elem.set_text(component_name)
55
56             # set site id
57             if 'authority_id' in node and node['authority_id']:
58                 node_elem.set('site_id', node['authority_id'])
59
60             # add locaiton
61             location = node.get('location')
62             if location:
63                 node_elem.add_instance('location', location, Location.fields)
64
65             # add 3D Position of the node
66             position_3d = node.get('position_3d')
67             if position_3d:
68                 node_elem.add_instance(
69                     'position_3d', position_3d, Position3D.fields)
70
71             # all nitos nodes are exculsive
72             exclusive_elem = node_elem.add_element('exclusive')
73             exclusive_elem.set_text('TRUE')
74
75             # In order to access nitos nodes, one need to pass through the nitos gateway
76             # here we advertise Nitos access gateway address
77             gateway_elem = node_elem.add_element('gateway')
78             gateway_elem.set_text(gateway_addr)
79
80             # add granularity of the reservation system
81             granularity = node.get('granularity')['grain']
82             if granularity:
83                 #node_elem.add_instance('granularity', granularity, granularity.fields)
84                 granularity_elem = node_elem.add_element('granularity')
85                 granularity_elem.set_text(str(granularity))
86             # add hardware type
87             #hardware_type = node.get('hardware_type')
88             # if hardware_type:
89             #    node_elem.add_instance('hardware_type', hardware_type)
90             hardware_type_elem = node_elem.add_element('hardware_type')
91             hardware_type_elem.set_text(node.get('hardware_type'))
92
93             if isinstance(node.get('interfaces'), list):
94                 for interface in node.get('interfaces', []):
95                     node_elem.add_instance('interface', interface, [
96                                            'component_id', 'client_id', 'ipv4'])
97
98             # if 'bw_unallocated' in node and node['bw_unallocated']:
99             #    bw_unallocated = etree.SubElement(node_elem, 'bw_unallocated', units='kbps').text = str(int(node['bw_unallocated'])/1000)
100
101             PGv2Services.add_services(node_elem, node.get('services', []))
102             tags = node.get('tags', [])
103             if tags:
104                 for tag in tags:
105                     tag_elem = node_elem.add_element(tag['tagname'])
106                     tag_elem.set_text(tag['value'])
107             NITOSv1Sliver.add_slivers(node_elem, node.get('slivers', []))
108
109             # add sliver tag in Request Rspec
110             if rspec_content_type == "request":
111                 node_elem.add_instance('sliver', '', [])
112
113     @staticmethod
114     def add_slivers(xml, slivers):
115         component_ids = []
116         for sliver in slivers:
117             filter = {}
118             if isinstance(sliver, str):
119                 filter['component_id'] = '*%s*' % sliver
120                 sliver = {}
121             elif 'component_id' in sliver and sliver['component_id']:
122                 filter['component_id'] = '*%s*' % sliver['component_id']
123             if not filter:
124                 continue
125             nodes = NITOSv1Node.get_nodes(xml, filter)
126             if not nodes:
127                 continue
128             node = nodes[0]
129             NITOSv1Sliver.add_slivers(node, sliver)
130
131     @staticmethod
132     def remove_slivers(xml, hostnames):
133         for hostname in hostnames:
134             nodes = NITOSv1Node.get_nodes(
135                 xml, {'component_id': '*%s*' % hostname})
136             for node in nodes:
137                 slivers = NITOSv1Sliver.get_slivers(node.element)
138                 for sliver in slivers:
139                     node.element.remove(sliver.element)
140
141     @staticmethod
142     def get_nodes(xml, filter=None):
143         if filter is None:
144             filter = {}
145         xpath = '//node%s | //default:node%s' % (
146             XpathFilter.xpath(filter), XpathFilter.xpath(filter))
147         node_elems = xml.xpath(xpath)
148         return NITOSv1Node.get_node_objs(node_elems)
149
150     @staticmethod
151     def get_nodes_with_slivers(xml):
152         xpath = '//node[count(sliver)>0] | //default:node[count(default:sliver)>0]'
153         node_elems = xml.xpath(xpath)
154         return NITOSv1Node.get_node_objs(node_elems)
155
156     @staticmethod
157     def get_node_objs(node_elems):
158         nodes = []
159         for node_elem in node_elems:
160             node = NodeElement(node_elem.attrib, node_elem)
161             if 'site_id' in node_elem.attrib:
162                 node['authority_id'] = node_elem.attrib['site_id']
163             # get location
164             location_elems = node_elem.xpath('./default:location | ./location')
165             locations = [loc_elem.get_instance(
166                 Location) for loc_elem in location_elems]
167             if len(locations) > 0:
168                 node['location'] = locations[0]
169             # get bwlimit
170             bwlimit_elems = node_elem.xpath('./default:bw_limit | ./bw_limit')
171             bwlimits = [bwlimit_elem.get_instance(
172                 BWlimit) for bwlimit_elem in bwlimit_elems]
173             if len(bwlimits) > 0:
174                 node['bwlimit'] = bwlimits[0]
175             # get interfaces
176             iface_elems = node_elem.xpath('./default:interface | ./interface')
177             ifaces = [iface_elem.get_instance(
178                 Interface) for iface_elem in iface_elems]
179             node['interfaces'] = ifaces
180             # get services
181             node['services'] = PGv2Services.get_services(node_elem)
182             # get slivers
183             node['slivers'] = NITOSv1Sliver.get_slivers(node_elem)
184             # get tags
185             node['tags'] = NITOSv1PLTag.get_pl_tags(
186                 node_elem, ignore=NodeElement.fields + ["hardware_type"])
187             # get hardware types
188             hardware_type_elems = node_elem.xpath(
189                 './default:hardware_type | ./hardware_type')
190             node['hardware_types'] = [hw_type.get_instance(
191                 HardwareType) for hw_type in hardware_type_elems]
192
193             # temporary... play nice with old slice manager rspec
194             if not node['component_name']:
195                 hostname_elem = node_elem.find("hostname")
196                 if hostname_elem != None:
197                     node['component_name'] = hostname_elem.text
198
199             nodes.append(node)
200         return nodes