BUG-5930: introduce PayloadVersion
[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 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.yangtools.concepts.WritableObject;
18
19 /**
20  * Enumeration of all ABI versions supported by this implementation of persistence. An ABI version has to be bumped
21  * whenever:
22  * - a new event is defined
23  * - serialization format is changed
24  *
25  * This version effectively defines the protocol version between actors participating on a particular shard. A shard
26  * participant instance should oppose RAFT candidates which produce persistence of an unsupported version. If a follower
27  * encounters an unsupported version it must not become fully-operational, as it does not have an accurate view
28  * of shard state.
29  *
30  * @author Robert Varga
31  */
32 @Beta
33 public enum PayloadVersion implements WritableObject {
34     // NOTE: enumeration values need to be sorted in asceding order of their version to keep Comparable working
35
36     /**
37      * Version which is older than any other version. This version exists purely for testing purposes.
38      */
39     @VisibleForTesting
40     TEST_PAST_VERSION(0),
41
42     /**
43      * Initial ABI version, as shipped with Boron Simultaneous release.
44      */
45     // We seed the initial version to be the same as DataStoreVersions.BORON-VERSION for compatibility reasons.
46     BORON(5),
47
48     /**
49      * Version which is newer than any other version. This version exists purely for testing purposes.
50      */
51     @VisibleForTesting
52     TEST_FUTURE_VERSION(65535);
53
54     private final short value;
55
56     PayloadVersion(final int intVersion) {
57         Preconditions.checkArgument(intVersion >= 0 && intVersion <= 65535);
58         value = (short) intVersion;
59     }
60
61     /**
62      * Return the unsigned short integer identifying this version.
63      *
64      * @return Unsigned short integer identifying this version
65      */
66     public short shortValue() {
67         return value;
68     }
69
70     /**
71      * Return the codebase-native persistence version. This version is the default version allocated to messages
72      * at runtime. Conversion to previous versions may incur additional overhead (such as object allocation).
73      *
74      * @return Current {@link PayloadVersion}
75      */
76     public static @Nonnull PayloadVersion current() {
77         return BORON;
78     }
79
80     /**
81      * Return the {@link PayloadVersion} corresponding to an unsigned short integer. This method is provided for callers
82      * which provide their own recovery strategy in case of version incompatibility.
83      *
84      * @param s Short integer as returned from {@link #shortValue()}
85      * @return {@link PayloadVersion}
86      * @throws FutureVersionException if the specified integer identifies a future version
87      * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
88      */
89     public static @Nonnull PayloadVersion valueOf(final short s) throws FutureVersionException, PastVersionException {
90         switch (Short.toUnsignedInt(s)) {
91             case 0:
92             case 1:
93             case 2:
94             case 3:
95             case 4:
96                 throw new PastVersionException(s, BORON);
97             case 5:
98                 return BORON;
99             default:
100                 throw new FutureVersionException(s, BORON);
101         }
102     }
103
104     @Override
105     public void writeTo(final DataOutput out) throws IOException {
106         out.writeShort(value);
107     }
108
109     /**
110      * Read an {@link PayloadVersion} from a {@link DataInput}. This method is provided for callers which do not have
111      * a recovery strategy for dealing with unsupported versions.
112      *
113      * @param in Input from which to read
114      * @return An {@link PayloadVersion}
115      * @throws IOException If read fails or an unsupported version is encountered
116      */
117     public static @Nonnull PayloadVersion readFrom(final @Nonnull DataInput in) throws IOException {
118         final short s = in.readShort();
119         try {
120             return valueOf(s);
121         } catch (FutureVersionException | PastVersionException e) {
122             throw new IOException("Unsupported version", e);
123         }
124     }
125
126 }