| 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.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.jclouds.ovf.OperatingSystemSection; |
| 26 | import org.jclouds.ovf.ProductSection; |
| 27 | import org.jclouds.ovf.Section; |
| 28 | import org.jclouds.ovf.VirtualHardwareSection; |
| 29 | |
| 30 | import com.google.common.collect.ImmutableMultimap; |
| 31 | import com.google.common.collect.ImmutableSet; |
| 32 | import com.google.common.collect.LinkedHashMultimap; |
| 33 | import com.google.common.collect.Multimap; |
| 34 | import com.google.common.collect.Sets; |
| 35 | |
| 36 | /** |
| 37 | * @author Adrian Cole |
| 38 | */ |
| 39 | public class BaseVirtualSystem<T extends BaseVirtualSystem<T>> extends Section<T> { |
| 40 | |
| 41 | @SuppressWarnings("unchecked") |
| 42 | public static Builder builder() { |
| 43 | return new Builder(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * {@inheritDoc} |
| 48 | */ |
| 49 | @Override |
| 50 | public Builder<T> toBuilder() { |
| 51 | return new Builder<T>().fromVirtualSystem(this); |
| 52 | } |
| 53 | |
| 54 | public static class Builder<T extends BaseVirtualSystem<T>> extends Section.Builder<T> { |
| 55 | protected String id; |
| 56 | protected String name; |
| 57 | protected OperatingSystemSection operatingSystem; |
| 58 | protected Set<VirtualHardwareSection> virtualHardwareSections = Sets.newLinkedHashSet(); |
| 59 | protected Set<ProductSection> productSections = Sets.newLinkedHashSet(); |
| 60 | @SuppressWarnings("unchecked") |
| 61 | protected Multimap<String, Section> additionalSections = LinkedHashMultimap.create(); |
| 62 | |
| 63 | /** |
| 64 | * @see BaseVirtualSystem#getName |
| 65 | */ |
| 66 | public Builder<T> name(String name) { |
| 67 | this.name = name; |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @see BaseVirtualSystem#getId |
| 73 | */ |
| 74 | public Builder<T> id(String id) { |
| 75 | this.id = id; |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @see BaseVirtualSystem#getOperatingSystemSection |
| 81 | */ |
| 82 | public Builder<T> operatingSystemSection(OperatingSystemSection operatingSystem) { |
| 83 | this.operatingSystem = operatingSystem; |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @see BaseVirtualSystem#getVirtualHardwareSections |
| 89 | */ |
| 90 | public Builder<T> virtualHardwareSection(VirtualHardwareSection virtualHardwareSection) { |
| 91 | this.virtualHardwareSections.add(checkNotNull(virtualHardwareSection, "virtualHardwareSection")); |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @see BaseVirtualSystem#getVirtualHardwareSections |
| 97 | */ |
| 98 | public Builder<T> virtualHardwareSections(Iterable<? extends VirtualHardwareSection> virtualHardwareSections) { |
| 99 | this.virtualHardwareSections = ImmutableSet.<VirtualHardwareSection> copyOf(checkNotNull(virtualHardwareSections, |
| 100 | "virtualHardwareSections")); |
| 101 | return this; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @see BaseVirtualSystem#getProductSections |
| 106 | */ |
| 107 | public Builder<T> productSection(ProductSection productSection) { |
| 108 | this.productSections.add(checkNotNull(productSection, "productSection")); |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @see BaseVirtualSystem#getProductSections |
| 114 | */ |
| 115 | public Builder<T> productSections(Iterable<? extends ProductSection> productSections) { |
| 116 | this.productSections = ImmutableSet.<ProductSection> copyOf(checkNotNull(productSections, "productSections")); |
| 117 | return this; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @see BaseVirtualSystem#getAdditionalSections |
| 122 | */ |
| 123 | @SuppressWarnings("unchecked") |
| 124 | public Builder<T> additionalSection(String name, Section additionalSection) { |
| 125 | this.additionalSections.put(checkNotNull(name, "name"), checkNotNull(additionalSection, "additionalSection")); |
| 126 | return this; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @see BaseVirtualSystem#getAdditionalSections |
| 131 | */ |
| 132 | @SuppressWarnings("unchecked") |
| 133 | public Builder<T> additionalSections(Multimap<String, Section> additionalSections) { |
| 134 | this.additionalSections = ImmutableMultimap.<String, Section> copyOf(checkNotNull(additionalSections, |
| 135 | "additionalSections")); |
| 136 | return this; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * {@inheritDoc} |
| 141 | */ |
| 142 | @Override |
| 143 | public BaseVirtualSystem<T> build() { |
| 144 | return new BaseVirtualSystem<T>(id, info, name, operatingSystem, virtualHardwareSections, productSections, |
| 145 | additionalSections); |
| 146 | } |
| 147 | |
| 148 | public Builder<T> fromVirtualSystem(BaseVirtualSystem<T> in) { |
| 149 | return fromSection(in).id(in.getId()).name(in.getName()) |
| 150 | .operatingSystemSection(in.getOperatingSystemSection()).virtualHardwareSections( |
| 151 | in.getVirtualHardwareSections()).productSections(in.getProductSections()) |
| 152 | .additionalSections(in.getAdditionalSections()); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * {@inheritDoc} |
| 157 | */ |
| 158 | @Override |
| 159 | public Builder<T> fromSection(Section<T> in) { |
| 160 | return (Builder<T>) super.fromSection(in); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * {@inheritDoc} |
| 165 | */ |
| 166 | @Override |
| 167 | public Builder<T> info(String info) { |
| 168 | return (Builder<T>) super.info(info); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | protected final String id; |
| 174 | protected final String name; |
| 175 | protected final OperatingSystemSection operatingSystem; |
| 176 | protected final Set<VirtualHardwareSection> virtualHardwareSections; |
| 177 | protected final Set<ProductSection> productSections; |
| 178 | @SuppressWarnings("unchecked") |
| 179 | protected final Multimap<String, Section> additionalSections; |
| 180 | |
| 181 | @SuppressWarnings("unchecked") |
| 182 | public BaseVirtualSystem(String id, String info, String name, OperatingSystemSection operatingSystem, |
| 183 | Iterable<? extends VirtualHardwareSection> virtualHardwareSections, |
| 184 | Iterable<? extends ProductSection> productSections, Multimap<String, Section> additionalSections) { |
| 185 | super(info); |
| 186 | this.id = id; |
| 187 | this.name = name; |
| 188 | this.operatingSystem = checkNotNull(operatingSystem, "operatingSystem"); |
| 189 | this.virtualHardwareSections = ImmutableSet.copyOf(checkNotNull(virtualHardwareSections, "virtualHardwareSections")); |
| 190 | this.productSections = ImmutableSet.copyOf(checkNotNull(productSections, "productSections")); |
| 191 | this.additionalSections = ImmutableMultimap.copyOf(checkNotNull(additionalSections, "additionalSections")); |
| 192 | } |
| 193 | |
| 194 | public String getId() { |
| 195 | return id; |
| 196 | } |
| 197 | |
| 198 | public String getName() { |
| 199 | return name; |
| 200 | } |
| 201 | |
| 202 | public OperatingSystemSection getOperatingSystemSection() { |
| 203 | return operatingSystem; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Each VirtualSystem element may contain one or more VirtualHardwareSection elements, each of |
| 208 | * which describes the virtual virtualHardwareSections required by the virtual system. |
| 209 | */ |
| 210 | public Set<? extends VirtualHardwareSection> getVirtualHardwareSections() { |
| 211 | return virtualHardwareSections; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Specifies product-information for a package, such as product name and version, along with a |
| 216 | * set of properties that can be configured |
| 217 | */ |
| 218 | public Set<? extends ProductSection> getProductSections() { |
| 219 | return productSections; |
| 220 | } |
| 221 | |
| 222 | @SuppressWarnings("unchecked") |
| 223 | public Multimap<String, Section> getAdditionalSections() { |
| 224 | return additionalSections; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public int hashCode() { |
| 229 | final int prime = 31; |
| 230 | int result = 1; |
| 231 | result = prime * result + ((id == null) ? 0 : id.hashCode()); |
| 232 | return result; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public boolean equals(Object obj) { |
| 237 | if (this == obj) |
| 238 | return true; |
| 239 | if (obj == null) |
| 240 | return false; |
| 241 | if (getClass() != obj.getClass()) |
| 242 | return false; |
| 243 | BaseVirtualSystem<?> other = (BaseVirtualSystem<?>) obj; |
| 244 | if (id == null) { |
| 245 | if (other.id != null) |
| 246 | return false; |
| 247 | } else if (!id.equals(other.id)) |
| 248 | return false; |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public String toString() { |
| 254 | return String |
| 255 | .format( |
| 256 | "[id=%s, name=%s, info=%s, operatingSystem=%s, virtualHardwareSections=%s, productSections=%s, additionalSections=%s]", |
| 257 | id, name, info, operatingSystem, virtualHardwareSections, productSections, additionalSections); |
| 258 | } |
| 259 | } |