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