2 * Copyright (c) 2016 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.access;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.annotations.VisibleForTesting;
13 import java.io.DataInput;
14 import java.io.DataOutput;
15 import java.io.IOException;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.concepts.WritableObject;
18 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Enumeration of all ABI versions supported by this implementation of the client access API.
25 public enum ABIVersion implements WritableObject {
26 // NOTE: enumeration values need to be sorted in ascending order of their version to keep Comparable working
29 * Version which is older than any other version. This version exists purely for testing purposes.
32 TEST_PAST_VERSION(0) {
34 public NormalizedNodeStreamVersion getStreamVersion() {
35 throw new UnsupportedOperationException();
43 * Oldest ABI version we support. The messages remain the same as {@code BORON}, but messages bearing QNames in any
44 * shape are using {@link NormalizedNodeStreamVersion#MAGNESIUM}, which improves encoding.
46 * @deprecated Support for this version is going to be removed in the next major version.
48 @Deprecated(since = "7.0.0", forRemoval = true)
51 public NormalizedNodeStreamVersion getStreamVersion() {
52 return NormalizedNodeStreamVersion.MAGNESIUM;
56 * Revised ABI version. The messages remain the same as {@link #MAGNESIUM}, the serialization proxies in use are
57 * flat objects without any superclasses.
61 public NormalizedNodeStreamVersion getStreamVersion() {
62 return NormalizedNodeStreamVersion.MAGNESIUM;
67 * Version which is newer than any other version. This version exists purely for testing purposes.
70 TEST_FUTURE_VERSION(65535) {
72 public NormalizedNodeStreamVersion getStreamVersion() {
73 throw new UnsupportedOperationException();
77 private static final Logger LOG = LoggerFactory.getLogger(ABIVersion.class);
79 private final short value;
81 ABIVersion(final int intVersion) {
82 checkArgument(intVersion >= 0 && intVersion <= 65535);
83 value = (short) intVersion;
87 * Return the unsigned short integer identifying this version.
89 * @return Unsigned short integer identifying this version
91 public short shortValue() {
96 * Return the codebase-native ABI version. This version is the default version allocated to messages at runtime.
97 * Conversion to previous versions may incur additional overhead (such as object allocation).
99 * @return Current {@link ABIVersion}
101 public static @NonNull ABIVersion current() {
106 * Return the {@link ABIVersion} corresponding to an unsigned short integer. This method is provided for callers
107 * which provide their own recovery strategy in case of version incompatibility.
109 * @param value Short integer as returned from {@link #shortValue()}
110 * @return {@link ABIVersion}
111 * @throws FutureVersionException if the specified integer identifies a future version
112 * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
114 public static @NonNull ABIVersion valueOf(final short value) throws FutureVersionException, PastVersionException {
115 return switch (Short.toUnsignedInt(value)) {
116 case 0, 1, 2, 3, 4, 6, 7 -> throw new PastVersionException(value, MAGNESIUM);
118 case 9 -> CHLORINE_SR2;
119 default -> throw new FutureVersionException(value, CHLORINE_SR2);
124 * Return {@code true} if this version is earier than some {@code other} version.
126 * @param other Other {@link ABIVersion}
127 * @return {@code true} if {@code other is later}
128 * @throws NullPointerException if {@code other} is null
130 public boolean lt(final @NonNull ABIVersion other) {
131 return compareTo(other) < 0;
135 public void writeTo(final DataOutput out) throws IOException {
136 out.writeShort(value);
140 * Return the NormalizedNode stream version corresponding to this particular ABI.
142 * @return Stream Version to use for this ABI version
144 public abstract @NonNull NormalizedNodeStreamVersion getStreamVersion();
147 * Read an {@link ABIVersion} from a {@link DataInput}. This method is provided for callers which do not have
148 * a recovery strategy for dealing with unsupported versions.
150 * @param in Input from which to read
151 * @return An {@link ABIVersion}
152 * @throws IOException If read fails or an unsupported version is encountered
154 public static @NonNull ABIVersion readFrom(final @NonNull DataInput in) throws IOException {
155 final short s = in.readShort();
158 } catch (FutureVersionException | PastVersionException e) {
159 throw new IOException("Unsupported version", e);
163 public static @NonNull ABIVersion inexactReadFrom(final @NonNull DataInput in) throws IOException {
164 final short onWire = in.readShort();
166 return ABIVersion.valueOf(onWire);
167 } catch (FutureVersionException e) {
168 LOG.debug("Received future version", e);
169 return ABIVersion.TEST_FUTURE_VERSION;
170 } catch (PastVersionException e) {
171 LOG.debug("Received past version", e);
172 return ABIVersion.TEST_PAST_VERSION;