6c9ae2a19ad4eb4840d9e5ab903b98d4995f286a
[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 java.util.Optional;
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     private static final long serialVersionUID = 1L;
32
33     private List<ActorSelection> alternates;
34     private ActorRef backend;
35     private long maxMessages;
36
37     public ConnectClientSuccessProxyV1() {
38         // For Externalizable
39     }
40
41     ConnectClientSuccessProxyV1(final ConnectClientSuccess success) {
42         super(success);
43         this.alternates = success.getAlternates();
44         this.backend = success.getBackend();
45         // We are ignoring the DataTree, it is not serializable anyway
46     }
47
48     @Override
49     public void writeExternal(final ObjectOutput out) throws IOException {
50         super.writeExternal(out);
51
52         out.writeUTF(Serialization.serializedActorPath(backend));
53
54         out.writeInt(alternates.size());
55         for (ActorSelection b : alternates) {
56             out.writeObject(b.toSerializationFormat());
57         }
58
59         out.writeLong(maxMessages);
60     }
61
62     @Override
63     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
64         super.readExternal(in);
65
66         backend = JavaSerializer.currentSystem().value().provider().resolveActorRef(in.readUTF());
67
68         final int backendsSize = in.readInt();
69         if (backendsSize < 1) {
70             throw new IOException("Illegal number of backends " + backendsSize);
71         }
72
73         alternates = new ArrayList<>(backendsSize);
74         for (int i = 0; i < backendsSize; ++i) {
75             alternates.add(ActorSelection.apply(ActorRef.noSender(), (String)in.readObject()));
76         }
77
78         maxMessages = in.readLong();
79     }
80
81     @Override
82     protected ConnectClientSuccess createSuccess(final ClientIdentifier target) {
83         return new ConnectClientSuccess(target, backend, alternates, Optional.empty(), maxMessages);
84     }
85
86     @Override
87     protected ClientIdentifier readTarget(final DataInput in) throws IOException {
88         return ClientIdentifier.readFrom(in);
89     }
90 }