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 java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
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 java.io.Serial;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.concepts.WritableIdentifier;
22 import org.opendaylight.yangtools.concepts.WritableObjects;
25 * Globally-unique identifier of a local history. This identifier is assigned on the frontend and is composed of
26 * - a {@link ClientIdentifier}, which uniquely identifies a single instantiation of a particular frontend
27 * - an unsigned long, which uniquely identifies the history on the backend
28 * - an unsigned long cookie, assigned by the client and meaningless on the backend, which just reflects it back
30 public final class LocalHistoryIdentifier implements WritableIdentifier {
32 * Implementation note: cookie is currently required only for module-based sharding, which is implemented as part
33 * of normal DataBroker interfaces. For DOMDataTreeProducer cookie will always be zero, hence
34 * we may end up not needing cookie at all.
36 * We use WritableObjects.writeLongs() to output historyId and cookie (in that order). If we
37 * end up not needing the cookie at all, we can switch to writeLong() and use zero flags for
40 private static final class Proxy implements Externalizable {
42 private static final long serialVersionUID = 1L;
44 private ClientIdentifier clientId;
45 private long historyId;
48 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
49 // be able to create instances via reflection.
50 @SuppressWarnings("checkstyle:RedundantModifier")
55 Proxy(final ClientIdentifier frontendId, final long historyId, final long cookie) {
56 clientId = requireNonNull(frontendId);
57 this.historyId = historyId;
62 public void writeExternal(final ObjectOutput out) throws IOException {
63 clientId.writeTo(out);
64 WritableObjects.writeLongs(out, historyId, cookie);
68 public void readExternal(final ObjectInput in) throws IOException {
69 clientId = ClientIdentifier.readFrom(in);
71 final byte header = WritableObjects.readLongHeader(in);
72 historyId = WritableObjects.readFirstLong(in, header);
73 cookie = WritableObjects.readSecondLong(in, header);
77 private Object readResolve() {
78 return new LocalHistoryIdentifier(clientId, historyId, cookie);
83 private static final long serialVersionUID = 1L;
85 private final @NonNull ClientIdentifier clientId;
86 private final long historyId;
87 private final long cookie;
89 public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
90 this(frontendId, historyId, 0);
93 public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
94 clientId = requireNonNull(frontendId);
95 this.historyId = historyId;
99 public static @NonNull LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
100 final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
102 final byte header = WritableObjects.readLongHeader(in);
103 return new LocalHistoryIdentifier(clientId, WritableObjects.readFirstLong(in, header),
104 WritableObjects.readSecondLong(in, header));
108 public void writeTo(final DataOutput out) throws IOException {
109 clientId.writeTo(out);
110 WritableObjects.writeLongs(out, historyId, cookie);
113 public @NonNull ClientIdentifier getClientId() {
117 public long getHistoryId() {
121 public long getCookie() {
126 public int hashCode() {
127 int ret = clientId.hashCode();
128 ret = 31 * ret + Long.hashCode(historyId);
129 ret = 31 * ret + Long.hashCode(cookie);
134 public boolean equals(final Object obj) {
138 if (!(obj instanceof LocalHistoryIdentifier other)) {
142 return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
146 public String toString() {
147 return MoreObjects.toStringHelper(LocalHistoryIdentifier.class).add("client", clientId)
148 .add("history", Long.toUnsignedString(historyId, 16))
149 .add("cookie", Long.toUnsignedString(cookie, 16)).toString();
153 private Object writeReplace() {
154 return new Proxy(clientId, historyId, cookie);