Define DataStoreVersions.MAGNESIUM_VERSION
[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.controller.cluster.datastore.node.utils.stream.NormalizedNodeStreamVersion;
17
18 /**
19  * Abstract base class for a versioned Externalizable message.
20  *
21  * @author Thomas Pantelis
22  */
23 public abstract class VersionedExternalizableMessage implements Externalizable, SerializableMessage {
24     private static final long serialVersionUID = 1L;
25
26     private short version = DataStoreVersions.CURRENT_VERSION;
27
28     public VersionedExternalizableMessage() {
29     }
30
31     public VersionedExternalizableMessage(final short version) {
32         this.version = version <= DataStoreVersions.CURRENT_VERSION ? version : DataStoreVersions.CURRENT_VERSION;
33     }
34
35     public short getVersion() {
36         return version;
37     }
38
39     protected final @NonNull NormalizedNodeStreamVersion getStreamVersion() {
40         if (version >= DataStoreVersions.MAGNESIUM_VERSION) {
41             return NormalizedNodeStreamVersion.MAGNESIUM;
42         } else if (version == DataStoreVersions.SODIUM_SR1_VERSION) {
43             return NormalizedNodeStreamVersion.SODIUM_SR1;
44         } else if (version == DataStoreVersions.NEON_SR2_VERSION) {
45             return NormalizedNodeStreamVersion.NEON_SR2;
46         } else {
47             return NormalizedNodeStreamVersion.LITHIUM;
48         }
49     }
50
51     @Override
52     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
53         version = in.readShort();
54     }
55
56     @Override
57     public void writeExternal(final ObjectOutput out) throws IOException {
58         out.writeShort(version);
59     }
60
61     @Override
62     public final Object toSerializable() {
63         final short ver = getVersion();
64         if (ver < DataStoreVersions.BORON_VERSION) {
65             throw new UnsupportedOperationException("Version " + ver
66                 + " is older than the oldest version supported version " + DataStoreVersions.BORON_VERSION);
67         }
68
69         return this;
70     }
71
72     @Override
73     public String toString() {
74         return getClass().getSimpleName() + " [version=" + getVersion() + "]";
75     }
76 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.