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