Do not leak DataTree from backend actor
[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.annotations.Beta;
16 import com.google.common.base.MoreObjects.ToStringHelper;
17 import com.google.common.collect.ImmutableList;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
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.api.schema.tree.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  * @author Robert Varga
33  */
34 @Beta
35 public final class ConnectClientSuccess extends RequestSuccess<ClientIdentifier, ConnectClientSuccess> {
36     private static final long serialVersionUID = 1L;
37
38     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
39             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
40             + "aren't serialized. FindBugs does not recognize this.")
41     private final @NonNull List<ActorSelection> alternates;
42
43     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See justification above.")
44     private final ReadOnlyDataTree dataTree;
45     private final @NonNull ActorRef backend;
46     private final int maxMessages;
47
48     ConnectClientSuccess(final ClientIdentifier target, final long sequence, final ActorRef backend,
49         final List<ActorSelection> alternates, final int maxMessages, final ReadOnlyDataTree dataTree) {
50         super(target, sequence);
51         this.backend = requireNonNull(backend);
52         this.alternates = ImmutableList.copyOf(alternates);
53         this.dataTree = dataTree;
54         checkArgument(maxMessages > 0, "Maximum messages has to be positive, not %s", maxMessages);
55         this.maxMessages = maxMessages;
56     }
57
58     public ConnectClientSuccess(final @NonNull ClientIdentifier target, final long sequence,
59             final @NonNull ActorRef backend, final @NonNull List<ActorSelection> alternates,
60             final @NonNull ReadOnlyDataTree dataTree, final int maxMessages) {
61         this(target, sequence, backend, alternates, maxMessages, requireNonNull(dataTree));
62     }
63
64     /**
65      * Return the list of known alternate backends. The client can use this list to perform recovery procedures.
66      *
67      * @return a list of known backend alternates
68      */
69     public @NonNull List<ActorSelection> getAlternates() {
70         return alternates;
71     }
72
73     public @NonNull ActorRef getBackend() {
74         return backend;
75     }
76
77     public Optional<ReadOnlyDataTree> getDataTree() {
78         return Optional.ofNullable(dataTree);
79     }
80
81     public int getMaxMessages() {
82         return maxMessages;
83     }
84
85     @Override
86     protected ConnectClientSuccessProxyV1 externalizableProxy(final ABIVersion version) {
87         return new ConnectClientSuccessProxyV1(this);
88     }
89
90     @Override
91     protected ConnectClientSuccess cloneAsVersion(final ABIVersion version) {
92         return this;
93     }
94
95     @Override
96     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
97         return super.addToStringAttributes(toStringHelper).add("alternates", alternates)
98                 .add("dataTree present", getDataTree().isPresent()).add("maxMessages", maxMessages);
99     }
100 }