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