2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.messages;
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;
19 * Abstract base class for a versioned Externalizable message.
21 * @author Thomas Pantelis
23 public abstract class VersionedExternalizableMessage implements Externalizable, SerializableMessage {
24 private static final long serialVersionUID = 1L;
26 private short version = DataStoreVersions.CURRENT_VERSION;
28 public VersionedExternalizableMessage() {
29 // Required for externalizable
32 public VersionedExternalizableMessage(final short version) {
33 this.version = version <= DataStoreVersions.CURRENT_VERSION ? version : DataStoreVersions.CURRENT_VERSION;
36 public final short getVersion() {
40 protected final @NonNull NormalizedNodeStreamVersion getStreamVersion() {
41 if (version >= DataStoreVersions.POTASSIUM_VERSION) {
42 return NormalizedNodeStreamVersion.POTASSIUM;
43 } else if (version >= DataStoreVersions.PHOSPHORUS_VERSION) {
44 return NormalizedNodeStreamVersion.MAGNESIUM;
45 } else if (version == DataStoreVersions.SODIUM_SR1_VERSION) {
46 return NormalizedNodeStreamVersion.SODIUM_SR1;
48 throw new IllegalStateException("Unsupported version " + version);
53 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
54 version = in.readShort();
58 public void writeExternal(final ObjectOutput out) throws IOException {
59 out.writeShort(version);
63 public final Object toSerializable() {
64 final short ver = getVersion();
65 if (ver < DataStoreVersions.SODIUM_SR1_VERSION) {
66 throw new UnsupportedOperationException("Version " + ver
67 + " is older than the oldest version supported version " + DataStoreVersions.SODIUM_SR1_VERSION);
74 public String toString() {
75 return getClass().getSimpleName() + " [version=" + getVersion() + "]";