Switch to use PayloadVersion.CHLORINE_SR2
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / PayloadVersion.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.persisted;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
20
21 /**
22  * Enumeration of all ABI versions supported by this implementation of persistence. An ABI version has to be bumped
23  * whenever:
24  * <ul>
25  * <li>a new event is defined</li>
26  * <li>serialization format is changed</li>
27  * </ul>
28  *
29  * <p>
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
33  * of shard state.
34  *
35  * @author Robert Varga
36  */
37 @Beta
38 public enum PayloadVersion implements WritableObject {
39     // NOTE: enumeration values need to be sorted in ascending order of their version to keep Comparable working
40
41     /**
42      * Version which is older than any other version. This version exists purely for testing purposes.
43      */
44     @VisibleForTesting
45     TEST_PAST_VERSION(0) {
46         @Override
47         public NormalizedNodeStreamVersion getStreamVersion() {
48             throw new UnsupportedOperationException();
49         }
50     },
51
52     /**
53      * ABI version as shipped in Sodium SR1 Simultaneous Release. QName-bearing messages are using
54      * {@link NormalizedNodeStreamVersion#SODIUM_SR1}, which improves encoding.
55      */
56     @Deprecated(since = "7.0.0", forRemoval = true)
57     SODIUM_SR1(7) {
58         @Override
59         public NormalizedNodeStreamVersion getStreamVersion() {
60             return NormalizedNodeStreamVersion.SODIUM_SR1;
61         }
62     },
63
64     /**
65      * Revised payload version. Payloads remain the same as {@link #SODIUM_SR1}, but messages bearing QNames in any
66      * shape are using {@link NormalizedNodeStreamVersion#MAGNESIUM}, which improves encoding.
67      */
68     @Deprecated(since = "7.0.0", forRemoval = true)
69     MAGNESIUM(8) {
70         @Override
71         public NormalizedNodeStreamVersion getStreamVersion() {
72             return NormalizedNodeStreamVersion.MAGNESIUM;
73         }
74     },
75
76     /**
77      * ABI version shipped enabled {@code 2022.09 Chlorine SR2}. This version revises the serialization format of
78      * payloads proxies to reduce their size. Otherwise this format is equivalent to {@link #MAGNESIUM}.
79      */
80     CHLORINE_SR2(9) {
81         @Override
82         public NormalizedNodeStreamVersion getStreamVersion() {
83             return NormalizedNodeStreamVersion.MAGNESIUM;
84         }
85     },
86
87     /**
88      * Version which is newer than any other version. This version exists purely for testing purposes.
89      */
90     @VisibleForTesting
91     TEST_FUTURE_VERSION(65535) {
92         @Override
93         public NormalizedNodeStreamVersion getStreamVersion() {
94             throw new UnsupportedOperationException();
95         }
96     };
97
98     private final short value;
99
100     PayloadVersion(final int intVersion) {
101         checkArgument(intVersion >= 0 && intVersion <= 65535);
102         value = (short) intVersion;
103     }
104
105     /**
106      * Return the unsigned short integer identifying this version.
107      *
108      * @return Unsigned short integer identifying this version
109      */
110     public short shortValue() {
111         return value;
112     }
113
114     /**
115      * Return the NormalizedNode stream version corresponding to this particular ABI.
116      *
117      * @return Stream Version to use for this ABI version
118      */
119     public abstract @NonNull NormalizedNodeStreamVersion getStreamVersion();
120
121     /**
122      * Return the codebase-native persistence version. This version is the default version allocated to messages
123      * at runtime. Conversion to previous versions may incur additional overhead (such as object allocation).
124      *
125      * @return Current {@link PayloadVersion}
126      */
127     public static @NonNull PayloadVersion current() {
128         return CHLORINE_SR2;
129     }
130
131     /**
132      * Return the {@link PayloadVersion} corresponding to an unsigned short integer. This method is provided for callers
133      * which provide their own recovery strategy in case of version incompatibility.
134      *
135      * @param version Short integer as returned from {@link #shortValue()}
136      * @return {@link PayloadVersion}
137      * @throws FutureVersionException if the specified integer identifies a future version
138      * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
139      */
140     public static @NonNull PayloadVersion valueOf(final short version)
141             throws FutureVersionException, PastVersionException {
142         return switch (Short.toUnsignedInt(version)) {
143             case 0, 1, 2, 3, 4, 5, 6 -> throw new PastVersionException(version, SODIUM_SR1);
144             case 7 -> SODIUM_SR1;
145             case 8 -> MAGNESIUM;
146             case 9 -> CHLORINE_SR2;
147             default -> throw new FutureVersionException(version, CHLORINE_SR2);
148         };
149     }
150
151     @Override
152     public void writeTo(final DataOutput out) throws IOException {
153         out.writeShort(value);
154     }
155
156     /**
157      * Read an {@link PayloadVersion} from a {@link DataInput}. This method is provided for callers which do not have
158      * a recovery strategy for dealing with unsupported versions.
159      *
160      * @param in Input from which to read
161      * @return An {@link PayloadVersion}
162      * @throws IOException If read fails or an unsupported version is encountered
163      */
164     public static @NonNull PayloadVersion readFrom(final @NonNull DataInput in) throws IOException {
165         final short s = in.readShort();
166         try {
167             return valueOf(s);
168         } catch (FutureVersionException | PastVersionException e) {
169             throw new IOException(e);
170         }
171     }
172 }