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.DataContainer;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.Identifiable;
25 import org.opendaylight.yangtools.yang.binding.Identifier;
26 import org.opendaylight.yangtools.yang.binding.OpaqueObject;
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 AnydataBindingStreamWriter,
36 Delegator<NormalizedNodeStreamWriter> {
37 private final Deque<NodeCodecContext> schema = new ArrayDeque<>();
38 private final NormalizedNodeStreamWriter delegate;
39 private final NodeCodecContext rootNodeSchema;
41 BindingToNormalizedStreamWriter(final NodeCodecContext rootNodeSchema, 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 checkArgument(current() instanceof DataContainerCodecContext, "Could not start node %s", name);
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 checkArgument(current() instanceof DataObjectCodecContext);
113 DataObjectCodecContext<?,?> currentCasted = (DataObjectCodecContext<?,?>) current();
114 ValueNodeCodecContext leafContext = currentCasted.getLeafChild(localName);
116 NodeIdentifier domArg = 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 anydataNode(final String name, final OpaqueObject<?> value) throws IOException {
132 final Entry<NodeIdentifier, Object> dom = serializeLeaf(name, value);
133 if (delegate.startAnydataNode(dom.getKey(), value.getValue().getObjectModel())) {
134 delegate.scalarValue(dom.getValue());
140 public void anyxmlNode(final String name, final Object value) throws IOException {
141 final Entry<NodeIdentifier, Object> dom = serializeLeaf(name, value);
142 // FIXME: this is not quite right -- we should be handling other object models, too
143 if (delegate.startAnyxmlNode(dom.getKey(), DOMSource.class)) {
144 delegate.domSourceValue((DOMSource) dom.getValue());
150 public void leafSetEntryNode(final Object value) throws IOException {
151 final LeafSetNodeCodecContext ctx = (LeafSetNodeCodecContext) current();
152 final Object domValue = ctx.getValueCodec().serialize(value);
153 delegate.startLeafSetEntryNode(new NodeWithValue<>(ctx.getSchema().getQName(), domValue));
154 delegate.scalarValue(domValue);
159 public void startAugmentationNode(final Class<? extends Augmentation<?>> augmentationType)
161 delegate.startAugmentationNode(enter(augmentationType, AugmentationIdentifier.class));
165 public void startCase(final Class<? extends DataObject> caze, final int childSizeHint) {
166 enter(caze, NodeIdentifier.class);
170 public void startChoiceNode(final Class<? extends DataContainer> type, final int childSizeHint)
172 delegate.startChoiceNode(enter(type, NodeIdentifier.class), childSizeHint);
176 public void startContainerNode(final Class<? extends DataObject> object, final int childSizeHint)
178 delegate.startContainerNode(enter(object, NodeIdentifier.class), childSizeHint);
182 public void startLeafSet(final String localName, final int childSizeHint) throws IOException {
183 final NodeIdentifier id = enter(localName, NodeIdentifier.class);
184 emitSchema(current().getSchema());
185 delegate.startLeafSet(id, childSizeHint);
189 public void startOrderedLeafSet(final String localName, final int childSizeHint) throws IOException {
190 delegate.startOrderedLeafSet(enter(localName, NodeIdentifier.class), childSizeHint);
194 public void startMapEntryNode(final Identifier<?> key, final int childSizeHint) throws IOException {
195 duplicateSchemaEnter();
196 NodeIdentifierWithPredicates identifier = ((KeyedListNodeCodecContext<?>) current()).serialize(key);
197 delegate.startMapEntryNode(identifier, childSizeHint);
201 public <T extends DataObject & Identifiable<?>> void startMapNode(final Class<T> mapEntryType,
202 final int childSizeHint) throws IOException {
203 delegate.startMapNode(enter(mapEntryType, NodeIdentifier.class), childSizeHint);
207 public <T extends DataObject & Identifiable<?>> void startOrderedMapNode(final Class<T> mapEntryType,
208 final int childSizeHint) throws IOException {
209 delegate.startOrderedMapNode(enter(mapEntryType, NodeIdentifier.class), childSizeHint);
213 public void startUnkeyedList(final Class<? extends DataObject> obj, final int childSizeHint) throws IOException {
214 delegate.startUnkeyedList(enter(obj, NodeIdentifier.class), childSizeHint);
218 public void startUnkeyedListItem(final int childSizeHint) throws IOException {
219 delegate.startUnkeyedListItem(duplicateSchemaEnter(), childSizeHint);
223 public void flush() throws IOException {
228 public void close() throws IOException {