2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
10 import java.io.DataOutput;
11 import java.io.IOException;
12 import java.util.HashMap;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
18 * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
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
23 * {@link #startContainerNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, int)},
25 * {@link #leafNode(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier, Object)} twice
26 * and then, {@link #endNode()} to end container node.
28 * <p>Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
31 class SodiumNormalizedNodeOutputStreamWriter extends LithiumNormalizedNodeOutputStreamWriter {
32 private final Map<AugmentationIdentifier, Integer> aidCodeMap = new HashMap<>();
33 private final Map<QName, Integer> qnameCodeMap = new HashMap<>();
35 SodiumNormalizedNodeOutputStreamWriter(final DataOutput output) {
40 protected short streamVersion() {
41 return TokenTypes.SODIUM_VERSION;
45 protected final void writeQName(final QName qname) throws IOException {
46 final Integer value = qnameCodeMap.get(qname);
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);
53 // We have already seen this QName: write its code
54 writeByte(TokenTypes.IS_QNAME_CODE);
60 void writeAugmentationIdentifier(final AugmentationIdentifier aid) throws IOException {
61 final Integer value = aidCodeMap.get(aid);
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);
68 // We have already seen this QName set: write its code
69 writeByte(TokenTypes.IS_AUGMENT_CODE);