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.datastore.persisted;
10 import com.google.common.annotations.Beta;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import java.io.DataInput;
14 import java.io.DataOutput;
15 import java.io.IOException;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeStreamVersion;
18 import org.opendaylight.yangtools.concepts.WritableObject;
21 * Enumeration of all ABI versions supported by this implementation of persistence. An ABI version has to be bumped
24 * <li>a new event is defined</li>
25 * <li>serialization format is changed</li>
29 * This version effectively defines the protocol version between actors participating on a particular shard. A shard
30 * participant instance should oppose RAFT candidates which produce persistence of an unsupported version. If a follower
31 * encounters an unsupported version it must not become fully-operational, as it does not have an accurate view
34 * @author Robert Varga
37 public enum PayloadVersion implements WritableObject {
38 // NOTE: enumeration values need to be sorted in ascending order of their version to keep Comparable working
41 * Version which is older than any other version. This version exists purely for testing purposes.
44 TEST_PAST_VERSION(0) {
46 public NormalizedNodeStreamVersion getStreamVersion() {
47 throw new UnsupportedOperationException();
52 * Initial ABI version, as shipped with Boron Simultaneous release.
54 // We seed the initial version to be the same as DataStoreVersions.BORON_VERSION for compatibility reasons.
57 public NormalizedNodeStreamVersion getStreamVersion() {
58 return NormalizedNodeStreamVersion.LITHIUM;
63 * Revised payload version. Payloads remain the same as {@link #BORON}, but messages bearing QNames in any shape
64 * are using {@link NormalizedNodeStreamVersion#SODIUM}, which improves encoding.
68 public NormalizedNodeStreamVersion getStreamVersion() {
69 return NormalizedNodeStreamVersion.SODIUM;
74 * Version which is newer than any other version. This version exists purely for testing purposes.
77 TEST_FUTURE_VERSION(65535) {
79 public NormalizedNodeStreamVersion getStreamVersion() {
80 throw new UnsupportedOperationException();
84 private final short value;
86 PayloadVersion(final int intVersion) {
87 Preconditions.checkArgument(intVersion >= 0 && intVersion <= 65535);
88 value = (short) intVersion;
92 * Return the unsigned short integer identifying this version.
94 * @return Unsigned short integer identifying this version
96 public short shortValue() {
101 * Return the NormalizedNode stream version corresponding to this particular ABI.
103 * @return Stream Version to use for this ABI version
106 public abstract NormalizedNodeStreamVersion getStreamVersion();
109 * Return the codebase-native persistence version. This version is the default version allocated to messages
110 * at runtime. Conversion to previous versions may incur additional overhead (such as object allocation).
112 * @return Current {@link PayloadVersion}
115 public static PayloadVersion current() {
120 * Return the {@link PayloadVersion} corresponding to an unsigned short integer. This method is provided for callers
121 * which provide their own recovery strategy in case of version incompatibility.
123 * @param version Short integer as returned from {@link #shortValue()}
124 * @return {@link PayloadVersion}
125 * @throws FutureVersionException if the specified integer identifies a future version
126 * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
129 public static PayloadVersion valueOf(final short version) throws FutureVersionException, PastVersionException {
130 switch (Short.toUnsignedInt(version)) {
136 throw new PastVersionException(version, BORON);
142 throw new FutureVersionException(version, SODIUM);
147 public void writeTo(final DataOutput out) throws IOException {
148 out.writeShort(value);
152 * Read an {@link PayloadVersion} from a {@link DataInput}. This method is provided for callers which do not have
153 * a recovery strategy for dealing with unsupported versions.
155 * @param in Input from which to read
156 * @return An {@link PayloadVersion}
157 * @throws IOException If read fails or an unsupported version is encountered
160 public static PayloadVersion readFrom(@Nonnull final DataInput in) throws IOException {
161 final short s = in.readShort();
164 } catch (FutureVersionException | PastVersionException e) {
165 throw new IOException("Unsupported version", e);