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