716083156b32aca9fe1e9c0eaf52b4eeddf84e13
[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     /**
81      * Version which is newer than any other version. This version exists purely for testing purposes.
82      */
83     @VisibleForTesting
84     TEST_FUTURE_VERSION(65535) {
85         @Override
86         public NormalizedNodeStreamVersion getStreamVersion() {
87             throw new UnsupportedOperationException();
88         }
89     };
90
91     private static final Logger LOG = LoggerFactory.getLogger(ABIVersion.class);
92
93     private final short value;
94
95     ABIVersion(final int intVersion) {
96         checkArgument(intVersion >= 0 && intVersion <= 65535);
97         value = (short) intVersion;
98     }
99
100     /**
101      * Return the unsigned short integer identifying this version.
102      *
103      * @return Unsigned short integer identifying this version
104      */
105     public short shortValue() {
106         return value;
107     }
108
109     /**
110      * Return the codebase-native ABI version. This version is the default version allocated to messages at runtime.
111      * Conversion to previous versions may incur additional overhead (such as object allocation).
112      *
113      * @return Current {@link ABIVersion}
114      */
115     public static @NonNull ABIVersion current() {
116         return MAGNESIUM;
117     }
118
119     /**
120      * Return the {@link ABIVersion} corresponding to an unsigned short integer. This method is provided for callers
121      * which provide their own recovery strategy in case of version incompatibility.
122      *
123      * @param value Short integer as returned from {@link #shortValue()}
124      * @return {@link ABIVersion}
125      * @throws FutureVersionException if the specified integer identifies a future version
126      * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
127      */
128     public static @NonNull ABIVersion valueOf(final short value) throws FutureVersionException, PastVersionException {
129         return switch (Short.toUnsignedInt(value)) {
130             case 0, 1, 2, 3, 4 -> throw new PastVersionException(value, BORON);
131             case 5 -> BORON;
132             case 6 -> NEON_SR2;
133             case 7 -> SODIUM_SR1;
134             case 8 -> MAGNESIUM;
135             default -> throw new FutureVersionException(value, MAGNESIUM);
136         };
137     }
138
139     /**
140      * Return {@code true} if this version is earier than some {@code other} version.
141      *
142      * @param other Other {@link ABIVersion}
143      * @return {@code true} if {@code other is later}
144      * @throws NullPointerException if {@code other} is null
145      */
146     public boolean lt(final @NonNull ABIVersion other) {
147         return compareTo(other) < 0;
148     }
149
150     @Override
151     public void writeTo(final DataOutput out) throws IOException {
152         out.writeShort(value);
153     }
154
155     /**
156      * Return the NormalizedNode stream version corresponding to this particular ABI.
157      *
158      * @return Stream Version to use for this ABI version
159      */
160     public abstract @NonNull NormalizedNodeStreamVersion getStreamVersion();
161
162     /**
163      * Read an {@link ABIVersion} from a {@link DataInput}. This method is provided for callers which do not have
164      * a recovery strategy for dealing with unsupported versions.
165      *
166      * @param in Input from which to read
167      * @return An {@link ABIVersion}
168      * @throws IOException If read fails or an unsupported version is encountered
169      */
170     public static @NonNull ABIVersion readFrom(final @NonNull DataInput in) throws IOException {
171         final short s = in.readShort();
172         try {
173             return valueOf(s);
174         } catch (FutureVersionException | PastVersionException e) {
175             throw new IOException("Unsupported version", e);
176         }
177     }
178
179     public static @NonNull ABIVersion inexactReadFrom(final @NonNull DataInput in) throws IOException {
180         final short onWire = in.readShort();
181         try {
182             return ABIVersion.valueOf(onWire);
183         } catch (FutureVersionException e) {
184             LOG.debug("Received future version", e);
185             return ABIVersion.TEST_FUTURE_VERSION;
186         } catch (PastVersionException e) {
187             LOG.debug("Received past version", e);
188             return ABIVersion.TEST_PAST_VERSION;
189         }
190     }
191 }