| 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.ovf; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | |
| 23 | /** |
| 24 | * |
| 25 | * @author Adrian Cole |
| 26 | */ |
| 27 | public class Disk implements Comparable<Disk>{ |
| 28 | public static Builder builder() { |
| 29 | return new Builder(); |
| 30 | } |
| 31 | |
| 32 | public static class Builder { |
| 33 | private String id; |
| 34 | private Long capacity; |
| 35 | private String parentRef; |
| 36 | private String fileRef; |
| 37 | private URI format; |
| 38 | private Long populatedSize; |
| 39 | private String capacityAllocationUnits; |
| 40 | |
| 41 | /** |
| 42 | * @see Disk#getId |
| 43 | */ |
| 44 | public Builder id(String id) { |
| 45 | this.id = id; |
| 46 | return this; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @see Disk#getCapacity |
| 51 | */ |
| 52 | public Builder capacity(Long capacity) { |
| 53 | this.capacity = capacity; |
| 54 | return this; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @see Disk#getParentRef |
| 59 | */ |
| 60 | public Builder parentRef(String parentRef) { |
| 61 | this.parentRef = parentRef; |
| 62 | return this; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @see Disk#getFileRef |
| 67 | */ |
| 68 | public Builder fileRef(String fileRef) { |
| 69 | this.fileRef = fileRef; |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @see Disk#getFormat |
| 75 | */ |
| 76 | public Builder format(URI format) { |
| 77 | this.format = format; |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @see Disk#getPopulatedSize |
| 83 | */ |
| 84 | public Builder populatedSize(Long populatedSize) { |
| 85 | this.populatedSize = populatedSize; |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @see Disk#getCapacityAllocationUnits |
| 91 | */ |
| 92 | public Builder capacityAllocationUnits(String capacityAllocationUnits) { |
| 93 | this.capacityAllocationUnits = capacityAllocationUnits; |
| 94 | return this; |
| 95 | } |
| 96 | |
| 97 | public Disk build() { |
| 98 | return new Disk(id, capacity, parentRef, fileRef, format, populatedSize, capacityAllocationUnits); |
| 99 | } |
| 100 | |
| 101 | public Builder fromDisk(Disk in) { |
| 102 | return id(in.getId()).capacity(in.getCapacity()).parentRef(in.getParentRef()).fileRef(in.getFileRef()).format( |
| 103 | in.getFormat()).populatedSize(in.getPopulatedSize()).capacityAllocationUnits( |
| 104 | in.getCapacityAllocationUnits()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private final String id; |
| 109 | private final Long capacity; |
| 110 | private final String parentRef; |
| 111 | private final String fileRef; |
| 112 | private final URI format; |
| 113 | private final Long populatedSize; |
| 114 | private final String capacityAllocationUnits; |
| 115 | |
| 116 | public Disk(String id, Long capacity, String parentRef, String fileRef, URI format, Long populatedSize, |
| 117 | String capacityAllocationUnits) { |
| 118 | this.id = id; |
| 119 | this.capacity = capacity; |
| 120 | this.parentRef = parentRef; |
| 121 | this.fileRef = fileRef; |
| 122 | this.format = format; |
| 123 | this.populatedSize = populatedSize; |
| 124 | this.capacityAllocationUnits = capacityAllocationUnits; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Each virtual disk is represented by a Disk element that shall be given a identifier using the |
| 129 | * {@code id} attribute, the identifier shall be unique within the {@link DiskSection}. |
| 130 | */ |
| 131 | public String getId() { |
| 132 | return id; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * The capacity of a virtual disk shall be specified by the {@code capacity} attribute with an |
| 137 | * xs:long integer value. The default unit of allocation shall be bytes. |
| 138 | */ |
| 139 | public Long getCapacity() { |
| 140 | return capacity; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * OVF allows a disk image to be represented as a set of modified blocks in comparison to a |
| 145 | * parent image. The use of parent disks can often significantly reduce the size of an OVF |
| 146 | * package, if it contains multiple disks with similar content. For a Disk element, a parent disk |
| 147 | * may optionally be specified using the {@code parentRef} attribute, which shall contain a valid |
| 148 | * ovf:id reference to a different Disk element. If a disk block does not exist locally, lookup |
| 149 | * for that disk block then occurs in the parent disk. In {@link DiskSection}, parent Disk |
| 150 | * elements shall occur before child Disk elements that refer to them. |
| 151 | */ |
| 152 | public String getParentRef() { |
| 153 | return parentRef; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * The ovf:fileRef attribute denotes the virtual disk content by identifying an existing File |
| 158 | * element in the References element, the File element is identified by matching its {@code id} |
| 159 | * attribute value with the {@code fileRef} attribute value. Omitting the {@code fileRef} |
| 160 | * attribute shall indicate an empty disk. In this case, the disk shall be created and the entire |
| 161 | * disk content zeroed at installation time. The guest software will typically format empty disks |
| 162 | * in some file system format. |
| 163 | */ |
| 164 | public String getFileRef() { |
| 165 | return fileRef; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * The format URI of a non-empty virtual disk shall be specified by the {@code format} attribute. |
| 170 | */ |
| 171 | public URI getFormat() { |
| 172 | return format; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * For non-empty disks, the actual used size of the disk may optionally be specified using the |
| 177 | * {@code populatedSize} attribute. The unit of this attribute is always bytes. {@code |
| 178 | * populatedSize} is allowed to be an estimate of used disk size but shall not be larger than |
| 179 | * {@code capacity}. |
| 180 | */ |
| 181 | public Long getPopulatedSize() { |
| 182 | return populatedSize; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * The optional string attribute {@code ovf:capacityAllocationUnits} may be used to specify a |
| 187 | * particular unit of allocation. Values for {@code ovf:capacityAllocationUnits} shall match the |
| 188 | * format for programmatic units defined in DSP0004. |
| 189 | */ |
| 190 | public String getCapacityAllocationUnits() { |
| 191 | return capacityAllocationUnits; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public int hashCode() { |
| 196 | final int prime = 31; |
| 197 | int result = 1; |
| 198 | result = prime * result + ((id == null) ? 0 : id.hashCode()); |
| 199 | return result; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public boolean equals(Object obj) { |
| 204 | if (this == obj) |
| 205 | return true; |
| 206 | if (obj == null) |
| 207 | return false; |
| 208 | if (getClass() != obj.getClass()) |
| 209 | return false; |
| 210 | Disk other = (Disk) obj; |
| 211 | if (id == null) { |
| 212 | if (other.id != null) |
| 213 | return false; |
| 214 | } else if (!id.equals(other.id)) |
| 215 | return false; |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public String toString() { |
| 221 | return String |
| 222 | .format( |
| 223 | "[id=%s, capacity=%s, capacityAllocationUnits=%s, fileRef=%s, format=%s, parentRef=%s, populatedSize=%s]", |
| 224 | id, capacity, capacityAllocationUnits, fileRef, format, parentRef, populatedSize); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * {@inheritDoc} |
| 229 | */ |
| 230 | @Override |
| 231 | public int compareTo(Disk o) { |
| 232 | if (id == null) |
| 233 | return -1; |
| 234 | return (this == o) ? 0 : id.compareTo(o.id); |
| 235 | } |
| 236 | } |