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 private <T extends YangInstanceIdentifier.PathArgument> T enter(final Class<?> name, final Class<T> identifier) {
59 final NodeCodecContext next;
60 if (current() == null) {
61 // Entry of first node
62 next = rootNodeSchema;
64 Preconditions.checkArgument((current() instanceof DataContainerCodecContext), "Could not start node %s",
66 next = ((DataContainerCodecContext<?>) current()).getStreamChild(name);
68 this.schema.push(next);
69 @SuppressWarnings("unchecked")
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, IllegalArgumentException {
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, IllegalArgumentException {
117 Entry<NodeIdentifier, Object> dom = serializeLeaf(name, value);
118 getDelegate().anyxmlNode(dom.getKey(), dom.getValue());
122 public void leafSetEntryNode(final Object value) throws IOException, IllegalArgumentException {
123 LeafNodeCodecContext ctx = (LeafNodeCodecContext) current();
124 getDelegate().leafSetEntryNode(ctx.getValueCodec().serialize(value));
128 public void startAugmentationNode(final Class<? extends Augmentation<?>> augmentationType)
129 throws IOException, IllegalArgumentException {
130 getDelegate().startAugmentationNode(enter(augmentationType, AugmentationIdentifier.class));
134 public void startCase(final Class<? extends DataObject> caze, final int childSizeHint)
135 throws IllegalArgumentException {
136 enter(caze, NodeIdentifier.class);
140 public void startChoiceNode(final Class<? extends DataContainer> type, final int childSizeHint)
141 throws IOException, IllegalArgumentException {
142 getDelegate().startChoiceNode(enter(type, NodeIdentifier.class), childSizeHint);
146 public void startContainerNode(final Class<? extends DataObject> object, final int childSizeHint)
147 throws IOException, IllegalArgumentException {
148 getDelegate().startContainerNode(enter(object, NodeIdentifier.class), childSizeHint);
152 public void startLeafSet(final String localName, final int childSizeHint) throws IOException, IllegalArgumentException {
153 getDelegate().startLeafSet(enter(localName, NodeIdentifier.class), childSizeHint);
157 public void startMapEntryNode(final Identifier<?> key, final int childSizeHint) throws IOException, IllegalArgumentException {
158 duplicateSchemaEnter();
159 NodeIdentifierWithPredicates identifier = ((KeyedListNodeCodecContext) current()).serialize(key);
160 getDelegate().startMapEntryNode(identifier, childSizeHint);
164 public <T extends DataObject & Identifiable<?>> void startMapNode(final Class<T> mapEntryType,
165 final int childSizeHint) throws IOException, IllegalArgumentException {
166 getDelegate().startMapNode(enter(mapEntryType, NodeIdentifier.class), childSizeHint);
170 public <T extends DataObject & Identifiable<?>> void startOrderedMapNode(final Class<T> mapEntryType,
171 final int childSizeHint) throws IOException, IllegalArgumentException {
172 getDelegate().startOrderedMapNode(enter(mapEntryType, NodeIdentifier.class), childSizeHint);
176 public void startUnkeyedList(final Class<? extends DataObject> obj, final int childSizeHint)
177 throws IOException, IllegalArgumentException {
178 getDelegate().startUnkeyedList(enter(obj, NodeIdentifier.class), childSizeHint);
182 public void startUnkeyedListItem(final int childSizeHint) throws IllegalStateException, IOException {
183 getDelegate().startUnkeyedListItem(duplicateSchemaEnter(), childSizeHint);
187 public void flush() throws IOException {
188 getDelegate().flush();
192 public void close() throws IOException {
193 getDelegate().close();