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.controller.cluster.datastore.node.utils.stream.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() {
31 public VersionedExternalizableMessage(final short version) {
32 this.version = version <= DataStoreVersions.CURRENT_VERSION ? version : DataStoreVersions.CURRENT_VERSION;
35 public short getVersion() {
39 protected final @NonNull NormalizedNodeStreamVersion getStreamVersion() {
40 return version < DataStoreVersions.NEON_SR2_VERSION
41 ? NormalizedNodeStreamVersion.LITHIUM : NormalizedNodeStreamVersion.NEON_SR2;
45 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
46 version = in.readShort();
50 public void writeExternal(final ObjectOutput out) throws IOException {
51 out.writeShort(version);
55 public final Object toSerializable() {
56 final short ver = getVersion();
57 if (ver < DataStoreVersions.BORON_VERSION) {
58 throw new UnsupportedOperationException("Version " + ver
59 + " is older than the oldest version supported version " + DataStoreVersions.BORON_VERSION);
66 public String toString() {
67 return getClass().getSimpleName() + " [version=" + getVersion() + "]";