0f52dacc5b8bbbf3dfcc0d8eafb0aeb5ec7dade6
[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  * <p/>
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     @Nonnull
77     public static PayloadVersion current() {
78         return BORON;
79     }
80
81     /**
82      * Return the {@link PayloadVersion} corresponding to an unsigned short integer. This method is provided for callers
83      * which provide their own recovery strategy in case of version incompatibility.
84      *
85      * @param version Short integer as returned from {@link #shortValue()}
86      * @return {@link PayloadVersion}
87      * @throws FutureVersionException if the specified integer identifies a future version
88      * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
89      */
90     @Nonnull
91     public static PayloadVersion valueOf(final short version) throws FutureVersionException, PastVersionException {
92         switch (Short.toUnsignedInt(version)) {
93             case 0:
94             case 1:
95             case 2:
96             case 3:
97             case 4:
98                 throw new PastVersionException(version, BORON);
99             case 5:
100                 return BORON;
101             default:
102                 throw new FutureVersionException(version, BORON);
103         }
104     }
105
106     @Override
107     public void writeTo(final DataOutput out) throws IOException {
108         out.writeShort(value);
109     }
110
111     /**
112      * Read an {@link PayloadVersion} from a {@link DataInput}. This method is provided for callers which do not have
113      * a recovery strategy for dealing with unsupported versions.
114      *
115      * @param in Input from which to read
116      * @return An {@link PayloadVersion}
117      * @throws IOException If read fails or an unsupported version is encountered
118      */
119     @Nonnull
120     public static PayloadVersion readFrom(@Nonnull final DataInput in) throws IOException {
121         final short s = in.readShort();
122         try {
123             return valueOf(s);
124         } catch (FutureVersionException | PastVersionException e) {
125             throw new IOException("Unsupported version", e);
126         }
127     }
128
129 }