More cds-access-api cleanup
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / ConnectClientSuccess.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.commands;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSelection;
15 import com.google.common.base.MoreObjects.ToStringHelper;
16 import com.google.common.collect.ImmutableList;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18 import java.io.Serial;
19 import java.util.List;
20 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.controller.cluster.access.ABIVersion;
23 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
24 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
25 import org.opendaylight.yangtools.yang.data.tree.api.ReadOnlyDataTree;
26
27 /**
28  * Successful reply to an {@link ConnectClientRequest}. Client actor which initiated this connection should use
29  * the version reported via {@link #getVersion()} of this message to communicate with this backend. Should this backend
30  * fail, the client can try accessing the provided alternates.
31  */
32 public final class ConnectClientSuccess extends RequestSuccess<ClientIdentifier, ConnectClientSuccess> {
33     @Serial
34     private static final long serialVersionUID = 1L;
35
36     private final @NonNull ImmutableList<ActorSelection> alternates;
37
38     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See justification above.")
39     private final ReadOnlyDataTree dataTree;
40     private final @NonNull ActorRef backend;
41     private final int maxMessages;
42
43     ConnectClientSuccess(final ClientIdentifier target, final long sequence, final ActorRef backend,
44         final List<ActorSelection> alternates, final int maxMessages, final ReadOnlyDataTree dataTree) {
45         super(target, sequence);
46         this.backend = requireNonNull(backend);
47         this.alternates = ImmutableList.copyOf(alternates);
48         this.dataTree = dataTree;
49         checkArgument(maxMessages > 0, "Maximum messages has to be positive, not %s", maxMessages);
50         this.maxMessages = maxMessages;
51     }
52
53     public ConnectClientSuccess(final @NonNull ClientIdentifier target, final long sequence,
54             final @NonNull ActorRef backend, final @NonNull List<ActorSelection> alternates,
55             final @NonNull ReadOnlyDataTree dataTree, final int maxMessages) {
56         this(target, sequence, backend, alternates, maxMessages, requireNonNull(dataTree));
57     }
58
59     /**
60      * Return the list of known alternate backends. The client can use this list to perform recovery procedures.
61      *
62      * @return a list of known backend alternates
63      */
64     public @NonNull List<ActorSelection> getAlternates() {
65         return alternates;
66     }
67
68     public @NonNull ActorRef getBackend() {
69         return backend;
70     }
71
72     public Optional<ReadOnlyDataTree> getDataTree() {
73         return Optional.ofNullable(dataTree);
74     }
75
76     public int getMaxMessages() {
77         return maxMessages;
78     }
79
80     @Override
81     protected ConnectClientSuccessProxyV1 externalizableProxy(final ABIVersion version) {
82         return new ConnectClientSuccessProxyV1(this);
83     }
84
85     @Override
86     protected ConnectClientSuccess cloneAsVersion(final ABIVersion version) {
87         return this;
88     }
89
90     @Override
91     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
92         return super.addToStringAttributes(toStringHelper).add("alternates", alternates)
93                 .add("dataTree present", getDataTree().isPresent()).add("maxMessages", maxMessages);
94     }
95 }