| 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.cloudsigma.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 | // TODO cloudsigma specific |
| 49 | protected String description; |
| 50 | |
| 51 | public Builder cpu(int cpu) { |
| 52 | this.cpu = cpu; |
| 53 | return this; |
| 54 | } |
| 55 | |
| 56 | public Builder smp(Integer smp) { |
| 57 | this.smp = smp; |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | public Builder mem(int mem) { |
| 62 | this.mem = mem; |
| 63 | return this; |
| 64 | } |
| 65 | |
| 66 | public Builder persistent(boolean persistent) { |
| 67 | this.persistent = persistent; |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | public Builder devices(Map<String, ? extends Device> devices) { |
| 72 | this.devices = ImmutableMap.copyOf(checkNotNull(devices, "devices")); |
| 73 | return this; |
| 74 | } |
| 75 | |
| 76 | public Builder bootDeviceIds(Iterable<String> bootDeviceIds) { |
| 77 | this.bootDeviceIds = ImmutableSet.copyOf(checkNotNull(bootDeviceIds, "bootDeviceIds")); |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | public Builder nics(Iterable<NIC> nics) { |
| 82 | this.nics = ImmutableList.copyOf(checkNotNull(nics, "nics")); |
| 83 | return this; |
| 84 | } |
| 85 | |
| 86 | public Builder vnc(VNC vnc) { |
| 87 | this.vnc = vnc; |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | public Builder description(String description) { |
| 92 | this.description = description; |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * {@inheritDoc} |
| 98 | */ |
| 99 | @Override |
| 100 | public Builder uuid(String uuid) { |
| 101 | return Builder.class.cast(super.uuid(uuid)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * {@inheritDoc} |
| 106 | */ |
| 107 | @Override |
| 108 | public Builder name(String name) { |
| 109 | return Builder.class.cast(super.name(name)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * {@inheritDoc} |
| 114 | */ |
| 115 | @Override |
| 116 | public Builder use(Iterable<String> use) { |
| 117 | return Builder.class.cast(super.use(use)); |
| 118 | } |
| 119 | |
| 120 | public Server build() { |
| 121 | return new Server(uuid, name, cpu, smp, mem, persistent, devices, bootDeviceIds, use, nics, vnc, description); |
| 122 | } |
| 123 | |
| 124 | public static Builder fromServer(Server in) { |
| 125 | return new Builder().uuid(in.getUuid()).name(in.getName()).cpu(in.getCpu()).mem(in.getMem()) |
| 126 | .persistent(in.isPersistent()).description(in.getDescription()).devices(in.getDevices()) |
| 127 | .bootDeviceIds(in.getBootDeviceIds()).use(in.getUse()).nics(in.getNics()).vnc(in.getVnc()); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | protected final int cpu; |
| 132 | protected final Integer smp; |
| 133 | protected final int mem; |
| 134 | protected final boolean persistent; |
| 135 | @Nullable |
| 136 | protected final Map<String, Device> devices; |
| 137 | protected final Set<String> bootDeviceIds; |
| 138 | protected final List<NIC> nics; |
| 139 | protected final VNC vnc; |
| 140 | @Nullable |
| 141 | private final String description; |
| 142 | |
| 143 | public Server(@Nullable String uuid, String name, int cpu, @Nullable Integer smp, int mem, boolean persistent, |
| 144 | Map<String, ? extends Device> devices, Iterable<String> bootDeviceIds, Iterable<String> use, |
| 145 | Iterable<NIC> nics, VNC vnc, String description) { |
| 146 | super(uuid, name, use); |
| 147 | this.cpu = cpu; |
| 148 | this.smp = smp; |
| 149 | this.mem = mem; |
| 150 | this.persistent = persistent; |
| 151 | this.devices = ImmutableMap.copyOf(checkNotNull(devices, "devices")); |
| 152 | this.bootDeviceIds = ImmutableSet.copyOf(checkNotNull(bootDeviceIds, "bootDeviceIds")); |
| 153 | this.nics = ImmutableList.copyOf(checkNotNull(nics, "nics")); |
| 154 | this.vnc = checkNotNull(vnc, "vnc"); |
| 155 | this.description = description; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * |
| 160 | * @return CPU quota in core MHz. |
| 161 | */ |
| 162 | public int getCpu() { |
| 163 | return cpu; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * |
| 168 | * @return number of virtual processors or null if calculated based on cpu. |
| 169 | */ |
| 170 | public Integer getSmp() { |
| 171 | return smp; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * |
| 176 | * @return virtual memory size in MB. |
| 177 | */ |
| 178 | public int getMem() { |
| 179 | return mem; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * |
| 184 | * @return 'true' means that server will revert to a 'stopped' status on server stop or shutdown, |
| 185 | * rather than being destroyed automatically. |
| 186 | */ |
| 187 | public boolean isPersistent() { |
| 188 | return persistent; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * |
| 193 | * @return devices present, mapped by id |
| 194 | */ |
| 195 | public Map<String, Device> getDevices() { |
| 196 | return devices; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * |
| 201 | * @return ids of the devices to boot, e.g. ide:0:0 or ide:1:0 |
| 202 | * @see Device#getId() |
| 203 | */ |
| 204 | public Set<String> getBootDeviceIds() { |
| 205 | return bootDeviceIds; |
| 206 | } |
| 207 | |
| 208 | public List<NIC> getNics() { |
| 209 | return nics; |
| 210 | } |
| 211 | |
| 212 | public VNC getVnc() { |
| 213 | return vnc; |
| 214 | } |
| 215 | |
| 216 | // TODO undocumented |
| 217 | public String getDescription() { |
| 218 | return description; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public int hashCode() { |
| 223 | final int prime = 31; |
| 224 | int result = super.hashCode(); |
| 225 | result = prime * result + ((bootDeviceIds == null) ? 0 : bootDeviceIds.hashCode()); |
| 226 | result = prime * result + cpu; |
| 227 | result = prime * result + ((description == null) ? 0 : description.hashCode()); |
| 228 | result = prime * result + ((devices == null) ? 0 : devices.hashCode()); |
| 229 | result = prime * result + mem; |
| 230 | result = prime * result + ((nics == null) ? 0 : nics.hashCode()); |
| 231 | result = prime * result + (persistent ? 1231 : 1237); |
| 232 | result = prime * result + ((smp == null) ? 0 : smp.hashCode()); |
| 233 | result = prime * result + ((vnc == null) ? 0 : vnc.hashCode()); |
| 234 | return result; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public boolean equals(Object obj) { |
| 239 | if (this == obj) |
| 240 | return true; |
| 241 | if (!super.equals(obj)) |
| 242 | return false; |
| 243 | if (getClass() != obj.getClass()) |
| 244 | return false; |
| 245 | Server other = (Server) obj; |
| 246 | if (bootDeviceIds == null) { |
| 247 | if (other.bootDeviceIds != null) |
| 248 | return false; |
| 249 | } else if (!bootDeviceIds.equals(other.bootDeviceIds)) |
| 250 | return false; |
| 251 | if (cpu != other.cpu) |
| 252 | return false; |
| 253 | if (description == null) { |
| 254 | if (other.description != null) |
| 255 | return false; |
| 256 | } else if (!description.equals(other.description)) |
| 257 | return false; |
| 258 | if (devices == null) { |
| 259 | if (other.devices != null) |
| 260 | return false; |
| 261 | } else if (!devices.equals(other.devices)) |
| 262 | return false; |
| 263 | if (mem != other.mem) |
| 264 | return false; |
| 265 | if (nics == null) { |
| 266 | if (other.nics != null) |
| 267 | return false; |
| 268 | } else if (!nics.equals(other.nics)) |
| 269 | return false; |
| 270 | if (persistent != other.persistent) |
| 271 | return false; |
| 272 | if (smp == null) { |
| 273 | if (other.smp != null) |
| 274 | return false; |
| 275 | } else if (!smp.equals(other.smp)) |
| 276 | return false; |
| 277 | if (vnc == null) { |
| 278 | if (other.vnc != null) |
| 279 | return false; |
| 280 | } else if (!vnc.equals(other.vnc)) |
| 281 | return false; |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public String toString() { |
| 287 | return "[uuid=" + uuid + ", name=" + name + ", use=" + use + ", cpu=" + cpu + ", smp=" + smp + ", mem=" + mem |
| 288 | + ", persistent=" + persistent + ", devices=" + devices + ", bootDeviceIds=" + bootDeviceIds + ", nics=" |
| 289 | + nics + ", vnc=" + vnc + ", description=" + description + "]"; |
| 290 | } |
| 291 | |
| 292 | } |