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,
41 final NormalizedNodeStreamWriter delegate) {
42 this.rootNodeSchema = requireNonNull(rootNodeSchema);
43 this.delegate = requireNonNull(delegate);
46 static BindingToNormalizedStreamWriter create(final NodeCodecContext<?> schema,
47 final NormalizedNodeStreamWriter delegate) {
48 return new BindingToNormalizedStreamWriter(schema, delegate);
51 private void emitSchema(final Object schemaNode) {
52 delegate.nextDataSchemaNode((DataSchemaNode) schemaNode);
55 NodeCodecContext<?> current() {
59 private NodeIdentifier duplicateSchemaEnter() {
60 final NodeCodecContext<?> next;
61 if (current() == null) {
62 // Entry of first node
63 next = rootNodeSchema;
67 this.schema.push(next);
68 return (NodeIdentifier) current().getDomPathArgument();
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;
78 Preconditions.checkArgument(current() instanceof DataContainerCodecContext, "Could not start node %s",
80 next = ((DataContainerCodecContext<?,?>) current()).streamChild((Class) name);
82 this.schema.push(next);
83 T arg = (T) next.getDomPathArgument();
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();
97 public NormalizedNodeStreamWriter getDelegate() {
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)) {
111 private Map.Entry<NodeIdentifier, Object> serializeLeaf(final String localName, final Object value) {
112 Preconditions.checkArgument(current() instanceof DataObjectCodecContext);
114 DataObjectCodecContext<?,?> currentCasted = (DataObjectCodecContext<?,?>) current();
115 LeafNodeCodecContext<?> leafContext = currentCasted.getLeafChild(localName);
117 NodeIdentifier domArg = (NodeIdentifier) leafContext.getDomPathArgument();
118 Object domValue = leafContext.getValueCodec().serialize(value);
119 emitSchema(leafContext.getSchema());
120 return new AbstractMap.SimpleEntry<>(domArg, domValue);
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());
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());
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);
149 public void startAugmentationNode(final Class<? extends Augmentation<?>> augmentationType)
151 delegate.startAugmentationNode(enter(augmentationType, AugmentationIdentifier.class));
155 public void startCase(final Class<? extends DataObject> caze, final int childSizeHint) {
156 enter(caze, NodeIdentifier.class);
160 public void startChoiceNode(final Class<? extends DataContainer> type, final int childSizeHint)
162 delegate.startChoiceNode(enter(type, NodeIdentifier.class), childSizeHint);
166 public void startContainerNode(final Class<? extends DataObject> object, final int childSizeHint)
168 delegate.startContainerNode(enter(object, NodeIdentifier.class), childSizeHint);
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);
179 public void startOrderedLeafSet(final String localName, final int childSizeHint) throws IOException {
180 delegate.startOrderedLeafSet(enter(localName, NodeIdentifier.class), childSizeHint);
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);
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);
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);
203 public void startUnkeyedList(final Class<? extends DataObject> obj, final int childSizeHint) throws IOException {
204 delegate.startUnkeyedList(enter(obj, NodeIdentifier.class), childSizeHint);
208 public void startUnkeyedListItem(final int childSizeHint) throws IOException {
209 delegate.startUnkeyedListItem(duplicateSchemaEnter(), childSizeHint);
213 public void flush() throws IOException {
218 public void close() throws IOException {