2 * Copyright (c) 2014, 2015 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
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
11 import com.google.common.base.Preconditions;
12 import java.io.DataOutput;
13 import java.io.DataOutputStream;
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.util.HashMap;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
24 * A stream writer wrapper around this class will write node objects to stream in recursive manner.
25 * for example - If you have a ContainerNode which has a two LeafNode as children, then
27 * {@link #startContainerNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, int)},
29 * {@link #leafNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, Object)} twice
30 * and then, {@link #endNode()} to end container node.
32 * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
35 public class NormalizedNodeOutputStreamWriter extends AbstractNormalizedNodeDataOutput implements NormalizedNodeStreamWriter {
36 private final Map<String, Integer> stringCodeMap = new HashMap<>();
39 * @deprecated Use {@link #NormalizedNodeOutputStreamWriter(DataOutput)} instead.
42 public NormalizedNodeOutputStreamWriter(final OutputStream stream) throws IOException {
43 this((DataOutput) new DataOutputStream(Preconditions.checkNotNull(stream)));
47 * @deprecated Use {@link NormalizedNodeInputOutput#newDataOutput(DataOutput)} instead.
50 public NormalizedNodeOutputStreamWriter(final DataOutput output) {
55 protected final short streamVersion() {
56 return TokenTypes.LITHIUM_VERSION;
60 protected void writeQName(final QName qname) throws IOException {
61 writeString(qname.getLocalName());
62 writeString(qname.getNamespace().toString());
63 writeString(qname.getFormattedRevision());
67 protected void writeString(final String string) throws IOException {
69 final Integer value = stringCodeMap.get(string);
71 stringCodeMap.put(string, stringCodeMap.size());
72 writeByte(TokenTypes.IS_STRING_VALUE);
75 writeByte(TokenTypes.IS_CODE_VALUE);
79 writeByte(TokenTypes.IS_NULL_VALUE);