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