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.yangtools.binding.data.codec.impl;
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;
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;
30 class BindingToNormalizedStreamWriter implements BindingStreamEventWriter, Delegator<NormalizedNodeStreamWriter> {
32 private final NormalizedNodeStreamWriter delegate;
33 private final Deque<NodeCodecContext<?>> schema = new ArrayDeque<>();
34 private final NodeCodecContext<?> rootNodeSchema;
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);
42 private NodeCodecContext<?> current() {
46 private NodeIdentifier duplicateSchemaEnter() {
47 final NodeCodecContext<?> next;
48 if (current() == null) {
49 // Entry of first node
50 next = rootNodeSchema;
54 this.schema.push(next);
55 return (NodeIdentifier) current().getDomPathArgument();
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;
65 Preconditions.checkArgument((current() instanceof DataContainerCodecContext), "Could not start node %s",
67 next = ((DataContainerCodecContext<?,?>) current()).streamChild((Class) name);
69 this.schema.push(next);
70 T arg = (T) next.getDomPathArgument();
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();
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();
94 public NormalizedNodeStreamWriter getDelegate() {
98 private Map.Entry<NodeIdentifier, Object> serializeLeaf(final String localName, final Object value) {
99 Preconditions.checkArgument(current() instanceof DataObjectCodecContext);
101 DataObjectCodecContext<?,?> currentCasted = (DataObjectCodecContext<?,?>) current();
102 LeafNodeCodecContext<?> leafContext = currentCasted.getLeafChild(localName);
104 NodeIdentifier domArg = (NodeIdentifier) leafContext.getDomPathArgument();
105 Object domValue = leafContext.getValueCodec().serialize(value);
106 return new AbstractMap.SimpleEntry<>(domArg, domValue);
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());
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());
122 public void leafSetEntryNode(final Object value) throws IOException {
123 LeafNodeCodecContext<?> ctx = (LeafNodeCodecContext<?>) current();
124 getDelegate().leafSetEntryNode(ctx.getValueCodec().serialize(value));
128 public void startAugmentationNode(final Class<? extends Augmentation<?>> augmentationType) throws IOException {
129 getDelegate().startAugmentationNode(enter(augmentationType, AugmentationIdentifier.class));
133 public void startCase(final Class<? extends DataObject> caze, final int childSizeHint) {
134 enter(caze, NodeIdentifier.class);
138 public void startChoiceNode(final Class<? extends DataContainer> type, final int childSizeHint) throws IOException {
139 getDelegate().startChoiceNode(enter(type, NodeIdentifier.class), childSizeHint);
143 public void startContainerNode(final Class<? extends DataObject> object, final int childSizeHint)
145 getDelegate().startContainerNode(enter(object, NodeIdentifier.class), childSizeHint);
149 public void startLeafSet(final String localName, final int childSizeHint) throws IOException {
150 getDelegate().startLeafSet(enter(localName, NodeIdentifier.class), childSizeHint);
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);
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);
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);
173 public void startUnkeyedList(final Class<? extends DataObject> obj, final int childSizeHint) throws IOException {
174 getDelegate().startUnkeyedList(enter(obj, NodeIdentifier.class), childSizeHint);
178 public void startUnkeyedListItem(final int childSizeHint) throws IOException {
179 getDelegate().startUnkeyedListItem(duplicateSchemaEnter(), childSizeHint);
183 public void flush() throws IOException {
184 getDelegate().flush();
188 public void close() throws IOException {
189 getDelegate().close();