Fix CS warnings in cds-access-api and enable enforcement
[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 com.google.common.annotations.Beta;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import java.io.DataInput;
14 import java.io.DataOutput;
15 import java.io.IOException;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.concepts.WritableObject;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Enumeration of all ABI versions supported by this implementation of the client access API.
23  *
24  * @author Robert Varga
25  */
26 @Beta
27 public enum ABIVersion implements WritableObject {
28     // NOTE: enumeration values need to be sorted in ascending order of their version to keep Comparable working
29
30     /**
31      * Version which is older than any other version. This version exists purely for testing purposes.
32      */
33     @VisibleForTesting
34     TEST_PAST_VERSION(0),
35
36     /**
37      * Initial ABI version, as shipped with Boron Simultaneous release.
38      */
39     // We seed the initial version to be the same as DataStoreVersions.BORON-VERSION for compatibility reasons.
40     BORON(5),
41
42     /**
43      * Version which is newer than any other version. This version exists purely for testing purposes.
44      */
45     @VisibleForTesting
46     TEST_FUTURE_VERSION(65535);
47
48     private static final Logger LOG = LoggerFactory.getLogger(ABIVersion.class);
49
50     private final short value;
51
52     ABIVersion(final int intVersion) {
53         Preconditions.checkArgument(intVersion >= 0 && intVersion <= 65535);
54         value = (short) intVersion;
55     }
56
57     /**
58      * Return the unsigned short integer identifying this version.
59      *
60      * @return Unsigned short integer identifying this version
61      */
62     public short shortValue() {
63         return value;
64     }
65
66     /**
67      * Return the codebase-native ABI version. This version is the default version allocated to messages at runtime.
68      * Conversion to previous versions may incur additional overhead (such as object allocation).
69      *
70      * @return Current {@link ABIVersion}
71      */
72     @Nonnull
73     public static 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     @Nonnull
87     public static ABIVersion valueOf(final short value) throws FutureVersionException, PastVersionException {
88         switch (Short.toUnsignedInt(value)) {
89             case 0:
90             case 1:
91             case 2:
92             case 3:
93             case 4:
94                 throw new PastVersionException(value, BORON);
95             case 5:
96                 return BORON;
97             default:
98                 throw new FutureVersionException(value, BORON);
99         }
100     }
101
102     @Override
103     public void writeTo(final DataOutput out) throws IOException {
104         out.writeShort(value);
105     }
106
107     /**
108      * Read an {@link ABIVersion} from a {@link DataInput}. This method is provided for callers which do not have
109      * a recovery strategy for dealing with unsupported versions.
110      *
111      * @param in Input from which to read
112      * @return An {@link ABIVersion}
113      * @throws IOException If read fails or an unsupported version is encountered
114      */
115     @Nonnull
116     public static ABIVersion readFrom(@Nonnull final DataInput in) throws IOException {
117         final short s = in.readShort();
118         try {
119             return valueOf(s);
120         } catch (FutureVersionException | PastVersionException e) {
121             throw new IOException("Unsupported version", e);
122         }
123     }
124
125     public static ABIVersion inexactReadFrom(@Nonnull final DataInput in) throws IOException {
126         final short onWire = in.readShort();
127         try {
128             return ABIVersion.valueOf(onWire);
129         } catch (FutureVersionException e) {
130             LOG.debug("Received future version", e);
131             return ABIVersion.TEST_FUTURE_VERSION;
132         } catch (PastVersionException e) {
133             LOG.debug("Received past version", e);
134             return ABIVersion.TEST_PAST_VERSION;
135         }
136     }
137 }