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