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