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