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