BUG-5280: introduce Request/Response and related concepts
[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 com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
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 org.opendaylight.yangtools.concepts.WritableIdentifier;
20 import org.opendaylight.yangtools.concepts.WritableObjects;
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  * @author Robert Varga
27  */
28 @Beta
29 public final class ClientIdentifier implements WritableIdentifier {
30     private static final class Proxy implements Externalizable {
31         private static final long serialVersionUID = 1L;
32         private FrontendIdentifier frontendId;
33         private long generation;
34
35         public Proxy() {
36             // Needed for Externalizable
37         }
38
39         Proxy(final FrontendIdentifier frontendId, final long generation) {
40             this.frontendId = Preconditions.checkNotNull(frontendId);
41             this.generation = generation;
42         }
43
44         @Override
45         public void writeExternal(final ObjectOutput out) throws IOException {
46             frontendId.writeTo(out);
47             WritableObjects.writeLong(out, generation);
48         }
49
50         @Override
51         public void readExternal(final ObjectInput in) throws IOException {
52             frontendId = FrontendIdentifier.readFrom(in);
53             generation = WritableObjects.readLong(in);
54         }
55
56         private Object readResolve() {
57             return new ClientIdentifier(frontendId, generation);
58         }
59     }
60
61     private static final long serialVersionUID = 1L;
62     private final FrontendIdentifier frontendId;
63     private final long generation;
64
65     ClientIdentifier(final FrontendIdentifier frontendId, final long generation) {
66         this.frontendId = Preconditions.checkNotNull(frontendId);
67         this.generation = generation;
68     }
69
70     public static ClientIdentifier create(final FrontendIdentifier frontendId,
71             final long generation) {
72         return new ClientIdentifier(frontendId, generation);
73     }
74
75     public static ClientIdentifier readFrom(final DataInput in) throws IOException {
76         final FrontendIdentifier frontendId = FrontendIdentifier.readFrom(in);
77         return new ClientIdentifier(frontendId, WritableObjects.readLong(in));
78     }
79
80     @Override
81     public void writeTo(final DataOutput out) throws IOException {
82         frontendId.writeTo(out);
83         WritableObjects.writeLong(out, generation);
84     }
85
86     public FrontendIdentifier getFrontendId() {
87         return frontendId;
88     }
89
90     public long getGeneration() {
91         return generation;
92     }
93
94     @Override
95     public int hashCode() {
96         return frontendId.hashCode() * 31 + Long.hashCode(generation);
97     }
98
99     @Override
100     public boolean equals(final Object o) {
101         if (this == o) {
102             return true;
103         }
104         if (!(o instanceof ClientIdentifier)) {
105             return false;
106         }
107
108         final ClientIdentifier other = (ClientIdentifier) o;
109         return generation == other.generation && frontendId.equals(other.frontendId);
110     }
111
112     @Override
113     public String toString() {
114         return MoreObjects.toStringHelper(ClientIdentifier.class).add("frontend", frontendId)
115                 .add("generation", Long.toUnsignedString(generation)).toString();
116     }
117
118     private Object writeReplace() {
119         return new Proxy(frontendId, generation);
120     }
121 }