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 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;
20 public class DataValidationException extends RuntimeException {
22 public DataValidationException(String message) {
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);
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);
40 public static void checkLegalChild(boolean isLegal, InstanceIdentifier.PathArgument child, ChoiceNode schema) {
41 if (isLegal == false) {
42 throw new IllegalChildException(child, schema);
46 public static void checkLegalData(boolean isLegal, String messageTemplate, Object... messageAttrs) {
47 if (isLegal == false) {
48 throw new DataValidationException(String.format(messageTemplate, messageAttrs));
52 public static void checkListKey(DataContainerChild<?, ?> childNode, Map<QName, Object> keyValues, QName keyQName,
53 InstanceIdentifier.NodeIdentifierWithPredicates nodeId) {
54 checkListKey(childNode, keyQName, nodeId);
56 Object expectedValue = nodeId.getKeyValues().get(keyQName);
57 Object actualValue = childNode.getValue();
58 if (childNode == null) {
59 throw new IllegalListKeyException(keyQName, nodeId, actualValue, expectedValue);
63 public static void checkListKey(DataContainerChild<?, ?> childNode, QName keyQName, InstanceIdentifier.NodeIdentifierWithPredicates nodeId) {
64 if (childNode == null) {
65 throw new IllegalListKeyException(keyQName, nodeId);
69 static final class IllegalChildException extends DataValidationException {
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));
77 public IllegalChildException(InstanceIdentifier.PathArgument child, ChoiceNode schema) {
78 super(String.format("Unknown child node: %s, not detected in choice: %s", child, schema));
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));
87 static final class IllegalListKeyException extends DataValidationException {
89 public IllegalListKeyException(QName keyQName, InstanceIdentifier.NodeIdentifierWithPredicates id) {
90 super(String.format("Key value not present for key: %s, in: %s", keyQName, id));
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));