예제 #1
0
 @Override
 protected MapSqlParameterSource createFullParametersMapper(ClusterPolicy entity) {
   return createIdParameterMapper(entity.getId())
       .addValue("name", entity.getName())
       .addValue("description", entity.getDescription())
       .addValue("is_locked", entity.isLocked())
       .addValue("is_default", entity.isDefaultPolicy())
       .addValue(
           "custom_properties",
           SerializationFactory.getSerializer().serialize(entity.getParameterMap()));
 }
  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);
      }
    }

    List<ValidationError> validationErrors =
        SimpleCustomPropertiesUtil.getInstance()
            .validateProperties(
                schedulingManager.getCustomPropertiesRegexMap(getClusterPolicy()),
                getClusterPolicy().getParameterMap());
    if (!validationErrors.isEmpty()) {
      SimpleCustomPropertiesUtil.getInstance()
          .handleCustomPropertiesError(validationErrors, getReturnValue().getValidationMessages());
      return false;
    }
    return true;
  }