Add the ability to report known connected clients
[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.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;
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 @Beta
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;
38
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")
42         public Proxy() {
43             // Needed for Externalizable
44         }
45
46         Proxy(final FrontendIdentifier frontendId, final long generation) {
47             this.frontendId = requireNonNull(frontendId);
48             this.generation = generation;
49         }
50
51         @Override
52         public void writeExternal(final ObjectOutput out) throws IOException {
53             frontendId.writeTo(out);
54             WritableObjects.writeLong(out, generation);
55         }
56
57         @Override
58         public void readExternal(final ObjectInput in) throws IOException {
59             frontendId = FrontendIdentifier.readFrom(in);
60             generation = WritableObjects.readLong(in);
61         }
62
63         private Object readResolve() {
64             return new ClientIdentifier(frontendId, generation);
65         }
66     }
67
68     private static final long serialVersionUID = 1L;
69
70     private final @NonNull FrontendIdentifier frontendId;
71     private final long generation;
72
73     ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
74         this.frontendId = requireNonNull(frontendId);
75         this.generation = generation;
76     }
77
78     public static @NonNull ClientIdentifier create(final FrontendIdentifier frontendId,
79             final long generation) {
80         return new ClientIdentifier(frontendId, generation);
81     }
82
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));
86     }
87
88     @Override
89     public void writeTo(final DataOutput out) throws IOException {
90         frontendId.writeTo(out);
91         WritableObjects.writeLong(out, generation);
92     }
93
94     public @NonNull FrontendIdentifier getFrontendId() {
95         return frontendId;
96     }
97
98     public long getGeneration() {
99         return generation;
100     }
101
102     public @NonNull ClientGeneration getYangGeneration() {
103         return new ClientGeneration(Uint64.fromLongBits(generation));
104     }
105
106     @Override
107     public int hashCode() {
108         return frontendId.hashCode() * 31 + Long.hashCode(generation);
109     }
110
111     @Override
112     public boolean equals(final Object obj) {
113         if (this == obj) {
114             return true;
115         }
116         if (!(obj instanceof ClientIdentifier)) {
117             return false;
118         }
119
120         final ClientIdentifier other = (ClientIdentifier) obj;
121         return generation == other.generation && frontendId.equals(other.frontendId);
122     }
123
124     @Override
125     public String toString() {
126         return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
127                 .add("generation", Long.toUnsignedString(generation)).toString();
128     }
129
130     private Object writeReplace() {
131         return new Proxy(frontendId, generation);
132     }
133 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.