Deprecate ask-based protocol messages
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / VersionedExternalizableMessage.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.datastore.messages;
9
10 import java.io.Externalizable;
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
16 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
17
18 /**
19  * Abstract base class for a versioned Externalizable message.
20  *
21  * @author Thomas Pantelis
22  */
23 @Deprecated(since = "9.0.0", forRemoval = true)
24 public abstract class VersionedExternalizableMessage implements Externalizable, SerializableMessage {
25     private static final long serialVersionUID = 1L;
26
27     private short version = DataStoreVersions.CURRENT_VERSION;
28
29     public VersionedExternalizableMessage() {
30         // Required for externalizable
31     }
32
33     public VersionedExternalizableMessage(final short version) {
34         this.version = version <= DataStoreVersions.CURRENT_VERSION ? version : DataStoreVersions.CURRENT_VERSION;
35     }
36
37     public final short getVersion() {
38         return version;
39     }
40
41     protected final @NonNull NormalizedNodeStreamVersion getStreamVersion() {
42         if (version >= DataStoreVersions.POTASSIUM_VERSION) {
43             return NormalizedNodeStreamVersion.POTASSIUM;
44         } else if (version >= DataStoreVersions.PHOSPHORUS_VERSION) {
45             return NormalizedNodeStreamVersion.MAGNESIUM;
46         } else if (version == DataStoreVersions.SODIUM_SR1_VERSION) {
47             return NormalizedNodeStreamVersion.SODIUM_SR1;
48         } else {
49             throw new IllegalStateException("Unsupported version " + version);
50         }
51     }
52
53     @Override
54     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
55         version = in.readShort();
56     }
57
58     @Override
59     public void writeExternal(final ObjectOutput out) throws IOException {
60         out.writeShort(version);
61     }
62
63     @Override
64     public final Object toSerializable() {
65         final short ver = getVersion();
66         if (ver < DataStoreVersions.SODIUM_SR1_VERSION) {
67             throw new UnsupportedOperationException("Version " + ver
68                 + " is older than the oldest version supported version " + DataStoreVersions.SODIUM_SR1_VERSION);
69         }
70
71         return this;
72     }
73
74     @Override
75     public String toString() {
76         return getClass().getSimpleName() + " [version=" + getVersion() + "]";
77     }
78 }