Promote cds-access-api
[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         private Object readResolve() {
65             return new ClientIdentifier(frontendId, generation);
66         }
67     }
68
69     @Serial
70     private static final long serialVersionUID = 1L;
71
72     private final @NonNull FrontendIdentifier frontendId;
73     private final long generation;
74
75     ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
76         this.frontendId = requireNonNull(frontendId);
77         this.generation = generation;
78     }
79
80     public static @NonNull ClientIdentifier create(final FrontendIdentifier frontendId,
81             final long generation) {
82         return new ClientIdentifier(frontendId, generation);
83     }
84
85     public static @NonNull ClientIdentifier readFrom(final DataInput in) throws IOException {
86         final FrontendIdentifier frontendId = FrontendIdentifier.readFrom(in);
87         return new ClientIdentifier(frontendId, WritableObjects.readLong(in));
88     }
89
90     @Override
91     public void writeTo(final DataOutput out) throws IOException {
92         frontendId.writeTo(out);
93         WritableObjects.writeLong(out, generation);
94     }
95
96     public @NonNull FrontendIdentifier getFrontendId() {
97         return frontendId;
98     }
99
100     public long getGeneration() {
101         return generation;
102     }
103
104     public @NonNull ClientGeneration getYangGeneration() {
105         return new ClientGeneration(Uint64.fromLongBits(generation));
106     }
107
108     @Override
109     public int hashCode() {
110         return frontendId.hashCode() * 31 + Long.hashCode(generation);
111     }
112
113     @Override
114     public boolean equals(final Object obj) {
115         return this == obj || obj instanceof ClientIdentifier other && generation == other.generation
116             && frontendId.equals(other.frontendId);
117     }
118
119     @Override
120     public String toString() {
121         return MoreObjects.toStringHelper(ClientIdentifier.class)
122             .add("frontend", frontendId)
123             .add("generation", Long.toUnsignedString(generation))
124             .toString();
125     }
126
127     private Object writeReplace() {
128         return new Proxy(frontendId, generation);
129     }
130 }