0f206b5d39d5b70f314801919f05292c683fa245
[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         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
37         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
38         @SuppressWarnings("checkstyle:RedundantModifier")
39         public Proxy() {
40             // For Externalizable
41         }
42
43         Proxy(final ServerConfigurationPayload payload) {
44             this.serverConfig = payload.getServerConfig();
45         }
46
47         @Override
48         public void writeExternal(final ObjectOutput out) throws IOException {
49             out.writeInt(serverConfig.size());
50             for (ServerInfo i : serverConfig) {
51                 out.writeObject(i.getId());
52                 out.writeBoolean(i.isVoting());
53             }
54         }
55
56         @Override
57         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
58             final int size = in.readInt();
59             serverConfig = new ArrayList<>(size);
60             for (int i = 0; i < size; ++i) {
61                 final String id = (String) in.readObject();
62                 final boolean voting = in.readBoolean();
63                 serverConfig.add(new ServerInfo(id, voting));
64             }
65         }
66
67         private Object readResolve() {
68             return new ServerConfigurationPayload(serverConfig);
69         }
70     }
71
72     private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationPayload.class);
73     private static final long serialVersionUID = 1L;
74
75     private final List<ServerInfo> serverConfig;
76     private final boolean migrated;
77     private int serializedSize = -1;
78
79     private ServerConfigurationPayload(final @Nonnull List<ServerInfo> serverConfig, boolean migrated) {
80         this.serverConfig = ImmutableList.copyOf(serverConfig);
81         this.migrated = migrated;
82     }
83
84     public ServerConfigurationPayload(final @Nonnull List<ServerInfo> serverConfig) {
85         this(serverConfig, false);
86     }
87
88     @Deprecated
89     public static ServerConfigurationPayload createMigrated(final @Nonnull List<ServerInfo> serverConfig) {
90         return new ServerConfigurationPayload(serverConfig, true);
91     }
92
93     @Deprecated
94     @Override
95     public boolean isMigrated() {
96         return migrated;
97     }
98
99     public @Nonnull List<ServerInfo> getServerConfig() {
100         return serverConfig;
101     }
102
103     @Override
104     public int size() {
105         if (serializedSize < 0) {
106             try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
107                 try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
108                     out.writeObject(writeReplace());
109                 }
110
111                 serializedSize = bos.toByteArray().length;
112             } catch (IOException e) {
113                 serializedSize = 0;
114                 LOG.error("Error serializing", e);
115             }
116         }
117
118         return serializedSize;
119     }
120
121     @Override
122     public String toString() {
123         return "ServerConfigurationPayload [serverConfig=" + serverConfig + "]";
124     }
125
126     @Override
127     public Object writeReplace() {
128         return new Proxy(this);
129     }
130 }