| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package org.jclouds.glesys.domain; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.beans.ConstructorProperties; |
| 22 | |
| 23 | import org.jclouds.javax.annotation.Nullable; |
| 24 | |
| 25 | import com.google.common.base.Objects; |
| 26 | import com.google.common.base.Objects.ToStringHelper; |
| 27 | |
| 28 | /** |
| 29 | * Detailed information server status including hardware usage (cpu, memory and disk), bandwidth and up-time. |
| 30 | * |
| 31 | * @author Adam Lowe |
| 32 | * @see <a href= "https://customer.glesys.com/api.php?a=doc#server_status" /> |
| 33 | */ |
| 34 | public class ServerStatus { |
| 35 | |
| 36 | public static Builder<?> builder() { |
| 37 | return new ConcreteBuilder(); |
| 38 | } |
| 39 | |
| 40 | public Builder<?> toBuilder() { |
| 41 | return new ConcreteBuilder().fromServerStatus(this); |
| 42 | } |
| 43 | |
| 44 | public abstract static class Builder<T extends Builder<T>> { |
| 45 | protected abstract T self(); |
| 46 | |
| 47 | protected Server.State state; |
| 48 | protected ResourceStatus cpu; |
| 49 | protected ResourceStatus memory; |
| 50 | protected ResourceStatus disk; |
| 51 | protected ServerUptime uptime; |
| 52 | |
| 53 | /** |
| 54 | * @see ServerStatus#getState() |
| 55 | */ |
| 56 | public T state(Server.State state) { |
| 57 | this.state = checkNotNull(state, "state"); |
| 58 | return self(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @see ServerStatus#getCpu() |
| 63 | */ |
| 64 | public T cpu(ResourceStatus cpu) { |
| 65 | this.cpu = checkNotNull(cpu, "cpu"); |
| 66 | return self(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @see ServerStatus#getMemory() |
| 71 | */ |
| 72 | public T memory(ResourceStatus memory) { |
| 73 | this.memory = checkNotNull(memory, "memory"); |
| 74 | return self(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @see ServerStatus#getDisk() |
| 79 | */ |
| 80 | public T disk(ResourceStatus disk) { |
| 81 | this.disk = checkNotNull(disk, "disk"); |
| 82 | return self(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @see ServerStatus#getUptime() |
| 87 | */ |
| 88 | public T uptime(ServerUptime uptime) { |
| 89 | this.uptime = checkNotNull(uptime, "uptime"); |
| 90 | return self(); |
| 91 | } |
| 92 | |
| 93 | public ServerStatus build() { |
| 94 | return new ServerStatus(state, cpu, memory, disk, uptime); |
| 95 | } |
| 96 | |
| 97 | public T fromServerStatus(ServerStatus in) { |
| 98 | return this.state(in.getState()).cpu(in.getCpu()).memory(in.getMemory()).disk(in.getDisk()).uptime(in.getUptime()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
| 103 | @Override |
| 104 | protected ConcreteBuilder self() { |
| 105 | return this; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private final Server.State state; |
| 110 | private final ResourceStatus cpu; |
| 111 | private final ResourceStatus memory; |
| 112 | private final ResourceStatus disk; |
| 113 | private final ServerUptime uptime; |
| 114 | |
| 115 | @ConstructorProperties({ |
| 116 | "state", "cpu", "memory", "disk", "uptime" |
| 117 | }) |
| 118 | protected ServerStatus(Server.State state, @Nullable ResourceStatus cpu, @Nullable ResourceStatus memory, |
| 119 | @Nullable ResourceStatus disk, @Nullable ServerUptime uptime) { |
| 120 | this.state = checkNotNull(state, "state"); |
| 121 | this.cpu = cpu; |
| 122 | this.memory = memory; |
| 123 | this.disk = disk; |
| 124 | this.uptime = uptime; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return the state of the server (e.g. "running") |
| 129 | */ |
| 130 | @Nullable |
| 131 | public Server.State getState() { |
| 132 | return this.state; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return CPU usage information |
| 137 | */ |
| 138 | @Nullable |
| 139 | public ResourceStatus getCpu() { |
| 140 | return this.cpu; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @return details of memory usage and limits |
| 145 | */ |
| 146 | @Nullable |
| 147 | public ResourceStatus getMemory() { |
| 148 | return this.memory; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @return details of disk usage and limits |
| 153 | */ |
| 154 | @Nullable |
| 155 | public ResourceStatus getDisk() { |
| 156 | return this.disk; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return the uptime of the server |
| 161 | */ |
| 162 | @Nullable |
| 163 | public ServerUptime getUptime() { |
| 164 | return this.uptime; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public int hashCode() { |
| 169 | return Objects.hashCode(state, cpu, memory, disk, uptime); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public boolean equals(Object obj) { |
| 174 | if (this == obj) return true; |
| 175 | if (obj == null || getClass() != obj.getClass()) return false; |
| 176 | ServerStatus that = ServerStatus.class.cast(obj); |
| 177 | return Objects.equal(this.state, that.state) |
| 178 | && Objects.equal(this.cpu, that.cpu) |
| 179 | && Objects.equal(this.memory, that.memory) |
| 180 | && Objects.equal(this.disk, that.disk) |
| 181 | && Objects.equal(this.uptime, that.uptime); |
| 182 | } |
| 183 | |
| 184 | protected ToStringHelper string() { |
| 185 | return Objects.toStringHelper("") |
| 186 | .add("state", state).add("cpu", cpu).add("memory", memory).add("disk", disk).add("uptime", uptime); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public String toString() { |
| 191 | return string().toString(); |
| 192 | } |
| 193 | |
| 194 | } |