Eliminate synthetic bridge methods in DataValidationException
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / valid / DataValidationException.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid;
9
10 import java.util.Map;
11 import java.util.Objects;
12 import java.util.Set;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21
22 public class DataValidationException extends RuntimeException {
23     private static final long serialVersionUID = 1L;
24
25     public DataValidationException(final String message) {
26         super(message);
27     }
28
29     public static void checkLegalChild(final boolean isLegal, final PathArgument child, final DataNodeContainer schema,
30             final Set<QName> childNodes, final Set<AugmentationIdentifier> augments) {
31         if (!isLegal) {
32             throw new IllegalChildException(child, schema, childNodes, augments);
33         }
34     }
35
36     public static void checkLegalChild(final boolean isLegal, final PathArgument child, final DataSchemaNode schema,
37             final Set<QName> childNodes) {
38         if (!isLegal) {
39             throw new IllegalChildException(child, schema, childNodes);
40         }
41     }
42
43     public static void checkLegalChild(final boolean isLegal, final PathArgument child, final ChoiceSchemaNode schema) {
44         if (!isLegal) {
45             throw new IllegalChildException(child, schema);
46         }
47     }
48
49     public static void checkLegalData(final boolean isLegal, final String messageTemplate,
50             final Object... messageAttrs) {
51         if (!isLegal) {
52             throw new DataValidationException(String.format(messageTemplate, messageAttrs));
53         }
54     }
55
56     public static void checkListKey(final NodeIdentifierWithPredicates nodeId, final QName keyQName,
57             final Object expected, final Object actual) {
58         // Objects.equals() does not deal with arrays, but is faster
59         if (!Objects.equals(expected, actual) && !Objects.deepEquals(expected, actual)) {
60             throw new IllegalListKeyException(keyQName, nodeId, actual, expected);
61         }
62     }
63
64     public static void checkListKey(final DataContainerChild<?, ?> childNode, final Map<QName, Object> keyValues,
65             final QName keyQName, final NodeIdentifierWithPredicates nodeId) {
66         checkListKey(childNode, keyQName, nodeId);
67
68         final Object expected = keyValues.get(keyQName);
69         final Object actual = childNode.getValue();
70
71         checkListKey(nodeId, keyQName, expected, actual);
72     }
73
74     public static void checkListKey(final DataContainerChild<?, ?> childNode, final QName keyQName,
75             final NodeIdentifierWithPredicates nodeId) {
76         if (childNode == null) {
77             throw new IllegalListKeyException(keyQName, nodeId);
78         }
79     }
80
81     private static final class IllegalChildException extends DataValidationException {
82         private static final long serialVersionUID = 1L;
83
84         IllegalChildException(final PathArgument child, final DataNodeContainer schema,
85                 final Set<QName> childNodes, final Set<AugmentationIdentifier> augments) {
86             super(String.format("Unknown child node: %s, does not belong to: %s as a direct child. "
87                     + "Direct child nodes: %s, augmented child nodes: %s", child, schema, childNodes, augments));
88         }
89
90         IllegalChildException(final PathArgument child, final ChoiceSchemaNode schema) {
91             super(String.format("Unknown child node: %s, not detected in choice: %s", child, schema));
92         }
93
94         IllegalChildException(final PathArgument child, final DataSchemaNode schema,
95                 final Set<QName> childNodes) {
96             super(String.format("Unknown child node: %s, does not belong to: %s as a child. Child nodes: %s", child,
97                 schema, childNodes));
98         }
99     }
100
101     private static final class IllegalListKeyException extends DataValidationException {
102         private static final long serialVersionUID = 1L;
103
104         IllegalListKeyException(final QName keyQName, final NodeIdentifierWithPredicates id) {
105             super(String.format("Key value not present for key: %s, in: %s", keyQName, id));
106         }
107
108         IllegalListKeyException(final QName keyQName, final NodeIdentifierWithPredicates id, final Object actualValue,
109                 final Object expectedValue) {
110             super(String.format("Illegal value for key: %s, in: %s, actual value: %s, expected value from key: %s",
111                 keyQName, id, actualValue, expectedValue));
112         }
113     }
114 }