a0aa813f64dd90cc0cceea7608f1c9aae5e950e8
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / LithiumNormalizedNodeOutputStreamWriter.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 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
9
10 import java.io.DataOutput;
11 import java.io.IOException;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.common.Revision;
17
18 /**
19  * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
20  * a stream.
21  * A stream writer wrapper around this class will write node objects to stream in recursive manner.
22  * for example - If you have a ContainerNode which has a two LeafNode as children, then
23  * you will first call
24  * {@link #startContainerNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, int)},
25  * then will call
26  * {@link #leafNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, Object)} twice
27  * and then, {@link #endNode()} to end container node.
28  *
29  * <p>Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
30  * while reading.
31  */
32 class LithiumNormalizedNodeOutputStreamWriter extends AbstractNormalizedNodeDataOutput {
33     private final Map<String, Integer> stringCodeMap = new HashMap<>();
34
35     LithiumNormalizedNodeOutputStreamWriter(final DataOutput output) {
36         super(output);
37     }
38
39     @Override
40     protected short streamVersion() {
41         return TokenTypes.LITHIUM_VERSION;
42     }
43
44     @Override
45     protected void writeQName(final QName qname) throws IOException {
46         writeString(qname.getLocalName());
47         writeModule(qname.getModule());
48     }
49
50     void writeModule(final QNameModule module) throws IOException {
51         writeString(module.getNamespace().toString());
52         writeString(module.getRevision().map(Revision::toString).orElse(null));
53     }
54
55     @Override
56     protected final void writeString(final String string) throws IOException {
57         if (string != null) {
58             final Integer value = stringCodeMap.get(string);
59             if (value == null) {
60                 stringCodeMap.put(string, stringCodeMap.size());
61                 writeByte(TokenTypes.IS_STRING_VALUE);
62                 writeUTF(string);
63             } else {
64                 writeByte(TokenTypes.IS_CODE_VALUE);
65                 writeInt(value);
66             }
67         } else {
68             writeByte(TokenTypes.IS_NULL_VALUE);
69         }
70     }
71 }