60250ffefd199e93f205ebe39ee96d9623ab6860
[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 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;
17 import java.util.Map;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20
21 /**
22  * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
23  * a stream.
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
26  * you will first call
27  * {@link #startContainerNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, int)},
28  * then will call
29  * {@link #leafNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, Object)} twice
30  * and then, {@link #endNode()} to end container node.
31  *
32  * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
33  * while reading.
34  */
35 public class NormalizedNodeOutputStreamWriter extends AbstractNormalizedNodeDataOutput implements NormalizedNodeStreamWriter {
36     private final Map<String, Integer> stringCodeMap = new HashMap<>();
37
38     /**
39      * @deprecated Use {@link #NormalizedNodeOutputStreamWriter(DataOutput)} instead.
40      */
41     @Deprecated
42     public NormalizedNodeOutputStreamWriter(final OutputStream stream) throws IOException {
43         this((DataOutput) new DataOutputStream(Preconditions.checkNotNull(stream)));
44     }
45
46     /**
47      * @deprecated Use {@link NormalizedNodeInputOutput#newDataOutput(DataOutput)} instead.
48      */
49     @Deprecated
50     public NormalizedNodeOutputStreamWriter(final DataOutput output) {
51         super(output);
52     }
53
54     @Override
55     protected final short streamVersion() {
56         return TokenTypes.LITHIUM_VERSION;
57     }
58
59     @Override
60     protected void writeQName(final QName qname) throws IOException {
61         writeString(qname.getLocalName());
62         writeString(qname.getNamespace().toString());
63         writeString(qname.getFormattedRevision());
64     }
65
66     @Override
67     protected void writeString(final String string) throws IOException {
68         if (string != null) {
69             final Integer value = stringCodeMap.get(string);
70             if (value == null) {
71                 stringCodeMap.put(string, stringCodeMap.size());
72                 writeByte(TokenTypes.IS_STRING_VALUE);
73                 writeUTF(string);
74             } else {
75                 writeByte(TokenTypes.IS_CODE_VALUE);
76                 writeInt(value);
77             }
78         } else {
79             writeByte(TokenTypes.IS_NULL_VALUE);
80         }
81     }
82 }