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;
11 import java.util.Objects;
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;
21 public class DataValidationException extends RuntimeException {
22 private static final long serialVersionUID = 1L;
24 public DataValidationException(final String message) {
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) {
31 throw new IllegalChildException(child, schema, childNodes, augments);
35 public static void checkLegalChild(final boolean isLegal, final YangInstanceIdentifier.PathArgument child, final DataSchemaNode schema,
36 final Set<QName> childNodes) {
38 throw new IllegalChildException(child, schema, childNodes);
42 public static void checkLegalChild(final boolean isLegal, final YangInstanceIdentifier.PathArgument child, final ChoiceSchemaNode schema) {
44 throw new IllegalChildException(child, schema);
48 public static void checkLegalData(final boolean isLegal, final String messageTemplate, final Object... messageAttrs) {
50 throw new DataValidationException(String.format(messageTemplate, messageAttrs));
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);
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);
65 final Object expected = keyValues.get(keyQName);
66 final Object actual = childNode.getValue();
68 checkListKey(nodeId, keyQName, expected, actual);
71 public static void checkListKey(final DataContainerChild<?, ?> childNode, final QName keyQName, final NodeIdentifierWithPredicates nodeId) {
72 if (childNode == null) {
73 throw new IllegalListKeyException(keyQName, nodeId);
77 private static final class IllegalChildException extends DataValidationException {
78 private static final long serialVersionUID = 1L;
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));
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));
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));
96 private static final class IllegalListKeyException extends DataValidationException {
97 private static final long serialVersionUID = 1L;
99 private IllegalListKeyException(final QName keyQName, final NodeIdentifierWithPredicates id) {
100 super(String.format("Key value not present for key: %s, in: %s", keyQName, id));
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));