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