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.yangtools.yang.data.codec.binfmt;
10 import static com.google.common.base.Verify.verify;
12 import java.io.DataInput;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 * Neon SR2 specialization of AbstractLithiumDataInput. Unlike its Lithium counterpart, this format uses coding for
23 * QNameModules, QNames, NodeIdentifiers and AugmentationIdentifiers, thus reducing stream duplication.
25 final class NeonSR2NormalizedNodeInputStreamReader extends AbstractLithiumDataInput {
26 private final ArrayList<NodeIdentifier> codedNodeIdentifiers = new ArrayList<>();
27 private final List<AugmentationIdentifier> codedAugments = new ArrayList<>();
28 private final List<QNameModule> codedModules = new ArrayList<>();
29 private final List<QName> codedQNames = new ArrayList<>();
31 NeonSR2NormalizedNodeInputStreamReader(final DataInput input) {
36 @Deprecated(since = "11.0.0", forRemoval = true)
37 public NormalizedNodeStreamVersion getVersion() {
38 return NormalizedNodeStreamVersion.NEON_SR2;
42 public QName readQName() throws IOException {
43 final byte valueType = readByte();
44 return switch (valueType) {
45 case NeonSR2Tokens.IS_QNAME_CODE -> codedQName(readInt());
46 case NeonSR2Tokens.IS_QNAME_VALUE -> rawQName();
47 default -> throw new IOException("Unhandled QName value type " + valueType);
52 AugmentationIdentifier readAugmentationIdentifier() throws IOException {
53 final byte valueType = readByte();
54 return switch (valueType) {
55 case NeonSR2Tokens.IS_AUGMENT_CODE -> codedAugmentId(readInt());
56 case NeonSR2Tokens.IS_AUGMENT_VALUE -> rawAugmentId();
57 default -> throw new IOException("Unhandled AugmentationIdentifier value type " + valueType);
62 NodeIdentifier readNodeIdentifier() throws IOException {
63 // NodeIdentifier rides on top of QName, with this method really saying 'interpret next QName as NodeIdentifier'
64 // to do that we inter-mingle with readQName()
65 final byte valueType = readByte();
66 return switch (valueType) {
67 case NeonSR2Tokens.IS_QNAME_CODE -> codedNodeIdentifier(readInt());
68 case NeonSR2Tokens.IS_QNAME_VALUE -> rawNodeIdentifier();
69 default -> throw new IOException("Unhandled NodeIdentifier value type " + valueType);
73 private QNameModule readModule() throws IOException {
74 final byte valueType = readByte();
75 return switch (valueType) {
76 case NeonSR2Tokens.IS_MODULE_CODE -> codedModule(readInt());
77 case NeonSR2Tokens.IS_MODULE_VALUE -> rawModule();
78 default -> throw new IOException("Unhandled QNameModule value type " + valueType);
82 private NodeIdentifier codedNodeIdentifier(final int code) throws IOException {
83 final NodeIdentifier existing = codedNodeIdentifiers.size() > code ? codedNodeIdentifiers.get(code) : null;
84 return existing != null ? existing : storeNodeIdentifier(code, codedQName(code));
87 private NodeIdentifier rawNodeIdentifier() throws IOException {
88 // Capture size before it incremented
89 final int code = codedQNames.size();
90 return storeNodeIdentifier(code, rawQName());
93 private NodeIdentifier storeNodeIdentifier(final int code, final QName qname) {
94 final NodeIdentifier ret = NodeIdentifier.create(qname);
95 final int size = codedNodeIdentifiers.size();
99 codedNodeIdentifiers.ensureCapacity(code + 1);
100 for (int i = size; i < code; ++i) {
101 codedNodeIdentifiers.add(null);
104 codedNodeIdentifiers.add(ret);
106 final NodeIdentifier check = codedNodeIdentifiers.set(code, ret);
107 verify(check == null);
113 private QName codedQName(final int code) throws IOException {
114 return getCode("QName", codedQNames, code);
117 private QName rawQName() throws IOException {
118 final String localName = readCodedString();
119 final QNameModule module = readModule();
120 final QName qname = QNameFactory.create(module, localName);
121 codedQNames.add(qname);
125 private AugmentationIdentifier codedAugmentId(final int code) throws IOException {
126 return getCode("QName set", codedAugments, code);
129 private AugmentationIdentifier rawAugmentId() throws IOException {
130 final AugmentationIdentifier aid = defaultReadAugmentationIdentifier();
131 codedAugments.add(aid);
135 private QNameModule codedModule(final int code) throws IOException {
136 return getCode("Module", codedModules, code);
139 private QNameModule rawModule() throws IOException {
140 final String namespace = readCodedString();
141 final String revision = readCodedString();
142 final QNameModule mod = QNameFactory.createModule(namespace, revision);
143 codedModules.add(mod);
147 private static <T> T getCode(final String name, final List<T> list, final int code) throws IOException {
149 return list.get(code);
150 } catch (IndexOutOfBoundsException e) {
151 throw new IOException(name + " code " + code + " was not found", e);