2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.commands;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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 java.io.DataInput;
20 import java.io.IOException;
21 import java.io.ObjectInput;
22 import java.io.ObjectInputStream;
23 import java.io.ObjectOutput;
24 import java.io.ObjectOutputStream;
25 import java.io.ObjectStreamException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Optional;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.opendaylight.controller.cluster.access.ABIVersion;
31 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
32 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
33 import org.opendaylight.yangtools.yang.data.tree.api.ReadOnlyDataTree;
36 * Successful reply to an {@link ConnectClientRequest}. Client actor which initiated this connection should use
37 * the version reported via {@link #getVersion()} of this message to communicate with this backend. Should this backend
38 * fail, the client can try accessing the provided alternates.
40 public final class ConnectClientSuccess extends RequestSuccess<ClientIdentifier, ConnectClientSuccess> {
41 interface SerialForm extends RequestSuccess.SerialForm<ClientIdentifier, ConnectClientSuccess> {
43 default ClientIdentifier readTarget(final DataInput in) throws IOException {
44 return ClientIdentifier.readFrom(in);
48 default ConnectClientSuccess readExternal(final ObjectInput in, final ClientIdentifier target,
49 final long sequence) throws IOException, ClassNotFoundException {
50 final var backend = JavaSerializer.currentSystem().value().provider()
51 .resolveActorRef((String) in.readObject());
52 final var maxMessages = in.readInt();
54 final int alternatesSize = in.readInt();
55 final var alternates = new ArrayList<ActorSelection>(alternatesSize);
56 for (int i = 0; i < alternatesSize; ++i) {
57 alternates.add(ActorSelection.apply(ActorRef.noSender(), (String)in.readObject()));
60 return new ConnectClientSuccess(target, sequence, backend, alternates, maxMessages, null);
64 default void writeExternal(final ObjectOutput out, final ConnectClientSuccess msg) throws IOException {
65 out.writeObject(Serialization.serializedActorPath(msg.backend));
66 out.writeInt(msg.maxMessages);
68 out.writeInt(msg.alternates.size());
69 for (ActorSelection b : msg.alternates) {
70 out.writeObject(b.toSerializationFormat());
73 // We are ignoring the DataTree, it is not serializable anyway
78 private static final long serialVersionUID = 1L;
80 private final @NonNull ImmutableList<ActorSelection> alternates;
81 private final ReadOnlyDataTree dataTree;
82 private final @NonNull ActorRef backend;
83 private final int maxMessages;
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;
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;
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));
110 * Return the list of known alternate backends. The client can use this list to perform recovery procedures.
112 * @return a list of known backend alternates
114 public @NonNull List<ActorSelection> getAlternates() {
118 public @NonNull ActorRef getBackend() {
122 public Optional<ReadOnlyDataTree> getDataTree() {
123 return Optional.ofNullable(dataTree);
126 public int getMaxMessages() {
131 protected SerialForm externalizableProxy(final ABIVersion version) {
132 return new CCS(this);
136 protected ConnectClientSuccess cloneAsVersion(final ABIVersion version) {
137 return new ConnectClientSuccess(this, version);
141 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
142 return super.addToStringAttributes(toStringHelper).add("alternates", alternates)
143 .add("dataTree present", getDataTree().isPresent()).add("maxMessages", maxMessages);
147 private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
152 private void readObjectNoData() throws ObjectStreamException {
157 private void writeObject(final ObjectOutputStream stream) throws IOException {