Improve segmented journal actor metrics
[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 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;
34
35 /**
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.
39  */
40 public final class ConnectClientSuccess extends RequestSuccess<ClientIdentifier, ConnectClientSuccess> {
41     interface SerialForm extends RequestSuccess.SerialForm<ClientIdentifier, ConnectClientSuccess> {
42         @Override
43         default ClientIdentifier readTarget(final DataInput in) throws IOException {
44             return ClientIdentifier.readFrom(in);
45         }
46
47         @Override
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();
53
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()));
58             }
59
60             return new ConnectClientSuccess(target, sequence, backend, alternates, maxMessages, null);
61         }
62
63         @Override
64         default void writeExternal(final ObjectOutput out, final ConnectClientSuccess msg) throws IOException {
65             out.writeObject(Serialization.serializedActorPath(msg.backend));
66             out.writeInt(msg.maxMessages);
67
68             out.writeInt(msg.alternates.size());
69             for (ActorSelection b : msg.alternates) {
70                 out.writeObject(b.toSerializationFormat());
71             }
72
73             // We are ignoring the DataTree, it is not serializable anyway
74         }
75     }
76
77     @java.io.Serial
78     private static final long serialVersionUID = 1L;
79
80     private final @NonNull ImmutableList<ActorSelection> alternates;
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 new CCS(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
146     @java.io.Serial
147     private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
148         throwNSE();
149     }
150
151     @java.io.Serial
152     private void readObjectNoData() throws ObjectStreamException {
153         throwNSE();
154     }
155
156     @java.io.Serial
157     private void writeObject(final ObjectOutputStream stream) throws IOException {
158         throwNSE();
159     }
160 }