Inline Entry.contains()
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / LocalHistoryIdentifier.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.access.concepts;
9
10 import static java.util.Objects.requireNonNull;
11
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;
21
22 /**
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
27  *
28  * @author Robert Varga
29  */
30 public final class LocalHistoryIdentifier implements WritableIdentifier {
31     /*
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.
35      *
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
38      *                      compatibility.
39      */
40     private static final class Proxy implements Externalizable {
41         private static final long serialVersionUID = 1L;
42         private ClientIdentifier clientId;
43         private long historyId;
44         private long cookie;
45
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")
49         public Proxy() {
50             // For Externalizable
51         }
52
53         Proxy(final ClientIdentifier frontendId, final long historyId, final long cookie) {
54             this.clientId = requireNonNull(frontendId);
55             this.historyId = historyId;
56             this.cookie = cookie;
57         }
58
59         @Override
60         public void writeExternal(final ObjectOutput out) throws IOException {
61             clientId.writeTo(out);
62             WritableObjects.writeLongs(out, historyId, cookie);
63         }
64
65         @Override
66         public void readExternal(final ObjectInput in) throws IOException {
67             clientId = ClientIdentifier.readFrom(in);
68
69             final byte header = WritableObjects.readLongHeader(in);
70             historyId = WritableObjects.readFirstLong(in, header);
71             cookie = WritableObjects.readSecondLong(in, header);
72         }
73
74         private Object readResolve() {
75             return new LocalHistoryIdentifier(clientId, historyId, cookie);
76         }
77     }
78
79     private static final long serialVersionUID = 1L;
80     private final ClientIdentifier clientId;
81     private final long historyId;
82     private final long cookie;
83
84     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
85         this(frontendId, historyId, 0);
86     }
87
88     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
89         this.clientId = requireNonNull(frontendId);
90         this.historyId = historyId;
91         this.cookie = cookie;
92     }
93
94     public static LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
95         final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
96
97         final byte header = WritableObjects.readLongHeader(in);
98         return new LocalHistoryIdentifier(clientId, WritableObjects.readFirstLong(in, header),
99             WritableObjects.readSecondLong(in, header));
100     }
101
102     @Override
103     public void writeTo(final DataOutput out) throws IOException {
104         clientId.writeTo(out);
105         WritableObjects.writeLongs(out, historyId, cookie);
106     }
107
108     public ClientIdentifier getClientId() {
109         return clientId;
110     }
111
112     public long getHistoryId() {
113         return historyId;
114     }
115
116     public long getCookie() {
117         return cookie;
118     }
119
120     @Override
121     public int hashCode() {
122         int ret = clientId.hashCode();
123         ret = 31 * ret + Long.hashCode(historyId);
124         ret = 31 * ret + Long.hashCode(cookie);
125         return ret;
126     }
127
128     @Override
129     public boolean equals(final Object obj) {
130         if (this == obj) {
131             return true;
132         }
133         if (!(obj instanceof LocalHistoryIdentifier)) {
134             return false;
135         }
136
137         final LocalHistoryIdentifier other = (LocalHistoryIdentifier) obj;
138         return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
139     }
140
141     @Override
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();
146     }
147
148     private Object writeReplace() {
149         return new Proxy(clientId, historyId, cookie);
150     }
151 }