Remove snapshot after startup and fix related bug
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / 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;
9
10 import com.google.common.base.Preconditions;
11 import java.io.ByteArrayOutputStream;
12 import java.io.IOException;
13 import java.io.ObjectOutputStream;
14 import java.io.Serializable;
15 import java.util.List;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
18 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.PersistentPayload;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Payload data for server configuration log entries.
24  *
25  * @author Thomas Pantelis
26  */
27 public class ServerConfigurationPayload extends Payload implements PersistentPayload, Serializable {
28     private static final long serialVersionUID = 1L;
29
30     private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationPayload.class);
31
32     private final List<ServerInfo> serverConfig;
33     private transient int serializedSize = -1;
34
35     public ServerConfigurationPayload(@Nonnull List<ServerInfo> serverConfig) {
36         this.serverConfig = Preconditions.checkNotNull(serverConfig);
37     }
38
39     @Nonnull
40     public List<ServerInfo> getServerConfig() {
41         return serverConfig;
42     }
43
44     @Override
45     public int size() {
46         if(serializedSize < 0) {
47             try {
48                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
49                 ObjectOutputStream out = new ObjectOutputStream(bos);
50                 out.writeObject(serverConfig);
51                 out.close();
52
53                 serializedSize = bos.toByteArray().length;
54             } catch (IOException e) {
55                 serializedSize = 0;
56                 LOG.error("Error serializing", e);
57             }
58         }
59
60         return serializedSize;
61     }
62
63     @Override
64     public String toString() {
65         return "ServerConfigurationPayload [serverConfig=" + serverConfig + "]";
66     }
67
68     public static class ServerInfo implements Serializable {
69         private static final long serialVersionUID = 1L;
70
71         private final String id;
72         private final boolean isVoting;
73
74         public ServerInfo(@Nonnull String id, boolean isVoting) {
75             this.id = Preconditions.checkNotNull(id);
76             this.isVoting = isVoting;
77         }
78
79         @Nonnull
80         public String getId() {
81             return id;
82         }
83
84         public boolean isVoting() {
85             return isVoting;
86         }
87
88         @Override
89         public int hashCode() {
90             final int prime = 31;
91             int result = 1;
92             result = prime * result + (isVoting ? 1231 : 1237);
93             result = prime * result + id.hashCode();
94             return result;
95         }
96
97         @Override
98         public boolean equals(Object obj) {
99             if (obj == null || getClass() != obj.getClass()) {
100                 return false;
101             }
102
103             ServerInfo other = (ServerInfo) obj;
104             return isVoting == other.isVoting && id.equals(other.id);
105         }
106
107         @Override
108         public String toString() {
109             return "ServerInfo [id=" + id + ", isVoting=" + isVoting + "]";
110         }
111     }
112 }