/**
* Copyright (C) 2008 - Abiquo Holdings S.L. All rights reserved.
*
* Please see /opt/abiquo/tomcat/webapps/legal/ on Abiquo server
* or contact contact@abiquo.com for licensing information.
*/
package com.abiquo.hypervisor.util;
import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.isEmpty;
import java.util.Set;
import com.abiquo.hypervisor.model.SecondaryDiskStateful;
import com.abiquo.hypervisor.model.VirtualMachineDefinition;
import com.abiquo.hypervisor.plugin.exception.HypervisorPluginError;
import com.abiquo.hypervisor.plugin.exception.HypervisorPluginException;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
/**
* Utility classes to validate common stuff.
*
* @author Ignasi Barrera
*/
public class ValidationUtils
{
/**
* Checks if all persistent volumes in the given virtual machine definition (including the
* primary disk) are supported by the hypervisor.
*
* @param virtualMachineDefinition The virtual machine definition to validate.
* @param supportedTypes The types supported by the plugin.
* @throws HypervisorPluginException If the virtual machine definition has any unsupported
* persistent disk.
*/
public static void checkSupportedExternalStorage(
final VirtualMachineDefinition virtualMachineDefinition, final Set<String> supportedTypes)
throws HypervisorPluginException
{
if (virtualMachineDefinition.getPrimaryDisk().isStateful())
{
if (!supportedTypes.contains(virtualMachineDefinition.getPrimaryDisk()
.getDiskStateful().getType()))
{
throw new HypervisorPluginException(HypervisorPluginError.VIRTUALMACHINE_DEFINITION_VALIDATION_ERROR,
"All external disks must be of one of the following types: "
+ Joiner.on(',').join(supportedTypes));
}
}
boolean unsupportedSecondaries =
any(virtualMachineDefinition.getSecondaryDisks().getStatefulDisks(),
new Predicate<SecondaryDiskStateful>()
{
@Override
public boolean apply(final SecondaryDiskStateful input)
{
return !supportedTypes.contains(input.getType());
}
});
if (unsupportedSecondaries)
{
throw new HypervisorPluginException(HypervisorPluginError.VIRTUALMACHINE_DEFINITION_VALIDATION_ERROR,
"All external disks must be of one of the following types: "
+ Joiner.on(',').join(supportedTypes));
}
}
/**
* Check if the given virtual machine definition contains at least one NIC. Should be called
* from all plugins in validate method that force vm to use at least use one private IP.
*
* @param virtualMachineDefinition The virtual machine definition to validate.
* @throws HypervisorPluginException If the virtual machine definition does not contain any NIC
*/
public static void checkVirtualNicsNotEmpty(
final VirtualMachineDefinition virtualMachineDefinition) throws HypervisorPluginException
{
if (isEmpty(virtualMachineDefinition.getNetworkConfiguration().getVirtualNICs()))
{
throw new HypervisorPluginException(HypervisorPluginError.VIRTUAL_MACHINE_AT_LEAST_ONE_NIC_SHOULD_BE_LINKED);
}
}
/**
* Check if the given virtual machine definition has not defined the coresPerSocket value.
* Should be called from plugins in validate method, which does not support seeting the cores
* per socket value.
*
* @param virtualMachineDefinition The virtual machine definition to validate.
* @throws HypervisorPluginException if the cores per socket is not null
*/
public static void checkCoresPerSocketNotSet(
final VirtualMachineDefinition virtualMachineDefinition) throws HypervisorPluginException
{
if (virtualMachineDefinition.getHardwareConfiguration().getCoresPerSocket() != null)
{
throw new HypervisorPluginException(HypervisorPluginError.CORES_PER_SOCKET_UNSUPPORTED);
}
}
}