Added response data to operational datastore, refactored data validation
[packetcable.git] / packetcable-policy-server / src / main / java / org / opendaylight / controller / packetcable / provider / validation / impl / validators / AbstractValidator.java
1 /*
2  * Copyright (c) 2015 CableLabs 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.controller.packetcable.provider.validation.impl.validators;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import javax.annotation.concurrent.NotThreadSafe;
14 import org.opendaylight.controller.packetcable.provider.validation.ValidationException;
15 import org.opendaylight.controller.packetcable.provider.validation.Validator;
16
17 /**
18  * Helper class to help support lazy initialization of error message array.<br>
19  * This delays array creation until bad data is found.
20  * <br><br>
21  * <strong>Subclasses must call {@link #throwErrorsIfNeeded()} at the end of validate()</strong>
22  *
23  * @author rvail
24  */
25 @NotThreadSafe
26 public abstract class AbstractValidator<T> implements Validator<T> {
27
28     private ArrayList<String> errorMessages = null;
29
30     /**
31      * If any error messages have been added to the list returned by {@link #getErrorMessages()}
32      * then a ValidationException will be thrown with those error messages.
33      *
34      * @throws ValidationException
35      */
36     protected void throwErrorsIfNeeded() throws ValidationException {
37         if (errorMessages != null && !errorMessages.isEmpty()) {
38             ValidationException exception = new ValidationException(errorMessages);
39             resetErrorMessages();
40             throw exception;
41         }
42     }
43
44     /**
45      * sets the error message list to null.
46      */
47     protected void resetErrorMessages() {
48         errorMessages = null;
49     }
50
51     /**
52      * Checks if the passed in object is null. If it is, then an error message will be
53      * appended to the current list of errors.
54      *
55      * @param obj
56      *         The object that must not be null.
57      * @param name
58      *         The name of the object (will be used in the error message).
59      */
60     protected void mustExist(Object obj, String name) {
61         if (obj == null) {
62             getErrorMessages().add(name + " must exist");
63         }
64     }
65
66     /**
67      * Lazy initalizer of an array list of error messages.
68      *
69      * @return The array list of error messages
70      */
71     protected ArrayList<String> getErrorMessages() {
72         if (errorMessages == null) {
73             errorMessages = new ArrayList<>(2);
74         }
75         return errorMessages;
76     }
77
78     /**
79      * Checks if the passed in collection is null or empty. If it is then an error
80      * will be appended to the current list of errors.
81      *
82      * @param collection
83      *         The collection to test
84      * @param name
85      *         The name of the object (will be used in the error message)
86      */
87     protected void mustExistAndNotBeEmpty(Collection<?> collection, String name) {
88         if (collection == null) {
89             getErrorMessages().add(name + " must exist");
90         } else if (collection.isEmpty()) {
91             getErrorMessages().add(name + " must not be empty");
92         }
93     }
94
95     protected <C> void validateChild(Validator<C> validator, C child) {
96         try {
97             validator.validate(child, Extent.NODE_AND_SUBTREE);
98         } catch (ValidationException e) {
99             getErrorMessages().addAll(e.getErrorMessages());
100         }
101     }
102 }