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