Clean YangStatementParserListenerImpl up
[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 import org.opendaylight.yangtools.yang.common.QName;
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;
18
19 public class DataModificationException extends Exception {
20     // TODO replace QName as identifier for node with schema or NodeIdentifier,
21     // Augmentation does not have a QName
22
23     private static final long serialVersionUID = 1L;
24     private final QName node;
25
26     public DataModificationException(final String message, final QName node) {
27         super(message);
28         this.node = node;
29     }
30
31     public QName getNodeQName() {
32         return node;
33     }
34
35     public static final class DataMissingException extends DataModificationException {
36         private static final long serialVersionUID = 1L;
37
38         public DataMissingException(final QName nodeType) {
39             super(String.format("Data missing for node: %s", nodeType), nodeType);
40         }
41
42         public DataMissingException(final QName nodeType, final NormalizedNode<?, ?> modificationNode) {
43             super(String.format("Data missing for node: %s, %s", nodeType, modificationNode), nodeType);
44         }
45
46         static void check(final QName nodeQName, final Optional<? extends NormalizedNode<?, ?>> actualNode) throws DataMissingException {
47             if (!actualNode.isPresent()) {
48                 throw new DataMissingException(nodeQName);
49             }
50         }
51
52         static void check(final QName nodeQName, final Optional<LeafSetNode<?>> actualNodes, final LeafSetEntryNode<?> modificationNode)
53                 throws DataMissingException {
54             if (!actualNodes.isPresent() || !actualNodes.get().getChild(modificationNode.getIdentifier()).isPresent()) {
55                 throw new DataMissingException(nodeQName, modificationNode);
56             }
57         }
58
59         static void check(final QName nodeQName, final Optional<MapNode> actualNodes, final MapEntryNode modificationNode)
60                 throws DataModificationException {
61             if (!actualNodes.isPresent() || !actualNodes.get().getChild(modificationNode.getIdentifier()).isPresent()) {
62                 throw new DataMissingException(nodeQName, modificationNode);
63             }
64         }
65     }
66
67     public static final class DataExistsException extends DataModificationException {
68         private static final long serialVersionUID = 1L;
69
70         public DataExistsException(final QName nodeType, final NormalizedNode<?, ?> actualNode, final NormalizedNode<?, ?> modificationNode) {
71             super(String
72                     .format("Data already exists for node: %s, current value: %s. modification value: %s", nodeType, actualNode, modificationNode),
73                     nodeType);
74         }
75
76         static void check(final QName nodeQName, final Optional<? extends NormalizedNode<?, ?>> actualNode, final NormalizedNode<?, ?> modificationNode) throws DataExistsException {
77             if (actualNode.isPresent()) {
78                 throw new DataExistsException(nodeQName, actualNode.get(), modificationNode);
79             }
80         }
81
82         static void check(final QName nodeQName, final Optional<LeafSetNode<?>> actualNodes, final LeafSetEntryNode<?> modificationNode)
83                 throws DataExistsException {
84             if (actualNodes.isPresent() && actualNodes.get().getChild(modificationNode.getIdentifier()).isPresent()) {
85                 throw new DataExistsException(nodeQName, actualNodes.get(), modificationNode);
86             }
87         }
88
89         public static void check(final QName qName, final Optional<MapNode> actualNodes, final MapEntryNode listModification)
90                 throws DataModificationException {
91             if (actualNodes.isPresent() && actualNodes.get().getChild(listModification.getIdentifier()).isPresent()) {
92                 throw new DataExistsException(qName, actualNodes.get(), listModification);
93             }
94         }
95     }
96
97     public static final class IllegalChoiceValuesException extends DataModificationException {
98         private static final long serialVersionUID = 1L;
99
100         public IllegalChoiceValuesException(final String message, final QName node) {
101             super(message, node);
102         }
103
104         public static void throwMultipleCasesReferenced(final QName choiceQName, final ChoiceNode modification,
105                 final QName case1QName, final QName case2QName) throws IllegalChoiceValuesException {
106             throw new IllegalChoiceValuesException(String.format(
107                     "Child nodes from multiple cases present in modification: %s, choice: %s, case1: %s, case2: %s",
108                     modification, choiceQName, case1QName, case2QName), choiceQName);
109         }
110
111         public static void throwUnknownChild(final QName choiceQName, final QName nodeQName) throws IllegalChoiceValuesException {
112             throw new IllegalChoiceValuesException(String.format(
113                     "Unknown child node detected, choice: %s, child node: %s",
114                     choiceQName, nodeQName), choiceQName);
115         }
116     }
117
118 }