Improve segmented journal actor metrics
[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.IOException;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.cds.types.rev191024.ClientGeneration;
18 import org.opendaylight.yangtools.concepts.WritableIdentifier;
19 import org.opendaylight.yangtools.concepts.WritableObjects;
20 import org.opendaylight.yangtools.yang.common.Uint64;
21
22 /**
23  * A cluster-wide unique identifier of a frontend instance. This identifier discerns between individual incarnations
24  * of a particular frontend.
25  */
26 public final class ClientIdentifier implements WritableIdentifier {
27     @java.io.Serial
28     private static final long serialVersionUID = 1L;
29
30     private final @NonNull FrontendIdentifier frontendId;
31     private final long generation;
32
33     ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
34         this.frontendId = requireNonNull(frontendId);
35         this.generation = generation;
36     }
37
38     public static @NonNull ClientIdentifier create(final FrontendIdentifier frontendId,
39             final long generation) {
40         return new ClientIdentifier(frontendId, generation);
41     }
42
43     public static @NonNull ClientIdentifier readFrom(final DataInput in) throws IOException {
44         final FrontendIdentifier frontendId = FrontendIdentifier.readFrom(in);
45         return new ClientIdentifier(frontendId, WritableObjects.readLong(in));
46     }
47
48     @Override
49     public void writeTo(final DataOutput out) throws IOException {
50         frontendId.writeTo(out);
51         WritableObjects.writeLong(out, generation);
52     }
53
54     public @NonNull FrontendIdentifier getFrontendId() {
55         return frontendId;
56     }
57
58     public long getGeneration() {
59         return generation;
60     }
61
62     public @NonNull ClientGeneration getYangGeneration() {
63         return new ClientGeneration(Uint64.fromLongBits(generation));
64     }
65
66     @Override
67     public int hashCode() {
68         return frontendId.hashCode() * 31 + Long.hashCode(generation);
69     }
70
71     @Override
72     public boolean equals(final Object obj) {
73         return this == obj || obj instanceof ClientIdentifier other && generation == other.generation
74             && frontendId.equals(other.frontendId);
75     }
76
77     @Override
78     public String toString() {
79         return MoreObjects.toStringHelper(ClientIdentifier.class)
80             .add("frontend", frontendId)
81             .add("generation", Long.toUnsignedString(generation))
82             .toString();
83     }
84
85     @java.io.Serial
86     private Object writeReplace() {
87         return new CI(this);
88     }
89 }