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.operations;
10 import org.opendaylight.yangtools.yang.common.QName;
11 import org.opendaylight.yangtools.yang.data.api.Node;
12 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import com.google.common.base.Optional;
21 public class DataModificationException extends Exception {
22 // TODO replace QName as identifier for node with schema or NodeIdentifier,
23 // Augmentation does not have a QName
25 private static final long serialVersionUID = 1L;
26 private final QName node;
28 public DataModificationException(String message, QName node) {
33 public QName getNodeQName() {
37 public static final class DataMissingException extends DataModificationException {
38 private static final long serialVersionUID = 1L;
40 public DataMissingException(QName nodeType) {
41 super(String.format("Data missing for node: %s", nodeType), nodeType);
44 public DataMissingException(QName nodeType, Node<?> modificationNode) {
45 super(String.format("Data missing for node: %s, %s", nodeType, modificationNode), nodeType);
48 static void check(QName nodeQName, Optional<? extends NormalizedNode<?, ?>> actualNode) throws DataMissingException {
49 if (actualNode.isPresent() == false) {
50 throw new DataMissingException(nodeQName);
54 static void check(QName nodeQName, Optional<LeafSetNode<?>> actualNodes, LeafSetEntryNode<?> modificationNode)
55 throws DataMissingException {
56 if (actualNodes.isPresent()==false || actualNodes.get().getChild(modificationNode.getIdentifier()).isPresent() == false) {
57 throw new DataMissingException(nodeQName, modificationNode);
61 static void check(QName nodeQName, Optional<MapNode> actualNodes, MapEntryNode modificationNode)
62 throws DataModificationException {
63 if (actualNodes.isPresent()==false || actualNodes.get().getChild(modificationNode.getIdentifier()).isPresent() == false) {
64 throw new DataMissingException(nodeQName, modificationNode);
69 public static final class DataExistsException extends DataModificationException {
70 private static final long serialVersionUID = 1L;
72 public DataExistsException(QName nodeType, NormalizedNode<?, ?> actualNode, NormalizedNode<?, ?> modificationNode) {
74 .format("Data already exists for node: %s, current value: %s. modification value: %s", nodeType, actualNode, modificationNode),
78 static void check(QName nodeQName, Optional<? extends NormalizedNode<?, ?>> actualNode, NormalizedNode<?, ?> modificationNode) throws DataExistsException {
79 if (actualNode.isPresent()) {
80 throw new DataExistsException(nodeQName, actualNode.get(), modificationNode);
84 static void check(QName nodeQName, Optional<LeafSetNode<?>> actualNodes, LeafSetEntryNode<?> modificationNode)
85 throws DataExistsException {
86 if (actualNodes.isPresent() && actualNodes.get().getChild(modificationNode.getIdentifier()).isPresent()) {
87 throw new DataExistsException(nodeQName, actualNodes.get(), modificationNode);
91 public static void check(QName qName, Optional<MapNode> actualNodes, MapEntryNode listModification)
92 throws DataModificationException {
93 if (actualNodes.isPresent() && actualNodes.get().getChild(listModification.getIdentifier()).isPresent()) {
94 throw new DataExistsException(qName, actualNodes.get(), listModification);
99 public static final class IllegalChoiceValuesException extends DataModificationException {
100 private static final long serialVersionUID = 1L;
102 public IllegalChoiceValuesException(String message, QName node) {
103 super(message, node);
106 public static void throwMultipleCasesReferenced(QName choiceQName, ChoiceNode modification,
107 QName case1QName, QName case2QName) throws IllegalChoiceValuesException {
108 throw new IllegalChoiceValuesException(String.format(
109 "Child nodes from multiple cases present in modification: %s, choice: %s, case1: %s, case2: %s",
110 modification, choiceQName, case1QName, case2QName), choiceQName);
113 public static void throwUnknownChild(QName choiceQName, QName nodeQName) throws IllegalChoiceValuesException {
114 throw new IllegalChoiceValuesException(String.format(
115 "Unknown child node detected, choice: %s, child node: %s",
116 choiceQName, nodeQName), choiceQName);