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;
49 Proxy(final ClientIdentifier frontendId, final long historyId, final long cookie) {
50 this.clientId = Preconditions.checkNotNull(frontendId);
51 this.historyId = historyId;
56 public void writeExternal(final ObjectOutput out) throws IOException {
57 clientId.writeTo(out);
58 WritableObjects.writeLongs(out, historyId, cookie);
62 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
63 clientId = ClientIdentifier.readFrom(in);
65 final byte header = WritableObjects.readLongHeader(in);
66 historyId = WritableObjects.readFirstLong(in, header);
67 cookie = WritableObjects.readSecondLong(in, header);
70 private Object readResolve() {
71 return new LocalHistoryIdentifier(clientId, historyId, cookie);
75 private static final long serialVersionUID = 1L;
76 private final ClientIdentifier clientId;
77 private final long historyId;
78 private final long cookie;
80 public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
81 this(frontendId, historyId, 0);
84 public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
85 this.clientId = Preconditions.checkNotNull(frontendId);
86 this.historyId = historyId;
90 public static LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
91 final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
93 final byte header = WritableObjects.readLongHeader(in);
94 return new LocalHistoryIdentifier(clientId, WritableObjects.readFirstLong(in, header),
95 WritableObjects.readSecondLong(in, header));
99 public void writeTo(final DataOutput out) throws IOException {
100 clientId.writeTo(out);
101 WritableObjects.writeLongs(out, historyId, cookie);
104 public ClientIdentifier getClientId() {
108 public long getHistoryId() {
112 public long getCookie() {
117 public int hashCode() {
118 int ret = clientId.hashCode();
119 ret = 31 * ret + Long.hashCode(historyId);
120 ret = 31 * ret + Long.hashCode(cookie);
125 public boolean equals(final Object o) {
129 if (!(o instanceof LocalHistoryIdentifier)) {
133 final LocalHistoryIdentifier other = (LocalHistoryIdentifier) o;
134 return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
138 public String toString() {
139 return MoreObjects.toStringHelper(LocalHistoryIdentifier.class).add("client", clientId)
140 .add("history", Long.toUnsignedString(historyId, 16))
141 .add("cookie", Long.toUnsignedString(cookie, 16)).toString();
144 private Object writeReplace() {
145 return new Proxy(clientId, historyId, cookie);