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