package org.ovirt.engine.core.common.utils;

import java.util.List;

import org.ovirt.engine.core.common.businessentities.BootSequence;
import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.core.common.businessentities.VmDevice;
import org.ovirt.engine.core.compat.Version;

public class VmDeviceCommonUtils {

    final static String NETWORK_CHAR = "N";
    final static String CDROM_CHAR = "D";
    final static String DRIVE_CHAR = "C";

    public static boolean isNetwork(VmDevice device) {
        return (device.getType().equals(VmDeviceType.getName(VmDeviceType.INTERFACE)));
    }

    public static boolean isDisk(VmDevice device) {
        return (device.getType().equals(VmDeviceType.getName(VmDeviceType.DISK)) && device.getDevice().equals(VmDeviceType.getName(VmDeviceType.DISK)));
    }

    public static boolean isCD(VmDevice device) {
        return (device.getType().equals(VmDeviceType.getName(VmDeviceType.DISK)) && device.getDevice().equals(VmDeviceType.getName(VmDeviceType.CDROM)));
    }

    /**
     * Computes old boot sequence enum value from the given list of devices.
     *
     * @param devices
     * @return
     */
    public static BootSequence getBootSequence(List<VmDevice> devices) {
        StringBuilder sb = new StringBuilder();
        BootSequence ret = BootSequence.C;
        for (VmDevice device : devices) {
            if (device.getBootOrder() > 0) {
                if (isNetwork(device) && sb.indexOf(NETWORK_CHAR) < 0) {
                    sb.append(NETWORK_CHAR);
                }
                if (isDisk(device) && sb.indexOf(DRIVE_CHAR) < 0) {
                    sb.append(DRIVE_CHAR);
                }
                if (isCD(device) && sb.indexOf(CDROM_CHAR) < 0) {
                    sb.append(CDROM_CHAR);
                }
                // maximum string is 3 characters, so, if reached , exit loop.
                if (sb.length() == 3) {
                    break;
                }
            }
        }

        for (BootSequence bs : BootSequence.values()) {
            if (bs.name().equals(sb.toString())) {
                ret = bs;
                break;
            }
        }
        return ret;
    }

    public static boolean isOldClusterVersion(VM vm) {
        Version version = vm.getvds_group_compatibility_version();
        return (!(version.getMajor() >= 3 && version.getMinor() >= 1));
    }
}

@@ -183,17 +183,23 @@
 tities.V
-M
+mDevice
 ;%0Aimport
@@ -232,33 +232,120 @@
 mon.
-businessentities.VmDevice
+config.Config;%0Aimport org.ovirt.engine.core.common.config.ConfigValues;%0Aimport org.ovirt.engine.core.compat.Guid
 ;%0Aim
@@ -551,16 +551,104 @@
 R = %22C%22;
+%0A    public final static String CDROM_IMAGE_ID = %2211111111-1111-1111-1111-111111111111%22;
 %0A%0A    pu
@@ -2441,152 +2441,1481 @@
 on(V
-M vm) %7B%0A        Version version = vm.getvds_group_compatibility_version();%0A        return (!(version.getMajor() %3E= 3 && version.getMinor() %3E= 1)
+ersion version) %7B%0A        return (!(version.getMajor() %3E= 3 && version.getMinor() %3E= 1));%0A    %7D%0A%0A    public static boolean isInWhiteList(String type, String device) %7B%0A        String expr = getDeviceTypeSearchExpr(type, device);%0A        String whiteList = Config.GetValue(ConfigValues.ManagedDevicesWhiteList);%0A        return (whiteList.indexOf(expr) %3E= 0);%0A    %7D%0A%0A    /**%0A     * appends device id key/value to specParams%0A     * @param deviceId%0A     * @param specParams%0A     * @return%0A     */%0A    public static String appendDeviceIdToSpecParams(Guid deviceId, String specParams) %7B%0A        StringBuilder sb = new StringBuilder();%0A        final String DEVICE = %22deviceId=%22;%0A        final String SEP = %22,%22;%0A        // device id is included in special parameters%0A        // this is done in order to recognize the device when changes are passed from VDSM%0A        if (specParams.indexOf(DEVICE) %3E= 0) %7B%0A            return specParams;%0A        %7D else %7B%0A            if (specParams.length() %3E 0) %7B%0A                sb.append(specParams);%0A                sb.append(SEP);%0A            %7D%0A            sb.append(DEVICE);%0A            sb.append(deviceId);%0A        %7D%0A        return sb.toString();%0A    %7D%0A%0A    private static String getDeviceTypeSearchExpr(String type, String device) %7B%0A        StringBuilder sb = new StringBuilder();%0A        sb.append(%22type=%22);%0A        sb.append(type);%0A        sb.append(%22 device=%22);%0A        sb.append(device);%0A        sb.append(%22 %22);%0A        return sb.toString(
 );%0A 
@@ -3919,10 +3919,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

add helper methods to VmDeviceCommonUtils.