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 static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.util.Set; |
24 | |
25 | import org.jclouds.cim.ResourceAllocationSettingData; |
26 | import org.jclouds.cim.VirtualSystemSettingData; |
27 | |
28 | import com.google.common.collect.ImmutableSet; |
29 | import com.google.common.collect.Sets; |
30 | |
31 | /** |
32 | * The virtual hardware required by a virtual machine is specified in VirtualHardwareSection. |
33 | * <p/> |
34 | * This specification supports abstract or incomplete hardware descriptions in which only the major |
35 | * devices are described. The hypervisor is allowed to create additional virtual hardware |
36 | * controllers and devices, as long as the required devices listed in the descriptor are realized. |
37 | * |
38 | * @author Adrian Cole |
39 | */ |
40 | public class VirtualHardwareSection extends Section<VirtualHardwareSection> { |
41 | |
42 | @SuppressWarnings("unchecked") |
43 | public static Builder builder() { |
44 | return new Builder(); |
45 | } |
46 | |
47 | /** |
48 | * {@inheritDoc} |
49 | */ |
50 | @Override |
51 | public Builder toBuilder() { |
52 | return builder().fromVirtualHardwareSection(this); |
53 | } |
54 | |
55 | public static class Builder extends Section.Builder<VirtualHardwareSection> { |
56 | protected VirtualSystemSettingData virtualSystem; |
57 | protected Set<String> transports = Sets.newLinkedHashSet(); |
58 | protected Set<ResourceAllocationSettingData> items = Sets.newLinkedHashSet(); |
59 | |
60 | /** |
61 | * @see VirtualHardwareSection#getSystem |
62 | */ |
63 | public Builder system(VirtualSystemSettingData virtualSystem) { |
64 | this.virtualSystem = virtualSystem; |
65 | return this; |
66 | } |
67 | |
68 | /** |
69 | * @see VirtualHardwareSection#getTransports |
70 | */ |
71 | public Builder transport(String transport) { |
72 | this.transports.add(checkNotNull(transport, "transport")); |
73 | return this; |
74 | } |
75 | |
76 | /** |
77 | * @see VirtualHardwareSection#getTransports |
78 | */ |
79 | public Builder transports(Iterable<String> transports) { |
80 | this.transports = ImmutableSet.<String> copyOf(checkNotNull(transports, "transports")); |
81 | return this; |
82 | } |
83 | |
84 | /** |
85 | * @see VirtualHardwareSection#getItems |
86 | */ |
87 | public Builder item(ResourceAllocationSettingData item) { |
88 | this.items.add(checkNotNull(item, "item")); |
89 | return this; |
90 | } |
91 | |
92 | /** |
93 | * @see VirtualHardwareSection#getItems |
94 | */ |
95 | public Builder items(Iterable<? extends ResourceAllocationSettingData> items) { |
96 | this.items = ImmutableSet.<ResourceAllocationSettingData> copyOf(checkNotNull( |
97 | items, "items")); |
98 | return this; |
99 | } |
100 | |
101 | /** |
102 | * {@inheritDoc} |
103 | */ |
104 | @Override |
105 | public VirtualHardwareSection build() { |
106 | return new VirtualHardwareSection(info, transports, virtualSystem, items); |
107 | } |
108 | |
109 | public Builder fromVirtualHardwareSection(VirtualHardwareSection in) { |
110 | return fromSection(in).items(in.getItems()).transports(in.getTransports()).system( |
111 | in.getSystem()).info(in.getInfo()); |
112 | } |
113 | |
114 | /** |
115 | * {@inheritDoc} |
116 | */ |
117 | @Override |
118 | public Builder fromSection(Section<VirtualHardwareSection> in) { |
119 | return Builder.class.cast(super.fromSection(in)); |
120 | } |
121 | |
122 | /** |
123 | * {@inheritDoc} |
124 | */ |
125 | @Override |
126 | public Builder info(String info) { |
127 | return Builder.class.cast(super.info(info)); |
128 | } |
129 | |
130 | } |
131 | |
132 | protected final VirtualSystemSettingData virtualSystem; |
133 | protected final Set<String> transports; |
134 | protected final Set<ResourceAllocationSettingData> items; |
135 | |
136 | public VirtualHardwareSection(String info, Iterable<String> transports, VirtualSystemSettingData virtualSystem, |
137 | Iterable<? extends ResourceAllocationSettingData> items) { |
138 | super(info); |
139 | this.virtualSystem = virtualSystem; |
140 | this.transports = ImmutableSet.<String> copyOf(checkNotNull(transports, "transports")); |
141 | this.items = ImmutableSet.<ResourceAllocationSettingData> copyOf(checkNotNull(items, |
142 | "items")); |
143 | } |
144 | |
145 | /** |
146 | * transport types define methods by which the environment document is communicated from the |
147 | * deployment platform to the guest software. |
148 | * <p/> |
149 | * To enable interoperability, this specification defines an "iso" transport type which all |
150 | * implementations that support CD-ROM devices are required to support. The iso transport |
151 | * communicates the environment 1346 document by making a dynamically generated ISO image |
152 | * available to the guest software. To support the iso transport type, prior to booting a virtual |
153 | * machine, an implementation shall make an ISO 9660 read-only disk image available as backing |
154 | * for a disconnected CD-ROM. If the iso transport is selected for a VirtualHardwareSection, at |
155 | * least one disconnected CD-ROM device shall be present in this section. |
156 | * <p/> |
157 | * Support for the "iso" transport type is not a requirement for virtual hardware architectures |
158 | * or guest 1351 operating systems which do not have CD-ROM device support. |
159 | * |
160 | * @return |
161 | */ |
162 | public Set<String> getTransports() { |
163 | return transports; |
164 | } |
165 | |
166 | public VirtualSystemSettingData getSystem() { |
167 | return virtualSystem; |
168 | } |
169 | |
170 | public Set<? extends ResourceAllocationSettingData> getItems() { |
171 | return items; |
172 | } |
173 | |
174 | @Override |
175 | public int hashCode() { |
176 | final int prime = 31; |
177 | int result = super.hashCode(); |
178 | result = prime * result + ((items == null) ? 0 : items.hashCode()); |
179 | result = prime * result + ((transports == null) ? 0 : transports.hashCode()); |
180 | result = prime * result + ((virtualSystem == null) ? 0 : virtualSystem.hashCode()); |
181 | return result; |
182 | } |
183 | |
184 | @Override |
185 | public boolean equals(Object obj) { |
186 | if (this == obj) |
187 | return true; |
188 | if (!super.equals(obj)) |
189 | return false; |
190 | if (getClass() != obj.getClass()) |
191 | return false; |
192 | VirtualHardwareSection other = (VirtualHardwareSection) obj; |
193 | if (items == null) { |
194 | if (other.items != null) |
195 | return false; |
196 | } else if (!items.equals(other.items)) |
197 | return false; |
198 | if (transports == null) { |
199 | if (other.transports != null) |
200 | return false; |
201 | } else if (!transports.equals(other.transports)) |
202 | return false; |
203 | if (virtualSystem == null) { |
204 | if (other.virtualSystem != null) |
205 | return false; |
206 | } else if (!virtualSystem.equals(other.virtualSystem)) |
207 | return false; |
208 | return true; |
209 | } |
210 | |
211 | @Override |
212 | public String toString() { |
213 | return String.format("[info=%s, items=%s, transports=%s, virtualSystem=%s]", info, |
214 | items, transports, virtualSystem); |
215 | } |
216 | |
217 | } |