68faf94b670a20bf1f87be813b01fa5ac003dceb
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / CanonicalValueValidator.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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 package org.opendaylight.yangtools.yang.common;
9
10 import com.google.common.annotations.Beta;
11 import org.eclipse.jdt.annotation.NonNullByDefault;
12 import org.opendaylight.yangtools.concepts.Variant;
13
14 /**
15  * {@link CanonicalValue} validator interface. Implementations of this interface can perform further validation of
16  * representation state such that it conforms to a YANG type derived from a type with a {@link CanonicalValue}
17  * representation.
18  *
19  * <p>
20  * Note: this interface should not be directly implemented. Use {@link AbstractCanonicalValueValidator} instead.
21  *
22  * @param <T> canonical value type
23  * @param <V> validated canonical value type
24  * @author Robert Varga
25  */
26 @Beta
27 @NonNullByDefault
28 public interface CanonicalValueValidator<T extends CanonicalValue<T>, V extends T> {
29     /**
30      * Returns the instantiated representation class. The representation class is a {@link CanonicalValue} which
31      * understands the semantics of modeled data and has some internal representation of it. All {@link CanonicalValue}s
32      * which share the same representation class are considered equal if their internal state would result in the
33      * same canonical string representation as defined by the YANG data model.
34      *
35      * @return Representation {@link CanonicalValue} class.
36      */
37     Class<T> getRepresentationClass();
38
39     /**
40      * Return the class which captures the fact it was validated by this validator.
41      *
42      * @return Validated capture of the representation class.
43      */
44     Class<V> getValidatedRepresentationClass();
45
46     /**
47      * Validate a {@link CanonicalValue} representation. Implementations should override this method if they can
48      * provide a validation algorithm which does not rely on canonical strings but works on representation state only.
49      *
50      * @param value Representation value
51      * @return Validated representation or a {@link CanonicalValueViolation}
52      * @throws NullPointerException if {@code value} is null
53      */
54     default Variant<T, CanonicalValueViolation> validateRepresentation(final T value) {
55         return validateRepresentation(value, value.toCanonicalString());
56     }
57
58     /**
59      * Validate a {@link CanonicalValue} representation. Implementations can chose whether they operate on
60      * representation state or canonical string -- both are considered equivalent. Users should call this method if they
61      * have a representation readily available.
62      *
63      * @param value Representation value
64      * @param canonicalString Canonical string matching the representation value
65      * @return Validated representation or a {@link CanonicalValueViolation}
66      * @throws NullPointerException if {@code value} or {@code canonicalString} is null.
67      */
68     Variant<T, CanonicalValueViolation> validateRepresentation(T value, String canonicalString);
69 }