Add new cds-access-api proxies
[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 akka.serialization.JavaSerializer;
16 import akka.serialization.Serialization;
17 import com.google.common.base.MoreObjects.ToStringHelper;
18 import com.google.common.collect.ImmutableList;
19 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20 import java.io.DataInput;
21 import java.io.IOException;
22 import java.io.ObjectInput;
23 import java.io.ObjectOutput;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Optional;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.opendaylight.controller.cluster.access.ABIVersion;
29 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
30 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
31 import org.opendaylight.yangtools.yang.data.tree.api.ReadOnlyDataTree;
32
33 /**
34  * Successful reply to an {@link ConnectClientRequest}. Client actor which initiated this connection should use
35  * the version reported via {@link #getVersion()} of this message to communicate with this backend. Should this backend
36  * fail, the client can try accessing the provided alternates.
37  */
38 public final class ConnectClientSuccess extends RequestSuccess<ClientIdentifier, ConnectClientSuccess> {
39     interface SerialForm extends RequestSuccess.SerialForm<ClientIdentifier, ConnectClientSuccess> {
40         @Override
41         default ClientIdentifier readTarget(final DataInput in) throws IOException {
42             return ClientIdentifier.readFrom(in);
43         }
44
45         @Override
46         default ConnectClientSuccess readExternal(final ObjectInput in, final ClientIdentifier target,
47                 final long sequence) throws IOException, ClassNotFoundException {
48             final var backend = JavaSerializer.currentSystem().value().provider()
49                 .resolveActorRef((String) in.readObject());
50             final var maxMessages = in.readInt();
51
52             final int alternatesSize = in.readInt();
53             final var alternates = new ArrayList<ActorSelection>(alternatesSize);
54             for (int i = 0; i < alternatesSize; ++i) {
55                 alternates.add(ActorSelection.apply(ActorRef.noSender(), (String)in.readObject()));
56             }
57
58             return new ConnectClientSuccess(target, sequence, backend, alternates, maxMessages, null);
59         }
60
61         @Override
62         default void writeExternal(final ObjectOutput out, final ConnectClientSuccess msg) throws IOException {
63             out.writeObject(Serialization.serializedActorPath(msg.backend));
64             out.writeInt(msg.maxMessages);
65
66             out.writeInt(msg.alternates.size());
67             for (ActorSelection b : msg.alternates) {
68                 out.writeObject(b.toSerializationFormat());
69             }
70
71             // We are ignoring the DataTree, it is not serializable anyway
72         }
73     }
74
75     @java.io.Serial
76     private static final long serialVersionUID = 1L;
77
78     private final @NonNull ImmutableList<ActorSelection> alternates;
79
80     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See justification above.")
81     private final ReadOnlyDataTree dataTree;
82     private final @NonNull ActorRef backend;
83     private final int maxMessages;
84
85     private ConnectClientSuccess(final ConnectClientSuccess success, final ABIVersion version) {
86         super(success, version);
87         alternates = success.alternates;
88         dataTree = success.dataTree;
89         backend = success.backend;
90         maxMessages = success.maxMessages;
91     }
92
93     ConnectClientSuccess(final ClientIdentifier target, final long sequence, final ActorRef backend,
94         final List<ActorSelection> alternates, final int maxMessages, final ReadOnlyDataTree dataTree) {
95         super(target, sequence);
96         this.backend = requireNonNull(backend);
97         this.alternates = ImmutableList.copyOf(alternates);
98         this.dataTree = dataTree;
99         checkArgument(maxMessages > 0, "Maximum messages has to be positive, not %s", maxMessages);
100         this.maxMessages = maxMessages;
101     }
102
103     public ConnectClientSuccess(final @NonNull ClientIdentifier target, final long sequence,
104             final @NonNull ActorRef backend, final @NonNull List<ActorSelection> alternates,
105             final @NonNull ReadOnlyDataTree dataTree, final int maxMessages) {
106         this(target, sequence, backend, alternates, maxMessages, requireNonNull(dataTree));
107     }
108
109     /**
110      * Return the list of known alternate backends. The client can use this list to perform recovery procedures.
111      *
112      * @return a list of known backend alternates
113      */
114     public @NonNull List<ActorSelection> getAlternates() {
115         return alternates;
116     }
117
118     public @NonNull ActorRef getBackend() {
119         return backend;
120     }
121
122     public Optional<ReadOnlyDataTree> getDataTree() {
123         return Optional.ofNullable(dataTree);
124     }
125
126     public int getMaxMessages() {
127         return maxMessages;
128     }
129
130     @Override
131     protected SerialForm externalizableProxy(final ABIVersion version) {
132         return ABIVersion.MAGNESIUM.lt(version) ? new CCS(this) : new ConnectClientSuccessProxyV1(this);
133     }
134
135     @Override
136     protected ConnectClientSuccess cloneAsVersion(final ABIVersion version) {
137         return new ConnectClientSuccess(this, version);
138     }
139
140     @Override
141     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
142         return super.addToStringAttributes(toStringHelper).add("alternates", alternates)
143                 .add("dataTree present", getDataTree().isPresent()).add("maxMessages", maxMessages);
144     }
145 }