Remove unused exceptions
[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 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;
20
21 /**
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
26  *
27  * @author Robert Varga
28  */
29 public final class LocalHistoryIdentifier implements WritableIdentifier {
30     /*
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.
34      *
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
37      *                      compatibility.
38      */
39     private static final class Proxy implements Externalizable {
40         private static final long serialVersionUID = 1L;
41         private ClientIdentifier clientId;
42         private long historyId;
43         private long cookie;
44
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")
48         public Proxy() {
49             // For Externalizable
50         }
51
52         Proxy(final ClientIdentifier frontendId, final long historyId, final long cookie) {
53             this.clientId = Preconditions.checkNotNull(frontendId);
54             this.historyId = historyId;
55             this.cookie = cookie;
56         }
57
58         @Override
59         public void writeExternal(final ObjectOutput out) throws IOException {
60             clientId.writeTo(out);
61             WritableObjects.writeLongs(out, historyId, cookie);
62         }
63
64         @Override
65         public void readExternal(final ObjectInput in) throws IOException {
66             clientId = ClientIdentifier.readFrom(in);
67
68             final byte header = WritableObjects.readLongHeader(in);
69             historyId = WritableObjects.readFirstLong(in, header);
70             cookie = WritableObjects.readSecondLong(in, header);
71         }
72
73         private Object readResolve() {
74             return new LocalHistoryIdentifier(clientId, historyId, cookie);
75         }
76     }
77
78     private static final long serialVersionUID = 1L;
79     private final ClientIdentifier clientId;
80     private final long historyId;
81     private final long cookie;
82
83     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId) {
84         this(frontendId, historyId, 0);
85     }
86
87     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
88         this.clientId = Preconditions.checkNotNull(frontendId);
89         this.historyId = historyId;
90         this.cookie = cookie;
91     }
92
93     public static LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
94         final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
95
96         final byte header = WritableObjects.readLongHeader(in);
97         return new LocalHistoryIdentifier(clientId, WritableObjects.readFirstLong(in, header),
98             WritableObjects.readSecondLong(in, header));
99     }
100
101     @Override
102     public void writeTo(final DataOutput out) throws IOException {
103         clientId.writeTo(out);
104         WritableObjects.writeLongs(out, historyId, cookie);
105     }
106
107     public ClientIdentifier getClientId() {
108         return clientId;
109     }
110
111     public long getHistoryId() {
112         return historyId;
113     }
114
115     public long getCookie() {
116         return cookie;
117     }
118
119     @Override
120     public int hashCode() {
121         int ret = clientId.hashCode();
122         ret = 31 * ret + Long.hashCode(historyId);
123         ret = 31 * ret + Long.hashCode(cookie);
124         return ret;
125     }
126
127     @Override
128     public boolean equals(final Object obj) {
129         if (this == obj) {
130             return true;
131         }
132         if (!(obj instanceof LocalHistoryIdentifier)) {
133             return false;
134         }
135
136         final LocalHistoryIdentifier other = (LocalHistoryIdentifier) obj;
137         return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
138     }
139
140     @Override
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();
145     }
146
147     private Object writeReplace() {
148         return new Proxy(clientId, historyId, cookie);
149     }
150 }