| 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.savvis.vpdc.domain; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.jclouds.compute.domain.CIMOperatingSystem; |
| 27 | |
| 28 | import com.google.common.collect.ImmutableMap; |
| 29 | import com.google.common.collect.Maps; |
| 30 | |
| 31 | /** |
| 32 | * A specification to launch a virtual machine |
| 33 | * |
| 34 | * @author Adrian Cole |
| 35 | */ |
| 36 | public class VMSpec { |
| 37 | |
| 38 | public static Builder builder() { |
| 39 | return new Builder(); |
| 40 | } |
| 41 | |
| 42 | public static class Builder { |
| 43 | private String name; |
| 44 | private String networkTierName; |
| 45 | private CIMOperatingSystem operatingSystem; |
| 46 | private int processorCount = 1; |
| 47 | private int memoryInGig = 1; |
| 48 | private String bootDeviceName = "/"; |
| 49 | // TODO doesn't seem to be changeable |
| 50 | private int bootDriveSize = 25; |
| 51 | private Map<String, Integer> dataDriveDeviceNameToSizeInGig = Maps.newLinkedHashMap(); |
| 52 | |
| 53 | public Builder name(String name) { |
| 54 | this.name = checkNotNull(name, "name"); |
| 55 | return this; |
| 56 | } |
| 57 | |
| 58 | public Builder networkTierName(String networkTierName) { |
| 59 | this.networkTierName = checkNotNull(networkTierName, "networkTierName"); |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | public Builder operatingSystem(CIMOperatingSystem operatingSystem) { |
| 64 | this.operatingSystem = checkNotNull(operatingSystem, "operatingSystem"); |
| 65 | return this; |
| 66 | } |
| 67 | |
| 68 | public Builder memoryInGig(int memoryInGig) { |
| 69 | checkArgument(memoryInGig > 0, "memoryInGig must be positive"); |
| 70 | this.memoryInGig = memoryInGig; |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | public Builder processorCount(int processorCount) { |
| 75 | checkProcessorCount(processorCount); |
| 76 | this.processorCount = processorCount; |
| 77 | return this; |
| 78 | } |
| 79 | |
| 80 | public Builder bootDeviceName(String bootDeviceName) { |
| 81 | this.bootDeviceName = checkNotNull(bootDeviceName, "bootDeviceName"); |
| 82 | return this; |
| 83 | } |
| 84 | |
| 85 | public Builder bootDiskSize(int bootDriveSize) { |
| 86 | checkArgument(bootDriveSize > 0, "bootDriveSize must be positive"); |
| 87 | this.bootDriveSize = bootDriveSize; |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | public Builder addDataDrive(String dataDriveDeviceName, int sizeInGig) { |
| 92 | checkArgument(sizeInGig > 0, "sizeInGig must be positive"); |
| 93 | this.dataDriveDeviceNameToSizeInGig.put(checkNotNull(dataDriveDeviceName, "dataDriveDeviceName"), sizeInGig); |
| 94 | return this; |
| 95 | } |
| 96 | |
| 97 | public Builder addDataDrives(Map<String, Integer> dataDriveDeviceNameToSizeInGig) { |
| 98 | this.dataDriveDeviceNameToSizeInGig = ImmutableMap.copyOf(checkNotNull(dataDriveDeviceNameToSizeInGig, |
| 99 | "dataDriveDeviceNameToSizeInGig")); |
| 100 | return this; |
| 101 | } |
| 102 | |
| 103 | public VMSpec build() { |
| 104 | return new VMSpec(name, networkTierName, operatingSystem, processorCount, memoryInGig, bootDeviceName, |
| 105 | bootDriveSize, dataDriveDeviceNameToSizeInGig); |
| 106 | } |
| 107 | |
| 108 | public static Builder fromVMSpec(VMSpec in) { |
| 109 | return new Builder().operatingSystem(in.getOperatingSystem()).memoryInGig(in.getMemoryInGig()).bootDeviceName( |
| 110 | in.getBootDeviceName()).bootDiskSize(in.getBootDiskSize()).addDataDrives( |
| 111 | in.getDataDiskDeviceNameToSizeInGig()).processorCount(in.getProcessorCount()); |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | static void checkProcessorCount(int processorCount) { |
| 117 | checkArgument(processorCount > 0, "processorCount must be positive and an increment of 0.5"); |
| 118 | checkArgument(processorCount % .5 == 0, "processorCount must be an increment of 0.5"); |
| 119 | } |
| 120 | |
| 121 | private final String name; |
| 122 | private final String networkTierName; |
| 123 | private final CIMOperatingSystem operatingSystem; |
| 124 | private final int processorCount; |
| 125 | private final int memoryInGig; |
| 126 | private final String bootDeviceName; |
| 127 | private final int bootDriveSize; |
| 128 | private final Map<String, Integer> dataDriveDeviceNameToSizeInGig; |
| 129 | |
| 130 | protected VMSpec(String name, String networkTierName, CIMOperatingSystem operatingSystem, int processorCount, |
| 131 | int memoryInGig, String bootDeviceName, int bootDriveSize, |
| 132 | Map<String, Integer> dataDriveDeviceNameToSizeInGig) { |
| 133 | this.name = name; |
| 134 | this.networkTierName = networkTierName; |
| 135 | this.operatingSystem = checkNotNull(operatingSystem, "operatingSystem not specified"); |
| 136 | checkProcessorCount(processorCount); |
| 137 | this.processorCount = processorCount; |
| 138 | checkArgument(memoryInGig > 0, "memoryInGig must be positive"); |
| 139 | this.memoryInGig = memoryInGig; |
| 140 | this.bootDeviceName = checkNotNull(bootDeviceName, "bootDeviceName name not specified"); |
| 141 | checkArgument(bootDriveSize > 0, "bootDriveSize must be positive"); |
| 142 | this.bootDriveSize = bootDriveSize; |
| 143 | this.dataDriveDeviceNameToSizeInGig = ImmutableMap.copyOf(checkNotNull(dataDriveDeviceNameToSizeInGig, |
| 144 | "dataDriveDeviceNameToSizeInGig")); |
| 145 | } |
| 146 | |
| 147 | public String getName() { |
| 148 | return name; |
| 149 | } |
| 150 | |
| 151 | public String getNetworkTierName() { |
| 152 | return networkTierName; |
| 153 | } |
| 154 | |
| 155 | public CIMOperatingSystem getOperatingSystem() { |
| 156 | return operatingSystem; |
| 157 | } |
| 158 | |
| 159 | public int getProcessorCount() { |
| 160 | return processorCount; |
| 161 | } |
| 162 | |
| 163 | public int getMemoryInGig() { |
| 164 | return memoryInGig; |
| 165 | } |
| 166 | |
| 167 | public Builder toBuilder() { |
| 168 | return Builder.fromVMSpec(this); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public int hashCode() { |
| 173 | final int prime = 31; |
| 174 | int result = 1; |
| 175 | result = prime * result + ((bootDeviceName == null) ? 0 : bootDeviceName.hashCode()); |
| 176 | result = prime * result + bootDriveSize; |
| 177 | result = prime * result |
| 178 | + ((dataDriveDeviceNameToSizeInGig == null) ? 0 : dataDriveDeviceNameToSizeInGig.hashCode()); |
| 179 | result = prime * result + memoryInGig; |
| 180 | result = prime * result + ((operatingSystem == null) ? 0 : operatingSystem.hashCode()); |
| 181 | result = prime * result + processorCount; |
| 182 | return result; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public boolean equals(Object obj) { |
| 187 | if (this == obj) |
| 188 | return true; |
| 189 | if (obj == null) |
| 190 | return false; |
| 191 | if (getClass() != obj.getClass()) |
| 192 | return false; |
| 193 | VMSpec other = (VMSpec) obj; |
| 194 | if (bootDeviceName == null) { |
| 195 | if (other.bootDeviceName != null) |
| 196 | return false; |
| 197 | } else if (!bootDeviceName.equals(other.bootDeviceName)) |
| 198 | return false; |
| 199 | if (bootDriveSize != other.bootDriveSize) |
| 200 | return false; |
| 201 | if (dataDriveDeviceNameToSizeInGig == null) { |
| 202 | if (other.dataDriveDeviceNameToSizeInGig != null) |
| 203 | return false; |
| 204 | } else if (!dataDriveDeviceNameToSizeInGig.equals(other.dataDriveDeviceNameToSizeInGig)) |
| 205 | return false; |
| 206 | if (memoryInGig != other.memoryInGig) |
| 207 | return false; |
| 208 | if (operatingSystem == null) { |
| 209 | if (other.operatingSystem != null) |
| 210 | return false; |
| 211 | } else if (!operatingSystem.equals(other.operatingSystem)) |
| 212 | return false; |
| 213 | if (processorCount != other.processorCount) |
| 214 | return false; |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | public String getBootDeviceName() { |
| 219 | return bootDeviceName; |
| 220 | } |
| 221 | |
| 222 | public int getBootDiskSize() { |
| 223 | return bootDriveSize; |
| 224 | } |
| 225 | |
| 226 | public Map<String, Integer> getDataDiskDeviceNameToSizeInGig() { |
| 227 | return dataDriveDeviceNameToSizeInGig; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public String toString() { |
| 232 | return "[name= " + name + ", operatingSystem=" + operatingSystem + ", processorCount=" + processorCount |
| 233 | + ", memoryInGig=" + memoryInGig + ", networkTierName=" + networkTierName + ", bootDeviceName=" |
| 234 | + bootDeviceName + ", bootDriveSize=" + bootDriveSize + ", dataDriveDeviceNameToSizeInGig=" |
| 235 | + dataDriveDeviceNameToSizeInGig + "]"; |
| 236 | } |
| 237 | |
| 238 | } |