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