Remove config module archetype
[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(@Nonnull final ClientIdentifier target, final long sequence,
51             @Nonnull final ActorRef backend, @Nonnull final List<ActorSelection> alternates,
52             @Nonnull final 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     @Nonnull
62     public List<ActorSelection> getAlternates() {
63         return alternates;
64     }
65
66     @Nonnull
67     public ActorRef getBackend() {
68         return backend;
69     }
70
71     public Optional<DataTree> getDataTree() {
72         return Optional.ofNullable(dataTree);
73     }
74
75     public int getMaxMessages() {
76         return maxMessages;
77     }
78
79     @Override
80     protected ConnectClientSuccessProxyV1 externalizableProxy(final ABIVersion version) {
81         return new ConnectClientSuccessProxyV1(this);
82     }
83
84     @Override
85     protected ConnectClientSuccess cloneAsVersion(final ABIVersion version) {
86         return this;
87     }
88
89     @Override
90     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
91         return super.addToStringAttributes(toStringHelper).add("alternates", alternates).add("dataTree", dataTree)
92                 .add("maxMessages", maxMessages);
93     }
94 }