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;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
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;
20 public class DataValidationException extends RuntimeException {
21 private static final long serialVersionUID = 1L;
23 public DataValidationException(final String message) {
27 public static void checkLegalChild(final boolean isLegal, final InstanceIdentifier.PathArgument child, final DataNodeContainer schema,
28 final Set<QName> childNodes, final Set<InstanceIdentifier.AugmentationIdentifier> augments) {
29 if (isLegal == false) {
30 throw new IllegalChildException(child, schema, childNodes, augments);
34 public static void checkLegalChild(final boolean isLegal, final InstanceIdentifier.PathArgument child, final DataSchemaNode schema,
35 final Set<QName> childNodes) {
36 if (isLegal == false) {
37 throw new IllegalChildException(child, schema, childNodes);
41 public static void checkLegalChild(final boolean isLegal, final InstanceIdentifier.PathArgument child, final ChoiceNode schema) {
42 if (isLegal == false) {
43 throw new IllegalChildException(child, schema);
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));
53 public static void checkListKey(final DataContainerChild<?, ?> childNode, final Map<QName, Object> keyValues, final QName keyQName,
54 final InstanceIdentifier.NodeIdentifierWithPredicates nodeId) {
55 checkListKey(childNode, keyQName, nodeId);
57 Object expectedValue = nodeId.getKeyValues().get(keyQName);
58 Object actualValue = childNode.getValue();
59 if (childNode == null) {
60 throw new IllegalListKeyException(keyQName, nodeId, actualValue, expectedValue);
64 public static void checkListKey(final DataContainerChild<?, ?> childNode, final QName keyQName, final InstanceIdentifier.NodeIdentifierWithPredicates nodeId) {
65 if (childNode == null) {
66 throw new IllegalListKeyException(keyQName, nodeId);
70 static final class IllegalChildException extends DataValidationException {
71 private static final long serialVersionUID = 1L;
73 public IllegalChildException(final InstanceIdentifier.PathArgument child, final DataNodeContainer schema,
74 final Set<QName> childNodes, final Set<InstanceIdentifier.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));
79 public IllegalChildException(final InstanceIdentifier.PathArgument child, final ChoiceNode schema) {
80 super(String.format("Unknown child node: %s, not detected in choice: %s", child, schema));
83 public IllegalChildException(final InstanceIdentifier.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));
89 static final class IllegalListKeyException extends DataValidationException {
90 private static final long serialVersionUID = 1L;
92 public IllegalListKeyException(final QName keyQName, final InstanceIdentifier.NodeIdentifierWithPredicates id) {
93 super(String.format("Key value not present for key: %s, in: %s", keyQName, id));
96 public IllegalListKeyException(final QName keyQName, final InstanceIdentifier.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));