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;
55 * Revised ABI version. The messages remain the same as {@link #BORON}, but messages bearing QNames in any shape
56 * are using {@link NormalizedNodeStreamVersion#SODIUM}, which improves encoding.
60 public NormalizedNodeStreamVersion getStreamVersion() {
61 return NormalizedNodeStreamVersion.SODIUM;
66 * Version which is newer than any other version. This version exists purely for testing purposes.
69 TEST_FUTURE_VERSION(65535) {
71 public NormalizedNodeStreamVersion getStreamVersion() {
72 throw new UnsupportedOperationException();
76 private static final Logger LOG = LoggerFactory.getLogger(ABIVersion.class);
78 private final short value;
80 ABIVersion(final int intVersion) {
81 checkArgument(intVersion >= 0 && intVersion <= 65535);
82 value = (short) intVersion;
86 * Return the unsigned short integer identifying this version.
88 * @return Unsigned short integer identifying this version
90 public short shortValue() {
95 * Return the codebase-native ABI version. This version is the default version allocated to messages at runtime.
96 * Conversion to previous versions may incur additional overhead (such as object allocation).
98 * @return Current {@link ABIVersion}
100 public static @NonNull ABIVersion current() {
105 * Return the {@link ABIVersion} corresponding to an unsigned short integer. This method is provided for callers
106 * which provide their own recovery strategy in case of version incompatibility.
108 * @param value Short integer as returned from {@link #shortValue()}
109 * @return {@link ABIVersion}
110 * @throws FutureVersionException if the specified integer identifies a future version
111 * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
113 public static @NonNull ABIVersion valueOf(final short value) throws FutureVersionException, PastVersionException {
114 switch (Short.toUnsignedInt(value)) {
120 throw new PastVersionException(value, BORON);
126 throw new FutureVersionException(value, SODIUM);
131 public void writeTo(final DataOutput out) throws IOException {
132 out.writeShort(value);
136 * Return the NormalizedNode stream version corresponding to this particular ABI.
138 * @return Stream Version to use for this ABI version
140 public abstract @NonNull NormalizedNodeStreamVersion getStreamVersion();
143 * Read an {@link ABIVersion} from a {@link DataInput}. This method is provided for callers which do not have
144 * a recovery strategy for dealing with unsupported versions.
146 * @param in Input from which to read
147 * @return An {@link ABIVersion}
148 * @throws IOException If read fails or an unsupported version is encountered
150 public static @NonNull ABIVersion readFrom(final @NonNull DataInput in) throws IOException {
151 final short s = in.readShort();
154 } catch (FutureVersionException | PastVersionException e) {
155 throw new IOException("Unsupported version", e);
159 public static @NonNull ABIVersion inexactReadFrom(final @NonNull DataInput in) throws IOException {
160 final short onWire = in.readShort();
162 return ABIVersion.valueOf(onWire);
163 } catch (FutureVersionException e) {
164 LOG.debug("Received future version", e);
165 return ABIVersion.TEST_FUTURE_VERSION;
166 } catch (PastVersionException e) {
167 LOG.debug("Received past version", e);
168 return ABIVersion.TEST_PAST_VERSION;