2 * Copyright (c) 2014 Cisco Systems, Inc. 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 static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.collect.ImmutableMap;
14 import java.io.DataOutput;
15 import java.io.IOException;
16 import java.math.BigDecimal;
17 import java.math.BigInteger;
18 import java.nio.charset.StandardCharsets;
19 import java.util.HashMap;
22 import org.opendaylight.yangtools.yang.common.Empty;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 * "original" type mapping. Baseline is Lithium but it really was introduced in Oxygen, where {@code type empty} was
33 * {@code uint8}, {@code uint16}, {@code uint32} use java.lang types with widening, hence their value types overlap with
34 * mapping of {@code int16}, {@code int32} and {@code int64}, making that difference indiscernible without YANG schema
37 abstract class AbstractLithiumDataOutput extends AbstractNormalizedNodeDataOutput {
38 private static final ImmutableMap<Class<?>, Byte> KNOWN_TYPES = ImmutableMap.<Class<?>, Byte>builder()
39 .put(String.class, ValueTypes.STRING_TYPE)
40 .put(Byte.class, ValueTypes.BYTE_TYPE)
41 .put(Integer.class, ValueTypes.INT_TYPE)
42 .put(Long.class, ValueTypes.LONG_TYPE)
43 .put(Boolean.class, ValueTypes.BOOL_TYPE)
44 .put(QName.class, ValueTypes.QNAME_TYPE)
45 .put(Short.class, ValueTypes.SHORT_TYPE)
46 .put(BigInteger.class, ValueTypes.BIG_INTEGER_TYPE)
47 .put(BigDecimal.class, ValueTypes.BIG_DECIMAL_TYPE)
48 .put(byte[].class, ValueTypes.BINARY_TYPE)
49 .put(Empty.class, ValueTypes.EMPTY_TYPE)
53 private final Map<String, Integer> stringCodeMap = new HashMap<>();
55 AbstractLithiumDataOutput(final DataOutput output) {
60 final void writeObject(final DataOutput rawOuput, final Object value) throws IOException {
61 byte type = getSerializableType(value);
62 // Write object type first
63 rawOuput.writeByte(type);
66 case ValueTypes.BOOL_TYPE:
67 rawOuput.writeBoolean((Boolean) value);
69 case ValueTypes.QNAME_TYPE:
70 writeQName((QName) value);
72 case ValueTypes.INT_TYPE:
73 rawOuput.writeInt((Integer) value);
75 case ValueTypes.BYTE_TYPE:
76 rawOuput.writeByte((Byte) value);
78 case ValueTypes.LONG_TYPE:
79 rawOuput.writeLong((Long) value);
81 case ValueTypes.SHORT_TYPE:
82 rawOuput.writeShort((Short) value);
84 case ValueTypes.BITS_TYPE:
85 writeObjSet((Set<?>) value);
87 case ValueTypes.BINARY_TYPE:
88 byte[] bytes = (byte[]) value;
89 rawOuput.writeInt(bytes.length);
90 rawOuput.write(bytes);
92 case ValueTypes.YANG_IDENTIFIER_TYPE:
93 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
95 case ValueTypes.EMPTY_TYPE:
97 case ValueTypes.STRING_BYTES_TYPE:
98 final byte[] valueBytes = value.toString().getBytes(StandardCharsets.UTF_8);
99 rawOuput.writeInt(valueBytes.length);
100 rawOuput.write(valueBytes);
103 rawOuput.writeUTF(value.toString());
108 final void defaultWriteQName(final QName qname) throws IOException {
109 writeString(qname.getLocalName());
110 writeModule(qname.getModule());
113 final void defaultWriteModule(final QNameModule module) throws IOException {
114 writeString(module.getNamespace().toString());
115 writeString(module.getRevision().map(Revision::toString).orElse(null));
119 protected final void writeString(final String string) throws IOException {
120 if (string != null) {
121 final Integer value = stringCodeMap.get(string);
123 stringCodeMap.put(string, stringCodeMap.size());
124 writeByte(TokenTypes.IS_STRING_VALUE);
127 writeByte(TokenTypes.IS_CODE_VALUE);
131 writeByte(TokenTypes.IS_NULL_VALUE);
135 abstract void writeModule(QNameModule module) throws IOException;
138 static final byte getSerializableType(final Object node) {
139 final Byte type = KNOWN_TYPES.get(requireNonNull(node).getClass());
141 if (type == ValueTypes.STRING_TYPE
142 && ((String) node).length() >= ValueTypes.STRING_BYTES_LENGTH_THRESHOLD) {
143 return ValueTypes.STRING_BYTES_TYPE;
148 if (node instanceof Set) {
149 return ValueTypes.BITS_TYPE;
152 if (node instanceof YangInstanceIdentifier) {
153 return ValueTypes.YANG_IDENTIFIER_TYPE;
156 throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());