BUG-5280: add client connect messages
[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 asceding 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     public static @Nonnull ABIVersion current() {
73         return BORON;
74     }
75
76     /**
77      * Return the {@link ABIVersion} corresponding to an unsigned short integer. This method is provided for callers
78      * which provide their own recovery strategy in case of version incompatibility.
79      *
80      * @param s Short integer as returned from {@link #shortValue()}
81      * @return {@link ABIVersion}
82      * @throws FutureVersionException if the specified integer identifies a future version
83      * @throws PastVersionException if the specified integer identifies a past version which is no longer supported
84      */
85     public static @Nonnull ABIVersion valueOf(final short s) throws FutureVersionException, PastVersionException {
86         switch (Short.toUnsignedInt(s)) {
87             case 0:
88             case 1:
89             case 2:
90             case 3:
91             case 4:
92                 throw new PastVersionException(s, BORON);
93             case 5:
94                 return BORON;
95             default:
96                 throw new FutureVersionException(s, BORON);
97         }
98     }
99
100     @Override
101     public void writeTo(final DataOutput out) throws IOException {
102         out.writeShort(value);
103     }
104
105     /**
106      * Read an {@link ABIVersion} from a {@link DataInput}. This method is provided for callers which do not have
107      * a recovery strategy for dealing with unsupported versions.
108      *
109      * @param in Input from which to read
110      * @return An {@link ABIVersion}
111      * @throws IOException If read fails or an unsupported version is encountered
112      */
113     public static @Nonnull ABIVersion readFrom(final @Nonnull DataInput in) throws IOException {
114         final short s = in.readShort();
115         try {
116             return valueOf(s);
117         } catch (FutureVersionException | PastVersionException e) {
118             throw new IOException("Unsupported version", e);
119         }
120     }
121
122     public static ABIVersion inexactReadFrom(final @Nonnull DataInput in) throws IOException {
123         final short onWire = in.readShort();
124         try {
125             return ABIVersion.valueOf(onWire);
126         } catch (FutureVersionException e) {
127             LOG.debug("Received future version", e);
128             return ABIVersion.TEST_FUTURE_VERSION;
129         } catch (PastVersionException e) {
130             LOG.debug("Received past version", e);
131             return ABIVersion.TEST_PAST_VERSION;
132         }
133     }
134 }