Fix metadata initialization
[osm/SO.git] / rwcal / plugins / vala / rwcal_openstack / rift / rwcal / openstack / utils / compute.py
index 55ece65..be7a969 100644 (file)
@@ -173,6 +173,31 @@ class ComputeUtils(object):
                                      %(image_name, checksum))
         return image['id']
         
+    def resolve_volume_n_validate(self, volume_ref):
+        """
+        Resolve the volume reference
+        
+        Arguments:
+          volume_ref (string): Name of volume reference
+
+        Raises VolumeValidateError in case of Errors
+        """
+        
+        for vol in self.driver._cinder_volume_list:
+            voldict = vol.to_dict()
+            if 'display_name' in voldict and voldict['display_name'] == volume_ref:
+                if 'status' in voldict:
+                    if voldict['status'] == 'available':
+                        return voldict['id']
+                    else:
+                        self.log.error("Volume %s not in available state. Current state: %s",
+                               volume_ref, voldict['status'])
+                        raise VolumeValidateError("Volume with name %s found in incorrect (%s) state"
+                                         %(volume_ref, voldict['status']))
+
+        self.log.info("No volume found with matching name: %s ", volume_ref)
+        raise VolumeValidateError("No volume found with matching name: %s " %(volume_ref))
+        
     def make_vdu_volume_args(self, volume, vdu_params):
         """
         Arguments:
@@ -186,40 +211,46 @@ class ComputeUtils(object):
         """
         kwargs = dict()
 
-        if volume.has_field('volume_ref'):
-            self.log.error("Unsupported option <Volume Reference> found for volume: %s", volume.name)
-            raise VolumeValidateError("Unsupported option <Volume Reference> found for volume: %s"
-                                      %(volume.name))
-        
-        kwargs['boot_index'] = volume.boot_priority
-        if "image" in volume:
+        if 'boot_priority' in volume:
+            # Rift-only field
+            kwargs['boot_index'] = volume.boot_priority
+        if volume.has_field("image"):
             # Support image->volume
-            if volume.image is not None:
-                kwargs['source_type'] = "image"
-                kwargs['uuid'] = self.resolve_image_n_validate(volume.image, volume.image_checksum)
-            else:
-                # Support blank->volume
-                kwargs['source_type'] = "blank"
+            kwargs['source_type'] = "image"
+            kwargs['uuid'] = self.resolve_image_n_validate(volume.image, volume.image_checksum)
+            kwargs['delete_on_termination'] = True
+        elif "volume_ref" in volume:
+            # Support volume-ref->volume (only ref)
+            # Rift-only field
+            kwargs['source_type'] = "volume"
+            kwargs['uuid'] = self.resolve_volume_n_validate(volume.volume_ref)
+            kwargs['delete_on_termination'] = False
+        else:
+            # Support blank->volume
+            kwargs['source_type'] = "blank"
+            kwargs['delete_on_termination'] = True
         kwargs['device_name'] = volume.name
         kwargs['destination_type'] = "volume"
         kwargs['volume_size'] = volume.size
-        kwargs['delete_on_termination'] = True
 
         if volume.has_field('device_type'):
-            if volume.device_type == 'cdrom':
-                kwargs['device_type'] = 'cdrom'
-            elif volume.device_bus == 'ide':
-                kwargs['disk_bus'] = 'ide'
+            if volume.device_type in ['cdrom', 'disk']:
+                kwargs['device_type'] = volume.device_type
             else:
                 self.log.error("Unsupported device_type <%s> found for volume: %s",
                                volume.device_type, volume.name)
                 raise VolumeValidateError("Unsupported device_type <%s> found for volume: %s"
-                                          %(volume.device_type, volume.name))        
-        else:
-            self.log.error("Mandatory field <device_type> not specified for volume: %s",
-                           volume.name)
-            raise VolumeValidateError("Mandatory field <device_type> not specified for volume: %s"
-                                      %(volume.name))
+                                          %(volume.device_type, volume.name))
+
+        if volume.has_field('device_bus'):
+            if volume.device_bus in ['ide', 'virtio', 'scsi']:
+                kwargs['disk_bus'] = volume.device_bus
+            else:
+                self.log.error("Unsupported device_type <%s> found for volume: %s",
+                               volume.device_type, volume.name)
+                raise VolumeValidateError("Unsupported device_type <%s> found for volume: %s"
+                                          %(volume.device_type, volume.name))
+
         return kwargs
             
     def make_vdu_storage_args(self, vdu_params):
@@ -235,6 +266,8 @@ class ComputeUtils(object):
         kwargs = dict()
         if vdu_params.has_field('volumes'):
             kwargs['block_device_mapping_v2'] = list()
+            # Ignore top-level image
+            kwargs['image_id']  = ""
             for volume in vdu_params.volumes:
                 kwargs['block_device_mapping_v2'].append(self.make_vdu_volume_args(volume, vdu_params))
         return kwargs
@@ -269,6 +302,12 @@ class ComputeUtils(object):
         }
         """
         kwargs = dict()
+        metadata = dict()
+
+        if vdu_params.has_field('node_id'):
+            metadata['rift_node_id'] = vdu_params.node_id
+            kwargs['metadata'] = metadata
+
         if vdu_params.has_field('vdu_init') and vdu_params.vdu_init.has_field('userdata'):
             kwargs['userdata'] = vdu_params.vdu_init.userdata
         else:
@@ -288,11 +327,14 @@ class ComputeUtils(object):
         else:
             kwargs['config_drive'] = False
 
-        if vdu_params.supplemental_boot_data.has_field('custom_meta_data'):
-            metadata = dict()
-            for cm in vdu_params.supplemental_boot_data.custom_meta_data:
-                metadata[cm] = cm.value                
-            kwargs['metadata'] = metadata
+        try:
+            # Rift model only
+            if vdu_params.supplemental_boot_data.has_field('custom_meta_data'):
+                for cm in vdu_params.supplemental_boot_data.custom_meta_data:
+                    metadata[cm.name] = cm.value
+                    kwargs['metadata'] = metadata
+        except Exception as e:
+            pass
 
         return kwargs
 
@@ -476,6 +518,38 @@ class ComputeUtils(object):
         else:
             return str()
 
+    def _parse_vdu_boot_config_data(self, vm_info):
+        """
+        Parses VDU supplemental boot data
+        Arguments:
+          vm_info : A dictionary returned by novaclient library listing VM attributes
+
+        Returns:
+          List of RwcalYang.VDUInfoParams_SupplementalBootData()
+        """
+        supplemental_boot_data = None
+        node_id = None
+        if 'config_drive' in vm_info:
+            supplemental_boot_data = RwcalYang.VDUInfoParams_SupplementalBootData()
+            supplemental_boot_data.boot_data_drive = vm_info['config_drive']
+        # Look for any metadata
+        if 'metadata' not in vm_info:
+            return node_id, supplemental_boot_data
+        if supplemental_boot_data is None:
+            supplemental_boot_data = RwcalYang.VDUInfoParams_SupplementalBootData()
+        for key, value in vm_info['metadata'].items():
+            if key == 'rift_node_id':
+                node_id = value
+            else:
+                try:
+                    # rift only
+                    cm = supplemental_boot_data.custom_meta_data.add()
+                    cm.name = key
+                    cm.value = str(value)
+                except Exception as e:
+                    pass
+        return node_id, supplemental_boot_data 
+
     def _parse_vdu_volume_info(self, vm_info):
         """
         Get VDU server group information
@@ -499,10 +573,14 @@ class ComputeUtils(object):
                 volume.name = (v['device']).split('/')[2]
                 volume.volume_id = v['volumeId']
                 details = self.driver.cinder_volume_get(volume.volume_id)
-                for k, v in details.metadata.items():
-                    vd = volume.custom_meta_data.add()
-                    vd.name = k
-                    vd.value = v
+                try:
+                    # Rift only
+                    for k, v in details.metadata.items():
+                        vd = volume.custom_meta_data.add()
+                        vd.name = k
+                        vd.value = v
+                except Exception as e:
+                    pass
             except Exception as e:
                 self.log.exception("Exception %s occured during volume list parsing", str(e))
                 continue
@@ -522,7 +600,6 @@ class ComputeUtils(object):
         console_url = None
         if self._parse_vdu_state_info(vm_info) == 'active':
             try:
-                console_url = self.driver.nova_server_console(vm_info['id'])
                 serv_console_url = self.driver.nova_server_console(vm_info['id'])
                 if 'console' in serv_console_url:
                     console_url = serv_console_url['console']['url']
@@ -549,9 +626,6 @@ class ComputeUtils(object):
         vdu.vdu_id = vm_info['id']
         vdu.cloud_type  = 'openstack'
 
-        if 'config_drive' in vm_info:
-            vdu.supplemental_boot_data.boot_data_drive = vm_info['config_drive']
-
         if 'image' in vm_info and 'id' in vm_info['image']:
             vdu.image_id = vm_info['image']['id']
 
@@ -582,6 +656,12 @@ class ComputeUtils(object):
                 ha = vdu.host_aggregate.add()
                 ha.from_dict(aggr.as_dict())
 
+        node_id, boot_data = self._parse_vdu_boot_config_data(vm_info)
+        if node_id:
+            vdu.node_id = node_id
+        if boot_data:
+            vdu.supplemental_boot_data = boot_data
+
         cp_list = self._parse_vdu_cp_info(vdu.vdu_id)
         for cp in cp_list:
             vdu.connection_points.append(cp)