More cds-access-api cleanup
[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 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 java.io.Serial;
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 public final class TransactionIdentifier implements WritableIdentifier {
27     private static final class Proxy implements Externalizable {
28         @Serial
29         private static final long serialVersionUID = 1L;
30
31         private LocalHistoryIdentifier historyId;
32         private long transactionId;
33
34         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
35         // be able to create instances via reflection.
36         @SuppressWarnings("checkstyle:RedundantModifier")
37         public Proxy() {
38             // For Externalizable
39         }
40
41         Proxy(final LocalHistoryIdentifier historyId, final long transactionId) {
42             this.historyId = requireNonNull(historyId);
43             this.transactionId = transactionId;
44         }
45
46         @Override
47         public void writeExternal(final ObjectOutput out) throws IOException {
48             historyId.writeTo(out);
49             WritableObjects.writeLong(out, transactionId);
50         }
51
52         @Override
53         public void readExternal(final ObjectInput in) throws IOException {
54             historyId = LocalHistoryIdentifier.readFrom(in);
55             transactionId = WritableObjects.readLong(in);
56         }
57
58         @Serial
59         private Object readResolve() {
60             return new TransactionIdentifier(historyId, transactionId);
61         }
62     }
63
64     @Serial
65     private static final long serialVersionUID = 1L;
66
67     private final @NonNull LocalHistoryIdentifier historyId;
68     private final long transactionId;
69     private String shortString;
70
71     public TransactionIdentifier(final @NonNull LocalHistoryIdentifier historyId, final long transactionId) {
72         this.historyId = requireNonNull(historyId);
73         this.transactionId = transactionId;
74     }
75
76     public static @NonNull TransactionIdentifier readFrom(final DataInput in) throws IOException {
77         final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
78         return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
79     }
80
81     @Override
82     public void writeTo(final DataOutput out) throws IOException {
83         historyId.writeTo(out);
84         WritableObjects.writeLong(out, transactionId);
85     }
86
87     public @NonNull LocalHistoryIdentifier getHistoryId() {
88         return historyId;
89     }
90
91     public long getTransactionId() {
92         return transactionId;
93     }
94
95     @Override
96     public int hashCode() {
97         return historyId.hashCode() * 31 + Long.hashCode(transactionId);
98     }
99
100     @Override
101     public boolean equals(final Object obj) {
102         return this == obj || obj instanceof TransactionIdentifier other && transactionId == other.transactionId
103             && historyId.equals(other.historyId);
104     }
105
106     public String toShortString() {
107         if (shortString == null) {
108             String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
109             shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-"
110                     + historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-"
111                     + historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId
112                     + "-" + historyId.getCookie();
113         }
114
115         return shortString;
116     }
117
118     @Override
119     public String toString() {
120         return toShortString();
121     }
122
123     @Serial
124     private Object writeReplace() {
125         return new Proxy(historyId, transactionId);
126     }
127 }