40ca4f2e8693ce6c025909c643fd2041b7331c15
[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 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 final class NormalizedNodeOutputStreamWriter extends AbstractNormalizedNodeDataOutput {
33     private final Map<String, Integer> stringCodeMap = new HashMap<>();
34
35     NormalizedNodeOutputStreamWriter(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         writeString(qname.getNamespace().toString());
48         writeString(qname.getRevision().map(Revision::toString).orElse(null));
49     }
50
51     @Override
52     protected void writeString(final String string) throws IOException {
53         if (string != null) {
54             final Integer value = stringCodeMap.get(string);
55             if (value == null) {
56                 stringCodeMap.put(string, stringCodeMap.size());
57                 writeByte(TokenTypes.IS_STRING_VALUE);
58                 writeUTF(string);
59             } else {
60                 writeByte(TokenTypes.IS_CODE_VALUE);
61                 writeInt(value);
62             }
63         } else {
64             writeByte(TokenTypes.IS_NULL_VALUE);
65         }
66     }
67 }