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 java.io.DataOutput;
12 import java.io.IOException;
13 import java.util.HashMap;
15 import org.opendaylight.yangtools.yang.common.QName;
18 * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
20 * A stream writer wrapper around this class will write node objects to stream in recursive manner.
21 * for example - If you have a ContainerNode which has a two LeafNode as children, then
23 * {@link #startContainerNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, int)},
25 * {@link #leafNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, Object)} twice
26 * and then, {@link #endNode()} to end container node.
28 * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
31 final class NormalizedNodeOutputStreamWriter extends AbstractNormalizedNodeDataOutput {
32 private final Map<String, Integer> stringCodeMap = new HashMap<>();
34 NormalizedNodeOutputStreamWriter(final DataOutput output) {
39 protected final short streamVersion() {
40 return TokenTypes.LITHIUM_VERSION;
44 protected void writeQName(final QName qname) throws IOException {
45 writeString(qname.getLocalName());
46 writeString(qname.getNamespace().toString());
47 writeString(qname.getFormattedRevision());
51 protected void writeString(final String string) throws IOException {
53 final Integer value = stringCodeMap.get(string);
55 stringCodeMap.put(string, stringCodeMap.size());
56 writeByte(TokenTypes.IS_STRING_VALUE);
59 writeByte(TokenTypes.IS_CODE_VALUE);
63 writeByte(TokenTypes.IS_NULL_VALUE);