Promote cds-access-api
[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         private Object readResolve() {
59             return new TransactionIdentifier(historyId, transactionId);
60         }
61     }
62
63     @Serial
64     private static final long serialVersionUID = 1L;
65
66     private final @NonNull LocalHistoryIdentifier historyId;
67     private final long transactionId;
68     private String shortString;
69
70     public TransactionIdentifier(final @NonNull LocalHistoryIdentifier historyId, final long transactionId) {
71         this.historyId = requireNonNull(historyId);
72         this.transactionId = transactionId;
73     }
74
75     public static @NonNull TransactionIdentifier readFrom(final DataInput in) throws IOException {
76         final LocalHistoryIdentifier historyId = LocalHistoryIdentifier.readFrom(in);
77         return new TransactionIdentifier(historyId, WritableObjects.readLong(in));
78     }
79
80     @Override
81     public void writeTo(final DataOutput out) throws IOException {
82         historyId.writeTo(out);
83         WritableObjects.writeLong(out, transactionId);
84     }
85
86     public @NonNull LocalHistoryIdentifier getHistoryId() {
87         return historyId;
88     }
89
90     public long getTransactionId() {
91         return transactionId;
92     }
93
94     @Override
95     public int hashCode() {
96         return historyId.hashCode() * 31 + Long.hashCode(transactionId);
97     }
98
99     @Override
100     public boolean equals(final Object obj) {
101         return this == obj || obj instanceof TransactionIdentifier other && transactionId == other.transactionId
102             && historyId.equals(other.historyId);
103     }
104
105     public String toShortString() {
106         if (shortString == null) {
107             String histStr = historyId.getHistoryId() == 0 ? "" : "-chn-" + historyId.getHistoryId();
108             shortString = historyId.getClientId().getFrontendId().getMemberName().getName() + "-"
109                     + historyId.getClientId().getFrontendId().getClientType().getName() + "-fe-"
110                     + historyId.getClientId().getGeneration() + histStr + "-txn-" + transactionId
111                     + "-" + historyId.getCookie();
112         }
113
114         return shortString;
115     }
116
117     @Override
118     public String toString() {
119         return toShortString();
120     }
121
122     private Object writeReplace() {
123         return new Proxy(historyId, transactionId);
124     }
125 }