| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.elasticstack.domain; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import org.jclouds.javax.annotation.Nullable; |
| 28 | |
| 29 | import com.google.common.collect.ImmutableList; |
| 30 | import com.google.common.collect.ImmutableMap; |
| 31 | import com.google.common.collect.ImmutableSet; |
| 32 | |
| 33 | /** |
| 34 | * |
| 35 | * @author Adrian Cole |
| 36 | */ |
| 37 | public class Server extends Item { |
| 38 | |
| 39 | public static class Builder extends Item.Builder { |
| 40 | protected int cpu; |
| 41 | protected Integer smp; |
| 42 | protected int mem; |
| 43 | protected boolean persistent; |
| 44 | protected Map<String, ? extends Device> devices = ImmutableMap.of(); |
| 45 | protected Set<String> bootDeviceIds = ImmutableSet.of(); |
| 46 | protected List<NIC> nics = ImmutableList.of(); |
| 47 | protected VNC vnc; |
| 48 | |
| 49 | public Builder cpu(int cpu) { |
| 50 | this.cpu = cpu; |
| 51 | return this; |
| 52 | } |
| 53 | |
| 54 | public Builder smp(Integer smp) { |
| 55 | this.smp = smp; |
| 56 | return this; |
| 57 | } |
| 58 | |
| 59 | public Builder mem(int mem) { |
| 60 | this.mem = mem; |
| 61 | return this; |
| 62 | } |
| 63 | |
| 64 | public Builder persistent(boolean persistent) { |
| 65 | this.persistent = persistent; |
| 66 | return this; |
| 67 | } |
| 68 | |
| 69 | public Builder devices(Map<String, ? extends Device> devices) { |
| 70 | this.devices = ImmutableMap.copyOf(checkNotNull(devices, "devices")); |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | public Builder bootDeviceIds(Iterable<String> bootDeviceIds) { |
| 75 | this.bootDeviceIds = ImmutableSet.copyOf(checkNotNull(bootDeviceIds, "bootDeviceIds")); |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | public Builder nics(Iterable<NIC> nics) { |
| 80 | this.nics = ImmutableList.copyOf(checkNotNull(nics, "nics")); |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | public Builder vnc(VNC vnc) { |
| 85 | this.vnc = vnc; |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * {@inheritDoc} |
| 91 | */ |
| 92 | @Override |
| 93 | public Builder uuid(String uuid) { |
| 94 | return Builder.class.cast(super.uuid(uuid)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * {@inheritDoc} |
| 99 | */ |
| 100 | @Override |
| 101 | public Builder name(String name) { |
| 102 | return Builder.class.cast(super.name(name)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * {@inheritDoc} |
| 107 | */ |
| 108 | @Override |
| 109 | public Builder tags(Iterable<String> tags) { |
| 110 | return Builder.class.cast(super.tags(tags)); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * {@inheritDoc} |
| 115 | */ |
| 116 | @Override |
| 117 | public Builder userMetadata(Map<String, String> userMetadata) { |
| 118 | return Builder.class.cast(super.userMetadata(userMetadata)); |
| 119 | } |
| 120 | |
| 121 | public Server build() { |
| 122 | return new Server(uuid, name, cpu, smp, mem, persistent, devices, bootDeviceIds, tags, userMetadata, nics, vnc); |
| 123 | } |
| 124 | |
| 125 | public static Builder fromServer(Server in) { |
| 126 | return new Builder().uuid(in.getUuid()).name(in.getName()).cpu(in.getCpu()).mem(in.getMem()) |
| 127 | .persistent(in.isPersistent()).devices(in.getDevices()).bootDeviceIds(in.getBootDeviceIds()) |
| 128 | .tags(in.getTags()).userMetadata(in.getUserMetadata()).nics(in.getNics()).vnc(in.getVnc()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | protected final int cpu; |
| 133 | protected final Integer smp; |
| 134 | protected final int mem; |
| 135 | protected final boolean persistent; |
| 136 | @Nullable |
| 137 | protected final Map<String, Device> devices; |
| 138 | protected final Set<String> bootDeviceIds; |
| 139 | protected final List<NIC> nics; |
| 140 | protected final VNC vnc; |
| 141 | |
| 142 | public Server(@Nullable String uuid, String name, int cpu, @Nullable Integer smp, int mem, boolean persistent, |
| 143 | Map<String, ? extends Device> devices, Iterable<String> bootDeviceIds, Iterable<String> tags, |
| 144 | Map<String, String> userMetadata, Iterable<NIC> nics, VNC vnc) { |
| 145 | super(uuid, name, tags, userMetadata); |
| 146 | this.cpu = cpu; |
| 147 | this.smp = smp; |
| 148 | this.mem = mem; |
| 149 | this.persistent = persistent; |
| 150 | this.devices = ImmutableMap.copyOf(checkNotNull(devices, "devices")); |
| 151 | this.bootDeviceIds = ImmutableSet.copyOf(checkNotNull(bootDeviceIds, "bootDeviceIds")); |
| 152 | this.nics = ImmutableList.copyOf(checkNotNull(nics, "nics")); |
| 153 | this.vnc = checkNotNull(vnc, "vnc"); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * |
| 158 | * @return CPU quota in core MHz. |
| 159 | */ |
| 160 | public int getCpu() { |
| 161 | return cpu; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * |
| 166 | * @return number of virtual processors or null if calculated based on cpu. |
| 167 | */ |
| 168 | public Integer getSmp() { |
| 169 | return smp; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * |
| 174 | * @return virtual memory size in MB. |
| 175 | */ |
| 176 | public int getMem() { |
| 177 | return mem; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * |
| 182 | * @return 'true' means that server will revert to a 'stopped' status on server stop or shutdown, |
| 183 | * rather than being destroyed automatically. |
| 184 | */ |
| 185 | public boolean isPersistent() { |
| 186 | return persistent; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * |
| 191 | * @return devices present, mapped by id |
| 192 | */ |
| 193 | public Map<String, Device> getDevices() { |
| 194 | return devices; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * |
| 199 | * @return ids of the devices to boot, e.g. ide:0:0 or ide:1:0 |
| 200 | * @see Device#getId() |
| 201 | */ |
| 202 | public Set<String> getBootDeviceIds() { |
| 203 | return bootDeviceIds; |
| 204 | } |
| 205 | |
| 206 | public List<NIC> getNics() { |
| 207 | return nics; |
| 208 | } |
| 209 | |
| 210 | public VNC getVnc() { |
| 211 | return vnc; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public int hashCode() { |
| 216 | final int prime = 31; |
| 217 | int result = super.hashCode(); |
| 218 | result = prime * result + ((bootDeviceIds == null) ? 0 : bootDeviceIds.hashCode()); |
| 219 | result = prime * result + cpu; |
| 220 | result = prime * result + ((devices == null) ? 0 : devices.hashCode()); |
| 221 | result = prime * result + mem; |
| 222 | result = prime * result + ((nics == null) ? 0 : nics.hashCode()); |
| 223 | result = prime * result + (persistent ? 1231 : 1237); |
| 224 | result = prime * result + ((smp == null) ? 0 : smp.hashCode()); |
| 225 | result = prime * result + ((vnc == null) ? 0 : vnc.hashCode()); |
| 226 | return result; |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public boolean equals(Object obj) { |
| 231 | if (this == obj) |
| 232 | return true; |
| 233 | if (!super.equals(obj)) |
| 234 | return false; |
| 235 | if (getClass() != obj.getClass()) |
| 236 | return false; |
| 237 | Server other = (Server) obj; |
| 238 | if (bootDeviceIds == null) { |
| 239 | if (other.bootDeviceIds != null) |
| 240 | return false; |
| 241 | } else if (!bootDeviceIds.equals(other.bootDeviceIds)) |
| 242 | return false; |
| 243 | if (cpu != other.cpu) |
| 244 | return false; |
| 245 | if (devices == null) { |
| 246 | if (other.devices != null) |
| 247 | return false; |
| 248 | } else if (!devices.equals(other.devices)) |
| 249 | return false; |
| 250 | if (mem != other.mem) |
| 251 | return false; |
| 252 | if (nics == null) { |
| 253 | if (other.nics != null) |
| 254 | return false; |
| 255 | } else if (!nics.equals(other.nics)) |
| 256 | return false; |
| 257 | if (persistent != other.persistent) |
| 258 | return false; |
| 259 | if (smp == null) { |
| 260 | if (other.smp != null) |
| 261 | return false; |
| 262 | } else if (!smp.equals(other.smp)) |
| 263 | return false; |
| 264 | if (vnc == null) { |
| 265 | if (other.vnc != null) |
| 266 | return false; |
| 267 | } else if (!vnc.equals(other.vnc)) |
| 268 | return false; |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public String toString() { |
| 274 | return "[uuid=" + uuid + ", name=" + name + ", tags=" + tags + ", userMetadata=" + userMetadata + ", cpu=" + cpu |
| 275 | + ", smp=" + smp + ", mem=" + mem + ", persistent=" + persistent + ", devices=" + devices |
| 276 | + ", bootDeviceIds=" + bootDeviceIds + ", nics=" + nics + ", vnc=" + vnc + "]"; |
| 277 | } |
| 278 | |
| 279 | } |