Search in sources :

Example 1 with ValidationError

use of org.ovirt.engine.core.common.utils.customprop.ValidationError in project ovirt-engine by oVirt.

the class HostSetupNetworksValidator method validateCustomProperties.

ValidationResult validateCustomProperties(SimpleCustomPropertiesUtil util, Map<String, String> validPropertiesForVm, Map<String, String> validPropertiesForNonVm) {
    for (NetworkAttachment attachment : params.getNetworkAttachments()) {
        Network network = existingNetworkRelatedToAttachment(attachment);
        if (attachment.hasProperties()) {
            List<ValidationError> errors = util.validateProperties(network.isVmNetwork() ? validPropertiesForVm : validPropertiesForNonVm, attachment.getProperties());
            if (!errors.isEmpty()) {
                handleCustomPropertiesError(util, errors);
                EngineMessage engineMessage = EngineMessage.ACTION_TYPE_FAILED_NETWORK_CUSTOM_PROPERTIES_BAD_INPUT;
                return new ValidationResult(engineMessage, ReplacementUtils.getVariableAssignmentStringWithMultipleValues(engineMessage, network.getName()));
            }
        }
    }
    return ValidationResult.VALID;
}
Also used : FindActiveVmsUsingNetwork(org.ovirt.engine.core.bll.network.FindActiveVmsUsingNetwork) Network(org.ovirt.engine.core.common.businessentities.network.Network) ValidationError(org.ovirt.engine.core.common.utils.customprop.ValidationError) ValidationResult(org.ovirt.engine.core.bll.ValidationResult) EngineMessage(org.ovirt.engine.core.common.errors.EngineMessage) NetworkAttachment(org.ovirt.engine.core.common.businessentities.network.NetworkAttachment)

Example 2 with ValidationError

use of org.ovirt.engine.core.common.utils.customprop.ValidationError in project ovirt-engine by oVirt.

the class ClusterPolicyCRUDCommand method checkAddEditValidations.

protected boolean checkAddEditValidations() {
    List<ClusterPolicy> clusterPolicies = schedulingManager.getClusterPolicies();
    if (getClusterPolicy() == null) {
        return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_PARAMETERS_INVALID);
    }
    for (ClusterPolicy clusterPolicy : clusterPolicies) {
        if (!clusterPolicy.getId().equals(getClusterPolicy().getId()) && clusterPolicy.getName().equals(getClusterPolicy().getName())) {
            return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_NAME_INUSE);
        }
    }
    Map<Guid, PolicyUnitImpl> map = schedulingManager.getPolicyUnitsMap();
    Set<Guid> existingPolicyUnits = new HashSet<>();
    // check filter policy units
    if (getClusterPolicy().getFilters() != null) {
        for (Guid filterId : getClusterPolicy().getFilters()) {
            if (isPolicyUnitExists(filterId, existingPolicyUnits)) {
                return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_DUPLICATE_POLICY_UNIT);
            }
            PolicyUnitImpl policyUnitImpl = map.get(filterId);
            if (policyUnitImpl == null) {
                return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_UNKNOWN_POLICY_UNIT);
            }
            if (policyUnitImpl.getPolicyUnit().getPolicyUnitType() != PolicyUnitType.FILTER) {
                return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_FILTER_NOT_IMPLEMENTED);
            }
        }
    }
    // check filters positions (there could be only one filter attached to first (-1) and last (-1)
    if (getClusterPolicy().getFilterPositionMap() != null) {
        boolean hasFirst = false;
        boolean hasLast = false;
        for (Integer position : getClusterPolicy().getFilterPositionMap().values()) {
            if (position == -1) {
                if (!hasFirst) {
                    hasFirst = true;
                } else {
                    return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_ONLY_ONE_FILTER_CAN_BE_FIRST);
                }
            } else if (position == 1) {
                if (!hasLast) {
                    hasLast = true;
                } else {
                    return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_ONLY_ONE_FILTER_CAN_BE_LAST);
                }
            }
        }
    }
    // check function policy units
    if (getClusterPolicy().getFunctions() != null) {
        for (Pair<Guid, Integer> functionPair : getClusterPolicy().getFunctions()) {
            if (isPolicyUnitExists(functionPair.getFirst(), existingPolicyUnits)) {
                return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_DUPLICATE_POLICY_UNIT);
            }
            PolicyUnitImpl policyUnitImpl = map.get(functionPair.getFirst());
            if (policyUnitImpl == null) {
                return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_UNKNOWN_POLICY_UNIT);
            }
            if (policyUnitImpl.getPolicyUnit().getPolicyUnitType() != PolicyUnitType.WEIGHT) {
                return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_FUNCTION_NOT_IMPLEMENTED);
            }
            if (functionPair.getSecond() < 0) {
                return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_FUNCTION_FACTOR_NEGATIVE);
            }
        }
    }
    // check balance policy unit
    if (getClusterPolicy().getBalance() != null) {
        PolicyUnitImpl policyUnitImpl = map.get(getClusterPolicy().getBalance());
        if (policyUnitImpl == null) {
            return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_UNKNOWN_POLICY_UNIT);
        }
        if (policyUnitImpl.getPolicyUnit().getPolicyUnitType() != PolicyUnitType.LOAD_BALANCING) {
            return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_BALANCE_NOT_IMPLEMENTED);
        }
    }
    // check selector policy unit
    if (getClusterPolicy().getSelector() != null) {
        PolicyUnitImpl policyUnitImpl = map.get(getClusterPolicy().getSelector());
        if (policyUnitImpl == null) {
            return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_UNKNOWN_POLICY_UNIT);
        }
        if (policyUnitImpl.getPolicyUnit().getPolicyUnitType() != PolicyUnitType.SELECTOR) {
            return failValidation(EngineMessage.ACTION_TYPE_FAILED_CLUSTER_POLICY_SELECTOR_NOT_IMPLEMENTED);
        }
    }
    List<ValidationError> validationErrors = SimpleCustomPropertiesUtil.getInstance().validateProperties(schedulingManager.getCustomPropertiesRegexMap(getClusterPolicy()), getClusterPolicy().getParameterMap());
    if (!validationErrors.isEmpty()) {
        SimpleCustomPropertiesUtil.getInstance().handleCustomPropertiesError(validationErrors, getReturnValue().getValidationMessages());
        return false;
    }
    return true;
}
Also used : PolicyUnitImpl(org.ovirt.engine.core.bll.scheduling.PolicyUnitImpl) Guid(org.ovirt.engine.core.compat.Guid) ValidationError(org.ovirt.engine.core.common.utils.customprop.ValidationError) ClusterPolicy(org.ovirt.engine.core.common.scheduling.ClusterPolicy) HashSet(java.util.HashSet)

Example 3 with ValidationError

use of org.ovirt.engine.core.common.utils.customprop.ValidationError in project ovirt-engine by oVirt.

the class DevicePropertiesUtils method validateProperties.

/**
 * Validates the custom properties of a device. These errors are checked during validation:
 * <ol>
 *   <li>Device custom properties are supported in version</li>
 *   <li>Device custom properties can be assigned to specified type</li>
 *   <li>Device custom properties syntax is ok</li>
 *   <li>Device custom property is defined for specified version and type</li>
 *   <li>Device custom property value matches its value constrains</li>
 * </ol>
 *
 * @param version
 *            version of the cluster that the VM containing the device is in
 * @param type
 *            type of a device
 * @param properties
 *            device custom properties
 * @return list of errors appeared during validation
 */
public List<ValidationError> validateProperties(Version version, VmDeviceGeneralType type, Map<String, String> properties) {
    if (properties == null || properties.isEmpty()) {
        // No errors in case of empty value
        return Collections.emptyList();
    }
    if (!supportedDeviceTypes.contains(type)) {
        return invalidDeviceTypeValidationError;
    }
    if (syntaxErrorInProperties(properties)) {
        return invalidSyntaxValidationError;
    }
    List<ValidationError> results = new ArrayList<>();
    Set<ValidationError> errorsSet = new HashSet<>();
    for (Map.Entry<String, String> e : properties.entrySet()) {
        String key = e.getKey();
        if (key == null || !deviceProperties.get(version).get(type).containsKey(key)) {
            errorsSet.add(new ValidationError(ValidationFailureReason.KEY_DOES_NOT_EXIST, key));
            continue;
        }
        String value = StringUtils.defaultString(e.getValue());
        if (!value.matches(deviceProperties.get(version).get(type).get(key))) {
            errorsSet.add(new ValidationError(ValidationFailureReason.INCORRECT_VALUE, key));
            continue;
        }
    }
    results.addAll(errorsSet);
    return results;
}
Also used : ArrayList(java.util.ArrayList) ValidationError(org.ovirt.engine.core.common.utils.customprop.ValidationError) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with ValidationError

use of org.ovirt.engine.core.common.utils.customprop.ValidationError in project ovirt-engine by oVirt.

the class ClusterOperationCommandBase method validateClusterPolicy.

protected boolean validateClusterPolicy(Cluster oldCluster) {
    Cluster newCluster = getCluster();
    boolean alreadyInUpgradeMode = oldCluster != null && oldCluster.isInUpgradeMode();
    ClusterPolicy clusterPolicy = getClusterPolicy(newCluster);
    if (clusterPolicy == null) {
        return false;
    }
    newCluster.setClusterPolicyId(clusterPolicy.getId());
    if (alreadyInUpgradeMode && !newCluster.isInUpgradeMode()) {
        // Check if we can safely stop the cluster upgrade
        final List<VDS> hosts = vdsDao.getAllForCluster(getClusterId());
        if (!validate(getUpgradeValidator().isUpgradeDone(hosts))) {
            return false;
        }
    } else if (!alreadyInUpgradeMode && newCluster.isInUpgradeMode()) {
        final List<VDS> hosts = vdsDao.getAllForCluster(getClusterId());
        final List<VM> vms = vmDao.getAllForCluster(getClusterId());
        populateVMNUMAInfo(vms);
        if (!validate(getUpgradeValidator().isUpgradePossible(hosts, vms))) {
            return false;
        }
    }
    Map<String, String> customPropertiesRegexMap = getSchedulingManager().getCustomPropertiesRegexMap(clusterPolicy);
    updateClusterPolicyProperties(getCluster(), clusterPolicy, customPropertiesRegexMap);
    List<ValidationError> validationErrors = SimpleCustomPropertiesUtil.getInstance().validateProperties(customPropertiesRegexMap, getCluster().getClusterPolicyProperties());
    if (!validationErrors.isEmpty()) {
        SimpleCustomPropertiesUtil.getInstance().handleCustomPropertiesError(validationErrors, getReturnValue().getValidationMessages());
        return false;
    }
    return true;
}
Also used : VDS(org.ovirt.engine.core.common.businessentities.VDS) Cluster(org.ovirt.engine.core.common.businessentities.Cluster) NetworkCluster(org.ovirt.engine.core.common.businessentities.network.NetworkCluster) ArrayList(java.util.ArrayList) List(java.util.List) ValidationError(org.ovirt.engine.core.common.utils.customprop.ValidationError) ClusterPolicy(org.ovirt.engine.core.common.scheduling.ClusterPolicy)

Example 5 with ValidationError

use of org.ovirt.engine.core.common.utils.customprop.ValidationError in project ovirt-engine by oVirt.

the class VnicProfileValidator method validateCustomProperties.

public boolean validateCustomProperties(List<String> messages) {
    StoragePool dataCenter = dcDao.get(getNetwork().getDataCenterId());
    List<ValidationError> errors = DevicePropertiesUtils.getInstance().validateProperties(dataCenter.getCompatibilityVersion(), VmDeviceGeneralType.INTERFACE, vnicProfile.getCustomProperties());
    if (!errors.isEmpty()) {
        DevicePropertiesUtils.getInstance().handleCustomPropertiesError(errors, messages);
        return false;
    }
    return true;
}
Also used : StoragePool(org.ovirt.engine.core.common.businessentities.StoragePool) ValidationError(org.ovirt.engine.core.common.utils.customprop.ValidationError)

Aggregations

ValidationError (org.ovirt.engine.core.common.utils.customprop.ValidationError)5 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ClusterPolicy (org.ovirt.engine.core.common.scheduling.ClusterPolicy)2 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ValidationResult (org.ovirt.engine.core.bll.ValidationResult)1 FindActiveVmsUsingNetwork (org.ovirt.engine.core.bll.network.FindActiveVmsUsingNetwork)1 PolicyUnitImpl (org.ovirt.engine.core.bll.scheduling.PolicyUnitImpl)1 Cluster (org.ovirt.engine.core.common.businessentities.Cluster)1 StoragePool (org.ovirt.engine.core.common.businessentities.StoragePool)1 VDS (org.ovirt.engine.core.common.businessentities.VDS)1 Network (org.ovirt.engine.core.common.businessentities.network.Network)1 NetworkAttachment (org.ovirt.engine.core.common.businessentities.network.NetworkAttachment)1 NetworkCluster (org.ovirt.engine.core.common.businessentities.network.NetworkCluster)1 EngineMessage (org.ovirt.engine.core.common.errors.EngineMessage)1 Guid (org.ovirt.engine.core.compat.Guid)1