65389ec17c4ca589ecc15c4895c2c69471eb9b2e
[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 int maxMessages;
39
40     ConnectClientSuccess(final ClientIdentifier target, final long sequence, final ActorRef backend,
41         final List<ActorSelection> alternates, final Optional<DataTree> dataTree, final int maxMessages) {
42         super(target, sequence);
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 long sequence,
51             final @Nonnull ActorRef backend, final @Nonnull List<ActorSelection> alternates,
52             final @Nonnull DataTree dataTree, final int maxMessages) {
53         this(target, sequence, backend, alternates, Optional.of(dataTree), maxMessages);
54     }
55
56     /**
57      * Return the list of known alternate backends. The client can use this list to perform recovery procedures.
58      *
59      * @return a list of known backend alternates
60      */
61     public @Nonnull List<ActorSelection> getAlternates() {
62         return alternates;
63     }
64
65     public @Nonnull ActorRef getBackend() {
66         return backend;
67     }
68
69     public Optional<DataTree> getDataTree() {
70         return Optional.ofNullable(dataTree);
71     }
72
73     public int getMaxMessages() {
74         return maxMessages;
75     }
76
77     @Override
78     protected ConnectClientSuccessProxyV1 externalizableProxy(final ABIVersion version) {
79         return new ConnectClientSuccessProxyV1(this);
80     }
81
82     @Override
83     protected ConnectClientSuccess cloneAsVersion(final ABIVersion version) {
84         return this;
85     }
86
87     @Override
88     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
89         return super.addToStringAttributes(toStringHelper).add("alternates", alternates).add("dataTree", dataTree)
90                 .add("maxMessages", maxMessages);
91     }
92 }