More cds-access-api cleanup
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / ClientIdentifier.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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024.ClientGeneration;
22 import org.opendaylight.yangtools.concepts.WritableIdentifier;
23 import org.opendaylight.yangtools.concepts.WritableObjects;
24 import org.opendaylight.yangtools.yang.common.Uint64;
25
26 /**
27  * A cluster-wide unique identifier of a frontend instance. This identifier discerns between individual incarnations
28  * of a particular frontend.
29  *
30  * @author Robert Varga
31  */
32 public final class ClientIdentifier implements WritableIdentifier {
33     private static final class Proxy implements Externalizable {
34         @Serial
35         private static final long serialVersionUID = 1L;
36
37         private FrontendIdentifier frontendId;
38         private long generation;
39
40         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
41         // be able to create instances via reflection.
42         @SuppressWarnings("checkstyle:RedundantModifier")
43         public Proxy() {
44             // Needed for Externalizable
45         }
46
47         Proxy(final FrontendIdentifier frontendId, final long generation) {
48             this.frontendId = requireNonNull(frontendId);
49             this.generation = generation;
50         }
51
52         @Override
53         public void writeExternal(final ObjectOutput out) throws IOException {
54             frontendId.writeTo(out);
55             WritableObjects.writeLong(out, generation);
56         }
57
58         @Override
59         public void readExternal(final ObjectInput in) throws IOException {
60             frontendId = FrontendIdentifier.readFrom(in);
61             generation = WritableObjects.readLong(in);
62         }
63
64         @Serial
65         private Object readResolve() {
66             return new ClientIdentifier(frontendId, generation);
67         }
68     }
69
70     @Serial
71     private static final long serialVersionUID = 1L;
72
73     private final @NonNull FrontendIdentifier frontendId;
74     private final long generation;
75
76     ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
77         this.frontendId = requireNonNull(frontendId);
78         this.generation = generation;
79     }
80
81     public static @NonNull ClientIdentifier create(final FrontendIdentifier frontendId,
82             final long generation) {
83         return new ClientIdentifier(frontendId, generation);
84     }
85
86     public static @NonNull ClientIdentifier readFrom(final DataInput in) throws IOException {
87         final FrontendIdentifier frontendId = FrontendIdentifier.readFrom(in);
88         return new ClientIdentifier(frontendId, WritableObjects.readLong(in));
89     }
90
91     @Override
92     public void writeTo(final DataOutput out) throws IOException {
93         frontendId.writeTo(out);
94         WritableObjects.writeLong(out, generation);
95     }
96
97     public @NonNull FrontendIdentifier getFrontendId() {
98         return frontendId;
99     }
100
101     public long getGeneration() {
102         return generation;
103     }
104
105     public @NonNull ClientGeneration getYangGeneration() {
106         return new ClientGeneration(Uint64.fromLongBits(generation));
107     }
108
109     @Override
110     public int hashCode() {
111         return frontendId.hashCode() * 31 + Long.hashCode(generation);
112     }
113
114     @Override
115     public boolean equals(final Object obj) {
116         return this == obj || obj instanceof ClientIdentifier other && generation == other.generation
117             && frontendId.equals(other.frontendId);
118     }
119
120     @Override
121     public String toString() {
122         return MoreObjects.toStringHelper(ClientIdentifier.class)
123             .add("frontend", frontendId)
124             .add("generation", Long.toUnsignedString(generation))
125             .toString();
126     }
127
128     @Serial
129     private Object writeReplace() {
130         return new Proxy(frontendId, generation);
131     }
132 }