8fa0b627027166abbf207be2a3f63f2da481f840
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / TransactionIdentifier.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.controller.cluster.access.concepts;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.io.DataInput;
14 import java.io.DataOutput;
15 import java.io.Externalizable;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.concepts.WritableIdentifier;
21 import org.opendaylight.yangtools.concepts.WritableObjects;
22
23 /**
24  * Globally-unique identifier of a transaction.
25  *
26  * @author Robert Varga
27  */
28 @Beta
29 public final class TransactionIdentifier implements WritableIdentifier {
30     private static final class Proxy implements Externalizable {
31         private static final long serialVersionUID = 1L;
32         private LocalHistoryIdentifier historyId;
33         private long transactionId;
34
35         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
36         // be able to create instances via reflection.
37         @SuppressWarnings("checkstyle:RedundantModifier")
38         public Proxy() {
39             // For Externalizable
40         }
41
42         Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
43             this.historyId = requireNonNull(historyId);
44             this.transactionId = transactionId;
45         }
46
47         @Override
48         public void writeExternal(final ObjectOutput out) throws IOException {
49             historyId.writeTo(out);
50             WritableObjects.writeLong(out, transactionId);
51         }
52
53         @Override
54         public void readExternal(final ObjectInput in) throws IOException {
55             historyId = LocalHistoryIdentifier.readFrom(in);
56             transactionId = WritableObjects.readLong(in);
57         }
58
59         private Object readResolve() {
60             return new TransactionIdentifier(historyId, transactionId);
61         }
62     }
63
64     private static final long serialVersionUID = 1L;
65     private final LocalHistoryIdentifier historyId;
66     private final long transactionId;
67     private String shortString;
68
69     public TransactionIdentifier(final @NonNull LocalHistoryIdentifier historyId, final long transactionId) {
70         this.historyId = requireNonNull(historyId);
71         this.transactionId = transactionId;
72     }
73
74     public static TransactionIdentifier readFrom(final DataInput in) throws IOException {
75         final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
76         return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
77     }
78
79     @Override
80     public void writeTo(final DataOutput out) throws IOException {
81         historyId.writeTo(out);
82         WritableObjects.writeLong(out, transactionId);
83     }
84
85     public LocalHistoryIdentifier getHistoryId() {
86         return historyId;
87     }
88
89     public long getTransactionId() {
90         return transactionId;
91     }
92
93     @Override
94     public int hashCode() {
95         return historyId.hashCode() * 31 + Long.hashCode(transactionId);
96     }
97
98     @Override
99     public boolean equals(final Object obj) {
100         if (this == obj) {
101             return true;
102         }
103         if (!(obj instanceof TransactionIdentifier)) {
104             return false;
105         }
106
107         final TransactionIdentifier other = (TransactionIdentifier) obj;
108         return transactionId == other.transactionId && historyId.equals(other.historyId);
109     }
110
111     public String toShortString() {
112         if (shortString == null) {
113             String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
114             shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-"
115                     + historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-"
116                     + historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId
117                     + "-" + historyId.getCookie();
118         }
119
120         return shortString;
121     }
122
123     @Override
124     public String toString() {
125         return toShortString();
126     }
127
128     private Object writeReplace() {
129         return new Proxy(historyId, transactionId);
130     }
131 }