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