Rename LeafNodeCodecContext to ValueNodeCodecContext
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / BindingToNormalizedStreamWriter.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.mdsal.binding.dom.codec.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import java.io.IOException;
14 import java.util.AbstractMap;
15 import java.util.ArrayDeque;
16 import java.util.Deque;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import javax.xml.transform.dom.DOMSource;
20 import org.opendaylight.yangtools.concepts.Delegator;
21 import org.opendaylight.yangtools.yang.binding.Augmentation;
22 import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
23 import org.opendaylight.yangtools.yang.binding.DataContainer;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.Identifiable;
26 import org.opendaylight.yangtools.yang.binding.Identifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34
35 final class BindingToNormalizedStreamWriter implements BindingStreamEventWriter, Delegator<NormalizedNodeStreamWriter> {
36     private final Deque<NodeCodecContext> schema = new ArrayDeque<>();
37     private final NormalizedNodeStreamWriter delegate;
38     private final NodeCodecContext rootNodeSchema;
39
40     BindingToNormalizedStreamWriter(final NodeCodecContext rootNodeSchema, final NormalizedNodeStreamWriter delegate) {
41         this.rootNodeSchema = requireNonNull(rootNodeSchema);
42         this.delegate = requireNonNull(delegate);
43     }
44
45     static BindingToNormalizedStreamWriter create(final NodeCodecContext schema,
46             final NormalizedNodeStreamWriter delegate) {
47         return new BindingToNormalizedStreamWriter(schema, delegate);
48     }
49
50     private void emitSchema(final Object schemaNode) {
51         delegate.nextDataSchemaNode((DataSchemaNode) schemaNode);
52     }
53
54     NodeCodecContext current() {
55         return schema.peek();
56     }
57
58     private NodeIdentifier duplicateSchemaEnter() {
59         final NodeCodecContext next;
60         if (current() == null) {
61             // Entry of first node
62             next = rootNodeSchema;
63         } else {
64             next = current();
65         }
66         this.schema.push(next);
67         return (NodeIdentifier) current().getDomPathArgument();
68     }
69
70     @SuppressWarnings({"unchecked", "rawtypes"})
71     private <T extends YangInstanceIdentifier.PathArgument> T enter(final Class<?> name, final Class<T> identifier) {
72         final NodeCodecContext next;
73         if (current() == null) {
74             // Entry of first node
75             next = rootNodeSchema;
76         } else {
77             Preconditions.checkArgument(current() instanceof DataContainerCodecContext, "Could not start node %s",
78                     name);
79             next = ((DataContainerCodecContext<?,?>) current()).streamChild((Class) name);
80         }
81         this.schema.push(next);
82         T arg = (T) next.getDomPathArgument();
83         return arg;
84     }
85
86     private <T extends YangInstanceIdentifier.PathArgument> T enter(final String localName, final Class<T> identifier) {
87         NodeCodecContext current = current();
88         NodeCodecContext next = ((DataObjectCodecContext<?, ?>) current).getLeafChild(localName);
89         this.schema.push(next);
90         @SuppressWarnings("unchecked")
91         T arg = (T) next.getDomPathArgument();
92         return arg;
93     }
94
95     @Override
96     public NormalizedNodeStreamWriter getDelegate() {
97         return delegate;
98     }
99
100     @Override
101     public void endNode() throws IOException {
102         NodeCodecContext left = schema.pop();
103         // NormalizedNode writer does not have entry into case, but into choice
104         // so for leaving case, we do not emit endNode.
105         if (!(left instanceof CaseNodeCodecContext)) {
106             delegate.endNode();
107         }
108     }
109
110     private Map.Entry<NodeIdentifier, Object> serializeLeaf(final String localName, final Object value) {
111         Preconditions.checkArgument(current() instanceof DataObjectCodecContext);
112
113         DataObjectCodecContext<?,?> currentCasted = (DataObjectCodecContext<?,?>) current();
114         ValueNodeCodecContext leafContext = currentCasted.getLeafChild(localName);
115
116         NodeIdentifier domArg = (NodeIdentifier) leafContext.getDomPathArgument();
117         Object domValue = leafContext.getValueCodec().serialize(value);
118         emitSchema(leafContext.getSchema());
119         return new AbstractMap.SimpleEntry<>(domArg, domValue);
120     }
121
122     @Override
123     public void leafNode(final String localName, final Object value) throws IOException {
124         final Entry<NodeIdentifier, Object> dom = serializeLeaf(localName, value);
125         delegate.startLeafNode(dom.getKey());
126         delegate.scalarValue(dom.getValue());
127         delegate.endNode();
128     }
129
130     @Override
131     public void anyxmlNode(final String name, final Object value) throws IOException {
132         final Entry<NodeIdentifier, Object> dom = serializeLeaf(name, value);
133         delegate.startAnyxmlNode(dom.getKey());
134         delegate.domSourceValue((DOMSource) dom.getValue());
135         delegate.endNode();
136     }
137
138     @Override
139     public void leafSetEntryNode(final Object value) throws IOException {
140         final ValueNodeCodecContext ctx = (ValueNodeCodecContext) current();
141         final Object domValue = ctx.getValueCodec().serialize(value);
142         delegate.startLeafSetEntryNode(new NodeWithValue<>(ctx.getSchema().getQName(), domValue));
143         delegate.scalarValue(domValue);
144         delegate.endNode();
145     }
146
147     @Override
148     public void startAugmentationNode(final Class<? extends Augmentation<?>> augmentationType)
149             throws IOException {
150         delegate.startAugmentationNode(enter(augmentationType, AugmentationIdentifier.class));
151     }
152
153     @Override
154     public void startCase(final Class<? extends DataObject> caze, final int childSizeHint) {
155         enter(caze, NodeIdentifier.class);
156     }
157
158     @Override
159     public void startChoiceNode(final Class<? extends DataContainer> type, final int childSizeHint)
160             throws IOException {
161         delegate.startChoiceNode(enter(type, NodeIdentifier.class), childSizeHint);
162     }
163
164     @Override
165     public void startContainerNode(final Class<? extends DataObject> object, final int childSizeHint)
166             throws IOException {
167         delegate.startContainerNode(enter(object, NodeIdentifier.class), childSizeHint);
168     }
169
170     @Override
171     public void startLeafSet(final String localName, final int childSizeHint) throws IOException {
172         final NodeIdentifier id = enter(localName, NodeIdentifier.class);
173         emitSchema(current().getSchema());
174         delegate.startLeafSet(id, childSizeHint);
175     }
176
177     @Override
178     public void startOrderedLeafSet(final String localName, final int childSizeHint) throws IOException {
179         delegate.startOrderedLeafSet(enter(localName, NodeIdentifier.class), childSizeHint);
180     }
181
182     @Override
183     public void startMapEntryNode(final Identifier<?> key, final int childSizeHint) throws IOException {
184         duplicateSchemaEnter();
185         NodeIdentifierWithPredicates identifier = ((KeyedListNodeCodecContext<?>) current()).serialize(key);
186         delegate.startMapEntryNode(identifier, childSizeHint);
187     }
188
189     @Override
190     public <T extends DataObject & Identifiable<?>> void startMapNode(final Class<T> mapEntryType,
191             final int childSizeHint) throws IOException {
192         delegate.startMapNode(enter(mapEntryType, NodeIdentifier.class), childSizeHint);
193     }
194
195     @Override
196     public <T extends DataObject & Identifiable<?>> void startOrderedMapNode(final Class<T> mapEntryType,
197             final int childSizeHint) throws IOException {
198         delegate.startOrderedMapNode(enter(mapEntryType, NodeIdentifier.class), childSizeHint);
199     }
200
201     @Override
202     public void startUnkeyedList(final Class<? extends DataObject> obj, final int childSizeHint) throws IOException {
203         delegate.startUnkeyedList(enter(obj, NodeIdentifier.class), childSizeHint);
204     }
205
206     @Override
207     public void startUnkeyedListItem(final int childSizeHint) throws IOException {
208         delegate.startUnkeyedListItem(duplicateSchemaEnter(), childSizeHint);
209     }
210
211     @Override
212     public void flush() throws IOException {
213         delegate.flush();
214     }
215
216     @Override
217     public void close() throws IOException {
218         delegate.close();
219     }
220 }