Merge changes Ic8e48ed1,Ie6bc9bbf,I3e94c637,I84c8ac42
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / ImmutableNormalizedNodeStreamWriter.java
1 /*
2  * Copyright (c) 2014 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.impl.schema;
9
10 import com.google.common.base.Preconditions;
11 import java.io.IOException;
12 import java.util.ArrayDeque;
13 import java.util.Deque;
14 import java.util.List;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
44
45 /**
46  *
47  * Implementation of {@link NormalizedNodeStreamWriter}, which constructs
48  * immutable instances of {@link NormalizedNode}s.
49  * <p>
50  * This writer supports two modes of behaviour one is using {@link #from(NormalizedNodeResult)}
51  * where resulting NormalizedNode will be stored in supplied result object.
52  *
53  * Other mode of operation is using {@link #from(NormalizedNodeContainerBuilder)},
54  * where all created nodes will be written to this builder.
55  *
56  *
57  */
58 public class ImmutableNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
59
60     @SuppressWarnings("rawtypes")
61     private final Deque<NormalizedNodeContainerBuilder> builders = new ArrayDeque<>();
62
63     @SuppressWarnings("rawtypes")
64     private ImmutableNormalizedNodeStreamWriter( final NormalizedNodeContainerBuilder topLevelBuilder) {
65         builders.push(topLevelBuilder);
66     }
67
68     /**
69      * Creates a {@link NormalizedNodeStreamWriter} which creates instances of supplied
70      * {@link NormalizedNode}s and writes them to supplied builder as child nodes.
71      * <p>
72      * Type of supplied {@link NormalizedNodeContainerBuilder} affects,
73      * which events could be emitted in order to ensure proper construction of
74      * data.
75      *
76      * @param builder Builder to which data will be written.
77      * @return {@link NormalizedNodeStreamWriter} which writes data
78      */
79     public static final NormalizedNodeStreamWriter from(final NormalizedNodeContainerBuilder<?, ?, ?, ?> builder) {
80         return new ImmutableNormalizedNodeStreamWriter(builder);
81     }
82
83     /**
84      *
85      * Creates a {@link NormalizedNodeStreamWriter} which creates one instance of top
86      * level {@link NormalizedNode} (type of NormalizedNode) is determined by first
87      * start event.
88      * <p>
89      * Result is built when {@link #endNode()} associated with that start event
90      * is emitted.
91      * <p>
92      * Writer properly creates also nested {@link NormalizedNode} instances,
93      * if their are supported inside the scope of first event.
94      * <p>
95      * This method is useful for clients, which knows there will be one
96      * top level node written, but does not know which type of {@link NormalizedNode}
97      * will be written.
98      *
99      * @param result {@link NormalizedNodeResult} object which will hold result value.
100      * @return {@link NormalizedNodeStreamWriter} which will write item to supplied result holder.
101      */
102     public static final NormalizedNodeStreamWriter from(final NormalizedNodeResult result) {
103         return new ImmutableNormalizedNodeStreamWriter(new NormalizedNodeResultBuilder(result));
104     }
105
106
107     @SuppressWarnings("rawtypes")
108     private NormalizedNodeContainerBuilder getCurrent() {
109         return builders.peek();
110     }
111
112     @SuppressWarnings("rawtypes")
113     private void enter(final NormalizedNodeContainerBuilder next) {
114         builders.push(next);
115     }
116
117     @SuppressWarnings("unchecked")
118     private void writeChild(final NormalizedNode<?, ?> child) {
119         getCurrent().addChild(child);
120     }
121
122     @Override
123     @SuppressWarnings({"rawtypes","unchecked"})
124     public void endNode() {
125         final NormalizedNodeContainerBuilder finishedBuilder = builders.poll();
126         Preconditions.checkState(finishedBuilder != null, "Node which should be closed does not exists.");
127         NormalizedNodeContainerBuilder current = getCurrent();
128         Preconditions.checkState(current != null, "Reached top level node, which could not be closed in this writer.");
129         NormalizedNode<PathArgument, ?> product = finishedBuilder.build();
130         current.addChild(product);
131     }
132
133     @Override
134     public void leafNode(final NodeIdentifier name, final Object value) throws IllegalArgumentException {
135         checkDataNodeContainer();
136         writeChild(ImmutableNodes.leafNode(name, value));
137     }
138
139     @Override
140     public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
141         checkDataNodeContainer();
142         ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = UNKNOWN_SIZE == childSizeHint ?
143                 ImmutableLeafSetNodeBuilder.create() : ImmutableLeafSetNodeBuilder.create(childSizeHint);
144         builder.withNodeIdentifier(name);
145         enter(builder);
146     }
147
148     @Override
149     public void leafSetEntryNode(final Object value) throws IllegalArgumentException {
150         Preconditions.checkArgument(getCurrent() instanceof ImmutableLeafSetNodeBuilder<?>);
151         @SuppressWarnings("unchecked")
152         ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = ((ImmutableLeafSetNodeBuilder<Object>) getCurrent());
153         builder.withChildValue(value);
154     }
155
156     @Override
157     public void anyxmlNode(final NodeIdentifier name, final Object value) throws IllegalArgumentException {
158         checkDataNodeContainer();
159     }
160
161     @Override
162     public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
163         checkDataNodeContainer();
164
165         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder = UNKNOWN_SIZE == childSizeHint ?
166                 ImmutableContainerNodeBuilder.create() : ImmutableContainerNodeBuilder.create(childSizeHint);
167         enter(builder.withNodeIdentifier(name));
168     }
169
170     @Override
171     public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
172         checkDataNodeContainer();
173
174         final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = UNKNOWN_SIZE == childSizeHint ?
175                 ImmutableUnkeyedListNodeBuilder.create() : ImmutableUnkeyedListNodeBuilder.create(childSizeHint);
176         enter(builder.withNodeIdentifier(name));
177     }
178
179     @Override
180     public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IllegalStateException {
181         Preconditions.checkArgument(getCurrent() instanceof ImmutableUnkeyedListNodeBuilder);
182
183         final DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = UNKNOWN_SIZE == childSizeHint ?
184                 ImmutableUnkeyedListEntryNodeBuilder.create() : ImmutableUnkeyedListEntryNodeBuilder.create(childSizeHint);
185         enter(builder.withNodeIdentifier(name));
186     }
187
188     @Override
189     public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
190         checkDataNodeContainer();
191
192         final CollectionNodeBuilder<MapEntryNode, MapNode> builder = UNKNOWN_SIZE == childSizeHint ?
193                 ImmutableMapNodeBuilder.create() : ImmutableMapNodeBuilder.create(childSizeHint);
194         enter(builder.withNodeIdentifier(name));
195     }
196
197     @Override
198     public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IllegalArgumentException {
199         if(!(getCurrent() instanceof NormalizedNodeResultBuilder)) {
200             Preconditions.checkArgument(getCurrent() instanceof ImmutableMapNodeBuilder || getCurrent() instanceof ImmutableOrderedMapNodeBuilder);
201         }
202
203         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = UNKNOWN_SIZE == childSizeHint ?
204                 ImmutableMapEntryNodeBuilder.create() : ImmutableMapEntryNodeBuilder.create(childSizeHint);
205         enter(builder.withNodeIdentifier(identifier));
206     }
207
208     @Override
209     public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
210         checkDataNodeContainer();
211
212         final CollectionNodeBuilder<MapEntryNode, OrderedMapNode> builder = UNKNOWN_SIZE == childSizeHint ?
213                 ImmutableOrderedMapNodeBuilder.create() : ImmutableOrderedMapNodeBuilder.create(childSizeHint);
214         enter(builder.withNodeIdentifier(name));
215     }
216
217     @Override
218     public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
219         checkDataNodeContainer();
220
221         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = UNKNOWN_SIZE == childSizeHint ?
222                 ImmutableChoiceNodeBuilder.create() : ImmutableChoiceNodeBuilder.create(childSizeHint);
223         enter(builder.withNodeIdentifier(name));
224     }
225
226     @Override
227     public void startAugmentationNode(final AugmentationIdentifier identifier) throws IllegalArgumentException {
228         checkDataNodeContainer();
229         Preconditions.checkArgument(!(getCurrent() instanceof ImmutableAugmentationNodeBuilder));
230         enter(Builders.augmentationBuilder().withNodeIdentifier(identifier));
231     }
232
233     private void checkDataNodeContainer() {
234         @SuppressWarnings("rawtypes")
235         NormalizedNodeContainerBuilder current = getCurrent();
236         if(!(current instanceof NormalizedNodeResultBuilder)) {
237         Preconditions.checkArgument(current instanceof DataContainerNodeBuilder<?, ?>, "Invalid nesting of data.");
238         }
239     }
240
241     @SuppressWarnings("rawtypes")
242     private static final class NormalizedNodeResultBuilder implements NormalizedNodeContainerBuilder {
243
244         private final NormalizedNodeResult result;
245
246         public NormalizedNodeResultBuilder(final NormalizedNodeResult result) {
247             this.result = result;
248         }
249
250         @Override
251         public NormalizedNodeBuilder withValue(final Object value) {
252             throw new UnsupportedOperationException();
253         }
254
255         @Override
256         public NormalizedNode build() {
257             throw new IllegalStateException("Can not close NormalizedNodeResult");
258         }
259
260         @Override
261         public NormalizedNodeContainerBuilder withNodeIdentifier(final PathArgument nodeIdentifier) {
262             throw new UnsupportedOperationException();
263         }
264
265         @Override
266         public NormalizedNodeContainerBuilder withValue(final List value) {
267             throw new UnsupportedOperationException();
268         }
269
270         @Override
271         public NormalizedNodeContainerBuilder addChild(final NormalizedNode child) {
272             result.setResult(child);
273             return this;
274         }
275
276         @Override
277         public NormalizedNodeContainerBuilder removeChild(final PathArgument key) {
278             throw new UnsupportedOperationException();
279         }
280
281     }
282
283     @Override
284     public void flush() {
285         // no-op
286     }
287
288     @Override
289     public void close() throws IOException {
290         // TODO Auto-generated method stub
291
292     }
293
294 }