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