3eddf3eb564f29c7b20b7ae06cec560793dc6144
[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.yangtools.concepts.WritableObject;
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  * @author Robert Varga
26  */
27 @Beta
28 public enum ABIVersion implements WritableObject {
29     // NOTE: enumeration values need to be sorted in ascending order of their version to keep Comparable working
30
31     /**
32      * Version which is older than any other version. This version exists purely for testing purposes.
33      */
34     @VisibleForTesting
35     TEST_PAST_VERSION(0),
36
37     /**
38      * Initial ABI version, as shipped with Boron Simultaneous release.
39      */
40     // We seed the initial version to be the same as DataStoreVersions.BORON-VERSION for compatibility reasons.
41     BORON(5),
42
43     /**
44      * Version which is newer than any other version. This version exists purely for testing purposes.
45      */
46     @VisibleForTesting
47     TEST_FUTURE_VERSION(65535);
48
49     private static final Logger LOG = LoggerFactory.getLogger(ABIVersion.class);
50
51     private final short value;
52
53     ABIVersion(final int intVersion) {
54         checkArgument(intVersion >= 0 && intVersion <= 65535);
55         value = (short) intVersion;
56     }
57
58     /**
59      * Return the unsigned short integer identifying this version.
60      *
61      * @return Unsigned short integer identifying this version
62      */
63     public short shortValue() {
64         return value;
65     }
66
67     /**
68      * Return the codebase-native ABI version. This version is the default version allocated to messages at runtime.
69      * Conversion to previous versions may incur additional overhead (such as object allocation).
70      *
71      * @return Current {@link ABIVersion}
72      */
73     public static @NonNull ABIVersion current() {
74         return BORON;
75     }
76
77     /**
78      * Return the {@link ABIVersion} corresponding to an unsigned short integer. This method is provided for callers
79      * which provide their own recovery strategy in case of version incompatibility.
80      *
81      * @param value Short integer as returned from {@link #shortValue()}
82      * @return {@link ABIVersion}
83      * @throws FutureVersionException if the specified integer identifies a future version
84      * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
85      */
86     public static @NonNull ABIVersion valueOf(final short value) throws FutureVersionException, PastVersionException {
87         switch (Short.toUnsignedInt(value)) {
88             case 0:
89             case 1:
90             case 2:
91             case 3:
92             case 4:
93                 throw new PastVersionException(value, BORON);
94             case 5:
95                 return BORON;
96             default:
97                 throw new FutureVersionException(value, BORON);
98         }
99     }
100
101     @Override
102     public void writeTo(final DataOutput out) throws IOException {
103         out.writeShort(value);
104     }
105
106     /**
107      * Read an {@link ABIVersion} from a {@link DataInput}. This method is provided for callers which do not have
108      * a recovery strategy for dealing with unsupported versions.
109      *
110      * @param in Input from which to read
111      * @return An {@link ABIVersion}
112      * @throws IOException If read fails or an unsupported version is encountered
113      */
114     public static @NonNull ABIVersion readFrom(final @NonNull DataInput in) throws IOException {
115         final short s = in.readShort();
116         try {
117             return valueOf(s);
118         } catch (FutureVersionException | PastVersionException e) {
119             throw new IOException("Unsupported version", e);
120         }
121     }
122
123     public static @NonNull ABIVersion inexactReadFrom(final @NonNull DataInput in) throws IOException {
124         final short onWire = in.readShort();
125         try {
126             return ABIVersion.valueOf(onWire);
127         } catch (FutureVersionException e) {
128             LOG.debug("Received future version", e);
129             return ABIVersion.TEST_FUTURE_VERSION;
130         } catch (PastVersionException e) {
131             LOG.debug("Received past version", e);
132             return ABIVersion.TEST_PAST_VERSION;
133         }
134     }
135 }