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 java.util.Objects.requireNonNull;
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;
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 Preconditions.checkArgument(current() instanceof DataContainerCodecContext, "Could not start node %s",
79 next = ((DataContainerCodecContext<?,?>) current()).streamChild((Class) name);
81 this.schema.push(next);
82 T arg = (T) next.getDomPathArgument();
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();
96 public NormalizedNodeStreamWriter getDelegate() {
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)) {
110 private Map.Entry<NodeIdentifier, Object> serializeLeaf(final String localName, final Object value) {
111 Preconditions.checkArgument(current() instanceof DataObjectCodecContext);
113 DataObjectCodecContext<?,?> currentCasted = (DataObjectCodecContext<?,?>) current();
114 ValueNodeCodecContext leafContext = currentCasted.getLeafChild(localName);
116 NodeIdentifier domArg = (NodeIdentifier) leafContext.getDomPathArgument();
117 Object domValue = leafContext.getValueCodec().serialize(value);
118 emitSchema(leafContext.getSchema());
119 return new AbstractMap.SimpleEntry<>(domArg, domValue);
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());
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());
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);
148 public void startAugmentationNode(final Class<? extends Augmentation<?>> augmentationType)
150 delegate.startAugmentationNode(enter(augmentationType, AugmentationIdentifier.class));
154 public void startCase(final Class<? extends DataObject> caze, final int childSizeHint) {
155 enter(caze, NodeIdentifier.class);
159 public void startChoiceNode(final Class<? extends DataContainer> type, final int childSizeHint)
161 delegate.startChoiceNode(enter(type, NodeIdentifier.class), childSizeHint);
165 public void startContainerNode(final Class<? extends DataObject> object, final int childSizeHint)
167 delegate.startContainerNode(enter(object, NodeIdentifier.class), childSizeHint);
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);
178 public void startOrderedLeafSet(final String localName, final int childSizeHint) throws IOException {
179 delegate.startOrderedLeafSet(enter(localName, NodeIdentifier.class), childSizeHint);
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);
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);
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);
202 public void startUnkeyedList(final Class<? extends DataObject> obj, final int childSizeHint) throws IOException {
203 delegate.startUnkeyedList(enter(obj, NodeIdentifier.class), childSizeHint);
207 public void startUnkeyedListItem(final int childSizeHint) throws IOException {
208 delegate.startUnkeyedListItem(duplicateSchemaEnter(), childSizeHint);
212 public void flush() throws IOException {
217 public void close() throws IOException {