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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import java.util.ArrayDeque;
15 import java.util.Deque;
16 import javax.xml.transform.dom.DOMSource;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyXmlSchemaNode;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAnyXmlNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableYangModeledAnyXmlNodeBuilder;
42 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
45 * Implementation of {@link NormalizedNodeStreamWriter}, which constructs immutable instances of
46 * {@link NormalizedNode}s.
49 * This writer supports two modes of behaviour one is using {@link #from(NormalizedNodeResult)} where resulting
50 * NormalizedNode will be stored in supplied result object.
53 * Other mode of operation is using {@link #from(NormalizedNodeContainerBuilder)}, where all created nodes will be
54 * written to this builder.
57 * This class is not final for purposes of customization, normal users should not need to subclass it.
59 public class ImmutableNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
60 @SuppressWarnings("rawtypes")
61 private final Deque<NormalizedNodeBuilder> builders = new ArrayDeque<>();
63 private DataSchemaNode nextSchema;
65 @SuppressWarnings("rawtypes")
66 protected ImmutableNormalizedNodeStreamWriter(final NormalizedNodeBuilder topLevelBuilder) {
67 builders.push(topLevelBuilder);
70 protected ImmutableNormalizedNodeStreamWriter(final NormalizedNodeResult result) {
71 this(new NormalizedNodeResultBuilder(result));
75 * Creates a {@link NormalizedNodeStreamWriter} which creates instances of supplied {@link NormalizedNode}s
76 * and writes them to supplied builder as child nodes.
79 * Type of supplied {@link NormalizedNodeContainerBuilder} affects, which events could be emitted in order
80 * to ensure proper construction of data.
82 * @param builder Builder to which data will be written.
83 * @return {@link NormalizedNodeStreamWriter} which writes data
85 public static @NonNull NormalizedNodeStreamWriter from(final NormalizedNodeContainerBuilder<?, ?, ?, ?> builder) {
86 return new ImmutableNormalizedNodeStreamWriter(builder);
90 * Creates a {@link NormalizedNodeStreamWriter} which creates one instance of top-level {@link NormalizedNode}
91 * (type of NormalizedNode) is determined by first start event.
94 * Result is built when {@link #endNode()} associated with that start event is emitted.
97 * Writer properly creates also nested {@link NormalizedNode} instances, if their are supported inside the scope
101 * This method is useful for clients, which knows there will be one top-level node written, but does not know which
102 * type of {@link NormalizedNode} will be written.
104 * @param result {@link NormalizedNodeResult} object which will hold result value.
105 * @return {@link NormalizedNodeStreamWriter} which will write item to supplied result holder.
107 public static NormalizedNodeStreamWriter from(final NormalizedNodeResult result) {
108 return result instanceof NormalizedNodeMetadataResult ? from((NormalizedNodeMetadataResult) result)
109 : new ImmutableNormalizedNodeStreamWriter(result);
113 * Creates a {@link NormalizedNodeStreamWriter} which creates one instance of top-level {@link NormalizedNode}
114 * (type of NormalizedNode) is determined by first start event.
117 * Result is built when {@link #endNode()} associated with that start event is emitted.
120 * Writer properly creates also nested {@link NormalizedNode} instances, if their are supported inside the scope
121 * of the first event.
124 * This method is useful for clients, which knows there will be one top-level node written, but does not know which
125 * type of {@link NormalizedNode} will be written.
127 * @param result {@link NormalizedNodeResult} object which will hold result value.
128 * @return {@link NormalizedNodeStreamWriter} which will write item to supplied result holder.
130 public static NormalizedNodeStreamWriter from(final NormalizedNodeMetadataResult result) {
131 return new ImmutableMetadataNormalizedNodeStreamWriter(result);
135 public void startLeafNode(final NodeIdentifier name) {
136 checkDataNodeContainer();
137 enter(name, InterningLeafNodeBuilder.forSchema(nextSchema));
142 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) {
143 checkDataNodeContainer();
144 enter(name, UNKNOWN_SIZE == childSizeHint ? InterningLeafSetNodeBuilder.create(nextSchema)
145 : InterningLeafSetNodeBuilder.create(nextSchema, childSizeHint));
149 public void startLeafSetEntryNode(final NodeWithValue<?> name) {
150 final NormalizedNodeBuilder<?, ?, ?> current = current();
151 checkArgument(current instanceof ImmutableLeafSetNodeBuilder
152 || current instanceof ImmutableOrderedLeafSetNodeBuilder || current instanceof NormalizedNodeResultBuilder,
153 "LeafSetEntryNode is not valid for parent %s", current);
154 enter(name, ImmutableLeafSetEntryNodeBuilder.create());
159 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) {
160 checkDataNodeContainer();
161 enter(name, Builders.orderedLeafSetBuilder());
165 public void startAnyxmlNode(final NodeIdentifier name) {
166 checkDataNodeContainer();
167 enter(name, ImmutableAnyXmlNodeBuilder.create());
172 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) {
173 checkDataNodeContainer();
174 enter(name, UNKNOWN_SIZE == childSizeHint ? ImmutableContainerNodeBuilder.create()
175 : ImmutableContainerNodeBuilder.create(childSizeHint));
179 public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) {
180 checkDataNodeContainer();
182 checkArgument(nextSchema instanceof YangModeledAnyXmlSchemaNode,
183 "Schema of this node should be instance of YangModeledAnyXmlSchemaNode");
184 final YangModeledAnyXmlSchemaNode anyxmlSchema = (YangModeledAnyXmlSchemaNode) nextSchema;
185 enter(name, UNKNOWN_SIZE == childSizeHint ? ImmutableYangModeledAnyXmlNodeBuilder.create(anyxmlSchema)
186 : ImmutableYangModeledAnyXmlNodeBuilder.create(anyxmlSchema, childSizeHint));
190 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) {
191 checkDataNodeContainer();
192 enter(name, UNKNOWN_SIZE == childSizeHint ? ImmutableUnkeyedListNodeBuilder.create()
193 : ImmutableUnkeyedListNodeBuilder.create(childSizeHint));
197 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) {
198 final NormalizedNodeBuilder<?, ?, ?> current = current();
199 checkArgument(current instanceof ImmutableUnkeyedListNodeBuilder
200 || current instanceof NormalizedNodeResultBuilder);
201 enter(name, UNKNOWN_SIZE == childSizeHint ? ImmutableUnkeyedListEntryNodeBuilder.create()
202 : ImmutableUnkeyedListEntryNodeBuilder.create(childSizeHint));
206 public void startMapNode(final NodeIdentifier name, final int childSizeHint) {
207 checkDataNodeContainer();
208 enter(name, UNKNOWN_SIZE == childSizeHint ? ImmutableMapNodeBuilder.create()
209 : ImmutableMapNodeBuilder.create(childSizeHint));
213 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) {
214 final NormalizedNodeBuilder<?, ?, ?> current = current();
215 checkArgument(current instanceof ImmutableMapNodeBuilder || current instanceof ImmutableOrderedMapNodeBuilder
216 || current instanceof NormalizedNodeResultBuilder);
218 enter(identifier, UNKNOWN_SIZE == childSizeHint ? ImmutableMapEntryNodeBuilder.create()
219 : ImmutableMapEntryNodeBuilder.create(childSizeHint));
223 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) {
224 checkDataNodeContainer();
225 enter(name, UNKNOWN_SIZE == childSizeHint ? ImmutableOrderedMapNodeBuilder.create()
226 : ImmutableOrderedMapNodeBuilder.create(childSizeHint));
230 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
231 checkDataNodeContainer();
232 enter(name, UNKNOWN_SIZE == childSizeHint ? ImmutableChoiceNodeBuilder.create()
233 : ImmutableChoiceNodeBuilder.create(childSizeHint));
237 public void startAugmentationNode(final AugmentationIdentifier identifier) {
238 checkDataNodeContainer();
239 checkArgument(!(current() instanceof ImmutableAugmentationNodeBuilder));
240 enter(identifier, Builders.augmentationBuilder());
244 public void flush() {
249 public void close() {
254 public void nextDataSchemaNode(final DataSchemaNode schema) {
255 nextSchema = requireNonNull(schema);
259 public void scalarValue(final Object value) {
260 currentScalar().withValue(value);
264 public void domSourceValue(final DOMSource value) {
265 currentScalar().withValue(value);
269 @SuppressWarnings({ "rawtypes", "unchecked" })
270 public void endNode() {
271 final NormalizedNodeBuilder finishedBuilder = builders.poll();
272 checkState(finishedBuilder != null, "Node which should be closed does not exists.");
273 final NormalizedNode<PathArgument, ?> product = finishedBuilder.build();
280 * Add a child not to the currently-open builder.
282 * @param child A new child
283 * @throws NullPointerException if {@code child} is null
284 * @throws IllegalStateException if there is no open builder
286 @SuppressWarnings({ "rawtypes", "unchecked" })
287 protected final void writeChild(final NormalizedNode<?, ?> child) {
288 final NormalizedNodeContainerBuilder current = currentContainer();
289 checkState(current != null, "Reached top level node, which could not be closed in this writer.");
290 current.addChild(requireNonNull(child));
293 // Exposed for ImmutableMetadataNormalizedNodeStreamWriter
294 @SuppressWarnings("rawtypes")
295 void enter(final PathArgument identifier, final NormalizedNodeBuilder next) {
296 builders.push(next.withNodeIdentifier(identifier));
300 // Exposed for ImmutableMetadataNormalizedNodeStreamWriter
301 protected final NormalizedNodeBuilder popBuilder() {
302 return builders.pop();
305 final void reset(final NormalizedNodeResultBuilder builder) {
308 builders.push(builder);
311 private void checkDataNodeContainer() {
312 @SuppressWarnings("rawtypes")
313 final NormalizedNodeContainerBuilder current = currentContainer();
314 if (!(current instanceof NormalizedNodeResultBuilder)) {
315 checkArgument(current instanceof DataContainerNodeBuilder<?, ?>, "Invalid nesting of data.");
319 @SuppressWarnings("rawtypes")
320 private NormalizedNodeBuilder current() {
321 return builders.peek();
324 @SuppressWarnings("rawtypes")
325 private NormalizedNodeContainerBuilder currentContainer() {
326 final NormalizedNodeBuilder current = current();
327 if (current == null) {
330 checkState(current instanceof NormalizedNodeContainerBuilder, "%s is not a node container", current);
331 return (NormalizedNodeContainerBuilder) current;
334 @SuppressWarnings("rawtypes")
335 private NormalizedNodeBuilder currentScalar() {
336 final NormalizedNodeBuilder current = current();
337 checkState(!(current instanceof NormalizedNodeContainerBuilder), "Unexpected node container %s", current);