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.util.ArrayDeque;
12 import java.util.Collection;
13 import java.util.Deque;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
46 * Implementation of {@link NormalizedNodeStreamWriter}, which constructs
47 * immutable instances of {@link NormalizedNode}s.
49 * This writer supports two modes of behaviour one is using {@link #from(NormalizedNodeResult)}
50 * where resulting NormalizedNode will be stored in supplied result object.
52 * Other mode of operation is using {@link #from(NormalizedNodeContainerBuilder)},
53 * where all created nodes will be written to this builder.
57 public class ImmutableNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
59 @SuppressWarnings("rawtypes")
60 private final Deque<NormalizedNodeContainerBuilder> builders = new ArrayDeque<>();
62 @SuppressWarnings("rawtypes")
63 private ImmutableNormalizedNodeStreamWriter( final NormalizedNodeContainerBuilder topLevelBuilder) {
64 builders.push(topLevelBuilder);
68 * Creates a {@link NormalizedNodeStreamWriter} which creates instances of supplied
69 * {@link NormalizedNode}s and writes them to supplied builder as child nodes.
71 * Type of supplied {@link NormalizedNodeContainerBuilder} affects,
72 * which events could be emitted in order to ensure proper construction of
75 * @param builder Builder to which data will be written.
76 * @return {@link NormalizedNodeStreamWriter} which writes data
78 public static final NormalizedNodeStreamWriter from(final NormalizedNodeContainerBuilder<?, ?, ?, ?> builder) {
79 return new ImmutableNormalizedNodeStreamWriter(builder);
84 * Creates a {@link NormalizedNodeStreamWriter} which creates one instance of top
85 * level {@link NormalizedNode} (type of NormalizedNode) is determined by first
88 * Result is built when {@link #endNode()} associated with that start event
91 * Writer properly creates also nested {@link NormalizedNode} instances,
92 * if their are supported inside the scope of first event.
94 * This method is useful for clients, which knows there will be one
95 * top level node written, but does not know which type of {@link NormalizedNode}
98 * @param result {@link NormalizedNodeResult} object which will hold result value.
99 * @return {@link NormalizedNodeStreamWriter} which will write item to supplied result holder.
101 public static final NormalizedNodeStreamWriter from(final NormalizedNodeResult result) {
102 return new ImmutableNormalizedNodeStreamWriter(new NormalizedNodeResultBuilder(result));
106 @SuppressWarnings("rawtypes")
107 private NormalizedNodeContainerBuilder getCurrent() {
108 return builders.peek();
111 @SuppressWarnings("rawtypes")
112 private void enter(final NormalizedNodeContainerBuilder next) {
116 @SuppressWarnings("unchecked")
117 private void writeChild(final NormalizedNode<?, ?> child) {
118 getCurrent().addChild(child);
122 @SuppressWarnings({"rawtypes","unchecked"})
123 public void endNode() {
124 final NormalizedNodeContainerBuilder finishedBuilder = builders.poll();
125 Preconditions.checkState(finishedBuilder != null, "Node which should be closed does not exists.");
126 NormalizedNodeContainerBuilder current = getCurrent();
127 Preconditions.checkState(current != null, "Reached top level node, which could not be closed in this writer.");
128 NormalizedNode<PathArgument, ?> product = finishedBuilder.build();
129 current.addChild(product);
133 public void leafNode(final NodeIdentifier name, final Object value) throws IllegalArgumentException {
134 checkDataNodeContainer();
135 writeChild(ImmutableNodes.leafNode(name, value));
139 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
140 checkDataNodeContainer();
141 ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = UNKNOWN_SIZE == childSizeHint ?
142 ImmutableLeafSetNodeBuilder.create() : ImmutableLeafSetNodeBuilder.create(childSizeHint);
143 builder.withNodeIdentifier(name);
148 public void leafSetEntryNode(final Object value) throws IllegalArgumentException {
149 Preconditions.checkArgument(getCurrent() instanceof ImmutableLeafSetNodeBuilder<?>);
150 @SuppressWarnings("unchecked")
151 ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = ((ImmutableLeafSetNodeBuilder<Object>) getCurrent());
152 builder.withChildValue(value);
156 public void anyxmlNode(final NodeIdentifier name, final Object value) throws IllegalArgumentException {
157 checkDataNodeContainer();
161 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
162 checkDataNodeContainer();
164 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder = UNKNOWN_SIZE == childSizeHint ?
165 ImmutableContainerNodeBuilder.create() : ImmutableContainerNodeBuilder.create(childSizeHint);
166 enter(builder.withNodeIdentifier(name));
170 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
171 checkDataNodeContainer();
173 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = UNKNOWN_SIZE == childSizeHint ?
174 ImmutableUnkeyedListNodeBuilder.create() : ImmutableUnkeyedListNodeBuilder.create(childSizeHint);
175 enter(builder.withNodeIdentifier(name));
179 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IllegalStateException {
180 Preconditions.checkArgument(getCurrent() instanceof ImmutableUnkeyedListNodeBuilder);
182 final DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = UNKNOWN_SIZE == childSizeHint ?
183 ImmutableUnkeyedListEntryNodeBuilder.create() : ImmutableUnkeyedListEntryNodeBuilder.create(childSizeHint);
184 enter(builder.withNodeIdentifier(name));
188 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
189 checkDataNodeContainer();
191 final CollectionNodeBuilder<MapEntryNode, MapNode> builder = UNKNOWN_SIZE == childSizeHint ?
192 ImmutableMapNodeBuilder.create() : ImmutableMapNodeBuilder.create(childSizeHint);
193 enter(builder.withNodeIdentifier(name));
197 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IllegalArgumentException {
198 if(!(getCurrent() instanceof NormalizedNodeResultBuilder)) {
199 Preconditions.checkArgument(getCurrent() instanceof ImmutableMapNodeBuilder || getCurrent() instanceof ImmutableOrderedMapNodeBuilder);
202 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = UNKNOWN_SIZE == childSizeHint ?
203 ImmutableMapEntryNodeBuilder.create() : ImmutableMapEntryNodeBuilder.create(childSizeHint);
204 enter(builder.withNodeIdentifier(identifier));
208 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
209 checkDataNodeContainer();
211 final CollectionNodeBuilder<MapEntryNode, OrderedMapNode> builder = UNKNOWN_SIZE == childSizeHint ?
212 ImmutableOrderedMapNodeBuilder.create() : ImmutableOrderedMapNodeBuilder.create(childSizeHint);
213 enter(builder.withNodeIdentifier(name));
217 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IllegalArgumentException {
218 checkDataNodeContainer();
220 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> builder = UNKNOWN_SIZE == childSizeHint ?
221 ImmutableChoiceNodeBuilder.create() : ImmutableChoiceNodeBuilder.create(childSizeHint);
222 enter(builder.withNodeIdentifier(name));
226 public void startAugmentationNode(final AugmentationIdentifier identifier) throws IllegalArgumentException {
227 checkDataNodeContainer();
228 Preconditions.checkArgument(!(getCurrent() instanceof ImmutableAugmentationNodeBuilder));
229 enter(Builders.augmentationBuilder().withNodeIdentifier(identifier));
232 private void checkDataNodeContainer() {
233 @SuppressWarnings("rawtypes")
234 NormalizedNodeContainerBuilder current = getCurrent();
235 if(!(current instanceof NormalizedNodeResultBuilder)) {
236 Preconditions.checkArgument(current instanceof DataContainerNodeBuilder<?, ?>, "Invalid nesting of data.");
240 @SuppressWarnings("rawtypes")
241 private static final class NormalizedNodeResultBuilder implements NormalizedNodeContainerBuilder {
243 private final NormalizedNodeResult result;
245 public NormalizedNodeResultBuilder(final NormalizedNodeResult result) {
246 this.result = result;
250 public NormalizedNodeBuilder withValue(final Object value) {
251 throw new UnsupportedOperationException();
255 public NormalizedNode build() {
256 throw new IllegalStateException("Can not close NormalizedNodeResult");
260 public NormalizedNodeContainerBuilder withNodeIdentifier(final PathArgument nodeIdentifier) {
261 throw new UnsupportedOperationException();
265 public NormalizedNodeContainerBuilder withValue(final Collection value) {
266 throw new UnsupportedOperationException();
270 public NormalizedNodeContainerBuilder addChild(final NormalizedNode child) {
271 result.setResult(child);
276 public NormalizedNodeContainerBuilder removeChild(final PathArgument key) {
277 throw new UnsupportedOperationException();
283 public void flush() {
288 public void close() {