'Build a device metadata dict based on the metadata objects. This is
done here in the metadata API as opposed to in the objects themselves
because the metadata dict is part of the guest API and thus must be
controlled.'

def _get_device_metadata(self, version):

    device_metadata_list = []
    vif_vlans_supported = self._check_os_version(OCATA, version)
    if (self.instance.device_metadata is not None):
        for device in self.instance.device_metadata.devices:
            device_metadata = {}
            bus = 'none'
            address = 'none'
            if ('bus' in device):
                if isinstance(device.bus, metadata_obj.PCIDeviceBus):
                    bus = 'pci'
                elif isinstance(device.bus, metadata_obj.USBDeviceBus):
                    bus = 'usb'
                elif isinstance(device.bus, metadata_obj.SCSIDeviceBus):
                    bus = 'scsi'
                elif isinstance(device.bus, metadata_obj.IDEDeviceBus):
                    bus = 'ide'
                elif isinstance(device.bus, metadata_obj.XenDeviceBus):
                    bus = 'xen'
                else:
                    LOG.debug('Metadata    for    device    with    unknown    bus    %s    has    not    been    included    in    the    output', device.bus.__class__.__name__)
                    continue
                if ('address' in device.bus):
                    address = device.bus.address
            if isinstance(device, metadata_obj.NetworkInterfaceMetadata):
                vlan = None
                if (vif_vlans_supported and ('vlan' in device)):
                    vlan = device.vlan
                if (not (vlan or ('tags' in device))):
                    continue
                device_metadata['type'] = 'nic'
                device_metadata['mac'] = device.mac
                if vlan:
                    device_metadata['vlan'] = vlan
            elif isinstance(device, metadata_obj.DiskMetadata):
                device_metadata['type'] = 'disk'
                if ('serial' in device):
                    device_metadata['serial'] = device.serial
                if ('path' in device):
                    device_metadata['path'] = device.path
            else:
                LOG.debug('Metadata    for    device    of    unknown    type    %s    has    not    been    included    in    the    output', device.__class__.__name__)
                continue
            device_metadata['bus'] = bus
            device_metadata['address'] = address
            if ('tags' in device):
                device_metadata['tags'] = device.tags
            device_metadata_list.append(device_metadata)
    return device_metadata_list
