private ClusterPolicyUnit getClusterPolicyUnit( ClusterPolicy clusterPolicy, Guid policyUnitId, Map<Guid, ClusterPolicyUnit> map) { ClusterPolicyUnit clusterPolicyUnit = map.get(policyUnitId); if (clusterPolicyUnit == null) { clusterPolicyUnit = new ClusterPolicyUnit(clusterPolicy.getId(), policyUnitId); map.put(policyUnitId, clusterPolicyUnit); } return clusterPolicyUnit; }
@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())); }
@Override public void update(ClusterPolicy clusterPolicy) { super.update(clusterPolicy); getCallsHandler() .executeModification( "DeleteClusterPolicyUnitsByClusterPolicyId", getCustomMapSqlParameterSource().addValue("id", clusterPolicy.getId())); List<ClusterPolicyUnit> clusterPolicyUnits = getclusterPolicyUnit(clusterPolicy); if (clusterPolicyUnits != null) { for (ClusterPolicyUnit clusterPolicyUnit : clusterPolicyUnits) { saveClusterPolicyUnit(clusterPolicyUnit); } } }
@Override public List<ClusterPolicy> getAll() { List<ClusterPolicy> clusterPolicies = super.getAll(); Map<Guid, ClusterPolicy> map = new HashMap<>(); for (ClusterPolicy clusterPolicy : clusterPolicies) { map.put(clusterPolicy.getId(), clusterPolicy); } List<ClusterPolicyUnit> clusterPolicyUnits = getCallsHandler() .executeReadList( "GetAllFromClusterPolicyUnits", createClusterPolicyUnitRowMapper(), getCustomMapSqlParameterSource()); fillClusterPolicy(map, clusterPolicyUnits); return clusterPolicies; }
@Override public ClusterPolicy get(Guid id) { ClusterPolicy clusterPolicy = super.get(id); if (clusterPolicy == null) { return null; } List<ClusterPolicyUnit> clusterPolicyUnits = getCallsHandler() .executeReadList( "GetClusterPolicyUnitsByClusterPolicyId", createClusterPolicyUnitRowMapper(), createIdParameterMapper(id)); Map<Guid, ClusterPolicy> map = new HashMap<Guid, ClusterPolicy>(); map.put(clusterPolicy.getId(), clusterPolicy); fillClusterPolicy(map, clusterPolicyUnits); return clusterPolicy; }
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; }