2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
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;
24 * Globally-unique identifier of a transaction.
26 public final class TransactionIdentifier implements WritableIdentifier {
27 interface SerialForm extends Externalizable {
28 @NonNull TransactionIdentifier identifier();
30 void setIdentifier(@NonNull TransactionIdentifier identifier);
36 default void readExternal(final ObjectInput in) throws IOException {
37 setIdentifier(new TransactionIdentifier(LocalHistoryIdentifier.readFrom(in), WritableObjects.readLong(in)));
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());
48 @Deprecated(since = "7.0.0", forRemoval = true)
49 private static final class Proxy implements SerialForm {
51 private static final long serialVersionUID = 1L;
53 private TransactionIdentifier identifier;
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")
62 Proxy(final TransactionIdentifier identifier) {
63 this.identifier = requireNonNull(identifier);
67 public @NonNull TransactionIdentifier identifier() {
68 return verifyNotNull(identifier);
72 public void setIdentifier(final TransactionIdentifier identifier) {
73 this.identifier = requireNonNull(identifier);
77 public Object readResolve() {
83 private static final long serialVersionUID = 1L;
85 private final @NonNull LocalHistoryIdentifier historyId;
86 private final long transactionId;
87 private String shortString;
89 public TransactionIdentifier(final @NonNull LocalHistoryIdentifier historyId, final long transactionId) {
90 this.historyId = requireNonNull(historyId);
91 this.transactionId = transactionId;
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));
100 public void writeTo(final DataOutput out) throws IOException {
101 historyId.writeTo(out);
102 WritableObjects.writeLong(out, transactionId);
105 public @NonNull LocalHistoryIdentifier getHistoryId() {
109 public long getTransactionId() {
110 return transactionId;
114 public int hashCode() {
115 return historyId.hashCode() * 31 + Long.hashCode(transactionId);
119 public boolean equals(final Object obj) {
120 return this == obj || obj instanceof TransactionIdentifier other && transactionId == other.transactionId
121 && historyId.equals(other.historyId);
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();
137 public String toString() {
138 return toShortString();
142 private Object writeReplace() {