dac331462833ceee613a0ba89c7ff8a0bec1aa0c
[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 com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.io.DataInput;
13 import java.io.DataOutput;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.concepts.WritableIdentifier;
20 import org.opendaylight.yangtools.concepts.WritableObjects;
21
22 /**
23  * Globally-unique identifier of a transaction.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public final class TransactionIdentifier implements WritableIdentifier {
29     private static final class Proxy implements Externalizable {
30         private static final long serialVersionUID = 1L;
31         private LocalHistoryIdentifier historyId;
32         private long transactionId;
33
34         public Proxy() {
35             // For Externalizable
36         }
37
38         Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
39             this.historyId = Preconditions.checkNotNull(historyId);
40             this.transactionId = transactionId;
41         }
42
43         @Override
44         public void writeExternal(final ObjectOutput out) throws IOException {
45             historyId.writeTo(out);
46             WritableObjects.writeLong(out, transactionId);
47         }
48
49         @Override
50         public void readExternal(final ObjectInput in) throws IOException {
51             historyId = LocalHistoryIdentifier.readFrom(in);
52             transactionId = WritableObjects.readLong(in);
53         }
54
55         private Object readResolve() {
56             return new TransactionIdentifier(historyId, transactionId);
57         }
58     }
59
60     private static final long serialVersionUID = 1L;
61     private final LocalHistoryIdentifier historyId;
62     private final long transactionId;
63     private transient String shortString;
64
65     public TransactionIdentifier(final @Nonnull LocalHistoryIdentifier historyId, final long transactionId) {
66         this.historyId = Preconditions.checkNotNull(historyId);
67         this.transactionId = transactionId;
68     }
69
70     public static TransactionIdentifier readFrom(final DataInput in) throws IOException {
71         final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
72         return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
73     }
74
75     @Override
76     public void writeTo(final DataOutput out) throws IOException {
77         historyId.writeTo(out);
78         WritableObjects.writeLong(out, transactionId);
79     }
80
81     public LocalHistoryIdentifier getHistoryId() {
82         return historyId;
83     }
84
85     public long getTransactionId() {
86         return transactionId;
87     }
88
89     @Override
90     public int hashCode() {
91         return historyId.hashCode() * 31 + Long.hashCode(transactionId);
92     }
93
94     @Override
95     public boolean equals(final Object o) {
96         if (this == o) {
97             return true;
98         }
99         if (!(o instanceof TransactionIdentifier)) {
100             return false;
101         }
102
103         final TransactionIdentifier other = (TransactionIdentifier) o;
104         return transactionId == other.transactionId && historyId.equals(other.historyId);
105     }
106
107     public String toShortString() {
108         if(shortString == null) {
109             String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
110             shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-" +
111                     historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-" +
112                     historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId;
113         }
114
115         return shortString;
116     }
117
118     @Override
119     public String toString() {
120         return toShortString();
121     }
122
123     private Object writeReplace() {
124         return new Proxy(historyId, transactionId);
125     }
126 }