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 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 local history. This identifier is assigned on the frontend and is composed of
25 * - a {@link ClientIdentifier}, which uniquely identifies a single instantiation of a particular frontend
26 * - an unsigned long, which uniquely identifies the history on the backend
27 * - an unsigned long cookie, assigned by the client and meaningless on the backend, which just reflects it back
29 * @author Robert Varga
31 public final class LocalHistoryIdentifier implements WritableIdentifier {
33 * Implementation note: cookie is currently required only for module-based sharding, which is implemented as part
34 * of normal DataBroker interfaces. For DOMDataTreeProducer cookie will always be zero, hence
35 * we may end up not needing cookie at all.
37 * We use WritableObjects.writeLongs() to output historyId and cookie (in that order). If we
38 * end up not needing the cookie at all, we can switch to writeLong() and use zero flags for
41 private static final class Proxy implements Externalizable {
42 private static final long serialVersionUID = 1L;
43 private ClientIdentifier clientId;
44 private long historyId;
47 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
48 // be able to create instances via reflection.
49 @SuppressWarnings("checkstyle:RedundantModifier")
54 Proxy(final ClientIdentifier frontendId, final long historyId, final long cookie) {
55 clientId = requireNonNull(frontendId);
56 this.historyId = historyId;
61 public void writeExternal(final ObjectOutput out) throws IOException {
62 clientId.writeTo(out);
63 WritableObjects.writeLongs(out, historyId, cookie);
67 public void readExternal(final ObjectInput in) throws IOException {
68 clientId = ClientIdentifier.readFrom(in);
70 final byte header = WritableObjects.readLongHeader(in);
71 historyId = WritableObjects.readFirstLong(in, header);
72 cookie = WritableObjects.readSecondLong(in, header);
75 private Object readResolve() {
76 return new LocalHistoryIdentifier(clientId, historyId, cookie);
80 private static final long serialVersionUID = 1L;
81 private final @NonNull ClientIdentifier clientId;
82 private final long historyId;
83 private final long cookie;
85 public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
86 this(frontendId, historyId, 0);
89 public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
90 clientId = requireNonNull(frontendId);
91 this.historyId = historyId;
95 public static @NonNull LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
96 final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
98 final byte header = WritableObjects.readLongHeader(in);
99 return new LocalHistoryIdentifier(clientId, WritableObjects.readFirstLong(in, header),
100 WritableObjects.readSecondLong(in, header));
104 public void writeTo(final DataOutput out) throws IOException {
105 clientId.writeTo(out);
106 WritableObjects.writeLongs(out, historyId, cookie);
109 public @NonNull ClientIdentifier getClientId() {
113 public long getHistoryId() {
117 public long getCookie() {
122 public int hashCode() {
123 int ret = clientId.hashCode();
124 ret = 31 * ret + Long.hashCode(historyId);
125 ret = 31 * ret + Long.hashCode(cookie);
130 public boolean equals(final Object obj) {
134 if (!(obj instanceof LocalHistoryIdentifier)) {
138 final LocalHistoryIdentifier other = (LocalHistoryIdentifier) obj;
139 return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
143 public String toString() {
144 return MoreObjects.toStringHelper(LocalHistoryIdentifier.class).add("client", clientId)
145 .add("history", Long.toUnsignedString(historyId, 16))
146 .add("cookie", Long.toUnsignedString(cookie, 16)).toString();
149 private Object writeReplace() {
150 return new Proxy(clientId, historyId, cookie);