94d1baddc5a86b5f115a19a53e19656d1e53a03e
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeOutputStreamWriter.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
10
11 import java.io.DataOutput;
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.Map;
15 import org.opendaylight.yangtools.yang.common.QName;
16
17 /**
18  * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
19  * a stream.
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
22  * you will first call
23  * {@link #startContainerNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, int)},
24  * then will call
25  * {@link #leafNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, Object)} twice
26  * and then, {@link #endNode()} to end container node.
27  * <p/>
28  * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
29  * while reading.
30  */
31 final class NormalizedNodeOutputStreamWriter extends AbstractNormalizedNodeDataOutput {
32     private final Map<String, Integer> stringCodeMap = new HashMap<>();
33
34     NormalizedNodeOutputStreamWriter(final DataOutput output) {
35         super(output);
36     }
37
38     @Override
39     protected short streamVersion() {
40         return TokenTypes.LITHIUM_VERSION;
41     }
42
43     @Override
44     protected void writeQName(final QName qname) throws IOException {
45         writeString(qname.getLocalName());
46         writeString(qname.getNamespace().toString());
47         writeString(qname.getFormattedRevision());
48     }
49
50     @Override
51     protected void writeString(final String string) throws IOException {
52         if (string != null) {
53             final Integer value = stringCodeMap.get(string);
54             if (value == null) {
55                 stringCodeMap.put(string, stringCodeMap.size());
56                 writeByte(TokenTypes.IS_STRING_VALUE);
57                 writeUTF(string);
58             } else {
59                 writeByte(TokenTypes.IS_CODE_VALUE);
60                 writeInt(value);
61             }
62         } else {
63             writeByte(TokenTypes.IS_NULL_VALUE);
64         }
65     }
66 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.