Follow-up to protobuff deprecation
[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     @Deprecated
24     private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply YES_SERIALIZED =
25             ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(true).build();
26
27     @Deprecated
28     private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply NO_SERIALIZED =
29             ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(false).build();
30
31     private boolean canCommit;
32
33     public CanCommitTransactionReply() {
34     }
35
36     private CanCommitTransactionReply(final boolean canCommit, final short version) {
37         super(version);
38         this.canCommit = canCommit;
39     }
40
41     public boolean getCanCommit() {
42         return canCommit;
43     }
44
45     @Override
46     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
47         super.readExternal(in);
48         canCommit = in.readBoolean();
49     }
50
51     @Override
52     public void writeExternal(ObjectOutput out) throws IOException {
53         super.writeExternal(out);
54         out.writeBoolean(canCommit);
55     }
56
57     @Deprecated
58     @Override
59     protected Object newLegacySerializedInstance() {
60         return canCommit ? YES_SERIALIZED : NO_SERIALIZED;
61     }
62
63     @Override
64     public String toString() {
65         return "CanCommitTransactionReply [canCommit=" + canCommit + ", version=" + getVersion() + "]";
66     }
67
68     public static CanCommitTransactionReply yes(short version) {
69         return version == DataStoreVersions.CURRENT_VERSION ? YES : new CanCommitTransactionReply(true, version);
70     }
71
72     public static CanCommitTransactionReply no(short version) {
73         return version == DataStoreVersions.CURRENT_VERSION ? NO : new CanCommitTransactionReply(false, version);
74     }
75
76     public static CanCommitTransactionReply fromSerializable(final Object serializable) {
77         if(serializable instanceof CanCommitTransactionReply) {
78             return (CanCommitTransactionReply)serializable;
79         } else {
80             ThreePhaseCommitCohortMessages.CanCommitTransactionReply serialized =
81                     (ThreePhaseCommitCohortMessages.CanCommitTransactionReply) serializable;
82             return serialized.getCanCommit() ? YES : NO;
83         }
84     }
85
86     public static boolean isSerializedType(Object message) {
87         return message instanceof CanCommitTransactionReply ||
88                 message instanceof ThreePhaseCommitCohortMessages.CanCommitTransactionReply;
89     }
90 }