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