2 * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.datastore.messages;
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
15 import org.opendaylight.controller.protobuff.messages.cohort3pc.ThreePhaseCommitCohortMessages;
17 public class CanCommitTransactionReply extends VersionedExternalizableMessage {
18 private static final CanCommitTransactionReply YES =
19 new CanCommitTransactionReply(true, DataStoreVersions.CURRENT_VERSION);
20 private static final CanCommitTransactionReply NO =
21 new CanCommitTransactionReply(false, DataStoreVersions.CURRENT_VERSION);
23 private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply YES_SERIALIZED =
24 ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(true).build();
26 private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply NO_SERIALIZED =
27 ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(false).build();
29 private boolean canCommit;
31 public CanCommitTransactionReply() {
34 private CanCommitTransactionReply(final boolean canCommit, final short version) {
36 this.canCommit = canCommit;
39 public boolean getCanCommit() {
44 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
45 super.readExternal(in);
46 canCommit = in.readBoolean();
50 public void writeExternal(ObjectOutput out) throws IOException {
51 super.writeExternal(out);
52 out.writeBoolean(canCommit);
56 public Object toSerializable() {
57 if(getVersion() >= DataStoreVersions.BORON_VERSION) {
60 return canCommit ? YES_SERIALIZED : NO_SERIALIZED;
65 public String toString() {
66 return "CanCommitTransactionReply [canCommit=" + canCommit + ", version=" + getVersion() + "]";
69 public static CanCommitTransactionReply yes(short version) {
70 return version == DataStoreVersions.CURRENT_VERSION ? YES : new CanCommitTransactionReply(true, version);
73 public static CanCommitTransactionReply no(short version) {
74 return version == DataStoreVersions.CURRENT_VERSION ? NO : new CanCommitTransactionReply(false, version);
77 public static CanCommitTransactionReply fromSerializable(final Object serializable) {
78 if(serializable instanceof CanCommitTransactionReply) {
79 return (CanCommitTransactionReply)serializable;
81 ThreePhaseCommitCohortMessages.CanCommitTransactionReply serialized =
82 (ThreePhaseCommitCohortMessages.CanCommitTransactionReply) serializable;
83 return serialized.getCanCommit() ? YES : NO;
87 public static boolean isSerializedType(Object message) {
88 return message instanceof CanCommitTransactionReply ||
89 message instanceof ThreePhaseCommitCohortMessages.CanCommitTransactionReply;