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