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