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