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 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.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
22 * Enumeration of all ABI versions supported by this implementation of persistence. An ABI version has to be bumped
25 * <li>a new event is defined</li>
26 * <li>serialization format is changed</li>
30 * This version effectively defines the protocol version between actors participating on a particular shard. A shard
31 * participant instance should oppose RAFT candidates which produce persistence of an unsupported version. If a follower
32 * encounters an unsupported version it must not become fully-operational, as it does not have an accurate view
35 * @author Robert Varga
38 public enum PayloadVersion implements WritableObject {
39 // NOTE: enumeration values need to be sorted in ascending order of their version to keep Comparable working
42 * Version which is older than any other version. This version exists purely for testing purposes.
45 TEST_PAST_VERSION(0) {
47 public NormalizedNodeStreamVersion getStreamVersion() {
48 throw new UnsupportedOperationException();
53 * ABI version as shipped in Sodium SR1 Simultaneous Release. QName-bearing messages are using
54 * {@link NormalizedNodeStreamVersion#SODIUM_SR1}, which improves encoding.
58 public NormalizedNodeStreamVersion getStreamVersion() {
59 return NormalizedNodeStreamVersion.SODIUM_SR1;
64 * Revised payload version. Payloads remain the same as {@link #SODIUM_SR1}, but messages bearing QNames in any
65 * shape are using {@link NormalizedNodeStreamVersion#MAGNESIUM}, which improves encoding.
69 public NormalizedNodeStreamVersion getStreamVersion() {
70 return NormalizedNodeStreamVersion.MAGNESIUM;
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 final short value;
87 PayloadVersion(final int intVersion) {
88 checkArgument(intVersion >= 0 && intVersion <= 65535);
89 value = (short) intVersion;
93 * Return the unsigned short integer identifying this version.
95 * @return Unsigned short integer identifying this version
97 public short shortValue() {
102 * Return the NormalizedNode stream version corresponding to this particular ABI.
104 * @return Stream Version to use for this ABI version
106 public abstract @NonNull 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}
114 public static @NonNull PayloadVersion current() {
119 * Return the {@link PayloadVersion} corresponding to an unsigned short integer. This method is provided for callers
120 * which provide their own recovery strategy in case of version incompatibility.
122 * @param version Short integer as returned from {@link #shortValue()}
123 * @return {@link PayloadVersion}
124 * @throws FutureVersionException if the specified integer identifies a future version
125 * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
127 public static @NonNull PayloadVersion valueOf(final short version)
128 throws FutureVersionException, PastVersionException {
129 return switch (Short.toUnsignedInt(version)) {
130 case 0, 1, 2, 3, 4, 5, 6 -> throw new PastVersionException(version, SODIUM_SR1);
131 case 7 -> SODIUM_SR1;
133 default -> throw new FutureVersionException(version, MAGNESIUM);
138 public void writeTo(final DataOutput out) throws IOException {
139 out.writeShort(value);
143 * Read an {@link PayloadVersion} 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 PayloadVersion}
148 * @throws IOException If read fails or an unsupported version is encountered
150 public static @NonNull PayloadVersion 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);