| 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.slicehost.domain; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.jclouds.javax.annotation.Nullable; |
| 26 | |
| 27 | import com.google.common.base.CaseFormat; |
| 28 | |
| 29 | /** |
| 30 | * A slice is a virtual machine instance in the Slicehost system. Flavor and image are requisite |
| 31 | * elements when creating a slice. |
| 32 | * |
| 33 | * @author Adrian Cole |
| 34 | */ |
| 35 | public class Slice { |
| 36 | /** |
| 37 | * The current status of the slice |
| 38 | * |
| 39 | */ |
| 40 | public enum Status { |
| 41 | |
| 42 | ACTIVE, BUILD, REBOOT, HARD_REBOOT, TERMINATED, UNRECOGNIZED; |
| 43 | |
| 44 | public String value() { |
| 45 | return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_UNDERSCORE, name())); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public String toString() { |
| 50 | return value(); |
| 51 | } |
| 52 | |
| 53 | public static Status fromValue(String state) { |
| 54 | try { |
| 55 | return valueOf(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(state, "state"))); |
| 56 | } catch (IllegalArgumentException e) { |
| 57 | return UNRECOGNIZED; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private final int id; |
| 63 | private final String name; |
| 64 | private final int flavorId; |
| 65 | @Nullable |
| 66 | private final Integer imageId; |
| 67 | @Nullable |
| 68 | private final Integer backupId; |
| 69 | private final Status status; |
| 70 | @Nullable |
| 71 | private final Integer progress; |
| 72 | private final float bandwidthIn; |
| 73 | private final float bandwidthOut; |
| 74 | private final Set<String> addresses; |
| 75 | @Nullable |
| 76 | private final String rootPassword; |
| 77 | |
| 78 | public Slice(int id, String name, int flavorId, @Nullable Integer imageId, @Nullable Integer backupId, |
| 79 | Status status, @Nullable Integer progress, float bandwidthIn, float bandwidthOut, Set<String> addresses, |
| 80 | @Nullable String rootPassword) { |
| 81 | this.id = id; |
| 82 | this.name = name; |
| 83 | this.flavorId = flavorId; |
| 84 | this.imageId = imageId; |
| 85 | this.backupId = backupId; |
| 86 | this.status = status; |
| 87 | this.progress = progress; |
| 88 | this.bandwidthIn = bandwidthIn; |
| 89 | this.bandwidthOut = bandwidthOut; |
| 90 | this.addresses = addresses; |
| 91 | this.rootPassword = rootPassword; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @return unique id within slicehost |
| 96 | */ |
| 97 | public int getId() { |
| 98 | return id; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @return A string to identify the slice |
| 103 | */ |
| 104 | public String getName() { |
| 105 | return name; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return the flavor of a slice |
| 110 | */ |
| 111 | public int getFlavorId() { |
| 112 | return flavorId; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @return the image used to create the slice or null |
| 117 | */ |
| 118 | public Integer getImageId() { |
| 119 | return imageId; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @return The current status of the slice |
| 124 | */ |
| 125 | public Status getStatus() { |
| 126 | return status; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @return The percentage of current action in percentage |
| 131 | */ |
| 132 | public Integer getProgress() { |
| 133 | return progress; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @return The incoming bandwidth total for this billing cycle, in Gigabytes |
| 138 | */ |
| 139 | public float getBandwidthIn() { |
| 140 | return bandwidthIn; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @return The outgoing bandwidth total for this billing cycle, in Gigabytes |
| 145 | */ |
| 146 | public float getBandwidthOut() { |
| 147 | return bandwidthOut; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @return an array of strings representing the Slice's IPs, including private IPs |
| 152 | */ |
| 153 | public Set<String> getAddresses() { |
| 154 | return addresses; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return root password, if just created |
| 159 | */ |
| 160 | public String getRootPassword() { |
| 161 | return rootPassword; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @return backup used to create this instance or null |
| 166 | */ |
| 167 | public Integer getBackupId() { |
| 168 | return backupId; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public int hashCode() { |
| 173 | final int prime = 31; |
| 174 | int result = 1; |
| 175 | result = prime * result + ((addresses == null) ? 0 : addresses.hashCode()); |
| 176 | result = prime * result + ((backupId == null) ? 0 : backupId.hashCode()); |
| 177 | result = prime * result + Float.floatToIntBits(bandwidthIn); |
| 178 | result = prime * result + Float.floatToIntBits(bandwidthOut); |
| 179 | result = prime * result + flavorId; |
| 180 | result = prime * result + id; |
| 181 | result = prime * result + ((imageId == null) ? 0 : imageId.hashCode()); |
| 182 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 183 | result = prime * result + ((progress == null) ? 0 : progress.hashCode()); |
| 184 | result = prime * result + ((rootPassword == null) ? 0 : rootPassword.hashCode()); |
| 185 | result = prime * result + ((status == null) ? 0 : status.hashCode()); |
| 186 | return result; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public boolean equals(Object obj) { |
| 191 | if (this == obj) |
| 192 | return true; |
| 193 | if (obj == null) |
| 194 | return false; |
| 195 | if (getClass() != obj.getClass()) |
| 196 | return false; |
| 197 | Slice other = (Slice) obj; |
| 198 | if (addresses == null) { |
| 199 | if (other.addresses != null) |
| 200 | return false; |
| 201 | } else if (!addresses.equals(other.addresses)) |
| 202 | return false; |
| 203 | if (backupId == null) { |
| 204 | if (other.backupId != null) |
| 205 | return false; |
| 206 | } else if (!backupId.equals(other.backupId)) |
| 207 | return false; |
| 208 | if (Float.floatToIntBits(bandwidthIn) != Float.floatToIntBits(other.bandwidthIn)) |
| 209 | return false; |
| 210 | if (Float.floatToIntBits(bandwidthOut) != Float.floatToIntBits(other.bandwidthOut)) |
| 211 | return false; |
| 212 | if (flavorId != other.flavorId) |
| 213 | return false; |
| 214 | if (id != other.id) |
| 215 | return false; |
| 216 | if (imageId == null) { |
| 217 | if (other.imageId != null) |
| 218 | return false; |
| 219 | } else if (!imageId.equals(other.imageId)) |
| 220 | return false; |
| 221 | if (name == null) { |
| 222 | if (other.name != null) |
| 223 | return false; |
| 224 | } else if (!name.equals(other.name)) |
| 225 | return false; |
| 226 | if (progress == null) { |
| 227 | if (other.progress != null) |
| 228 | return false; |
| 229 | } else if (!progress.equals(other.progress)) |
| 230 | return false; |
| 231 | if (rootPassword == null) { |
| 232 | if (other.rootPassword != null) |
| 233 | return false; |
| 234 | } else if (!rootPassword.equals(other.rootPassword)) |
| 235 | return false; |
| 236 | if (status == null) { |
| 237 | if (other.status != null) |
| 238 | return false; |
| 239 | } else if (!status.equals(other.status)) |
| 240 | return false; |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public String toString() { |
| 246 | return "[id=" + id + ", name=" + name + ", flavorId=" + flavorId + ", imageId=" + imageId + ", backupId=" |
| 247 | + backupId + ", status=" + status + ", progress=" + progress + ", bandwidthIn=" + bandwidthIn |
| 248 | + ", bandwidthOut=" + bandwidthOut + ", addresses=" + addresses + ", rootPassword=" |
| 249 | + (rootPassword != null) + "]"; |
| 250 | } |
| 251 | |
| 252 | } |