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.opendaylight.yangtools.concepts.WritableIdentifier;
20 import org.opendaylight.yangtools.concepts.WritableObjects;
23 * Globally-unique identifier of a local history. This identifier is assigned on the frontend and is composed of
24 * - a {@link ClientIdentifier}, which uniquely identifies a single instantiation of a particular frontend
25 * - an unsigned long, which uniquely identifies the history on the backend
26 * - an unsigned long cookie, assigned by the client and meaningless on the backend, which just reflects it back
28 * @author Robert Varga
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 {
41 private static final long serialVersionUID = 1L;
42 private ClientIdentifier clientId;
43 private long historyId;
46 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
47 // be able to create instances via reflection.
48 @SuppressWarnings("checkstyle:RedundantModifier")
53 Proxy(final ClientIdentifier frontendId, final long historyId, final long cookie) {
54 this.clientId = requireNonNull(frontendId);
55 this.historyId = historyId;
60 public void writeExternal(final ObjectOutput out) throws IOException {
61 clientId.writeTo(out);
62 WritableObjects.writeLongs(out, historyId, cookie);
66 public void readExternal(final ObjectInput in) throws IOException {
67 clientId = ClientIdentifier.readFrom(in);
69 final byte header = WritableObjects.readLongHeader(in);
70 historyId = WritableObjects.readFirstLong(in, header);
71 cookie = WritableObjects.readSecondLong(in, header);
74 private Object readResolve() {
75 return new LocalHistoryIdentifier(clientId, historyId, cookie);
79 private static final long serialVersionUID = 1L;
80 private final ClientIdentifier clientId;
81 private final long historyId;
82 private final long cookie;
84 public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
85 this(frontendId, historyId, 0);
88 public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
89 this.clientId = requireNonNull(frontendId);
90 this.historyId = historyId;
94 public static LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
95 final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
97 final byte header = WritableObjects.readLongHeader(in);
98 return new LocalHistoryIdentifier(clientId, WritableObjects.readFirstLong(in, header),
99 WritableObjects.readSecondLong(in, header));
103 public void writeTo(final DataOutput out) throws IOException {
104 clientId.writeTo(out);
105 WritableObjects.writeLongs(out, historyId, cookie);
108 public ClientIdentifier getClientId() {
112 public long getHistoryId() {
116 public long getCookie() {
121 public int hashCode() {
122 int ret = clientId.hashCode();
123 ret = 31 * ret + Long.hashCode(historyId);
124 ret = 31 * ret + Long.hashCode(cookie);
129 public boolean equals(final Object obj) {
133 if (!(obj instanceof LocalHistoryIdentifier)) {
137 final LocalHistoryIdentifier other = (LocalHistoryIdentifier) obj;
138 return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
142 public String toString() {
143 return MoreObjects.toStringHelper(LocalHistoryIdentifier.class).add("client", clientId)
144 .add("history", Long.toUnsignedString(historyId, 16))
145 .add("cookie", Long.toUnsignedString(cookie, 16)).toString();
148 private Object writeReplace() {
149 return new Proxy(clientId, historyId, cookie);