2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.resolver.validator;
11 import java.util.Collections;
12 import java.util.List;
14 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
15 import org.opendaylight.yangtools.yang.binding.ChildOf;
16 import org.opendaylight.yangtools.yang.binding.DataContainer;
19 * Abstract class for all Validators, that will be used to validate concrete
20 * Policy objects. Contains basic functions needed for validation.
24 public abstract class AbstractValidator<U extends DataContainer> implements Validator<U> {
27 * Instance of {@link PolicyResolver} that contains additional data required
28 * for individual validation process
30 private PolicyResolver policyResolver;
34 * @return Instance of {@link PolicyResolver}
36 protected final PolicyResolver getPolicyResolver() {
37 return policyResolver;
41 * {@link PolicyResolver} contains additional data required for individual
44 * @param policyResolver Istance of {@link PolicyResolver}
46 public final void setPolicyResolver(PolicyResolver policyResolver) {
47 this.policyResolver = policyResolver;
51 public ValidationResult validate(U objectToValidate) {
52 ValidationResult result = new ValidationResult(this.getClass());
54 SimpleResult selfValidationResult = validateSelf(objectToValidate);
56 if (selfValidationResult.isFailure()) {
57 result.setResult(ValidationResult.Result.FAIL_BASE);
60 result.setDescription(selfValidationResult.getDescription());
62 List<? extends DataContainer> childObjects = getChildObjects(objectToValidate);
63 this.validateChildren(childObjects, result);
69 * This function is supposed to do base validation of given object (e.g.
72 * Individual validators can override this method, to provide their own
73 * validation function.
75 * @param objectToValidate Object to validate by validator
76 * @return {@link SimpleResult} object
78 protected SimpleResult validateSelf(U objectToValidate) {
80 return new SimpleResult(true, "Default validation");
84 * Function to validate all children in list. Result of all validations is
85 * saved to its parent {@link ValidationResult}.
87 * @param childObjects List of objects to validate
88 * @param parentResult Result of base validation.
90 private void validateChildren(List<? extends DataContainer> childObjects, ValidationResult parentResult) {
91 if (childObjects == null) {
95 for (DataContainer child : childObjects) {
96 Validator<DataContainer> validator = PolicyValidator.createValidator(child, getPolicyResolver());
97 if (validator != null) {
98 parentResult.addChildResult(validator.validate(child));
104 * Return list of all children objects of given object, that should be
107 * Individual validators must override this method, if they want to execute
108 * validation on their children.
110 * @param objectToValidate Object that is validated by this validator
111 * @return List of child objects associated to {@code objectToValidate}. If
112 * there is not any child, return empty list or {@code null}.
114 protected <T extends DataContainer & ChildOf<U>> List<T> getChildObjects(U objectToValidate) {
115 return Collections.emptyList();
119 public abstract Class<U> getType();