1   /*
2    * Copyright 2024 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  
17  package com.linecorp.centraldogma.server.management;
18  
19  import com.fasterxml.jackson.annotation.JsonProperty;
20  
21  /**
22   * The status of the server.
23   */
24  public enum ServerStatus {
25  
26      READ_ONLY(false, false),
27      REPLICATION_ONLY(false, true),
28      WRITABLE(true, true);
29  
30      private final boolean writable;
31      private final boolean replicating;
32  
33      // TODO(trustin): Add more properties, e.g. method, host name, isLeader and config.
34  
35      ServerStatus(boolean writable, boolean replicating) {
36          this.writable = writable;
37          this.replicating = replicating;
38      }
39  
40      /**
41       * Returns the {@link ServerStatus} instance with the specified properties.
42       */
43      public static ServerStatus of(boolean writable, boolean replicating) {
44          if (writable) {
45              if (replicating) {
46                  return WRITABLE;
47              } else {
48                  throw new IllegalArgumentException("replicating must be true if writable is true");
49              }
50          } else {
51              if (replicating) {
52                  return REPLICATION_ONLY;
53              } else {
54                  return READ_ONLY;
55              }
56          }
57      }
58  
59      /**
60       * Returns whether the server is writable.
61       */
62      @JsonProperty("writable")
63      public boolean writable() {
64          return writable;
65      }
66  
67      /**
68       * Returns whether the server is replicating.
69       */
70      @JsonProperty("replicating")
71      public boolean replicating() {
72          return replicating;
73      }
74  }