autopep8
[sfa.git] / sfa / rspecs / elements / versions / pgv2Node.py
1 from sfa.util.xrn import Xrn, get_leaf
2 from sfa.util.xml import XpathFilter
3
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.disk_image import DiskImage
9 from sfa.rspecs.elements.interface import Interface
10 from sfa.rspecs.elements.bwlimit import BWlimit
11 from sfa.rspecs.elements.pltag import PLTag
12 from sfa.rspecs.elements.versions.pgv2Services import PGv2Services
13 from sfa.rspecs.elements.versions.pgv2SliverType import PGv2SliverType
14 from sfa.rspecs.elements.versions.pgv2Interface import PGv2Interface
15 from sfa.rspecs.elements.versions.sfav1PLTag import SFAv1PLTag
16 from sfa.rspecs.elements.granularity import Granularity
17 from sfa.rspecs.elements.attribute import Attribute
18
19
20 class PGv2Node:
21
22     @staticmethod
23     def add_nodes(xml, nodes, rspec_content_type=None):
24         node_elems = []
25         for node in nodes:
26             node_fields = ['component_manager_id', 'component_id',
27                            'client_id', 'sliver_id', 'exclusive']
28             node_elem = xml.add_instance('node', node, node_fields)
29             node_elems.append(node_elem)
30             # set component name
31             if node.get('component_id'):
32                 component_name = Xrn.unescape(
33                     get_leaf(Xrn(node['component_id']).get_hrn()))
34                 node_elem.set('component_name', component_name)
35             # set hardware types
36             if node.get('hardware_types'):
37                 for hardware_type in node.get('hardware_types', []):
38                     node_elem.add_instance(
39                         'hardware_type', hardware_type, HardwareType.fields)
40             # set location
41             if node.get('location'):
42                 node_elem.add_instance(
43                     'location', node['location'], Location.fields)
44
45             # set granularity
46             if node.get('exclusive') == "true":
47                 granularity = node.get('granularity')
48                 node_elem.add_instance(
49                     'granularity', granularity, granularity.fields)
50             # set interfaces
51             PGv2Interface.add_interfaces(node_elem, node.get('interfaces'))
52             # if node.get('interfaces'):
53             #    for interface in  node.get('interfaces', []):
54             #        node_elem.add_instance('interface', interface, ['component_id', 'client_id'])
55             # set available element
56             if node.get('available'):
57                 available_elem = node_elem.add_element(
58                     'available', now=node['available'])
59             # add services
60             PGv2Services.add_services(node_elem, node.get('services', []))
61             # add slivers
62             slivers = node.get('slivers', [])
63             if not slivers:
64                 # we must still advertise the available sliver types
65                 if node.get('sliver_type'):
66                     slivers = Sliver({'type': node['sliver_type']})
67                 else:
68                     # Planet lab
69                     slivers = Sliver({'type': 'plab-vserver'})
70                 # we must also advertise the available initscripts
71                 slivers['tags'] = []
72                 if node.get('pl_initscripts'):
73                     for initscript in node.get('pl_initscripts', []):
74                         slivers['tags'].append(
75                             {'name': 'initscript', 'value': initscript['name']})
76             PGv2SliverType.add_slivers(node_elem, slivers)
77
78             # advertise the node tags
79             tags = node.get('tags', [])
80             if tags:
81                 for tag in tags:
82                     tag['name'] = tag.pop('tagname')
83                     node_elem.add_instance('{%s}attribute' % xml.namespaces[
84                                            'planetlab'], tag, ['name', 'value'])
85
86             # add sliver tag in Request Rspec
87             # if rspec_content_type == "request":
88             #    node_elem.add_instance('sliver', '', [])
89
90         return node_elems
91
92     @staticmethod
93     def get_nodes(xml, filter=None):
94         if filter is None:
95             filter = {}
96         xpath = '//node%s | //default:node%s' % (
97             XpathFilter.xpath(filter), XpathFilter.xpath(filter))
98         node_elems = xml.xpath(xpath)
99         return PGv2Node.get_node_objs(node_elems)
100
101     @staticmethod
102     def get_nodes_with_slivers(xml, filter=None):
103         if filter is None:
104             filter = {}
105         xpath = '//node[count(sliver_type)>0] | //default:node[count(default:sliver_type) > 0]'
106         node_elems = xml.xpath(xpath)
107         return PGv2Node.get_node_objs(node_elems)
108
109     @staticmethod
110     def get_node_objs(node_elems):
111         nodes = []
112         for node_elem in node_elems:
113             node = NodeElement(node_elem.attrib, node_elem)
114             nodes.append(node)
115             if 'component_id' in node_elem.attrib:
116                 node['authority_id'] = Xrn(
117                     node_elem.attrib['component_id']).get_authority_urn()
118
119             # get hardware types
120             hardware_type_elems = node_elem.xpath(
121                 './default:hardware_type | ./hardware_type')
122             node['hardware_types'] = [dict(hw_type.get_instance(
123                 HardwareType)) for hw_type in hardware_type_elems]
124
125             # get location
126             location_elems = node_elem.xpath('./default:location | ./location')
127             locations = [dict(location_elem.get_instance(Location))
128                          for location_elem in location_elems]
129             if len(locations) > 0:
130                 node['location'] = locations[0]
131
132             # get granularity
133             granularity_elems = node_elem.xpath(
134                 './default:granularity | ./granularity')
135             if len(granularity_elems) > 0:
136                 node['granularity'] = granularity_elems[
137                     0].get_instance(Granularity)
138
139             # get interfaces
140             iface_elems = node_elem.xpath('./default:interface | ./interface')
141             node['interfaces'] = [dict(iface_elem.get_instance(
142                 Interface)) for iface_elem in iface_elems]
143
144             # get services
145             node['services'] = PGv2Services.get_services(node_elem)
146
147             # get slivers
148             node['slivers'] = PGv2SliverType.get_slivers(node_elem)
149
150             # get boot state
151             available_elems = node_elem.xpath(
152                 './default:available | ./available')
153             if len(available_elems) > 0 and 'now' in available_elems[0].attrib:
154                 if available_elems[0].attrib.get('now', '').lower() == 'true':
155                     node['boot_state'] = 'boot'
156                 else:
157                     node['boot_state'] = 'disabled'
158
159             # get initscripts
160             try:
161                 node['pl_initscripts'] = []
162                 initscript_elems = node_elem.xpath(
163                     './default:sliver_type/planetlab:initscript | ./sliver_type/initscript')
164                 if len(initscript_elems) > 0:
165                     for initscript_elem in initscript_elems:
166                         if 'name' in initscript_elem.attrib:
167                             node['pl_initscripts'].append(
168                                 dict(initscript_elem.attrib))
169             except:
170                 pass
171
172             # get node tags
173             try:
174                 tag_elems = node_elem.xpath(
175                     './planetlab:attribute | ./attribute')
176                 node['tags'] = []
177                 if len(tag_elems) > 0:
178                     for tag_elem in tag_elems:
179                         tag = dict(tag_elem.get_instance(Attribute))
180                         tag['tagname'] = tag.pop('name')
181                         node['tags'].append(tag)
182             except:
183                 pass
184
185         return nodes
186
187     @staticmethod
188     def add_slivers(xml, slivers):
189         component_ids = []
190         for sliver in slivers:
191             filter = {}
192             if isinstance(sliver, str):
193                 filter['component_id'] = '*%s*' % sliver
194                 sliver = {}
195             elif 'component_id' in sliver and sliver['component_id']:
196                 filter['component_id'] = '*%s*' % sliver['component_id']
197             if not filter:
198                 continue
199             nodes = PGv2Node.get_nodes(xml, filter)
200             if not nodes:
201                 continue
202             node = nodes[0]
203             PGv2SliverType.add_slivers(node, sliver)
204
205     @staticmethod
206     def remove_slivers(xml, hostnames):
207         for hostname in hostnames:
208             nodes = PGv2Node.get_nodes(
209                 xml, {'component_id': '*%s*' % hostname})
210             for node in nodes:
211                 slivers = PGv2SliverType.get_slivers(node.element)
212                 for sliver in slivers:
213                     node.element.remove(sliver.element)
214 if __name__ == '__main__':
215     from sfa.rspecs.rspec import RSpec
216     import pdb
217     r = RSpec('/tmp/emulab.rspec')
218     r2 = RSpec(version='ProtoGENI')
219     nodes = PGv2Node.get_nodes(r.xml)
220     PGv2Node.add_nodes(r2.xml.root, nodes)
221     # pdb.set_trace()