ed04f031974c6915b323ac73ad009985d3b7aab0
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / ConnectClientSuccessProxyV1.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 akka.serialization.JavaSerializer;
13 import akka.serialization.Serialization;
14 import java.io.DataInput;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.io.Serial;
19 import java.util.ArrayList;
20 import java.util.List;
21 import org.opendaylight.controller.cluster.access.concepts.AbstractSuccessProxy;
22 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
23
24 /**
25  * Externalizable proxy for use with {@link ConnectClientSuccess}. It implements the initial (Boron) serialization
26  * format.
27  *
28  * @author Robert Varga
29  */
30 final class ConnectClientSuccessProxyV1 extends AbstractSuccessProxy<ClientIdentifier, ConnectClientSuccess> {
31     @Serial
32     private static final long serialVersionUID = 1L;
33
34     private List<ActorSelection> alternates;
35     private ActorRef backend;
36     private int maxMessages;
37
38     // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
39     // be able to create instances via reflection.
40     @SuppressWarnings("checkstyle:RedundantModifier")
41     public ConnectClientSuccessProxyV1() {
42         // For Externalizable
43     }
44
45     ConnectClientSuccessProxyV1(final ConnectClientSuccess success) {
46         super(success);
47         alternates = success.getAlternates();
48         backend = success.getBackend();
49         maxMessages = success.getMaxMessages();
50         // We are ignoring the DataTree, it is not serializable anyway
51     }
52
53     @Override
54     public void writeExternal(final ObjectOutput out) throws IOException {
55         super.writeExternal(out);
56
57         out.writeObject(Serialization.serializedActorPath(backend));
58         out.writeInt(maxMessages);
59
60         out.writeInt(alternates.size());
61         for (ActorSelection b : alternates) {
62             out.writeObject(b.toSerializationFormat());
63         }
64     }
65
66     @Override
67     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
68         super.readExternal(in);
69
70         backend = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
71         maxMessages = in.readInt();
72
73         final int alternatesSize = in.readInt();
74         alternates = new ArrayList<>(alternatesSize);
75         for (int i = 0; i < alternatesSize; ++i) {
76             alternates.add(ActorSelection.apply(ActorRef.noSender(), (String)in.readObject()));
77         }
78     }
79
80     @Override
81     protected ConnectClientSuccess createSuccess(final ClientIdentifier target, final long sequence) {
82         return new ConnectClientSuccess(target, sequence, backend, alternates, maxMessages, null);
83     }
84
85     @Override
86     protected ClientIdentifier readTarget(final DataInput in) throws IOException {
87         return ClientIdentifier.readFrom(in);
88     }
89 }