fa4fca508916725db298b4bb7bc20e1863005b3e
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / CanCommitTransactionReply.java
1 /*
2  * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.datastore.messages;
10
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;
16
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);
22
23     private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply YES_SERIALIZED =
24             ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(true).build();
25
26     private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply NO_SERIALIZED =
27             ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(false).build();
28
29     private boolean canCommit;
30
31     public CanCommitTransactionReply() {
32     }
33
34     private CanCommitTransactionReply(final boolean canCommit, final short version) {
35         super(version);
36         this.canCommit = canCommit;
37     }
38
39     public boolean getCanCommit() {
40         return canCommit;
41     }
42
43     @Override
44     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
45         super.readExternal(in);
46         canCommit = in.readBoolean();
47     }
48
49     @Override
50     public void writeExternal(ObjectOutput out) throws IOException {
51         super.writeExternal(out);
52         out.writeBoolean(canCommit);
53     }
54
55     @Override
56     public Object toSerializable() {
57         if(getVersion() >= DataStoreVersions.BORON_VERSION) {
58             return this;
59         } else {
60             return canCommit ? YES_SERIALIZED : NO_SERIALIZED;
61         }
62     }
63
64     @Override
65     public String toString() {
66         return "CanCommitTransactionReply [canCommit=" + canCommit + ", version=" + getVersion() + "]";
67     }
68
69     public static CanCommitTransactionReply yes(short version) {
70         return version == DataStoreVersions.CURRENT_VERSION ? YES : new CanCommitTransactionReply(true, version);
71     }
72
73     public static CanCommitTransactionReply no(short version) {
74         return version == DataStoreVersions.CURRENT_VERSION ? NO : new CanCommitTransactionReply(false, version);
75     }
76
77     public static CanCommitTransactionReply fromSerializable(final Object serializable) {
78         if(serializable instanceof CanCommitTransactionReply) {
79             return (CanCommitTransactionReply)serializable;
80         } else {
81             ThreePhaseCommitCohortMessages.CanCommitTransactionReply serialized =
82                     (ThreePhaseCommitCohortMessages.CanCommitTransactionReply) serializable;
83             return serialized.getCanCommit() ? YES : NO;
84         }
85     }
86
87     public static boolean isSerializedType(Object message) {
88         return message instanceof CanCommitTransactionReply ||
89                 message instanceof ThreePhaseCommitCohortMessages.CanCommitTransactionReply;
90     }
91 }