Move ServerConfigurationPayload to cluster.raft.persisted
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / ServerConfigurationPayload.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.raft.persisted;
9
10 import com.google.common.collect.ImmutableList;
11 import java.io.ByteArrayOutputStream;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.io.ObjectOutputStream;
17 import java.util.ArrayList;
18 import java.util.List;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
21 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.PersistentPayload;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Payload data for server configuration log entries.
27  *
28  * @author Thomas Pantelis
29  */
30 public final class ServerConfigurationPayload extends Payload implements PersistentPayload, MigratedSerializable {
31     private static final class Proxy implements Externalizable {
32         private static final long serialVersionUID = 1L;
33
34         private List<ServerInfo> serverConfig;
35
36         public Proxy() {
37             // For Externalizable
38         }
39
40         Proxy(final ServerConfigurationPayload payload) {
41             this.serverConfig = payload.getServerConfig();
42         }
43
44         @Override
45         public void writeExternal(final ObjectOutput out) throws IOException {
46             out.writeInt(serverConfig.size());
47             for (ServerInfo i : serverConfig) {
48                 out.writeObject(i.getId());
49                 out.writeBoolean(i.isVoting());
50             }
51          }
52
53         @Override
54         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
55             final int size = in.readInt();
56             serverConfig = new ArrayList<>(size);
57             for (int i = 0; i < size; ++i) {
58                 final String id = (String) in.readObject();
59                 final boolean voting = in.readBoolean();
60                 serverConfig.add(new ServerInfo(id, voting));
61             }
62         }
63
64         private Object readResolve() {
65             return new ServerConfigurationPayload(serverConfig);
66         }
67     }
68
69     private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationPayload.class);
70     private static final long serialVersionUID = 1L;
71
72     private final List<ServerInfo> serverConfig;
73     private final boolean migrated;
74     private int serializedSize = -1;
75
76     private ServerConfigurationPayload(final @Nonnull List<ServerInfo> serverConfig, boolean migrated) {
77         this.serverConfig = ImmutableList.copyOf(serverConfig);
78         this.migrated = migrated;
79     }
80
81     public ServerConfigurationPayload(final @Nonnull List<ServerInfo> serverConfig) {
82         this(serverConfig, false);
83     }
84
85     @Deprecated
86     public static ServerConfigurationPayload createMigrated(final @Nonnull List<ServerInfo> serverConfig) {
87         return new ServerConfigurationPayload(serverConfig, true);
88     }
89
90     @Deprecated
91     @Override
92     public boolean isMigrated() {
93         return migrated;
94     }
95
96     public @Nonnull List<ServerInfo> getServerConfig() {
97         return serverConfig;
98     }
99
100     @Override
101     public int size() {
102         if (serializedSize < 0) {
103             try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
104                 try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
105                     out.writeObject(writeReplace());
106                 }
107
108                 serializedSize = bos.toByteArray().length;
109             } catch (IOException e) {
110                 serializedSize = 0;
111                 LOG.error("Error serializing", e);
112             }
113         }
114
115         return serializedSize;
116     }
117
118     @Override
119     public String toString() {
120         return "ServerConfigurationPayload [serverConfig=" + serverConfig + "]";
121     }
122
123     @Override
124     public Object writeReplace() {
125         return new Proxy(this);
126     }
127 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.