Fix followerDistributedDataStore tear down
[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.List;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.controller.cluster.raft.messages.Payload;
21 import org.opendaylight.controller.cluster.raft.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 {
31     private static final class Proxy implements Externalizable {
32         @java.io.Serial
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             serverConfig = payload.getServerConfig();
46         }
47
48         @Override
49         public void writeExternal(final ObjectOutput out) throws IOException {
50             out.writeInt(serverConfig.size());
51             for (var serverInfo : serverConfig) {
52                 out.writeObject(serverInfo.peerId());
53                 out.writeBoolean(serverInfo.isVoting());
54             }
55         }
56
57         @Override
58         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
59             final int size = in.readInt();
60
61             final var builder = ImmutableList.<ServerInfo>builderWithExpectedSize(size);
62             for (int i = 0; i < size; ++i) {
63                 final String id = (String) in.readObject();
64                 final boolean voting = in.readBoolean();
65                 builder.add(new ServerInfo(id, voting));
66             }
67             serverConfig = builder.build();
68         }
69
70         @java.io.Serial
71         private Object readResolve() {
72             return new ServerConfigurationPayload(serverConfig);
73         }
74     }
75
76     private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationPayload.class);
77     @java.io.Serial
78     private static final long serialVersionUID = 1L;
79
80     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
81             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
82             + "aren't serialized. FindBugs does not recognize this.")
83     private final List<ServerInfo> serverConfig;
84     private int serializedSize = -1;
85
86     public ServerConfigurationPayload(final @NonNull List<ServerInfo> serverConfig) {
87         this.serverConfig = ImmutableList.copyOf(serverConfig);
88     }
89
90     public @NonNull List<ServerInfo> getServerConfig() {
91         return serverConfig;
92     }
93
94     @Override
95     public int size() {
96         return serializedSize();
97     }
98
99     @Override
100     public int serializedSize() {
101         if (serializedSize < 0) {
102             try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
103                 try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
104                     out.writeObject(writeReplace());
105                 }
106
107                 serializedSize = bos.toByteArray().length;
108             } catch (IOException e) {
109                 serializedSize = 0;
110                 LOG.error("Error serializing", e);
111             }
112         }
113
114         return serializedSize;
115     }
116
117     @Override
118     public int hashCode() {
119         return serverConfig.hashCode();
120     }
121
122     @Override
123     public boolean equals(final Object obj) {
124         return this == obj || obj instanceof ServerConfigurationPayload other
125             && serverConfig.equals(other.serverConfig);
126     }
127
128     @Override
129     public String toString() {
130         return "ServerConfigurationPayload [serverConfig=" + serverConfig + "]";
131     }
132
133     @Override
134     protected Object writeReplace() {
135         return new Proxy(this);
136     }
137 }