e6050e76c5055f5517d1fef7674a27b269147121
[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 java.io.Serial;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.concepts.WritableIdentifier;
22 import org.opendaylight.yangtools.concepts.WritableObjects;
23
24 /**
25  * Globally-unique identifier of a local history. This identifier is assigned on the frontend and is composed of
26  * - a {@link ClientIdentifier}, which uniquely identifies a single instantiation of a particular frontend
27  * - an unsigned long, which uniquely identifies the history on the backend
28  * - an unsigned long cookie, assigned by the client and meaningless on the backend, which just reflects it back
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         @Serial
42         private static final long serialVersionUID = 1L;
43
44         private ClientIdentifier clientId;
45         private long historyId;
46         private long cookie;
47
48         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
49         // be able to create instances via reflection.
50         @SuppressWarnings("checkstyle:RedundantModifier")
51         public Proxy() {
52             // For Externalizable
53         }
54
55         Proxy(final ClientIdentifier frontendId, final long historyId, final long cookie) {
56             clientId = requireNonNull(frontendId);
57             this.historyId = historyId;
58             this.cookie = cookie;
59         }
60
61         @Override
62         public void writeExternal(final ObjectOutput out) throws IOException {
63             clientId.writeTo(out);
64             WritableObjects.writeLongs(out, historyId, cookie);
65         }
66
67         @Override
68         public void readExternal(final ObjectInput in) throws IOException {
69             clientId = ClientIdentifier.readFrom(in);
70
71             final byte header = WritableObjects.readLongHeader(in);
72             historyId = WritableObjects.readFirstLong(in, header);
73             cookie = WritableObjects.readSecondLong(in, header);
74         }
75
76         @Serial
77         private Object readResolve() {
78             return new LocalHistoryIdentifier(clientId, historyId, cookie);
79         }
80     }
81
82     @Serial
83     private static final long serialVersionUID = 1L;
84
85     private final @NonNull ClientIdentifier clientId;
86     private final long historyId;
87     private final long cookie;
88
89     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
90         this(frontendId, historyId, 0);
91     }
92
93     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
94         clientId = requireNonNull(frontendId);
95         this.historyId = historyId;
96         this.cookie = cookie;
97     }
98
99     public static @NonNull LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
100         final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
101
102         final byte header = WritableObjects.readLongHeader(in);
103         return new LocalHistoryIdentifier(clientId, WritableObjects.readFirstLong(in, header),
104             WritableObjects.readSecondLong(in, header));
105     }
106
107     @Override
108     public void writeTo(final DataOutput out) throws IOException {
109         clientId.writeTo(out);
110         WritableObjects.writeLongs(out, historyId, cookie);
111     }
112
113     public @NonNull ClientIdentifier getClientId() {
114         return clientId;
115     }
116
117     public long getHistoryId() {
118         return historyId;
119     }
120
121     public long getCookie() {
122         return cookie;
123     }
124
125     @Override
126     public int hashCode() {
127         int ret = clientId.hashCode();
128         ret = 31 * ret + Long.hashCode(historyId);
129         ret = 31 * ret + Long.hashCode(cookie);
130         return ret;
131     }
132
133     @Override
134     public boolean equals(final Object obj) {
135         if (this == obj) {
136             return true;
137         }
138         if (!(obj instanceof LocalHistoryIdentifier other)) {
139             return false;
140         }
141
142         return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
143     }
144
145     @Override
146     public String toString() {
147         return MoreObjects.toStringHelper(LocalHistoryIdentifier.class).add("client", clientId)
148                 .add("history", Long.toUnsignedString(historyId, 16))
149                 .add("cookie", Long.toUnsignedString(cookie, 16)).toString();
150     }
151
152     @Serial
153     private Object writeReplace() {
154         return new Proxy(clientId, historyId, cookie);
155     }
156 }