2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
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;
27 * A cluster-wide unique identifier of a frontend instance. This identifier discerns between individual incarnations
28 * of a particular frontend.
30 * @author Robert Varga
33 public final class ClientIdentifier implements WritableIdentifier {
34 private static final class Proxy implements Externalizable {
35 private static final long serialVersionUID = 1L;
36 private FrontendIdentifier frontendId;
37 private long generation;
39 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
40 // be able to create instances via reflection.
41 @SuppressWarnings("checkstyle:RedundantModifier")
43 // Needed for Externalizable
46 Proxy(final FrontendIdentifier frontendId, final long generation) {
47 this.frontendId = requireNonNull(frontendId);
48 this.generation = generation;
52 public void writeExternal(final ObjectOutput out) throws IOException {
53 frontendId.writeTo(out);
54 WritableObjects.writeLong(out, generation);
58 public void readExternal(final ObjectInput in) throws IOException {
59 frontendId = FrontendIdentifier.readFrom(in);
60 generation = WritableObjects.readLong(in);
63 private Object readResolve() {
64 return new ClientIdentifier(frontendId, generation);
68 private static final long serialVersionUID = 1L;
70 private final @NonNull FrontendIdentifier frontendId;
71 private final long generation;
73 ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
74 this.frontendId = requireNonNull(frontendId);
75 this.generation = generation;
78 public static @NonNull ClientIdentifier create(final FrontendIdentifier frontendId,
79 final long generation) {
80 return new ClientIdentifier(frontendId, generation);
83 public static @NonNull ClientIdentifier readFrom(final DataInput in) throws IOException {
84 final FrontendIdentifier frontendId = FrontendIdentifier.readFrom(in);
85 return new ClientIdentifier(frontendId, WritableObjects.readLong(in));
89 public void writeTo(final DataOutput out) throws IOException {
90 frontendId.writeTo(out);
91 WritableObjects.writeLong(out, generation);
94 public @NonNull FrontendIdentifier getFrontendId() {
98 public long getGeneration() {
102 public @NonNull ClientGeneration getYangGeneration() {
103 return new ClientGeneration(Uint64.fromLongBits(generation));
107 public int hashCode() {
108 return frontendId.hashCode() * 31 + Long.hashCode(generation);
112 public boolean equals(final Object obj) {
116 if (!(obj instanceof ClientIdentifier)) {
120 final ClientIdentifier other = (ClientIdentifier) obj;
121 return generation == other.generation && frontendId.equals(other.frontendId);
125 public String toString() {
126 return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
127 .add("generation", Long.toUnsignedString(generation)).toString();
130 private Object writeReplace() {
131 return new Proxy(frontendId, generation);