Add yang generated packages in .gitignore
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / resolver / validator / AbstractValidator.java
1 package org.opendaylight.groupbasedpolicy.resolver.validator;
2
3 /*
4  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
5  *
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
9  */
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
14 import org.opendaylight.yangtools.yang.binding.ChildOf;
15 import org.opendaylight.yangtools.yang.binding.DataContainer;
16
17 /**
18  * Abstract class for all Validators, that will be used to validate concrete
19  * Policy objects. Contains basic functions needed for validation.
20  *
21  * @param <U>
22  */
23 public abstract class AbstractValidator<U extends DataContainer> implements Validator<U> {
24
25     /**
26      * Instance of {@link PolicyResolver} that contains additional data required
27      * for individual validation process
28      */
29     private PolicyResolver policyResolver;
30
31     /**
32      *
33      * @return Instance of {@link PolicyResolver}
34      */
35     protected final PolicyResolver getPolicyResolver() {
36         return policyResolver;
37     }
38
39     /**
40      * {@link PolicyResolver} contains additional data required for individual
41      * validation process
42      *
43      * @param policyResolver Istance of {@link PolicyResolver}
44      */
45     public final void setPolicyResolver(PolicyResolver policyResolver) {
46         this.policyResolver = policyResolver;
47     }
48
49     @Override
50     public ValidationResult validate(U objectToValidate) {
51         ValidationResult result = new ValidationResult(this.getClass());
52
53         SimpleResult selfValidationResult = validateSelf(objectToValidate);
54
55         if (selfValidationResult.isFailure()) {
56             result.setResult(ValidationResult.Result.FAIL_BASE);
57         }
58
59         result.setDescription(selfValidationResult.getDescription());
60
61         List<? extends DataContainer> childObjects = getChildObjects(objectToValidate);
62         this.validateChildren(childObjects, result);
63
64         return result;
65     }
66
67     /**
68      * This function is supposed to do base validation of given object (e.g.
69      * metadata check).
70      *
71      * Individual validators can override this method, to provide their own
72      * validation function.
73      *
74      * @param objectToValidate Object to validate by validator
75      * @return {@link SimpleResult} object
76      */
77     protected SimpleResult validateSelf(U objectToValidate) {
78
79         return new SimpleResult(true, "Default validation");
80     }
81
82     /**
83      * Function to validate all children in list. Result of all validations is
84      * saved to its parent {@link ValidationResult}.
85      *
86      * @param childObjects List of objects to validate
87      * @param parentResult Result of base validation.
88      */
89     private void validateChildren(List<? extends DataContainer> childObjects, ValidationResult parentResult) {
90         if (childObjects == null) {
91             return;
92         }
93
94         for (DataContainer child : childObjects) {
95             Validator<DataContainer> validator = PolicyValidator.createValidator(child, getPolicyResolver());
96             if (validator != null) {
97                 parentResult.addChildResult(validator.validate(child));
98             }
99         }
100     }
101
102     /**
103      * Return list of all children objects of given object, that should be
104      * validated.
105      *
106      * Individual validators must override this method, if they want to execute
107      * validation on their children.
108      *
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}.
112      */
113     protected <T extends DataContainer & ChildOf<U>> List<T> getChildObjects(U objectToValidate) {
114         return Collections.emptyList();
115     }
116
117     @Override
118     public abstract Class<U> getType();
119 }