Add support for coded QNames/AugmentationIdentifiers
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / SodiumNormalizedNodeOutputStreamWriter.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.data.api.YangInstanceIdentifier.AugmentationIdentifier;
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  *
28  * <p>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 class SodiumNormalizedNodeOutputStreamWriter extends LithiumNormalizedNodeOutputStreamWriter {
32     private final Map<AugmentationIdentifier, Integer> aidCodeMap = new HashMap<>();
33     private final Map<QName, Integer> qnameCodeMap = new HashMap<>();
34
35     SodiumNormalizedNodeOutputStreamWriter(final DataOutput output) {
36         super(output);
37     }
38
39     @Override
40     protected short streamVersion() {
41         return TokenTypes.SODIUM_VERSION;
42     }
43
44     @Override
45     protected final void writeQName(final QName qname) throws IOException {
46         final Integer value = qnameCodeMap.get(qname);
47         if (value == null) {
48             // Fresh QName, remember it and emit as three strings
49             qnameCodeMap.put(qname, qnameCodeMap.size());
50             writeByte(TokenTypes.IS_QNAME_VALUE);
51             super.writeQName(qname);
52         } else {
53             // We have already seen this QName: write its code
54             writeByte(TokenTypes.IS_QNAME_CODE);
55             writeInt(value);
56         }
57     }
58
59     @Override
60     void writeAugmentationIdentifier(final AugmentationIdentifier aid) throws IOException {
61         final Integer value = aidCodeMap.get(aid);
62         if (value == null) {
63             // Fresh QName, remember it and emit as three strings
64             aidCodeMap.put(aid, aidCodeMap.size());
65             writeByte(TokenTypes.IS_AUGMENT_VALUE);
66             super.writeAugmentationIdentifier(aid);
67         } else {
68             // We have already seen this QName set: write its code
69             writeByte(TokenTypes.IS_AUGMENT_CODE);
70             writeInt(value);
71         }
72     }
73 }