/**
* 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 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));
}
}
}