BUG 1440 - json stream to normalized node stream writer
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / stream / NormalizedNodeWriter.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.api.schema.stream;
9
10 import static org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.UNKNOWN_SIZE;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Preconditions;
14
15 import java.io.Closeable;
16 import java.io.Flushable;
17 import java.io.IOException;
18
19 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
32
33 /**
34  * This is an experimental
35  */
36 @Beta
37 public final class NormalizedNodeWriter implements Closeable, Flushable {
38     private final NormalizedNodeStreamWriter writer;
39
40     private NormalizedNodeWriter(final NormalizedNodeStreamWriter writer) {
41         this.writer = Preconditions.checkNotNull(writer);
42     }
43
44     public static NormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer) {
45         return new NormalizedNodeWriter(writer);
46     }
47
48     public NormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
49         if (wasProcessedAsCompositeNode(node)) {
50             return this;
51         }
52
53         if (wasProcessAsSimpleNode(node)) {
54             return this;
55         }
56
57         throw new IllegalStateException("It wasn't possible to serialize node " + node);
58     }
59
60     private boolean wasProcessAsSimpleNode(final NormalizedNode<?, ?> node) throws IOException {
61         if (node instanceof LeafSetEntryNode) {
62             final LeafSetEntryNode<?> nodeAsLeafList = (LeafSetEntryNode<?>)node;
63             writer.leafSetEntryNode(nodeAsLeafList.getValue());
64             return true;
65         } else if (node instanceof LeafNode) {
66             final LeafNode<?> nodeAsLeaf = (LeafNode<?>)node;
67             writer.leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue());
68             return true;
69         } else if (node instanceof AnyXmlNode) {
70             final AnyXmlNode anyXmlNode = (AnyXmlNode)node;
71             writer.anyxmlNode(anyXmlNode.getIdentifier(), anyXmlNode.getValue());
72             return true;
73         }
74
75         return false;
76     }
77
78     private boolean wasProcessedAsCompositeNode(final NormalizedNode<?, ?> node) throws IOException {
79         boolean hasDataContainerChild = false;
80         if (node instanceof ContainerNode) {
81             writer.startContainerNode(((ContainerNode) node).getIdentifier(), UNKNOWN_SIZE);
82             hasDataContainerChild = true;
83         } else if (node instanceof MapEntryNode) {
84             writer.startMapEntryNode(((MapEntryNode) node).getIdentifier(), UNKNOWN_SIZE);
85             hasDataContainerChild = true;
86         } else if (node instanceof UnkeyedListEntryNode) {
87             writer.startUnkeyedListItem(((UnkeyedListEntryNode) node).getIdentifier(), UNKNOWN_SIZE);
88             hasDataContainerChild = true;
89         } else if (node instanceof ChoiceNode) {
90             writer.startChoiceNode(((ChoiceNode) node).getIdentifier(), UNKNOWN_SIZE);
91             hasDataContainerChild = true;
92         } else if (node instanceof AugmentationNode) {
93             writer.startAugmentationNode(((AugmentationNode) node).getIdentifier());
94             hasDataContainerChild = true;
95         } else if (node instanceof UnkeyedListNode) {
96             writer.startUnkeyedList(((UnkeyedListNode) node).getIdentifier(), UNKNOWN_SIZE);
97             hasDataContainerChild = true;
98         } else if (node instanceof OrderedMapNode) {
99             writer.startOrderedMapNode(((OrderedMapNode) node).getIdentifier(), UNKNOWN_SIZE);
100             hasDataContainerChild = true;
101         } else if (node instanceof MapNode) {
102             writer.startMapNode(((MapNode) node).getIdentifier(), UNKNOWN_SIZE);
103             hasDataContainerChild = true;
104           //covers also OrderedLeafSetNode for which doesn't exist start* method
105         } else if (node instanceof LeafSetNode) {
106             writer.startLeafSet(((LeafSetNode<?>) node).getIdentifier(), UNKNOWN_SIZE);
107             hasDataContainerChild = true;
108         }
109
110         if (hasDataContainerChild) {
111             for (NormalizedNode<?, ?> childNode : ((NormalizedNode<?, Iterable<NormalizedNode<?, ?>>>) node).getValue()) {
112                 write(childNode);
113             }
114
115             writer.endNode();
116             return true;
117         }
118         return false;
119
120     }
121
122     @Override
123     public void flush() throws IOException {
124         writer.flush();
125     }
126
127     @Override
128     public void close() throws IOException {
129         writer.close();
130     }
131 }