2 * Copyright (c) 2014 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.impl.schema;
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;
47 * Implementation of {@link NormalizedNodeStreamWriter}, which constructs
48 * immutable instances of {@link NormalizedNode}s.
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.
53 * Other mode of operation is using {@link #from(NormalizedNodeContainerBuilder)},
54 * where all created nodes will be written to this builder.
58 public class ImmutableNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
60 @SuppressWarnings("rawtypes")
61 private final Deque<NormalizedNodeContainerBuilder> builders = new ArrayDeque<>();
63 @SuppressWarnings("rawtypes")
64 private ImmutableNormalizedNodeStreamWriter( final NormalizedNodeContainerBuilder topLevelBuilder) {
65 builders.push(topLevelBuilder);
69 * Creates a {@link NormalizedNodeStreamWriter} which creates instances of supplied
70 * {@link NormalizedNode}s and writes them to supplied builder as child nodes.
72 * Type of supplied {@link NormalizedNodeContainerBuilder} affects,
73 * which events could be emitted in order to ensure proper construction of
76 * @param builder Builder to which data will be written.
77 * @return {@link NormalizedNodeStreamWriter} which writes data
79 public static final NormalizedNodeStreamWriter from(final NormalizedNodeContainerBuilder<?, ?, ?, ?> builder) {
80 return new ImmutableNormalizedNodeStreamWriter(builder);
85 * Creates a {@link NormalizedNodeStreamWriter} which creates one instance of top
86 * level {@link NormalizedNode} (type of NormalizedNode) is determined by first
89 * Result is built when {@link #endNode()} associated with that start event
92 * Writer properly creates also nested {@link NormalizedNode} instances,
93 * if their are supported inside the scope of first event.
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}
99 * @param result {@link NormalizedNodeResult} object which will hold result value.
100 * @return {@link NormalizedNodeStreamWriter} which will write item to supplied result holder.
102 public static final NormalizedNodeStreamWriter from(final NormalizedNodeResult result) {
103 return new ImmutableNormalizedNodeStreamWriter(new NormalizedNodeResultBuilder(result));
107 @SuppressWarnings("rawtypes")
108 private NormalizedNodeContainerBuilder getCurrent() {
109 return builders.peek();
112 @SuppressWarnings("rawtypes")
113 private void enter(final NormalizedNodeContainerBuilder next) {
117 @SuppressWarnings("unchecked")
118 private void writeChild(final NormalizedNode<?, ?> child) {
119 getCurrent().addChild(child);
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);
134 public void leafNode(final NodeIdentifier name, final Object value) throws IllegalArgumentException {
135 checkDataNodeContainer();
136 writeChild(ImmutableNodes.leafNode(name, value));
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);
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);
157 public void anyxmlNode(final NodeIdentifier name, final Object value) throws IllegalArgumentException {
158 checkDataNodeContainer();
162 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
163 checkDataNodeContainer();
165 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder = UNKNOWN_SIZE == childSizeHint ?
166 ImmutableContainerNodeBuilder.create() : ImmutableContainerNodeBuilder.create(childSizeHint);
167 enter(builder.withNodeIdentifier(name));
171 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
172 checkDataNodeContainer();
174 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = UNKNOWN_SIZE == childSizeHint ?
175 ImmutableUnkeyedListNodeBuilder.create() : ImmutableUnkeyedListNodeBuilder.create(childSizeHint);
176 enter(builder.withNodeIdentifier(name));
180 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IllegalStateException {
181 Preconditions.checkArgument(getCurrent() instanceof ImmutableUnkeyedListNodeBuilder);
183 final DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = UNKNOWN_SIZE == childSizeHint ?
184 ImmutableUnkeyedListEntryNodeBuilder.create() : ImmutableUnkeyedListEntryNodeBuilder.create(childSizeHint);
185 enter(builder.withNodeIdentifier(name));
189 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
190 checkDataNodeContainer();
192 final CollectionNodeBuilder<MapEntryNode, MapNode> builder = UNKNOWN_SIZE == childSizeHint ?
193 ImmutableMapNodeBuilder.create() : ImmutableMapNodeBuilder.create(childSizeHint);
194 enter(builder.withNodeIdentifier(name));
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);
203 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = UNKNOWN_SIZE == childSizeHint ?
204 ImmutableMapEntryNodeBuilder.create() : ImmutableMapEntryNodeBuilder.create(childSizeHint);
205 enter(builder.withNodeIdentifier(identifier));
209 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
210 checkDataNodeContainer();
212 final CollectionNodeBuilder<MapEntryNode, OrderedMapNode> builder = UNKNOWN_SIZE == childSizeHint ?
213 ImmutableOrderedMapNodeBuilder.create() : ImmutableOrderedMapNodeBuilder.create(childSizeHint);
214 enter(builder.withNodeIdentifier(name));
218 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
219 checkDataNodeContainer();
221 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = UNKNOWN_SIZE == childSizeHint ?
222 ImmutableChoiceNodeBuilder.create() : ImmutableChoiceNodeBuilder.create(childSizeHint);
223 enter(builder.withNodeIdentifier(name));
227 public void startAugmentationNode(final AugmentationIdentifier identifier) throws IllegalArgumentException {
228 checkDataNodeContainer();
229 Preconditions.checkArgument(!(getCurrent() instanceof ImmutableAugmentationNodeBuilder));
230 enter(Builders.augmentationBuilder().withNodeIdentifier(identifier));
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.");
241 @SuppressWarnings("rawtypes")
242 private static final class NormalizedNodeResultBuilder implements NormalizedNodeContainerBuilder {
244 private final NormalizedNodeResult result;
246 public NormalizedNodeResultBuilder(final NormalizedNodeResult result) {
247 this.result = result;
251 public NormalizedNodeBuilder withValue(final Object value) {
252 throw new UnsupportedOperationException();
256 public NormalizedNode build() {
257 throw new IllegalStateException("Can not close NormalizedNodeResult");
261 public NormalizedNodeContainerBuilder withNodeIdentifier(final PathArgument nodeIdentifier) {
262 throw new UnsupportedOperationException();
266 public NormalizedNodeContainerBuilder withValue(final List value) {
267 throw new UnsupportedOperationException();
271 public NormalizedNodeContainerBuilder addChild(final NormalizedNode child) {
272 result.setResult(child);
277 public NormalizedNodeContainerBuilder removeChild(final PathArgument key) {
278 throw new UnsupportedOperationException();
284 public void flush() {
289 public void close() throws IOException {
290 // TODO Auto-generated method stub