bipinprasad commented on a change in pull request #3215: Storm3585 - New 
compact Constraint config including maxNodeCoLocationCnt and 
incompatibleComponents
URL: https://github.com/apache/storm/pull/3215#discussion_r389758936
 
 

 ##########
 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) {
+                return;
+            }
+
+            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
 
 Review comment:
   Originally I thought this could be used in testing and parsing. But due to 
package structure it cant. Removed this class. Check for validity only (also 
added test for the String in list check.

----------------------------------------------------------------
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