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 private static final class Proxy implements SerialForm {
50 private static final long serialVersionUID = 1L;
52 private TransactionIdentifier identifier;
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")
61 Proxy(final TransactionIdentifier identifier) {
62 this.identifier = requireNonNull(identifier);
66 public @NonNull TransactionIdentifier identifier() {
67 return verifyNotNull(identifier);
71 public void setIdentifier(final TransactionIdentifier identifier) {
72 this.identifier = requireNonNull(identifier);
76 public Object readResolve() {
82 private static final long serialVersionUID = 1L;
84 private final @NonNull LocalHistoryIdentifier historyId;
85 private final long transactionId;
86 private String shortString;
88 public TransactionIdentifier(final @NonNull LocalHistoryIdentifier historyId, final long transactionId) {
89 this.historyId = requireNonNull(historyId);
90 this.transactionId = transactionId;
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));
99 public void writeTo(final DataOutput out) throws IOException {
100 historyId.writeTo(out);
101 WritableObjects.writeLong(out, transactionId);
104 public @NonNull LocalHistoryIdentifier getHistoryId() {
108 public long getTransactionId() {
109 return transactionId;
113 public int hashCode() {
114 return historyId.hashCode() * 31 + Long.hashCode(transactionId);
118 public boolean equals(final Object obj) {
119 return this == obj || obj instanceof TransactionIdentifier other && transactionId == other.transactionId
120 && historyId.equals(other.historyId);
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();
136 public String toString() {
137 return toShortString();
141 private Object writeReplace() {
142 return new Proxy(this);