2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid;
10 import com.google.common.annotations.Beta;
12 import java.util.Objects;
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;
23 public class DataValidationException extends RuntimeException {
24 private static final long serialVersionUID = 1L;
26 public DataValidationException(final String message) {
30 public static void checkLegalChild(final boolean isLegal, final PathArgument child, final DataNodeContainer schema,
31 final Set<QName> childNodes, final Set<AugmentationIdentifier> augments) {
33 throw new IllegalChildException(child, schema, childNodes, augments);
37 public static void checkLegalChild(final boolean isLegal, final PathArgument child, final DataSchemaNode schema,
38 final Set<QName> childNodes) {
40 throw new IllegalChildException(child, schema, childNodes);
44 public static void checkLegalChild(final boolean isLegal, final PathArgument child, final ChoiceSchemaNode schema) {
46 throw new IllegalChildException(child, schema);
50 public static void checkLegalData(final boolean isLegal, final String messageTemplate,
51 final Object... messageAttrs) {
53 throw new DataValidationException(String.format(messageTemplate, messageAttrs));
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);
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);
69 final Object expected = keyValues.get(keyQName);
70 final Object actual = childNode.getValue();
72 checkListKey(nodeId, keyQName, expected, actual);
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);
82 private static final class IllegalChildException extends DataValidationException {
83 private static final long serialVersionUID = 1L;
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));
91 IllegalChildException(final PathArgument child, final ChoiceSchemaNode schema) {
92 super(String.format("Unknown child node: %s, not detected in choice: %s", child, schema));
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,
103 public static final class IllegalListKeyException extends DataValidationException {
104 private static final long serialVersionUID = 1L;
106 public IllegalListKeyException(final String format, final Object... args) {
107 super(String.format(format, args));
110 IllegalListKeyException(final QName keyQName, final NodeIdentifierWithPredicates id) {
111 this("Key value not present for key: %s, in: %s", keyQName, id);
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);