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.yangtools.concepts.WritableObject;
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 * @author Robert Varga
28 public enum ABIVersion implements WritableObject {
29 // NOTE: enumeration values need to be sorted in ascending order of their version to keep Comparable working
32 * Version which is older than any other version. This version exists purely for testing purposes.
38 * Initial ABI version, as shipped with Boron Simultaneous release.
40 // We seed the initial version to be the same as DataStoreVersions.BORON-VERSION for compatibility reasons.
44 * Version which is newer than any other version. This version exists purely for testing purposes.
47 TEST_FUTURE_VERSION(65535);
49 private static final Logger LOG = LoggerFactory.getLogger(ABIVersion.class);
51 private final short value;
53 ABIVersion(final int intVersion) {
54 checkArgument(intVersion >= 0 && intVersion <= 65535);
55 value = (short) intVersion;
59 * Return the unsigned short integer identifying this version.
61 * @return Unsigned short integer identifying this version
63 public short shortValue() {
68 * Return the codebase-native ABI version. This version is the default version allocated to messages at runtime.
69 * Conversion to previous versions may incur additional overhead (such as object allocation).
71 * @return Current {@link ABIVersion}
73 public static @NonNull ABIVersion current() {
78 * Return the {@link ABIVersion} corresponding to an unsigned short integer. This method is provided for callers
79 * which provide their own recovery strategy in case of version incompatibility.
81 * @param value Short integer as returned from {@link #shortValue()}
82 * @return {@link ABIVersion}
83 * @throws FutureVersionException if the specified integer identifies a future version
84 * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
86 public static @NonNull ABIVersion valueOf(final short value) throws FutureVersionException, PastVersionException {
87 switch (Short.toUnsignedInt(value)) {
93 throw new PastVersionException(value, BORON);
97 throw new FutureVersionException(value, BORON);
102 public void writeTo(final DataOutput out) throws IOException {
103 out.writeShort(value);
107 * Read an {@link ABIVersion} from a {@link DataInput}. This method is provided for callers which do not have
108 * a recovery strategy for dealing with unsupported versions.
110 * @param in Input from which to read
111 * @return An {@link ABIVersion}
112 * @throws IOException If read fails or an unsupported version is encountered
114 public static @NonNull ABIVersion readFrom(final @NonNull DataInput in) throws IOException {
115 final short s = in.readShort();
118 } catch (FutureVersionException | PastVersionException e) {
119 throw new IOException("Unsupported version", e);
123 public static @NonNull ABIVersion inexactReadFrom(final @NonNull DataInput in) throws IOException {
124 final short onWire = in.readShort();
126 return ABIVersion.valueOf(onWire);
127 } catch (FutureVersionException e) {
128 LOG.debug("Received future version", e);
129 return ABIVersion.TEST_FUTURE_VERSION;
130 } catch (PastVersionException e) {
131 LOG.debug("Received past version", e);
132 return ABIVersion.TEST_PAST_VERSION;