1 package org.opendaylight.groupbasedpolicy.resolver.validator;
4 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
6 * This program and the accompanying materials are made available under the
7 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 import java.util.Collections;
11 import java.util.List;
13 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
14 import org.opendaylight.yangtools.yang.binding.ChildOf;
15 import org.opendaylight.yangtools.yang.binding.DataContainer;
18 * Abstract class for all Validators, that will be used to validate concrete
19 * Policy objects. Contains basic functions needed for validation.
23 public abstract class AbstractValidator<U extends DataContainer> implements Validator<U> {
26 * Instance of {@link PolicyResolver} that contains additional data required
27 * for individual validation process
29 private PolicyResolver policyResolver;
33 * @return Instance of {@link PolicyResolver}
35 protected final PolicyResolver getPolicyResolver() {
36 return policyResolver;
40 * {@link PolicyResolver} contains additional data required for individual
43 * @param policyResolver Istance of {@link PolicyResolver}
45 public final void setPolicyResolver(PolicyResolver policyResolver) {
46 this.policyResolver = policyResolver;
50 public ValidationResult validate(U objectToValidate) {
51 ValidationResult result = new ValidationResult(this.getClass());
53 SimpleResult selfValidationResult = validateSelf(objectToValidate);
55 if (selfValidationResult.isFailure()) {
56 result.setResult(ValidationResult.Result.FAIL_BASE);
59 result.setDescription(selfValidationResult.getDescription());
61 List<? extends DataContainer> childObjects = getChildObjects(objectToValidate);
62 this.validateChildren(childObjects, result);
68 * This function is supposed to do base validation of given object (e.g.
71 * Individual validators can override this method, to provide their own
72 * validation function.
74 * @param objectToValidate Object to validate by validator
75 * @return {@link SimpleResult} object
77 protected SimpleResult validateSelf(U objectToValidate) {
79 return new SimpleResult(true, "Default validation");
83 * Function to validate all children in list. Result of all validations is
84 * saved to its parent {@link ValidationResult}.
86 * @param childObjects List of objects to validate
87 * @param parentResult Result of base validation.
89 private void validateChildren(List<? extends DataContainer> childObjects, ValidationResult parentResult) {
90 if (childObjects == null) {
94 for (DataContainer child : childObjects) {
95 Validator<DataContainer> validator = PolicyValidator.createValidator(child, getPolicyResolver());
96 if (validator != null) {
97 parentResult.addChildResult(validator.validate(child));
103 * Return list of all children objects of given object, that should be
106 * Individual validators must override this method, if they want to execute
107 * validation on their children.
109 * @param objectToValidate Object that is validated by this validator
110 * @return List of child objects associated to {@code objectToValidate}. If
111 * there is not any child, return empty list or {@code null}.
113 protected <T extends DataContainer & ChildOf<U>> List<T> getChildObjects(U objectToValidate) {
114 return Collections.emptyList();
118 public abstract Class<U> getType();