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 / SodiumNormalizedNodeInputStreamReader.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.DataInput;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
16
17 final class SodiumNormalizedNodeInputStreamReader extends LithiumNormalizedNodeInputStreamReader {
18     private final List<AugmentationIdentifier> codedAugments = new ArrayList<>();
19     private final List<QName> codedQNames = new ArrayList<>();
20
21     SodiumNormalizedNodeInputStreamReader(final DataInput input) {
22         super(input);
23     }
24
25     @Override
26     public NormalizedNodeStreamVersion getVersion() throws IOException {
27         return NormalizedNodeStreamVersion.SODIUM;
28     }
29
30     @Override
31     QName readQName() throws IOException {
32         final byte valueType = readByte();
33         switch (valueType) {
34             case TokenTypes.IS_QNAME_CODE:
35                 return codedQName(readInt());
36             case TokenTypes.IS_QNAME_VALUE:
37                 return rawQName();
38             default:
39                 throw new IOException("Unhandled QName value type " + valueType);
40         }
41     }
42
43     @Override
44     AugmentationIdentifier readAugmentationIdentifier() throws IOException {
45         final byte valueType = readByte();
46         switch (valueType) {
47             case TokenTypes.IS_AUGMENT_CODE:
48                 return codecAugmentId(readInt());
49             case TokenTypes.IS_AUGMENT_VALUE:
50                 return rawAugmentId();
51             default:
52                 throw new IOException("Unhandled QName value type " + valueType);
53         }
54     }
55
56     private QName codedQName(final int code) throws IOException {
57         try {
58             return codedQNames.get(code);
59         } catch (IndexOutOfBoundsException e) {
60             throw new IOException("QName code " + code + " was not found", e);
61         }
62     }
63
64     private QName rawQName() throws IOException {
65         final QName qname = super.readQName();
66         codedQNames.add(qname);
67         return qname;
68     }
69
70     private AugmentationIdentifier codecAugmentId(final int code) throws IOException {
71         try {
72             return codedAugments.get(code);
73         } catch (IndexOutOfBoundsException e) {
74             throw new IOException("QName set code " + code + " was not found", e);
75         }
76     }
77
78     private AugmentationIdentifier rawAugmentId() throws IOException {
79         final AugmentationIdentifier aid = super.readAugmentationIdentifier();
80         codedAugments.add(aid);
81         return aid;
82     }
83 }