Merge "BUG-1276: fixed generated union constructor"
[yangtools.git] / yang / yang-data-operations / src / main / java / org / opendaylight / yangtools / yang / data / operations / DataModificationException.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.operations;
9
10 import com.google.common.base.Optional;
11
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.Node;
14 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20
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
24
25     private static final long serialVersionUID = 1L;
26     private final QName node;
27
28     public DataModificationException(final String message, final QName node) {
29         super(message);
30         this.node = node;
31     }
32
33     public QName getNodeQName() {
34         return node;
35     }
36
37     public static final class DataMissingException extends DataModificationException {
38         private static final long serialVersionUID = 1L;
39
40         public DataMissingException(final QName nodeType) {
41             super(String.format("Data missing for node: %s", nodeType), nodeType);
42         }
43
44         public DataMissingException(final QName nodeType, final Node<?> modificationNode) {
45             super(String.format("Data missing for node: %s, %s", nodeType, modificationNode), nodeType);
46         }
47
48         public DataMissingException(final QName nodeType, final NormalizedNode<?, ?> modificationNode) {
49             super(String.format("Data missing for node: %s, %s", nodeType, modificationNode), nodeType);
50         }
51
52         static void check(final QName nodeQName, final Optional<? extends NormalizedNode<?, ?>> actualNode) throws DataMissingException {
53             if (!actualNode.isPresent()) {
54                 throw new DataMissingException(nodeQName);
55             }
56         }
57
58         static void check(final QName nodeQName, final Optional<LeafSetNode<?>> actualNodes, final LeafSetEntryNode<?> modificationNode)
59                 throws DataMissingException {
60             if (!actualNodes.isPresent() || !actualNodes.get().getChild(modificationNode.getIdentifier()).isPresent()) {
61                 throw new DataMissingException(nodeQName, modificationNode);
62             }
63         }
64
65         static void check(final QName nodeQName, final Optional<MapNode> actualNodes, final MapEntryNode modificationNode)
66                 throws DataModificationException {
67             if (!actualNodes.isPresent() || !actualNodes.get().getChild(modificationNode.getIdentifier()).isPresent()) {
68                 throw new DataMissingException(nodeQName, modificationNode);
69             }
70         }
71     }
72
73     public static final class DataExistsException extends DataModificationException {
74         private static final long serialVersionUID = 1L;
75
76         public DataExistsException(final QName nodeType, final NormalizedNode<?, ?> actualNode, final NormalizedNode<?, ?> modificationNode) {
77             super(String
78                     .format("Data already exists for node: %s, current value: %s. modification value: %s", nodeType, actualNode, modificationNode),
79                     nodeType);
80         }
81
82         static void check(final QName nodeQName, final Optional<? extends NormalizedNode<?, ?>> actualNode, final NormalizedNode<?, ?> modificationNode) throws DataExistsException {
83             if (actualNode.isPresent()) {
84                 throw new DataExistsException(nodeQName, actualNode.get(), modificationNode);
85             }
86         }
87
88         static void check(final QName nodeQName, final Optional<LeafSetNode<?>> actualNodes, final LeafSetEntryNode<?> modificationNode)
89                 throws DataExistsException {
90             if (actualNodes.isPresent() && actualNodes.get().getChild(modificationNode.getIdentifier()).isPresent()) {
91                 throw new DataExistsException(nodeQName, actualNodes.get(), modificationNode);
92             }
93         }
94
95         public static void check(final QName qName, final Optional<MapNode> actualNodes, final MapEntryNode listModification)
96                 throws DataModificationException {
97             if (actualNodes.isPresent() && actualNodes.get().getChild(listModification.getIdentifier()).isPresent()) {
98                 throw new DataExistsException(qName, actualNodes.get(), listModification);
99             }
100         }
101     }
102
103     public static final class IllegalChoiceValuesException extends DataModificationException {
104         private static final long serialVersionUID = 1L;
105
106         public IllegalChoiceValuesException(final String message, final QName node) {
107             super(message, node);
108         }
109
110         public static void throwMultipleCasesReferenced(final QName choiceQName, final ChoiceNode modification,
111                 final QName case1QName, final QName case2QName) throws IllegalChoiceValuesException {
112             throw new IllegalChoiceValuesException(String.format(
113                     "Child nodes from multiple cases present in modification: %s, choice: %s, case1: %s, case2: %s",
114                     modification, choiceQName, case1QName, case2QName), choiceQName);
115         }
116
117         public static void throwUnknownChild(final QName choiceQName, final QName nodeQName) throws IllegalChoiceValuesException {
118             throw new IllegalChoiceValuesException(String.format(
119                     "Unknown child node detected, choice: %s, child node: %s",
120                     choiceQName, nodeQName), choiceQName);
121         }
122     }
123
124 }