Fix FindBugs warnings in sal-akk-raft
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.io.ByteArrayOutputStream;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.ObjectOutputStream;
18 import java.util.ArrayList;
19 import java.util.List;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
22 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.PersistentPayload;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Payload data for server configuration log entries.
28  *
29  * @author Thomas Pantelis
30  */
31 public final class ServerConfigurationPayload extends Payload implements PersistentPayload, MigratedSerializable {
32     private static final class Proxy implements Externalizable {
33         private static final long serialVersionUID = 1L;
34
35         private List<ServerInfo> serverConfig;
36
37         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
38         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
39         @SuppressWarnings("checkstyle:RedundantModifier")
40         public Proxy() {
41             // For Externalizable
42         }
43
44         Proxy(final ServerConfigurationPayload payload) {
45             this.serverConfig = payload.getServerConfig();
46         }
47
48         @Override
49         public void writeExternal(final ObjectOutput out) throws IOException {
50             out.writeInt(serverConfig.size());
51             for (ServerInfo i : serverConfig) {
52                 out.writeObject(i.getId());
53                 out.writeBoolean(i.isVoting());
54             }
55         }
56
57         @Override
58         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
59             final int size = in.readInt();
60             serverConfig = new ArrayList<>(size);
61             for (int i = 0; i < size; ++i) {
62                 final String id = (String) in.readObject();
63                 final boolean voting = in.readBoolean();
64                 serverConfig.add(new ServerInfo(id, voting));
65             }
66         }
67
68         private Object readResolve() {
69             return new ServerConfigurationPayload(serverConfig);
70         }
71     }
72
73     private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationPayload.class);
74     private static final long serialVersionUID = 1L;
75
76     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
77             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
78             + "aren't serialized. FindBugs does not recognize this.")
79     private final List<ServerInfo> serverConfig;
80     private final boolean migrated;
81     private int serializedSize = -1;
82
83     private ServerConfigurationPayload(@Nonnull final List<ServerInfo> serverConfig, boolean migrated) {
84         this.serverConfig = ImmutableList.copyOf(serverConfig);
85         this.migrated = migrated;
86     }
87
88     public ServerConfigurationPayload(@Nonnull final List<ServerInfo> serverConfig) {
89         this(serverConfig, false);
90     }
91
92     @Deprecated
93     public static ServerConfigurationPayload createMigrated(@Nonnull final List<ServerInfo> serverConfig) {
94         return new ServerConfigurationPayload(serverConfig, true);
95     }
96
97     @Deprecated
98     @Override
99     public boolean isMigrated() {
100         return migrated;
101     }
102
103     @Nonnull
104     public List<ServerInfo> getServerConfig() {
105         return serverConfig;
106     }
107
108     @Override
109     public int size() {
110         if (serializedSize < 0) {
111             try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
112                 try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
113                     out.writeObject(writeReplace());
114                 }
115
116                 serializedSize = bos.toByteArray().length;
117             } catch (IOException e) {
118                 serializedSize = 0;
119                 LOG.error("Error serializing", e);
120             }
121         }
122
123         return serializedSize;
124     }
125
126     @Override
127     public String toString() {
128         return "ServerConfigurationPayload [serverConfig=" + serverConfig + "]";
129     }
130
131     @Override
132     public Object writeReplace() {
133         return new Proxy(this);
134     }
135 }