Ethanlm commented on a change in pull request #3215: Storm3585 - New compact 
Constraint config including maxCoLocationCnt
URL: https://github.com/apache/storm/pull/3215#discussion_r387440555
 
 

 ##########
 File path: 
storm-client/src/jvm/org/apache/storm/validation/ConfigValidation.java
 ##########
 @@ -850,6 +852,128 @@ public void validateField(String name, Object o) {
         }
     }
 
+    public static class CustomIsExactlyOneOfValidators extends Validator {
+        private Class<?>[] subValidators;
+        private List<String> validatorClassNames;
+
+        public CustomIsExactlyOneOfValidators(Map<String, Object> params) {
+            this.subValidators = (Class<?>[]) 
params.get(ConfigValidationAnnotations.ValidatorParams.VALUE_VALIDATOR_CLASSES);
+            this.validatorClassNames = 
Arrays.asList(subValidators).stream().map(x -> 
x.getName()).collect(Collectors.toList());
+        }
+
+        @Override
+        public void validateField(String name, Object o) {
+            if (o == null) {
+                throw new IllegalArgumentException("Field " + name + " must be 
set.");
+            }
+
+            HashMap<String, Exception> validatorExceptions = new HashMap<>();
+            Set<String> selectedValidators = new HashSet<>();
+            for (Class<?> vv : subValidators) {
+                Object valueValidator;
+                try {
+                    valueValidator = vv.getConstructor().newInstance();
+                } catch (Exception ex) {
+                    throw new IllegalArgumentException(vv.getName() + " 
instantiation failure", ex);
+                }
+                if (valueValidator instanceof Validator) {
+                    try {
+                        ((Validator) valueValidator).validateField(name + " " 
+ vv.getSimpleName() + " value", o);
+                        selectedValidators.add(vv.getName());
+                    } catch (Exception ex) {
+                        // only one will pass, so ignore all validation errors 
- stored for future use
+                        validatorExceptions.put(vv.getName(), ex);
+                    }
+                } else {
+                    String err = String.format("validator: %s cannot be used 
in CustomExactlyOneOfValidators to validate values. "
+                            + "Individual entry validators must a instance of 
Validator class", vv.getName());
+                    LOG.warn(err);
+                }
+            }
+            // check if one and only one validation succeeded
+            if (selectedValidators.isEmpty()) {
+                String parseErrs = String.join(";\n\t", 
validatorExceptions.entrySet().stream()
+                        .map(e -> String.format("%s:%s", e.getKey(), 
e.getValue())).collect(Collectors.toList()));
+                String err = String.format("Field %s must be one of %s; parse 
errors are \n\t%s", name,
+                        String.join(", ", validatorClassNames), parseErrs);
+                throw new IllegalArgumentException(err);
+            }
+            if (selectedValidators.size() > 1) {
+                throw new IllegalArgumentException("Field " + name + " must 
match exactly one of " + String.join(", ", selectedValidators));
+            }
+        }
+    }
+
+    public static class RasConstraintsTypeValidator extends Validator {
+        public static final String CONSTRAINT_TYPE_MAX_NODE_CO_LOCATION_CNT = 
"maxNodeCoLocationCnt";
+        public static final String CONSTRAINT_TYPE_INCOMPATIBLE_COMPONENTS = 
"incompatibleComponents";
+
+        public static class RasConstraint {
+            int maxNodeCoLocationCnt = -1;
+            Set<String> incompatibleComponents = new HashSet<>();
+        }
+
+        public Map<String, RasConstraint> rasConstraints = new HashMap<>(); // 
parsedConstraints
+
+        @Override
+        public void validateField(String name, Object o) {
+            if (o == null) {
+                throw new IllegalArgumentException("Field " + name + " must be 
set.");
+            }
+            if (!(o instanceof Map)) {
+                throw new IllegalArgumentException(
+                        "Field " + name + " must be an Iterable containing 
only Map of Maps");
+            }
+            Map<String, Object> map1 = (Map<String, Object>)o;
+            for (Map.Entry<String, Object> entry1: map1.entrySet()) {
+                String comp1 = entry1.getKey();
+                Object o2 = entry1.getValue();
+                RasConstraint rasConstraint = new RasConstraint();
+                rasConstraints.put(comp1, rasConstraint);
+                if (!(o2 instanceof Map)) {
+                    String err = String.format("Field %s, component %s, 
expecting constraints Map with keys [\"%s\", \"%s\"], in \"%s\"",
+                            name, comp1, 
CONSTRAINT_TYPE_MAX_NODE_CO_LOCATION_CNT, 
CONSTRAINT_TYPE_INCOMPATIBLE_COMPONENTS, o);
+                    throw new IllegalArgumentException(err);
+                }
+                Map<String, Object> map2 = (Map<String, Object>)o2;
+                for (Map.Entry<String, Object> entry2: map2.entrySet()) {
+                    String constraintType = entry2.getKey();
+                    Object o3 = entry2.getValue();
+                    switch (constraintType) {
+                        case CONSTRAINT_TYPE_MAX_NODE_CO_LOCATION_CNT:
+                            try {
+                                int intVal = Integer.parseInt("" + o3);
 
 Review comment:
   maybe `(String) o3` is clearer than `"" + o3`?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to