921c9fca96cfc505f17a6b3be25564f3cad69163
[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.controller.cluster.datastore.node.utils.stream.NormalizedNodeStreamVersion;
19 import org.opendaylight.yangtools.concepts.WritableObject;
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      * Initial ABI version, as shipped with Boron Simultaneous release.
54      */
55     // We seed the initial version to be the same as DataStoreVersions.BORON_VERSION for compatibility reasons.
56     BORON(5) {
57         @Override
58         public NormalizedNodeStreamVersion getStreamVersion() {
59             return NormalizedNodeStreamVersion.LITHIUM;
60         }
61     },
62
63     /**
64      * Revised payload version. Payloads remain the same as {@link #BORON}, but messages bearing QNames in any shape
65      * are using {@link NormalizedNodeStreamVersion#NEON_SR2}, which improves encoding.
66      */
67     NEON_SR2(6) {
68         @Override
69         public NormalizedNodeStreamVersion getStreamVersion() {
70             return NormalizedNodeStreamVersion.NEON_SR2;
71         }
72     },
73
74     /**
75      * Revised payload version. Payloads remain the same as {@link #NEON_SR2}, but messages bearing QNames in any shape
76      * are using {@link NormalizedNodeStreamVersion#SODIUM_SR1}, which improves encoding.
77      */
78     SODIUM_SR1(7) {
79         @Override
80         public NormalizedNodeStreamVersion getStreamVersion() {
81             return NormalizedNodeStreamVersion.SODIUM_SR1;
82         }
83     },
84
85     /**
86      * Revised payload version. Payloads remain the same as {@link #SODIUM_SR1}, but messages bearing QNames in any
87      * shape are using {@link NormalizedNodeStreamVersion#MAGNESIUM}, which improves encoding.
88      */
89     MAGNESIUM(8) {
90         @Override
91         public NormalizedNodeStreamVersion getStreamVersion() {
92             return NormalizedNodeStreamVersion.MAGNESIUM;
93         }
94     },
95
96     /**
97      * Version which is newer than any other version. This version exists purely for testing purposes.
98      */
99     @VisibleForTesting
100     TEST_FUTURE_VERSION(65535) {
101         @Override
102         public NormalizedNodeStreamVersion getStreamVersion() {
103             throw new UnsupportedOperationException();
104         }
105     };
106
107     private final short value;
108
109     PayloadVersion(final int intVersion) {
110         checkArgument(intVersion >= 0 && intVersion <= 65535);
111         value = (short) intVersion;
112     }
113
114     /**
115      * Return the unsigned short integer identifying this version.
116      *
117      * @return Unsigned short integer identifying this version
118      */
119     public short shortValue() {
120         return value;
121     }
122
123     /**
124      * Return the NormalizedNode stream version corresponding to this particular ABI.
125      *
126      * @return Stream Version to use for this ABI version
127      */
128     public abstract @NonNull NormalizedNodeStreamVersion getStreamVersion();
129
130     /**
131      * Return the codebase-native persistence version. This version is the default version allocated to messages
132      * at runtime. Conversion to previous versions may incur additional overhead (such as object allocation).
133      *
134      * @return Current {@link PayloadVersion}
135      */
136     public static @NonNull PayloadVersion current() {
137         return NEON_SR2;
138     }
139
140     /**
141      * Return the {@link PayloadVersion} corresponding to an unsigned short integer. This method is provided for callers
142      * which provide their own recovery strategy in case of version incompatibility.
143      *
144      * @param version Short integer as returned from {@link #shortValue()}
145      * @return {@link PayloadVersion}
146      * @throws FutureVersionException if the specified integer identifies a future version
147      * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
148      */
149     public static @NonNull PayloadVersion valueOf(final short version)
150             throws FutureVersionException, PastVersionException {
151         switch (Short.toUnsignedInt(version)) {
152             case 0:
153             case 1:
154             case 2:
155             case 3:
156             case 4:
157                 throw new PastVersionException(version, BORON);
158             case 5:
159                 return BORON;
160             case 6:
161                 return NEON_SR2;
162             case 7:
163                 return SODIUM_SR1;
164             case 8:
165                 return MAGNESIUM;
166             default:
167                 throw new FutureVersionException(version, MAGNESIUM);
168         }
169     }
170
171     @Override
172     public void writeTo(final DataOutput out) throws IOException {
173         out.writeShort(value);
174     }
175
176     /**
177      * Read an {@link PayloadVersion} from a {@link DataInput}. This method is provided for callers which do not have
178      * a recovery strategy for dealing with unsupported versions.
179      *
180      * @param in Input from which to read
181      * @return An {@link PayloadVersion}
182      * @throws IOException If read fails or an unsupported version is encountered
183      */
184     public static @NonNull PayloadVersion readFrom(final @NonNull DataInput in) throws IOException {
185         final short s = in.readShort();
186         try {
187             return valueOf(s);
188         } catch (FutureVersionException | PastVersionException e) {
189             throw new IOException("Unsupported version", e);
190         }
191     }
192 }