BUG-8494: do not attempt to reconnect ReconnectingClientConnection
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / ConnectionEntry.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.client;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Preconditions;
14 import java.util.function.Consumer;
15 import org.opendaylight.controller.cluster.access.concepts.Request;
16 import org.opendaylight.controller.cluster.access.concepts.Response;
17 import org.opendaylight.yangtools.concepts.Immutable;
18
19 /**
20  * Single entry in a {@link AbstractClientConnection}. Tracks the request, the associated callback and time when
21  * the request was first enqueued.
22  *
23  * @author Robert Varga
24  */
25 @Beta
26 public class ConnectionEntry implements Immutable {
27     private final Consumer<Response<?, ?>> callback;
28     private final Request<?, ?> request;
29     private final long enqueuedTicks;
30
31     ConnectionEntry(final Request<?, ?> request, final Consumer<Response<?, ?>> callback, final long now) {
32         this.request = Preconditions.checkNotNull(request);
33         this.callback = Preconditions.checkNotNull(callback);
34         this.enqueuedTicks = now;
35     }
36
37     ConnectionEntry(final ConnectionEntry entry) {
38         this(entry.request, entry.callback, entry.enqueuedTicks);
39     }
40
41     public final Consumer<Response<?, ?>> getCallback() {
42         return callback;
43     }
44
45     public final Request<?, ?> getRequest() {
46         return request;
47     }
48
49     public void complete(final Response<?, ?> response) {
50         callback.accept(response);
51     }
52
53     public final long getEnqueuedTicks() {
54         return enqueuedTicks;
55     }
56
57     @Override
58     public final String toString() {
59         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
60     }
61
62     ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
63         return toStringHelper.add("request", request).add("enqueuedTicks", enqueuedTicks);
64     }
65 }