fc8a2a4954ee9e51dbf64eb18a9e476caac310bc
[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 com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects;
14 import java.io.DataInput;
15 import java.io.DataOutput;
16 import java.io.Externalizable;
17 import java.io.IOException;
18 import java.io.ObjectInput;
19 import java.io.ObjectOutput;
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 public final class ClientIdentifier implements WritableIdentifier {
31     interface SerialForm extends Externalizable {
32         @NonNull ClientIdentifier identifier();
33
34         void setIdentifier(@NonNull ClientIdentifier identifier);
35
36         @java.io.Serial
37         Object readResolve();
38
39         @Override
40         default void readExternal(final ObjectInput in) throws IOException {
41             setIdentifier(new ClientIdentifier(FrontendIdentifier.readFrom(in), WritableObjects.readLong(in)));
42         }
43
44         @Override
45         default void writeExternal(final ObjectOutput out) throws IOException {
46             final var id = identifier();
47             id.getFrontendId().writeTo(out);
48             WritableObjects.writeLong(out, id.getGeneration());
49         }
50     }
51
52     private static final class Proxy implements SerialForm {
53         @java.io.Serial
54         private static final long serialVersionUID = 1L;
55
56         private ClientIdentifier identifier;
57
58         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
59         // be able to create instances via reflection.
60         @SuppressWarnings("checkstyle:RedundantModifier")
61         public Proxy() {
62             // Needed for Externalizable
63         }
64
65         Proxy(final ClientIdentifier identifier) {
66             this.identifier = requireNonNull(identifier);
67         }
68
69         @Override
70         public ClientIdentifier identifier() {
71             return verifyNotNull(identifier);
72         }
73
74         @Override
75         public void setIdentifier(final ClientIdentifier identifier) {
76             this.identifier = requireNonNull(identifier);
77         }
78
79         @Override
80         public Object readResolve() {
81             return identifier();
82         }
83     }
84
85     @java.io.Serial
86     private static final long serialVersionUID = 1L;
87
88     private final @NonNull FrontendIdentifier frontendId;
89     private final long generation;
90
91     ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
92         this.frontendId = requireNonNull(frontendId);
93         this.generation = generation;
94     }
95
96     public static @NonNull ClientIdentifier create(final FrontendIdentifier frontendId,
97             final long generation) {
98         return new ClientIdentifier(frontendId, generation);
99     }
100
101     public static @NonNull ClientIdentifier readFrom(final DataInput in) throws IOException {
102         final FrontendIdentifier frontendId = FrontendIdentifier.readFrom(in);
103         return new ClientIdentifier(frontendId, WritableObjects.readLong(in));
104     }
105
106     @Override
107     public void writeTo(final DataOutput out) throws IOException {
108         frontendId.writeTo(out);
109         WritableObjects.writeLong(out, generation);
110     }
111
112     public @NonNull FrontendIdentifier getFrontendId() {
113         return frontendId;
114     }
115
116     public long getGeneration() {
117         return generation;
118     }
119
120     public @NonNull ClientGeneration getYangGeneration() {
121         return new ClientGeneration(Uint64.fromLongBits(generation));
122     }
123
124     @Override
125     public int hashCode() {
126         return frontendId.hashCode() * 31 + Long.hashCode(generation);
127     }
128
129     @Override
130     public boolean equals(final Object obj) {
131         return this == obj || obj instanceof ClientIdentifier other && generation == other.generation
132             && frontendId.equals(other.frontendId);
133     }
134
135     @Override
136     public String toString() {
137         return MoreObjects.toStringHelper(ClientIdentifier.class)
138             .add("frontend", frontendId)
139             .add("generation", Long.toUnsignedString(generation))
140             .toString();
141     }
142
143     @java.io.Serial
144     private Object writeReplace() {
145         return new Proxy(this);
146     }
147 }