Refactor VersionedExternalizableMessage messages
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / CreateTransactionReply.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.protobuff.messages.transaction.ShardTransactionMessages;
15
16 public class CreateTransactionReply extends VersionedExternalizableMessage {
17     private static final long serialVersionUID = 1L;
18
19     private String transactionPath;
20     private String transactionId;
21
22     public CreateTransactionReply() {
23     }
24
25     public CreateTransactionReply(final String transactionPath, final String transactionId, final short version) {
26         super(version);
27         this.transactionPath = transactionPath;
28         this.transactionId = transactionId;
29     }
30
31     public String getTransactionPath() {
32         return transactionPath;
33     }
34
35     public String getTransactionId() {
36         return transactionId;
37     }
38
39     @Override
40     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
41         super.readExternal(in);
42         transactionId = in.readUTF();
43         transactionPath = in.readUTF();
44     }
45
46     @Override
47     public void writeExternal(ObjectOutput out) throws IOException {
48         super.writeExternal(out);
49         out.writeUTF(transactionId);
50         out.writeUTF(transactionPath);
51     }
52
53     @Override
54     protected Object newLegacySerializedInstance() {
55         return ShardTransactionMessages.CreateTransactionReply.newBuilder().setTransactionActorPath(transactionPath)
56                 .setTransactionId(transactionId).setMessageVersion(getVersion()).build();
57     }
58
59     @Override
60     public String toString() {
61         StringBuilder builder = new StringBuilder();
62         builder.append("CreateTransactionReply [transactionPath=").append(transactionPath).append(", transactionId=")
63                 .append(transactionId).append(", version=").append(getVersion()).append("]");
64         return builder.toString();
65     }
66
67     public static CreateTransactionReply fromSerializable(Object serializable) {
68         if(serializable instanceof CreateTransactionReply) {
69             return (CreateTransactionReply)serializable;
70         } else {
71             ShardTransactionMessages.CreateTransactionReply o =
72                     (ShardTransactionMessages.CreateTransactionReply) serializable;
73             return new CreateTransactionReply(o.getTransactionActorPath(), o.getTransactionId(),
74                     (short)o.getMessageVersion());
75         }
76     }
77
78     public static boolean isSerializedType(Object message) {
79         return message instanceof CreateTransactionReply ||
80                 message instanceof ShardTransactionMessages.CreateTransactionReply;
81     }
82 }