6c03e5f1713f9fb8716514125944540f6851459c
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / ABIVersion.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.access;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import java.io.DataInput;
14 import java.io.DataOutput;
15 import java.io.IOException;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.concepts.WritableObject;
18 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Enumeration of all ABI versions supported by this implementation of the client access API.
24  */
25 public enum ABIVersion implements WritableObject {
26     // NOTE: enumeration values need to be sorted in ascending order of their version to keep Comparable working
27
28     /**
29      * Version which is older than any other version. This version exists purely for testing purposes.
30      */
31     @VisibleForTesting
32     TEST_PAST_VERSION(0) {
33         @Override
34         public NormalizedNodeStreamVersion getStreamVersion() {
35             throw new UnsupportedOperationException();
36         }
37     },
38
39     /**
40      * Initial ABI version, as shipped with Boron Simultaneous release.
41      *
42      * @deprecated This version scheduled for removal in the next major release.
43      */
44     // We seed the initial version to be the same as DataStoreVersions.BORON-VERSION for compatibility reasons.
45     @Deprecated(since = "6.0.4", forRemoval = true)
46     BORON(5) {
47         @Override
48         public NormalizedNodeStreamVersion getStreamVersion() {
49             return NormalizedNodeStreamVersion.LITHIUM;
50         }
51     },
52     /**
53      * Revised ABI version. The messages remain the same as {@link #BORON}, but messages bearing QNames in any shape
54      * are using {@link NormalizedNodeStreamVersion#NEON_SR2}, which improves encoding.
55      *
56      * @deprecated This version scheduled for removal in the next major release.
57      */
58     @Deprecated(since = "6.0.4", forRemoval = true)
59     NEON_SR2(6) {
60         @Override
61         public NormalizedNodeStreamVersion getStreamVersion() {
62             return NormalizedNodeStreamVersion.NEON_SR2;
63         }
64     },
65     /**
66      * Revised ABI version. The messages remain the same as {@link #BORON}, but messages bearing QNames in any shape
67      * are using {@link NormalizedNodeStreamVersion#SODIUM_SR1}, which improves encoding.
68      *
69      * @deprecated This version scheduled for removal in the next major release.
70      */
71     @Deprecated(since = "6.0.4", forRemoval = true)
72     SODIUM_SR1(7) {
73         @Override
74         public NormalizedNodeStreamVersion getStreamVersion() {
75             return NormalizedNodeStreamVersion.SODIUM_SR1;
76         }
77     },
78     /**
79      * Revised ABI version. The messages remain the same as {@link #BORON}, but messages bearing QNames in any shape
80      * are using {@link NormalizedNodeStreamVersion#MAGNESIUM}, which improves encoding.
81      */
82     MAGNESIUM(8) {
83         @Override
84         public NormalizedNodeStreamVersion getStreamVersion() {
85             return NormalizedNodeStreamVersion.MAGNESIUM;
86         }
87     },
88     /**
89      * Revised ABI version. The messages remain the same as {@link #MAGNESIUM}, the serialization proxies in use are
90      * flat objects without any superclasses.
91      */
92     CHLORINE_SR2(9) {
93         @Override
94         public NormalizedNodeStreamVersion getStreamVersion() {
95             return NormalizedNodeStreamVersion.MAGNESIUM;
96         }
97     },
98
99     /**
100      * Version which is newer than any other version. This version exists purely for testing purposes.
101      */
102     @VisibleForTesting
103     TEST_FUTURE_VERSION(65535) {
104         @Override
105         public NormalizedNodeStreamVersion getStreamVersion() {
106             throw new UnsupportedOperationException();
107         }
108     };
109
110     private static final Logger LOG = LoggerFactory.getLogger(ABIVersion.class);
111
112     private final short value;
113
114     ABIVersion(final int intVersion) {
115         checkArgument(intVersion >= 0 && intVersion <= 65535);
116         value = (short) intVersion;
117     }
118
119     /**
120      * Return the unsigned short integer identifying this version.
121      *
122      * @return Unsigned short integer identifying this version
123      */
124     public short shortValue() {
125         return value;
126     }
127
128     /**
129      * Return the codebase-native ABI version. This version is the default version allocated to messages at runtime.
130      * Conversion to previous versions may incur additional overhead (such as object allocation).
131      *
132      * @return Current {@link ABIVersion}
133      */
134     public static @NonNull ABIVersion current() {
135         return CHLORINE_SR2;
136     }
137
138     /**
139      * Return the {@link ABIVersion} corresponding to an unsigned short integer. This method is provided for callers
140      * which provide their own recovery strategy in case of version incompatibility.
141      *
142      * @param value Short integer as returned from {@link #shortValue()}
143      * @return {@link ABIVersion}
144      * @throws FutureVersionException if the specified integer identifies a future version
145      * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
146      */
147     public static @NonNull ABIVersion valueOf(final short value) throws FutureVersionException, PastVersionException {
148         return switch (Short.toUnsignedInt(value)) {
149             case 0, 1, 2, 3, 4 -> throw new PastVersionException(value, BORON);
150             case 5 -> BORON;
151             case 6 -> NEON_SR2;
152             case 7 -> SODIUM_SR1;
153             case 8 -> MAGNESIUM;
154             case 9 -> CHLORINE_SR2;
155             default -> throw new FutureVersionException(value, CHLORINE_SR2);
156         };
157     }
158
159     /**
160      * Return {@code true} if this version is earier than some {@code other} version.
161      *
162      * @param other Other {@link ABIVersion}
163      * @return {@code true} if {@code other is later}
164      * @throws NullPointerException if {@code other} is null
165      */
166     public boolean lt(final @NonNull ABIVersion other) {
167         return compareTo(other) < 0;
168     }
169
170     @Override
171     public void writeTo(final DataOutput out) throws IOException {
172         out.writeShort(value);
173     }
174
175     /**
176      * Return the NormalizedNode stream version corresponding to this particular ABI.
177      *
178      * @return Stream Version to use for this ABI version
179      */
180     public abstract @NonNull NormalizedNodeStreamVersion getStreamVersion();
181
182     /**
183      * Read an {@link ABIVersion} from a {@link DataInput}. This method is provided for callers which do not have
184      * a recovery strategy for dealing with unsupported versions.
185      *
186      * @param in Input from which to read
187      * @return An {@link ABIVersion}
188      * @throws IOException If read fails or an unsupported version is encountered
189      */
190     public static @NonNull ABIVersion readFrom(final @NonNull DataInput in) throws IOException {
191         final short s = in.readShort();
192         try {
193             return valueOf(s);
194         } catch (FutureVersionException | PastVersionException e) {
195             throw new IOException("Unsupported version", e);
196         }
197     }
198
199     public static @NonNull ABIVersion inexactReadFrom(final @NonNull DataInput in) throws IOException {
200         final short onWire = in.readShort();
201         try {
202             return ABIVersion.valueOf(onWire);
203         } catch (FutureVersionException e) {
204             LOG.debug("Received future version", e);
205             return ABIVersion.TEST_FUTURE_VERSION;
206         } catch (PastVersionException e) {
207             LOG.debug("Received past version", e);
208             return ABIVersion.TEST_PAST_VERSION;
209         }
210     }
211 }