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.Beta;
13 import com.google.common.annotations.VisibleForTesting;
14 import java.io.DataInput;
15 import java.io.DataOutput;
16 import java.io.IOException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeStreamVersion;
19 import org.opendaylight.yangtools.concepts.WritableObject;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Enumeration of all ABI versions supported by this implementation of the client access API.
26 * @author Robert Varga
29 public enum ABIVersion implements WritableObject {
30 // NOTE: enumeration values need to be sorted in ascending order of their version to keep Comparable working
33 * Version which is older than any other version. This version exists purely for testing purposes.
36 TEST_PAST_VERSION(0) {
38 public NormalizedNodeStreamVersion getStreamVersion() {
39 throw new UnsupportedOperationException();
44 * Initial ABI version, as shipped with Boron Simultaneous release.
46 // We seed the initial version to be the same as DataStoreVersions.BORON-VERSION for compatibility reasons.
49 public NormalizedNodeStreamVersion getStreamVersion() {
50 return NormalizedNodeStreamVersion.LITHIUM;
54 * Revised ABI version. The messages remain the same as {@link #BORON}, but messages bearing QNames in any shape
55 * are using {@link NormalizedNodeStreamVersion#NEON_SR2}, which improves encoding.
59 public NormalizedNodeStreamVersion getStreamVersion() {
60 return NormalizedNodeStreamVersion.NEON_SR2;
64 * Revised ABI version. The messages remain the same as {@link #BORON}, but messages bearing QNames in any shape
65 * are using {@link NormalizedNodeStreamVersion#SODIUM_SR1}, which improves encoding.
69 public NormalizedNodeStreamVersion getStreamVersion() {
70 return NormalizedNodeStreamVersion.SODIUM_SR1;
75 * Version which is newer than any other version. This version exists purely for testing purposes.
78 TEST_FUTURE_VERSION(65535) {
80 public NormalizedNodeStreamVersion getStreamVersion() {
81 throw new UnsupportedOperationException();
85 private static final Logger LOG = LoggerFactory.getLogger(ABIVersion.class);
87 private final short value;
89 ABIVersion(final int intVersion) {
90 checkArgument(intVersion >= 0 && intVersion <= 65535);
91 value = (short) intVersion;
95 * Return the unsigned short integer identifying this version.
97 * @return Unsigned short integer identifying this version
99 public short shortValue() {
104 * Return the codebase-native ABI version. This version is the default version allocated to messages at runtime.
105 * Conversion to previous versions may incur additional overhead (such as object allocation).
107 * @return Current {@link ABIVersion}
109 public static @NonNull ABIVersion current() {
114 * Return the {@link ABIVersion} corresponding to an unsigned short integer. This method is provided for callers
115 * which provide their own recovery strategy in case of version incompatibility.
117 * @param value Short integer as returned from {@link #shortValue()}
118 * @return {@link ABIVersion}
119 * @throws FutureVersionException if the specified integer identifies a future version
120 * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
122 public static @NonNull ABIVersion valueOf(final short value) throws FutureVersionException, PastVersionException {
123 switch (Short.toUnsignedInt(value)) {
129 throw new PastVersionException(value, BORON);
137 throw new FutureVersionException(value, SODIUM_SR1);
142 public void writeTo(final DataOutput out) throws IOException {
143 out.writeShort(value);
147 * Return the NormalizedNode stream version corresponding to this particular ABI.
149 * @return Stream Version to use for this ABI version
151 public abstract @NonNull NormalizedNodeStreamVersion getStreamVersion();
154 * Read an {@link ABIVersion} from a {@link DataInput}. This method is provided for callers which do not have
155 * a recovery strategy for dealing with unsupported versions.
157 * @param in Input from which to read
158 * @return An {@link ABIVersion}
159 * @throws IOException If read fails or an unsupported version is encountered
161 public static @NonNull ABIVersion readFrom(final @NonNull DataInput in) throws IOException {
162 final short s = in.readShort();
165 } catch (FutureVersionException | PastVersionException e) {
166 throw new IOException("Unsupported version", e);
170 public static @NonNull ABIVersion inexactReadFrom(final @NonNull DataInput in) throws IOException {
171 final short onWire = in.readShort();
173 return ABIVersion.valueOf(onWire);
174 } catch (FutureVersionException e) {
175 LOG.debug("Received future version", e);
176 return ABIVersion.TEST_FUTURE_VERSION;
177 } catch (PastVersionException e) {
178 LOG.debug("Received past version", e);
179 return ABIVersion.TEST_PAST_VERSION;