BUG-5280: add client connect messages
[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
39     ConnectClientSuccess(final ClientIdentifier target, final ActorRef backend, final List<ActorSelection> alternates,
40         final Optional<DataTree> dataTree) {
41         super(target);
42         this.backend = Preconditions.checkNotNull(backend);
43         this.alternates = ImmutableList.copyOf(alternates);
44         this.dataTree = dataTree.orElse(null);
45     }
46
47     public ConnectClientSuccess(final @Nonnull ClientIdentifier target, final @Nonnull ActorRef backend,
48             final @Nonnull List<ActorSelection> alternates,
49             final @Nonnull DataTree dataTree) {
50         this(target, backend, alternates, Optional.of(dataTree));
51     }
52
53     /**
54      * Return the list of known alternate backends. The client can use this list to perform recovery procedures.
55      *
56      * @return a list of known backend alternates
57      */
58     public @Nonnull List<ActorSelection> getAlternates() {
59         return alternates;
60     }
61
62     public @Nonnull ActorRef getBackend() {
63         return backend;
64     }
65
66     public Optional<DataTree> getDataTree() {
67         return Optional.ofNullable(dataTree);
68     }
69
70     @Override
71     protected ConnectClientSuccessProxyV1 externalizableProxy(final ABIVersion version) {
72         return new ConnectClientSuccessProxyV1(this);
73     }
74
75     @Override
76     protected ConnectClientSuccess cloneAsVersion(final ABIVersion version) {
77         return this;
78     }
79
80     @Override
81     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
82         return super.addToStringAttributes(toStringHelper).add("alternates", alternates).add("dataTree", dataTree);
83     }
84 }