Implement edit-config operations for NormalizedNode yang-data-api
[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 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;
18
19 import com.google.common.base.Optional;
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(String message, 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(QName nodeType) {
41             super(String.format("Data missing for node: %s", nodeType), nodeType);
42         }
43
44         public DataMissingException(QName nodeType, Node<?> modificationNode) {
45             super(String.format("Data missing for node: %s, %s", nodeType, modificationNode), nodeType);
46         }
47
48         static void check(QName nodeQName, Optional<? extends NormalizedNode<?, ?>> actualNode) throws DataMissingException {
49             if (actualNode.isPresent() == false) {
50                 throw new DataMissingException(nodeQName);
51             }
52         }
53
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);
58             }
59         }
60
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);
65             }
66         }
67     }
68
69     public static final class DataExistsException extends DataModificationException {
70         private static final long serialVersionUID = 1L;
71
72         public DataExistsException(QName nodeType, NormalizedNode<?, ?> actualNode, NormalizedNode<?, ?> modificationNode) {
73             super(String
74                     .format("Data already exists for node: %s, current value: %s. modification value: %s", nodeType, actualNode, modificationNode),
75                     nodeType);
76         }
77
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);
81             }
82         }
83
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);
88             }
89         }
90
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);
95             }
96         }
97     }
98
99     public static final class IllegalChoiceValuesException extends DataModificationException {
100         private static final long serialVersionUID = 1L;
101
102         public IllegalChoiceValuesException(String message, QName node) {
103             super(message, node);
104         }
105
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);
111         }
112
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);
117         }
118     }
119
120 }