be7a969fb09284b72a97d5ca98aadb267e4bdbd0
[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 resolve_volume_n_validate(self, volume_ref):
177 """
178 Resolve the volume reference
179
180 Arguments:
181 volume_ref (string): Name of volume reference
182
183 Raises VolumeValidateError in case of Errors
184 """
185
186 for vol in self.driver._cinder_volume_list:
187 voldict = vol.to_dict()
188 if 'display_name' in voldict and voldict['display_name'] == volume_ref:
189 if 'status' in voldict:
190 if voldict['status'] == 'available':
191 return voldict['id']
192 else:
193 self.log.error("Volume %s not in available state. Current state: %s",
194 volume_ref, voldict['status'])
195 raise VolumeValidateError("Volume with name %s found in incorrect (%s) state"
196 %(volume_ref, voldict['status']))
197
198 self.log.info("No volume found with matching name: %s ", volume_ref)
199 raise VolumeValidateError("No volume found with matching name: %s " %(volume_ref))
200
201 def make_vdu_volume_args(self, volume, vdu_params):
202 """
203 Arguments:
204 volume: Protobuf GI object RwcalYang.VDUInitParams_Volumes()
205 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
206
207 Returns:
208 A dictionary required to create volume for VDU
209
210 Raises VolumeValidateError in case of Errors
211 """
212 kwargs = dict()
213
214 if 'boot_priority' in volume:
215 # Rift-only field
216 kwargs['boot_index'] = volume.boot_priority
217 if volume.has_field("image"):
218 # Support image->volume
219 kwargs['source_type'] = "image"
220 kwargs['uuid'] = self.resolve_image_n_validate(volume.image, volume.image_checksum)
221 kwargs['delete_on_termination'] = True
222 elif "volume_ref" in volume:
223 # Support volume-ref->volume (only ref)
224 # Rift-only field
225 kwargs['source_type'] = "volume"
226 kwargs['uuid'] = self.resolve_volume_n_validate(volume.volume_ref)
227 kwargs['delete_on_termination'] = False
228 else:
229 # Support blank->volume
230 kwargs['source_type'] = "blank"
231 kwargs['delete_on_termination'] = True
232 kwargs['device_name'] = volume.name
233 kwargs['destination_type'] = "volume"
234 kwargs['volume_size'] = volume.size
235
236 if volume.has_field('device_type'):
237 if volume.device_type in ['cdrom', 'disk']:
238 kwargs['device_type'] = volume.device_type
239 else:
240 self.log.error("Unsupported device_type <%s> found for volume: %s",
241 volume.device_type, volume.name)
242 raise VolumeValidateError("Unsupported device_type <%s> found for volume: %s"
243 %(volume.device_type, volume.name))
244
245 if volume.has_field('device_bus'):
246 if volume.device_bus in ['ide', 'virtio', 'scsi']:
247 kwargs['disk_bus'] = volume.device_bus
248 else:
249 self.log.error("Unsupported device_type <%s> found for volume: %s",
250 volume.device_type, volume.name)
251 raise VolumeValidateError("Unsupported device_type <%s> found for volume: %s"
252 %(volume.device_type, volume.name))
253
254 return kwargs
255
256 def make_vdu_storage_args(self, vdu_params):
257 """
258 Creates volume related arguments for VDU operation
259
260 Arguments:
261 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
262
263 Returns:
264 A dictionary required for volumes creation for VDU instantiation
265 """
266 kwargs = dict()
267 if vdu_params.has_field('volumes'):
268 kwargs['block_device_mapping_v2'] = list()
269 # Ignore top-level image
270 kwargs['image_id'] = ""
271 for volume in vdu_params.volumes:
272 kwargs['block_device_mapping_v2'].append(self.make_vdu_volume_args(volume, vdu_params))
273 return kwargs
274
275 def make_vdu_network_args(self, vdu_params):
276 """
277 Creates VDU network related arguments for VDU operation
278 Arguments:
279 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
280
281 Returns:
282 A dictionary {'port_list' : [ports], 'network_list': [networks]}
283
284 """
285 kwargs = dict()
286 kwargs['port_list'], kwargs['network_list'] = self.driver.utils.network.setup_vdu_networking(vdu_params)
287 return kwargs
288
289
290 def make_vdu_boot_config_args(self, vdu_params):
291 """
292 Creates VDU boot config related arguments for VDU operation
293 Arguments:
294 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
295
296 Returns:
297 A dictionary {
298 'userdata' : <cloud-init> ,
299 'config_drive': True/False,
300 'files' : [ file name ],
301 'metadata' : <metadata string>
302 }
303 """
304 kwargs = dict()
305 metadata = dict()
306
307 if vdu_params.has_field('node_id'):
308 metadata['rift_node_id'] = vdu_params.node_id
309 kwargs['metadata'] = metadata
310
311 if vdu_params.has_field('vdu_init') and vdu_params.vdu_init.has_field('userdata'):
312 kwargs['userdata'] = vdu_params.vdu_init.userdata
313 else:
314 kwargs['userdata'] = ''
315
316 if not vdu_params.has_field('supplemental_boot_data'):
317 return kwargs
318
319 if vdu_params.supplemental_boot_data.has_field('config_file'):
320 files = dict()
321 for cf in vdu_params.supplemental_boot_data.config_file:
322 files[cf.dest] = cf.source
323 kwargs['files'] = files
324
325 if vdu_params.supplemental_boot_data.has_field('boot_data_drive'):
326 kwargs['config_drive'] = vdu_params.supplemental_boot_data.boot_data_drive
327 else:
328 kwargs['config_drive'] = False
329
330 try:
331 # Rift model only
332 if vdu_params.supplemental_boot_data.has_field('custom_meta_data'):
333 for cm in vdu_params.supplemental_boot_data.custom_meta_data:
334 metadata[cm.name] = cm.value
335 kwargs['metadata'] = metadata
336 except Exception as e:
337 pass
338
339 return kwargs
340
341 def _select_affinity_group(self, group_name):
342 """
343 Selects the affinity group based on name and return its id
344 Arguments:
345 group_name (string): Name of the Affinity/Anti-Affinity group
346 Returns:
347 Id of the matching group
348
349 Raises exception AffinityGroupError if no matching group is found
350 """
351 groups = [g['id'] for g in self.driver._nova_affinity_group if g['name'] == group_name]
352 if not groups:
353 self.log.error("No affinity/anti-affinity group with name: %s found", group_name)
354 raise AffinityGroupError("No affinity/anti-affinity group with name: %s found" %(group_name))
355 return groups[0]
356
357
358 def make_vdu_server_placement_args(self, vdu_params):
359 """
360 Function to create kwargs required for nova server placement
361
362 Arguments:
363 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
364
365 Returns:
366 A dictionary { 'availability_zone' : < Zone >, 'scheduler_hints': <group-id> }
367
368 """
369 kwargs = dict()
370
371 if vdu_params.has_field('availability_zone') \
372 and vdu_params.availability_zone.has_field('name'):
373 kwargs['availability_zone'] = vdu_params.availability_zone
374
375 if vdu_params.has_field('server_group'):
376 kwargs['scheduler_hints'] = {
377 'group': self._select_affinity_group(vdu_params.server_group)
378 }
379 return kwargs
380
381 def make_vdu_server_security_args(self, vdu_params, account):
382 """
383 Function to create kwargs required for nova security group
384
385 Arguments:
386 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
387 account: Protobuf GI object RwcalYang.CloudAccount()
388
389 Returns:
390 A dictionary {'security_groups' : < group > }
391 """
392 kwargs = dict()
393 if account.openstack.security_groups:
394 kwargs['security_groups'] = account.openstack.security_groups
395 return kwargs
396
397
398 def make_vdu_create_args(self, vdu_params, account):
399 """
400 Function to create kwargs required for nova_server_create API
401
402 Arguments:
403 vdu_params: Protobuf GI object RwcalYang.VDUInitParams()
404 account: Protobuf GI object RwcalYang.CloudAccount()
405
406 Returns:
407 A kwargs dictionary for VDU create operation
408 """
409 kwargs = dict()
410
411 kwargs['name'] = vdu_params.name
412
413 kwargs.update(self.make_vdu_flavor_args(vdu_params))
414 kwargs.update(self.make_vdu_storage_args(vdu_params))
415 kwargs.update(self.make_vdu_image_args(vdu_params))
416 kwargs.update(self.make_vdu_network_args(vdu_params))
417 kwargs.update(self.make_vdu_boot_config_args(vdu_params))
418 kwargs.update(self.make_vdu_server_placement_args(vdu_params))
419 kwargs.update(self.make_vdu_server_security_args(vdu_params, account))
420 return kwargs
421
422
423 def _parse_vdu_mgmt_address_info(self, vm_info):
424 """
425 Get management_ip and public_ip for VDU
426
427 Arguments:
428 vm_info : A dictionary object return by novaclient library listing VM attributes
429
430 Returns:
431 A tuple of mgmt_ip (string) and public_ip (string)
432 """
433 mgmt_ip = None
434 public_ip = None
435 if 'addresses' in vm_info:
436 for network_name, network_info in vm_info['addresses'].items():
437 if network_info and network_name == self.driver.mgmt_network:
438 for interface in network_info:
439 if 'OS-EXT-IPS:type' in interface:
440 if interface['OS-EXT-IPS:type'] == 'fixed':
441 mgmt_ip = interface['addr']
442 elif interface['OS-EXT-IPS:type'] == 'floating':
443 public_ip = interface['addr']
444 return (mgmt_ip, public_ip)
445
446 def get_vdu_epa_info(self, vm_info):
447 """
448 Get flavor information (including EPA) for VDU
449
450 Arguments:
451 vm_info : A dictionary returned by novaclient library listing VM attributes
452 Returns:
453 flavor_info: A dictionary object returned by novaclient library listing flavor attributes
454 """
455 if 'flavor' in vm_info and 'id' in vm_info['flavor']:
456 try:
457 flavor_info = self.driver.nova_flavor_get(vm_info['flavor']['id'])
458 return flavor_info
459 except Exception as e:
460 self.log.exception("Exception %s occured during get-flavor", str(e))
461 return dict()
462
463 def _parse_vdu_cp_info(self, vdu_id):
464 """
465 Get connection point information for VDU identified by vdu_id
466 Arguments:
467 vdu_id (string) : VDU Id (vm_info['id'])
468 Returns:
469 A List of object RwcalYang.VDUInfoParams_ConnectionPoints()
470
471 """
472 cp_list = []
473 # Fill the port information
474 port_list = self.driver.neutron_port_list(**{'device_id': vdu_id})
475 for port in port_list:
476 cp_info = self.driver.utils.network._parse_cp(port)
477 cp = RwcalYang.VDUInfoParams_ConnectionPoints()
478 cp.from_dict(cp_info.as_dict())
479 cp_list.append(cp)
480 return cp_list
481
482 def _parse_vdu_state_info(self, vm_info):
483 """
484 Get VDU state information
485
486 Arguments:
487 vm_info : A dictionary returned by novaclient library listing VM attributes
488
489 Returns:
490 state (string): State of the VDU
491 """
492 if 'status' in vm_info:
493 if vm_info['status'] == 'ACTIVE':
494 vdu_state = 'active'
495 elif vm_info['status'] == 'ERROR':
496 vdu_state = 'failed'
497 else:
498 vdu_state = 'inactive'
499 else:
500 vdu_state = 'unknown'
501 return vdu_state
502
503 def _parse_vdu_server_group_info(self, vm_info):
504 """
505 Get VDU server group information
506 Arguments:
507 vm_info : A dictionary returned by novaclient library listing VM attributes
508
509 Returns:
510 server_group_name (string): Name of the server group to which VM belongs, else empty string
511
512 """
513 server_group = [ v['name']
514 for v in self.driver.nova_server_group_list()
515 if vm_info['id'] in v['members'] ]
516 if server_group:
517 return server_group[0]
518 else:
519 return str()
520
521 def _parse_vdu_boot_config_data(self, vm_info):
522 """
523 Parses VDU supplemental boot data
524 Arguments:
525 vm_info : A dictionary returned by novaclient library listing VM attributes
526
527 Returns:
528 List of RwcalYang.VDUInfoParams_SupplementalBootData()
529 """
530 supplemental_boot_data = None
531 node_id = None
532 if 'config_drive' in vm_info:
533 supplemental_boot_data = RwcalYang.VDUInfoParams_SupplementalBootData()
534 supplemental_boot_data.boot_data_drive = vm_info['config_drive']
535 # Look for any metadata
536 if 'metadata' not in vm_info:
537 return node_id, supplemental_boot_data
538 if supplemental_boot_data is None:
539 supplemental_boot_data = RwcalYang.VDUInfoParams_SupplementalBootData()
540 for key, value in vm_info['metadata'].items():
541 if key == 'rift_node_id':
542 node_id = value
543 else:
544 try:
545 # rift only
546 cm = supplemental_boot_data.custom_meta_data.add()
547 cm.name = key
548 cm.value = str(value)
549 except Exception as e:
550 pass
551 return node_id, supplemental_boot_data
552
553 def _parse_vdu_volume_info(self, vm_info):
554 """
555 Get VDU server group information
556 Arguments:
557 vm_info : A dictionary returned by novaclient library listing VM attributes
558
559 Returns:
560 List of RwcalYang.VDUInfoParams_Volumes()
561 """
562 volumes = list()
563
564 try:
565 volume_list = self.driver.nova_volume_list(vm_info['id'])
566 except Exception as e:
567 self.log.exception("Exception %s occured during nova-volume-list", str(e))
568 return volumes
569
570 for v in volume_list:
571 volume = RwcalYang.VDUInfoParams_Volumes()
572 try:
573 volume.name = (v['device']).split('/')[2]
574 volume.volume_id = v['volumeId']
575 details = self.driver.cinder_volume_get(volume.volume_id)
576 try:
577 # Rift only
578 for k, v in details.metadata.items():
579 vd = volume.custom_meta_data.add()
580 vd.name = k
581 vd.value = v
582 except Exception as e:
583 pass
584 except Exception as e:
585 self.log.exception("Exception %s occured during volume list parsing", str(e))
586 continue
587 else:
588 volumes.append(volume)
589 return volumes
590
591 def _parse_vdu_console_url(self, vm_info):
592 """
593 Get VDU console URL
594 Arguments:
595 vm_info : A dictionary returned by novaclient library listing VM attributes
596
597 Returns:
598 console_url(string): Console URL for VM
599 """
600 console_url = None
601 if self._parse_vdu_state_info(vm_info) == 'active':
602 try:
603 serv_console_url = self.driver.nova_server_console(vm_info['id'])
604 if 'console' in serv_console_url:
605 console_url = serv_console_url['console']['url']
606 else:
607 self.log.error("Error fetching console url. This could be an Openstack issue. Console : " + str(serv_console_url))
608
609
610 except Exception as e:
611 self.log.exception("Exception %s occured during volume list parsing", str(e))
612 return console_url
613
614 def parse_cloud_vdu_info(self, vm_info):
615 """
616 Parse vm_info dictionary (return by python-client) and put values in GI object for VDU
617
618 Arguments:
619 vm_info : A dictionary object return by novaclient library listing VM attributes
620
621 Returns:
622 Protobuf GI Object of type RwcalYang.VDUInfoParams()
623 """
624 vdu = RwcalYang.VDUInfoParams()
625 vdu.name = vm_info['name']
626 vdu.vdu_id = vm_info['id']
627 vdu.cloud_type = 'openstack'
628
629 if 'image' in vm_info and 'id' in vm_info['image']:
630 vdu.image_id = vm_info['image']['id']
631
632 if 'availability_zone' in vm_info:
633 vdu.availability_zone = vm_info['availability_zone']
634
635 vdu.state = self._parse_vdu_state_info(vm_info)
636 management_ip,public_ip = self._parse_vdu_mgmt_address_info(vm_info)
637
638 if management_ip:
639 vdu.management_ip = management_ip
640
641 if public_ip:
642 vdu.public_ip = public_ip
643
644 if 'flavor' in vm_info and 'id' in vm_info['flavor']:
645 vdu.flavor_id = vm_info['flavor']['id']
646 flavor_info = self.get_vdu_epa_info(vm_info)
647 vm_flavor = self.driver.utils.flavor.parse_vm_flavor_epa_info(flavor_info)
648 guest_epa = self.driver.utils.flavor.parse_guest_epa_info(flavor_info)
649 host_epa = self.driver.utils.flavor.parse_host_epa_info(flavor_info)
650 host_aggregates = self.driver.utils.flavor.parse_host_aggregate_epa_info(flavor_info)
651
652 vdu.vm_flavor.from_dict(vm_flavor.as_dict())
653 vdu.guest_epa.from_dict(guest_epa.as_dict())
654 vdu.host_epa.from_dict(host_epa.as_dict())
655 for aggr in host_aggregates:
656 ha = vdu.host_aggregate.add()
657 ha.from_dict(aggr.as_dict())
658
659 node_id, boot_data = self._parse_vdu_boot_config_data(vm_info)
660 if node_id:
661 vdu.node_id = node_id
662 if boot_data:
663 vdu.supplemental_boot_data = boot_data
664
665 cp_list = self._parse_vdu_cp_info(vdu.vdu_id)
666 for cp in cp_list:
667 vdu.connection_points.append(cp)
668
669 vdu.server_group.name = self._parse_vdu_server_group_info(vm_info)
670
671 for v in self._parse_vdu_volume_info(vm_info):
672 vdu.volumes.append(v)
673
674 vdu.console_url = self._parse_vdu_console_url(vm_info)
675 return vdu
676
677
678 def perform_vdu_network_cleanup(self, vdu_id):
679 """
680 This function cleans up networking resources related to VDU
681 Arguments:
682 vdu_id(string): VDU id
683 Returns:
684 None
685 """
686 ### Get list of floating_ips associated with this instance and delete them
687 floating_ips = [ f for f in self.driver.nova_floating_ip_list() if f.instance_id == vdu_id ]
688 for f in floating_ips:
689 self.driver.nova_floating_ip_delete(f)
690
691 ### Get list of port on VM and delete them.
692 port_list = self.driver.neutron_port_list(**{'device_id': vdu_id})
693
694 for port in port_list:
695 if ((port['device_owner'] == 'compute:None') or (port['device_owner'] == '')):
696 self.driver.neutron_port_delete(port['id'])
697