1   /*
2    * Copyright 2018 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.server;
17  
18  import static com.google.common.base.Preconditions.checkArgument;
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.Objects;
22  
23  import javax.annotation.Nullable;
24  
25  import com.fasterxml.jackson.annotation.JsonCreator;
26  import com.fasterxml.jackson.annotation.JsonProperty;
27  import com.google.common.base.MoreObjects;
28  
29  /**
30   * Represents the address and port numbers of a ZooKeeper node.
31   */
32  public final class ZooKeeperServerConfig {
33  
34      private final String host;
35      private final int quorumPort;
36      private final int electionPort;
37      private final int clientPort;
38      @Nullable
39      private final Integer groupId;
40      private final int weight;
41  
42      /**
43       * Creates a new instance.
44       *
45       * @param host the IP address or host name of the ZooKeeper server
46       * @param quorumPort the quorum port number
47       * @param electionPort the election port number
48       * @param clientPort the client port number (0-65535)
49       * @param groupId the group ID to use for hierarchical quorums
50       * @param weight the weight of the Zookeeper server
51       */
52      @JsonCreator
53      public ZooKeeperServerConfig(
54              @JsonProperty(value = "host", required = true) String host,
55              @JsonProperty(value = "quorumPort", required = true) int quorumPort,
56              @JsonProperty(value = "electionPort", required = true) int electionPort,
57              @JsonProperty(value = "clientPort", defaultValue = "0") @Nullable Integer clientPort,
58              @JsonProperty("groupId") @Nullable Integer groupId,
59              @JsonProperty(value = "weight", defaultValue = "1") @Nullable Integer weight) {
60  
61          this.host = requireNonNull(host, "host");
62          this.quorumPort = validatePort(quorumPort, "quorumPort");
63          this.electionPort = validatePort(electionPort, "electionPort");
64  
65          if (clientPort == null) {
66              clientPort = 0;
67          }
68          checkArgument(clientPort >= 0 && clientPort <= 65535,
69                        "clientPort: %s (expected: 0-65535)", clientPort);
70          this.clientPort = clientPort;
71          this.groupId = groupId;
72          if (weight == null) {
73              weight = 1;
74          }
75          checkArgument(weight >= 0, "weight: %s (expected: >= 0)", weight);
76          this.weight = weight;
77      }
78  
79      private static int validatePort(int port, String name) {
80          checkArgument(port > 0 && port <= 65535, "%s: %s (expected: 1-65535)", name, port);
81          return port;
82      }
83  
84      /**
85       * Returns the IP address or host name of the ZooKeeper server.
86       */
87      @JsonProperty
88      public String host() {
89          return host;
90      }
91  
92      /**
93       * Returns the quorum port number.
94       */
95      @JsonProperty
96      public int quorumPort() {
97          return quorumPort;
98      }
99  
100     /**
101      * Returns the election port number.
102      */
103     @JsonProperty
104     public int electionPort() {
105         return electionPort;
106     }
107 
108     /**
109      * Returns the client port number.
110      */
111     @JsonProperty
112     public int clientPort() {
113         return clientPort;
114     }
115 
116     /**
117      * Returns the group ID to use hierarchical quorums.
118      */
119     @Nullable
120     @JsonProperty
121     public Integer groupId() {
122         return groupId;
123     }
124 
125     /**
126      * Returns the weight of the ZooKeeper server.
127      */
128     @JsonProperty
129     public int weight() {
130         return weight;
131     }
132 
133     @Override
134     public int hashCode() {
135         return Objects.hash(host, quorumPort, electionPort, clientPort, groupId, weight);
136     }
137 
138     @Override
139     public boolean equals(Object o) {
140         if (this == o) {
141             return true;
142         }
143 
144         if (o == null || getClass() != o.getClass()) {
145             return false;
146         }
147 
148         final ZooKeeperServerConfig that = (ZooKeeperServerConfig) o;
149         return host.equals(that.host) &&
150                quorumPort == that.quorumPort &&
151                electionPort == that.electionPort &&
152                clientPort == that.clientPort &&
153                Objects.equals(groupId, that.groupId) &&
154                weight == that.weight;
155     }
156 
157     @Override
158     public String toString() {
159         return MoreObjects.toStringHelper(this)
160                           .add("host", host)
161                           .add("quorumPort", quorumPort)
162                           .add("electionPort", electionPort)
163                           .add("clientPort", clientPort)
164                           .add("groupId", groupId)
165                           .add("weight", weight)
166                           .toString();
167     }
168 }