Remove Augmentation{Identifier,Node}
[yangtools.git] / data / 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 com.google.common.annotations.Beta;
11 import java.util.Map;
12 import java.util.Objects;
13 import java.util.Set;
14 import org.opendaylight.yangtools.yang.common.QName;
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) {
31         if (!isLegal) {
32             throw new IllegalChildException(child, schema, childNodes);
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.body();
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, final Set<QName> childNodes) {
85             super(String.format(
86                 "Unknown child node: %s, does not belong to: %s as a direct child. Direct child nodes: %s",
87                 child, schema, childNodes));
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     @Beta
102     public static final class IllegalListKeyException extends DataValidationException {
103         private static final long serialVersionUID = 1L;
104
105         public IllegalListKeyException(final String format, final Object... args) {
106             super(String.format(format, args));
107         }
108
109         IllegalListKeyException(final QName keyQName, final NodeIdentifierWithPredicates id) {
110             this("Key value not present for key: %s, in: %s", keyQName, id);
111         }
112
113         IllegalListKeyException(final QName keyQName, final NodeIdentifierWithPredicates id, final Object actualValue,
114                 final Object expectedValue) {
115             this("Illegal value for key: %s, in: %s, actual value: %s, expected value from key: %s",
116                 keyQName, id, actualValue, expectedValue);
117         }
118     }
119 }