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.NodeIdentifierWithPredicates;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 public class DataValidationException extends RuntimeException {
23 private static final long serialVersionUID = 1L;
25 public DataValidationException(final String message) {
29 public static void checkLegalChild(final boolean isLegal, final PathArgument child, final DataNodeContainer schema,
30 final Set<QName> childNodes) {
32 throw new IllegalChildException(child, schema, childNodes);
36 public static void checkLegalChild(final boolean isLegal, final PathArgument child, final DataSchemaNode schema,
37 final Set<QName> childNodes) {
39 throw new IllegalChildException(child, schema, childNodes);
43 public static void checkLegalChild(final boolean isLegal, final PathArgument child, final ChoiceSchemaNode schema) {
45 throw new IllegalChildException(child, schema);
49 public static void checkLegalData(final boolean isLegal, final String messageTemplate,
50 final Object... messageAttrs) {
52 throw new DataValidationException(String.format(messageTemplate, messageAttrs));
56 public static void checkListKey(final NodeIdentifierWithPredicates nodeId, final QName keyQName,
57 final Object expected, final Object actual) {
58 // Objects.equals() does not deal with arrays, but is faster
59 if (!Objects.equals(expected, actual) && !Objects.deepEquals(expected, actual)) {
60 throw new IllegalListKeyException(keyQName, nodeId, actual, expected);
64 public static void checkListKey(final DataContainerChild childNode, final Map<QName, Object> keyValues,
65 final QName keyQName, final NodeIdentifierWithPredicates nodeId) {
66 checkListKey(childNode, keyQName, nodeId);
68 final Object expected = keyValues.get(keyQName);
69 final Object actual = childNode.body();
71 checkListKey(nodeId, keyQName, expected, actual);
74 public static void checkListKey(final DataContainerChild childNode, final QName keyQName,
75 final NodeIdentifierWithPredicates nodeId) {
76 if (childNode == null) {
77 throw new IllegalListKeyException(keyQName, nodeId);
81 private static final class IllegalChildException extends DataValidationException {
82 private static final long serialVersionUID = 1L;
84 IllegalChildException(final PathArgument child, final DataNodeContainer schema, final Set<QName> childNodes) {
86 "Unknown child node: %s, does not belong to: %s as a direct child. Direct child nodes: %s",
87 child, schema, childNodes));
90 IllegalChildException(final PathArgument child, final ChoiceSchemaNode schema) {
91 super(String.format("Unknown child node: %s, not detected in choice: %s", child, schema));
94 IllegalChildException(final PathArgument child, final DataSchemaNode schema,
95 final Set<QName> childNodes) {
96 super(String.format("Unknown child node: %s, does not belong to: %s as a child. Child nodes: %s", child,
102 public static final class IllegalListKeyException extends DataValidationException {
103 private static final long serialVersionUID = 1L;
105 public IllegalListKeyException(final String format, final Object... args) {
106 super(String.format(format, args));
109 IllegalListKeyException(final QName keyQName, final NodeIdentifierWithPredicates id) {
110 this("Key value not present for key: %s, in: %s", keyQName, id);
113 IllegalListKeyException(final QName keyQName, final NodeIdentifierWithPredicates id, final Object actualValue,
114 final Object expectedValue) {
115 this("Illegal value for key: %s, in: %s, actual value: %s, expected value from key: %s",
116 keyQName, id, actualValue, expectedValue);