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