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.mdsal.binding.dom.codec.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import java.io.IOException;
14 import java.util.AbstractMap;
15 import java.util.ArrayDeque;
16 import java.util.Deque;
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;
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;
40 BindingToNormalizedStreamWriter(final NodeCodecContext rootNodeSchema, final NormalizedNodeStreamWriter delegate) {
41 this.rootNodeSchema = requireNonNull(rootNodeSchema);
42 this.delegate = requireNonNull(delegate);
45 static BindingToNormalizedStreamWriter create(final NodeCodecContext schema,
46 final NormalizedNodeStreamWriter delegate) {
47 return new BindingToNormalizedStreamWriter(schema, delegate);
50 private void emitSchema(final Object schemaNode) {
51 delegate.nextDataSchemaNode((DataSchemaNode) schemaNode);
54 NodeCodecContext current() {
58 private NodeIdentifier duplicateSchemaEnter() {
59 final NodeCodecContext next;
60 if (current() == null) {
61 // Entry of first node
62 next = rootNodeSchema;
66 this.schema.push(next);
67 return (NodeIdentifier) current().getDomPathArgument();
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;
77 checkArgument(current() instanceof DataContainerCodecContext, "Could not start node %s", name);
78 next = ((DataContainerCodecContext<?,?>) current()).streamChild((Class) name);
80 this.schema.push(next);
81 T arg = (T) next.getDomPathArgument();
85 private <T extends YangInstanceIdentifier.PathArgument> T enter(final String localName, final Class<T> identifier) {
86 NodeCodecContext current = current();
87 NodeCodecContext next = ((DataObjectCodecContext<?, ?>) current).getLeafChild(localName);
88 this.schema.push(next);
89 @SuppressWarnings("unchecked")
90 T arg = (T) next.getDomPathArgument();
95 public NormalizedNodeStreamWriter getDelegate() {
100 public void endNode() throws IOException {
101 NodeCodecContext left = schema.pop();
102 // NormalizedNode writer does not have entry into case, but into choice
103 // so for leaving case, we do not emit endNode.
104 if (!(left instanceof CaseNodeCodecContext)) {
109 private Map.Entry<NodeIdentifier, Object> serializeLeaf(final String localName, final Object value) {
110 checkArgument(current() instanceof DataObjectCodecContext);
112 DataObjectCodecContext<?,?> currentCasted = (DataObjectCodecContext<?,?>) current();
113 ValueNodeCodecContext leafContext = currentCasted.getLeafChild(localName);
115 NodeIdentifier domArg = leafContext.getDomPathArgument();
116 Object domValue = leafContext.getValueCodec().serialize(value);
117 emitSchema(leafContext.getSchema());
118 return new AbstractMap.SimpleEntry<>(domArg, domValue);
122 public void leafNode(final String localName, final Object value) throws IOException {
123 final Entry<NodeIdentifier, Object> dom = serializeLeaf(localName, value);
124 delegate.startLeafNode(dom.getKey());
125 delegate.scalarValue(dom.getValue());
130 public void anyxmlNode(final String name, final Object value) throws IOException {
131 final Entry<NodeIdentifier, Object> dom = serializeLeaf(name, value);
132 delegate.startAnyxmlNode(dom.getKey());
133 delegate.domSourceValue((DOMSource) dom.getValue());
138 public void leafSetEntryNode(final Object value) throws IOException {
139 final LeafSetNodeCodecContext ctx = (LeafSetNodeCodecContext) current();
140 final Object domValue = ctx.getValueCodec().serialize(value);
141 delegate.startLeafSetEntryNode(new NodeWithValue<>(ctx.getSchema().getQName(), domValue));
142 delegate.scalarValue(domValue);
147 public void startAugmentationNode(final Class<? extends Augmentation<?>> augmentationType)
149 delegate.startAugmentationNode(enter(augmentationType, AugmentationIdentifier.class));
153 public void startCase(final Class<? extends DataObject> caze, final int childSizeHint) {
154 enter(caze, NodeIdentifier.class);
158 public void startChoiceNode(final Class<? extends DataContainer> type, final int childSizeHint)
160 delegate.startChoiceNode(enter(type, NodeIdentifier.class), childSizeHint);
164 public void startContainerNode(final Class<? extends DataObject> object, final int childSizeHint)
166 delegate.startContainerNode(enter(object, NodeIdentifier.class), childSizeHint);
170 public void startLeafSet(final String localName, final int childSizeHint) throws IOException {
171 final NodeIdentifier id = enter(localName, NodeIdentifier.class);
172 emitSchema(current().getSchema());
173 delegate.startLeafSet(id, childSizeHint);
177 public void startOrderedLeafSet(final String localName, final int childSizeHint) throws IOException {
178 delegate.startOrderedLeafSet(enter(localName, NodeIdentifier.class), childSizeHint);
182 public void startMapEntryNode(final Identifier<?> key, final int childSizeHint) throws IOException {
183 duplicateSchemaEnter();
184 NodeIdentifierWithPredicates identifier = ((KeyedListNodeCodecContext<?>) current()).serialize(key);
185 delegate.startMapEntryNode(identifier, childSizeHint);
189 public <T extends DataObject & Identifiable<?>> void startMapNode(final Class<T> mapEntryType,
190 final int childSizeHint) throws IOException {
191 delegate.startMapNode(enter(mapEntryType, NodeIdentifier.class), childSizeHint);
195 public <T extends DataObject & Identifiable<?>> void startOrderedMapNode(final Class<T> mapEntryType,
196 final int childSizeHint) throws IOException {
197 delegate.startOrderedMapNode(enter(mapEntryType, NodeIdentifier.class), childSizeHint);
201 public void startUnkeyedList(final Class<? extends DataObject> obj, final int childSizeHint) throws IOException {
202 delegate.startUnkeyedList(enter(obj, NodeIdentifier.class), childSizeHint);
206 public void startUnkeyedListItem(final int childSizeHint) throws IOException {
207 delegate.startUnkeyedListItem(duplicateSchemaEnter(), childSizeHint);
211 public void flush() throws IOException {
216 public void close() throws IOException {