CAL refactoring
[osm/SO.git] / rwcal / plugins / vala / rwcal_openstack / rift / rwcal / openstack / utils / compute.py
1 #!/usr/bin/python
2
3 #
4 # Copyright 2017 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 import uuid
19 import gi
20 gi.require_version('RwcalYang', '1.0')
21 from gi.repository import RwcalYang
22
23
24 class ImageValidateError(Exception):
25 pass
26
27 class VolumeValidateError(Exception):
28 pass
29
30 class AffinityGroupError(Exception):
31 pass
32
33
34 class ComputeUtils(object):
35 """
36 Utility class for compute operations
37 """
38 epa_types = ['vm_flavor',
39 'guest_epa',
40 'host_epa',
41 'host_aggregate',
42 'hypervisor_epa',
43 'vswitch_epa']
44 def __init__(self, driver):
45 """
46 Constructor for class
47 Arguments:
48 driver: object of OpenstackDriver()
49 """
50 self._driver = driver
51 self.log = driver.log
52
53 @property
54 def driver(self):
55 return self._driver
56
57 def search_vdu_flavor(self, vdu_params):
58 """
59 Function to search a matching flavor for VDU instantiation
60 from already existing flavors
61
62 Arguments:
63 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
64
65 Returns:
66 flavor_id(string): Flavor id for VDU instantiation
67 None if no flavor could be found
68 """
69 kwargs = { 'vcpus': vdu_params.vm_flavor.vcpu_count,
70 'ram' : vdu_params.vm_flavor.memory_mb,
71 'disk' : vdu_params.vm_flavor.storage_gb,}
72
73 flavors = self.driver.nova_flavor_find(**kwargs)
74 flavor_list = list()
75 for flv in flavors:
76 flavor_list.append(self.driver.utils.flavor.parse_flavor_info(flv))
77
78 flavor_id = self.driver.utils.flavor.match_resource_flavor(vdu_params, flavor_list)
79 return flavor_id
80
81 def select_vdu_flavor(self, vdu_params):
82 """
83 This function attempts to find a pre-existing flavor matching required
84 parameters for VDU instantiation. If no such flavor is found, a new one
85 is created.
86
87 Arguments:
88 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
89
90 Returns:
91 flavor_id(string): Flavor id for VDU instantiation
92 """
93 flavor_id = self.search_vdu_flavor(vdu_params)
94 if flavor_id is not None:
95 self.log.info("Found flavor with id: %s matching requirements for VDU: %s",
96 flavor_id, vdu_params.name)
97 return flavor_id
98
99 flavor = RwcalYang.FlavorInfoItem()
100 flavor.name = str(uuid.uuid4())
101
102 epa_dict = { k: v for k, v in vdu_params.as_dict().items()
103 if k in ComputeUtils.epa_types }
104
105 flavor.from_dict(epa_dict)
106
107 flavor_id = self.driver.nova_flavor_create(name = flavor.name,
108 ram = flavor.vm_flavor.memory_mb,
109 vcpus = flavor.vm_flavor.vcpu_count,
110 disk = flavor.vm_flavor.storage_gb,
111 epa_specs = self.driver.utils.flavor.get_extra_specs(flavor))
112 return flavor_id
113
114 def make_vdu_flavor_args(self, vdu_params):
115 """
116 Creates flavor related arguments for VDU operation
117 Arguments:
118 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
119
120 Returns:
121 A dictionary {'flavor_id': <flavor-id>}
122 """
123 return {'flavor_id': self.select_vdu_flavor(vdu_params)}
124
125
126 def make_vdu_image_args(self, vdu_params):
127 """
128 Creates image related arguments for VDU operation
129 Arguments:
130 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
131
132 Returns:
133 A dictionary {'image_id': <image-id>}
134
135 """
136 kwargs = dict()
137 if vdu_params.has_field('image_name'):
138 kwargs['image_id'] = self.resolve_image_n_validate(vdu_params.image_name,
139 vdu_params.image_checksum)
140 elif vdu_params.has_field('image_id'):
141 kwargs['image_id'] = vdu_params.image_id
142
143 return kwargs
144
145 def resolve_image_n_validate(self, image_name, checksum = None):
146 """
147 Resolve the image_name to image-object by matching image_name and checksum
148
149 Arguments:
150 image_name (string): Name of image
151 checksums (string): Checksum associated with image
152
153 Raises ImageValidateError in case of Errors
154 """
155 image_info = [ i for i in self.driver._glance_image_list if i['name'] == image_name]
156
157 if not image_info:
158 self.log.error("No image with name: %s found", image_name)
159 raise ImageValidateError("No image with name %s found" %(image_name))
160
161 for image in image_info:
162 if 'status' not in image or image['status'] != 'active':
163 self.log.error("Image %s not in active state. Current state: %s",
164 image_name, image['status'])
165 raise ImageValidateError("Image with name %s found in incorrect (%s) state"
166 %(image_name, image['status']))
167 if not checksum or checksum == image['checksum']:
168 break
169 else:
170 self.log.info("No image found with matching name: %s and checksum: %s",
171 image_name, checksum)
172 raise ImageValidateError("No image found with matching name: %s and checksum: %s"
173 %(image_name, checksum))
174 return image['id']
175
176 def make_vdu_volume_args(self, volume, vdu_params):
177 """
178 Arguments:
179 volume: Protobuf GI object RwcalYang.VDUInitParams_Volumes()
180 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
181
182 Returns:
183 A dictionary required to create volume for VDU
184
185 Raises VolumeValidateError in case of Errors
186 """
187 kwargs = dict()
188
189 if volume.has_field('volume_ref'):
190 self.log.error("Unsupported option <Volume Reference> found for volume: %s", volume.name)
191 raise VolumeValidateError("Unsupported option <Volume Reference> found for volume: %s"
192 %(volume.name))
193
194 kwargs['boot_index'] = volume.boot_priority
195 if "image" in volume:
196 # Support image->volume
197 if volume.image is not None:
198 kwargs['source_type'] = "image"
199 kwargs['uuid'] = self.resolve_image_n_validate(volume.image, volume.image_checksum)
200 else:
201 # Support blank->volume
202 kwargs['source_type'] = "blank"
203 kwargs['device_name'] = volume.name
204 kwargs['destination_type'] = "volume"
205 kwargs['volume_size'] = volume.size
206 kwargs['delete_on_termination'] = True
207
208 if volume.has_field('device_type'):
209 if volume.device_type == 'cdrom':
210 kwargs['device_type'] = 'cdrom'
211 elif volume.device_bus == 'ide':
212 kwargs['disk_bus'] = 'ide'
213 else:
214 self.log.error("Unsupported device_type <%s> found for volume: %s",
215 volume.device_type, volume.name)
216 raise VolumeValidateError("Unsupported device_type <%s> found for volume: %s"
217 %(volume.device_type, volume.name))
218 else:
219 self.log.error("Mandatory field <device_type> not specified for volume: %s",
220 volume.name)
221 raise VolumeValidateError("Mandatory field <device_type> not specified for volume: %s"
222 %(volume.name))
223 return kwargs
224
225 def make_vdu_storage_args(self, vdu_params):
226 """
227 Creates volume related arguments for VDU operation
228
229 Arguments:
230 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
231
232 Returns:
233 A dictionary required for volumes creation for VDU instantiation
234 """
235 kwargs = dict()
236 if vdu_params.has_field('volumes'):
237 kwargs['block_device_mapping_v2'] = list()
238 for volume in vdu_params.volumes:
239 kwargs['block_device_mapping_v2'].append(self.make_vdu_volume_args(volume, vdu_params))
240 return kwargs
241
242 def make_vdu_network_args(self, vdu_params):
243 """
244 Creates VDU network related arguments for VDU operation
245 Arguments:
246 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
247
248 Returns:
249 A dictionary {'port_list' : [ports], 'network_list': [networks]}
250
251 """
252 kwargs = dict()
253 kwargs['port_list'], kwargs['network_list'] = self.driver.utils.network.setup_vdu_networking(vdu_params)
254 return kwargs
255
256
257 def make_vdu_boot_config_args(self, vdu_params):
258 """
259 Creates VDU boot config related arguments for VDU operation
260 Arguments:
261 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
262
263 Returns:
264 A dictionary {
265 'userdata' : <cloud-init> ,
266 'config_drive': True/False,
267 'files' : [ file name ],
268 'metadata' : <metadata string>
269 }
270 """
271 kwargs = dict()
272 if vdu_params.has_field('vdu_init') and vdu_params.vdu_init.has_field('userdata'):
273 kwargs['userdata'] = vdu_params.vdu_init.userdata
274 else:
275 kwargs['userdata'] = ''
276
277 if not vdu_params.has_field('supplemental_boot_data'):
278 return kwargs
279
280 if vdu_params.supplemental_boot_data.has_field('config_file'):
281 files = dict()
282 for cf in vdu_params.supplemental_boot_data.config_file:
283 files[cf.dest] = cf.source
284 kwargs['files'] = files
285
286 if vdu_params.supplemental_boot_data.has_field('boot_data_drive'):
287 kwargs['config_drive'] = vdu_params.supplemental_boot_data.boot_data_drive
288 else:
289 kwargs['config_drive'] = False
290
291 if vdu_params.supplemental_boot_data.has_field('custom_meta_data'):
292 metadata = dict()
293 for cm in vdu_params.supplemental_boot_data.custom_meta_data:
294 metadata[cm] = cm.value
295 kwargs['metadata'] = metadata
296
297 return kwargs
298
299 def _select_affinity_group(self, group_name):
300 """
301 Selects the affinity group based on name and return its id
302 Arguments:
303 group_name (string): Name of the Affinity/Anti-Affinity group
304 Returns:
305 Id of the matching group
306
307 Raises exception AffinityGroupError if no matching group is found
308 """
309 groups = [g['id'] for g in self.driver._nova_affinity_group if g['name'] == group_name]
310 if not groups:
311 self.log.error("No affinity/anti-affinity group with name: %s found", group_name)
312 raise AffinityGroupError("No affinity/anti-affinity group with name: %s found" %(group_name))
313 return groups[0]
314
315
316 def make_vdu_server_placement_args(self, vdu_params):
317 """
318 Function to create kwargs required for nova server placement
319
320 Arguments:
321 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
322
323 Returns:
324 A dictionary { 'availability_zone' : < Zone >, 'scheduler_hints': <group-id> }
325
326 """
327 kwargs = dict()
328
329 if vdu_params.has_field('availability_zone') \
330 and vdu_params.availability_zone.has_field('name'):
331 kwargs['availability_zone'] = vdu_params.availability_zone
332
333 if vdu_params.has_field('server_group'):
334 kwargs['scheduler_hints'] = {
335 'group': self._select_affinity_group(vdu_params.server_group)
336 }
337 return kwargs
338
339 def make_vdu_server_security_args(self, vdu_params, account):
340 """
341 Function to create kwargs required for nova security group
342
343 Arguments:
344 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
345 account: Protobuf GI object RwcalYang.CloudAccount()
346
347 Returns:
348 A dictionary {'security_groups' : < group > }
349 """
350 kwargs = dict()
351 if account.openstack.security_groups:
352 kwargs['security_groups'] = account.openstack.security_groups
353 return kwargs
354
355
356 def make_vdu_create_args(self, vdu_params, account):
357 """
358 Function to create kwargs required for nova_server_create API
359
360 Arguments:
361 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
362 account: Protobuf GI object RwcalYang.CloudAccount()
363
364 Returns:
365 A kwargs dictionary for VDU create operation
366 """
367 kwargs = dict()
368
369 kwargs['name'] = vdu_params.name
370
371 kwargs.update(self.make_vdu_flavor_args(vdu_params))
372 kwargs.update(self.make_vdu_storage_args(vdu_params))
373 kwargs.update(self.make_vdu_image_args(vdu_params))
374 kwargs.update(self.make_vdu_network_args(vdu_params))
375 kwargs.update(self.make_vdu_boot_config_args(vdu_params))
376 kwargs.update(self.make_vdu_server_placement_args(vdu_params))
377 kwargs.update(self.make_vdu_server_security_args(vdu_params, account))
378 return kwargs
379
380
381 def _parse_vdu_mgmt_address_info(self, vm_info):
382 """
383 Get management_ip and public_ip for VDU
384
385 Arguments:
386 vm_info : A dictionary object return by novaclient library listing VM attributes
387
388 Returns:
389 A tuple of mgmt_ip (string) and public_ip (string)
390 """
391 mgmt_ip = None
392 public_ip = None
393 if 'addresses' in vm_info:
394 for network_name, network_info in vm_info['addresses'].items():
395 if network_info and network_name == self.driver.mgmt_network:
396 for interface in network_info:
397 if 'OS-EXT-IPS:type' in interface:
398 if interface['OS-EXT-IPS:type'] == 'fixed':
399 mgmt_ip = interface['addr']
400 elif interface['OS-EXT-IPS:type'] == 'floating':
401 public_ip = interface['addr']
402 return (mgmt_ip, public_ip)
403
404 def get_vdu_epa_info(self, vm_info):
405 """
406 Get flavor information (including EPA) for VDU
407
408 Arguments:
409 vm_info : A dictionary returned by novaclient library listing VM attributes
410 Returns:
411 flavor_info: A dictionary object returned by novaclient library listing flavor attributes
412 """
413 if 'flavor' in vm_info and 'id' in vm_info['flavor']:
414 try:
415 flavor_info = self.driver.nova_flavor_get(vm_info['flavor']['id'])
416 return flavor_info
417 except Exception as e:
418 self.log.exception("Exception %s occured during get-flavor", str(e))
419 return dict()
420
421 def _parse_vdu_cp_info(self, vdu_id):
422 """
423 Get connection point information for VDU identified by vdu_id
424 Arguments:
425 vdu_id (string) : VDU Id (vm_info['id'])
426 Returns:
427 A List of object RwcalYang.VDUInfoParams_ConnectionPoints()
428
429 """
430 cp_list = []
431 # Fill the port information
432 port_list = self.driver.neutron_port_list(**{'device_id': vdu_id})
433 for port in port_list:
434 cp_info = self.driver.utils.network._parse_cp(port)
435 cp = RwcalYang.VDUInfoParams_ConnectionPoints()
436 cp.from_dict(cp_info.as_dict())
437 cp_list.append(cp)
438 return cp_list
439
440 def _parse_vdu_state_info(self, vm_info):
441 """
442 Get VDU state information
443
444 Arguments:
445 vm_info : A dictionary returned by novaclient library listing VM attributes
446
447 Returns:
448 state (string): State of the VDU
449 """
450 if 'status' in vm_info:
451 if vm_info['status'] == 'ACTIVE':
452 vdu_state = 'active'
453 elif vm_info['status'] == 'ERROR':
454 vdu_state = 'failed'
455 else:
456 vdu_state = 'inactive'
457 else:
458 vdu_state = 'unknown'
459 return vdu_state
460
461 def _parse_vdu_server_group_info(self, vm_info):
462 """
463 Get VDU server group information
464 Arguments:
465 vm_info : A dictionary returned by novaclient library listing VM attributes
466
467 Returns:
468 server_group_name (string): Name of the server group to which VM belongs, else empty string
469
470 """
471 server_group = [ v['name']
472 for v in self.driver.nova_server_group_list()
473 if vm_info['id'] in v['members'] ]
474 if server_group:
475 return server_group[0]
476 else:
477 return str()
478
479 def _parse_vdu_volume_info(self, vm_info):
480 """
481 Get VDU server group information
482 Arguments:
483 vm_info : A dictionary returned by novaclient library listing VM attributes
484
485 Returns:
486 List of RwcalYang.VDUInfoParams_Volumes()
487 """
488 volumes = list()
489
490 try:
491 volume_list = self.driver.nova_volume_list(vm_info['id'])
492 except Exception as e:
493 self.log.exception("Exception %s occured during nova-volume-list", str(e))
494 return volumes
495
496 for v in volume_list:
497 volume = RwcalYang.VDUInfoParams_Volumes()
498 try:
499 volume.name = (v['device']).split('/')[2]
500 volume.volume_id = v['volumeId']
501 details = self.driver.cinder_volume_get(volume.volume_id)
502 for k, v in details.metadata.items():
503 vd = volume.custom_meta_data.add()
504 vd.name = k
505 vd.value = v
506 except Exception as e:
507 self.log.exception("Exception %s occured during volume list parsing", str(e))
508 continue
509 else:
510 volumes.append(volume)
511 return volumes
512
513 def _parse_vdu_console_url(self, vm_info):
514 """
515 Get VDU console URL
516 Arguments:
517 vm_info : A dictionary returned by novaclient library listing VM attributes
518
519 Returns:
520 console_url(string): Console URL for VM
521 """
522 console_url = None
523 if self._parse_vdu_state_info(vm_info) == 'ACTIVE':
524 try:
525 console_url = self.driver.nova_server_console(vm_info['id'])
526 except Exception as e:
527 self.log.exception("Exception %s occured during volume list parsing", str(e))
528 return console_url
529
530 def parse_cloud_vdu_info(self, vm_info):
531 """
532 Parse vm_info dictionary (return by python-client) and put values in GI object for VDU
533
534 Arguments:
535 vm_info : A dictionary object return by novaclient library listing VM attributes
536
537 Returns:
538 Protobuf GI Object of type RwcalYang.VDUInfoParams()
539 """
540 vdu = RwcalYang.VDUInfoParams()
541 vdu.name = vm_info['name']
542 vdu.vdu_id = vm_info['id']
543 vdu.cloud_type = 'openstack'
544
545 if 'config_drive' in vm_info:
546 vdu.supplemental_boot_data.boot_data_drive = vm_info['config_drive']
547
548 if 'image' in vm_info and 'id' in vm_info['image']:
549 vdu.image_id = vm_info['image']['id']
550
551 if 'availability_zone' in vm_info:
552 vdu.availability_zone = vm_info['availability_zone']
553
554 vdu.state = self._parse_vdu_state_info(vm_info)
555 management_ip,public_ip = self._parse_vdu_mgmt_address_info(vm_info)
556
557 if management_ip:
558 vdu.management_ip = management_ip
559
560 if public_ip:
561 vdu.public_ip = public_ip
562
563 if 'flavor' in vm_info and 'id' in vm_info['flavor']:
564 vdu.flavor_id = vm_info['flavor']['id']
565 flavor_info = self.get_vdu_epa_info(vm_info)
566 vm_flavor = self.driver.utils.flavor.parse_vm_flavor_epa_info(flavor_info)
567 guest_epa = self.driver.utils.flavor.parse_guest_epa_info(flavor_info)
568 host_epa = self.driver.utils.flavor.parse_host_epa_info(flavor_info)
569 host_aggregates = self.driver.utils.flavor.parse_host_aggregate_epa_info(flavor_info)
570
571 vdu.vm_flavor.from_dict(vm_flavor.as_dict())
572 vdu.guest_epa.from_dict(guest_epa.as_dict())
573 vdu.host_epa.from_dict(host_epa.as_dict())
574 for aggr in host_aggregates:
575 ha = vdu.host_aggregate.add()
576 ha.from_dict(aggr.as_dict())
577
578 cp_list = self._parse_vdu_cp_info(vdu.vdu_id)
579 for cp in cp_list:
580 vdu.connection_points.append(cp)
581
582 vdu.server_group.name = self._parse_vdu_server_group_info(vm_info)
583
584 for v in self._parse_vdu_volume_info(vm_info):
585 vdu.volumes.append(v)
586
587 vdu.console_url = self._parse_vdu_console_url(vm_info)
588 return vdu
589
590
591 def perform_vdu_network_cleanup(self, vdu_id):
592 """
593 This function cleans up networking resources related to VDU
594 Arguments:
595 vdu_id(string): VDU id
596 Returns:
597 None
598 """
599 ### Get list of floating_ips associated with this instance and delete them
600 floating_ips = [ f for f in self.driver.nova_floating_ip_list() if f.instance_id == vdu_id ]
601 for f in floating_ips:
602 self.driver.nova_floating_ip_delete(f)
603
604 ### Get list of port on VM and delete them.
605 port_list = self.driver.neutron_port_list(**{'device_id': vdu_id})
606
607 for port in port_list:
608 if ((port['device_owner'] == 'compute:None') or (port['device_owner'] == '')):
609 self.driver.neutron_port_delete(port['id'])
610