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 com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import java.io.DataInput;
13 import java.io.DataOutput;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import org.opendaylight.yangtools.concepts.WritableIdentifier;
19 import org.opendaylight.yangtools.concepts.WritableObjects;
22 * Globally-unique identifier of a local history. This identifier is assigned on the frontend and is composed of
23 * - a {@link ClientIdentifier}, which uniquely identifies a single instantiation of a particular frontend
24 * - an unsigned long, which uniquely identifies the history on the backend
25 * - an unsigned long cookie, assigned by the client and meaningless on the backend, which just reflects it back
27 * @author Robert Varga
29 public final class LocalHistoryIdentifier implements WritableIdentifier {
31 * Implementation note: cookie is currently required only for module-based sharding, which is implemented as part
32 * of normal DataBroker interfaces. For DOMDataTreeProducer cookie will always be zero, hence
33 * we may end up not needing cookie at all.
35 * We use WritableObjects.writeLongs() to output historyId and cookie (in that order). If we
36 * end up not needing the cookie at all, we can switch to writeLong() and use zero flags for
39 private static final class Proxy implements Externalizable {
40 private static final long serialVersionUID = 1L;
41 private ClientIdentifier clientId;
42 private long historyId;
45 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
46 // be able to create instances via reflection.
47 @SuppressWarnings("checkstyle:RedundantModifier")
52 Proxy(final ClientIdentifier frontendId, final long historyId, final long cookie) {
53 this.clientId = Preconditions.checkNotNull(frontendId);
54 this.historyId = historyId;
59 public void writeExternal(final ObjectOutput out) throws IOException {
60 clientId.writeTo(out);
61 WritableObjects.writeLongs(out, historyId, cookie);
65 public void readExternal(final ObjectInput in) throws IOException {
66 clientId = ClientIdentifier.readFrom(in);
68 final byte header = WritableObjects.readLongHeader(in);
69 historyId = WritableObjects.readFirstLong(in, header);
70 cookie = WritableObjects.readSecondLong(in, header);
73 private Object readResolve() {
74 return new LocalHistoryIdentifier(clientId, historyId, cookie);
78 private static final long serialVersionUID = 1L;
79 private final ClientIdentifier clientId;
80 private final long historyId;
81 private final long cookie;
83 public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
84 this(frontendId, historyId, 0);
87 public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
88 this.clientId = Preconditions.checkNotNull(frontendId);
89 this.historyId = historyId;
93 public static LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
94 final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
96 final byte header = WritableObjects.readLongHeader(in);
97 return new LocalHistoryIdentifier(clientId, WritableObjects.readFirstLong(in, header),
98 WritableObjects.readSecondLong(in, header));
102 public void writeTo(final DataOutput out) throws IOException {
103 clientId.writeTo(out);
104 WritableObjects.writeLongs(out, historyId, cookie);
107 public ClientIdentifier getClientId() {
111 public long getHistoryId() {
115 public long getCookie() {
120 public int hashCode() {
121 int ret = clientId.hashCode();
122 ret = 31 * ret + Long.hashCode(historyId);
123 ret = 31 * ret + Long.hashCode(cookie);
128 public boolean equals(final Object obj) {
132 if (!(obj instanceof LocalHistoryIdentifier)) {
136 final LocalHistoryIdentifier other = (LocalHistoryIdentifier) obj;
137 return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
141 public String toString() {
142 return MoreObjects.toStringHelper(LocalHistoryIdentifier.class).add("client", clientId)
143 .add("history", Long.toUnsignedString(historyId, 16))
144 .add("cookie", Long.toUnsignedString(cookie, 16)).toString();
147 private Object writeReplace() {
148 return new Proxy(clientId, historyId, cookie);