| 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.trmk.vcloud_0_8.binders; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | import static com.google.common.base.Preconditions.checkState; |
| 24 | import static org.jclouds.trmk.vcloud_0_8.reference.VCloudConstants.PROPERTY_VCLOUD_DEFAULT_FENCEMODE; |
| 25 | import static org.jclouds.trmk.vcloud_0_8.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; |
| 26 | import static org.jclouds.trmk.vcloud_0_8.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA; |
| 27 | |
| 28 | import java.net.URI; |
| 29 | import java.util.Map; |
| 30 | import java.util.Map.Entry; |
| 31 | import java.util.Properties; |
| 32 | import java.util.SortedMap; |
| 33 | |
| 34 | import org.jclouds.javax.annotation.Nullable; |
| 35 | import javax.inject.Named; |
| 36 | import javax.inject.Singleton; |
| 37 | import javax.xml.parsers.FactoryConfigurationError; |
| 38 | import javax.xml.parsers.ParserConfigurationException; |
| 39 | import javax.xml.transform.TransformerException; |
| 40 | |
| 41 | import org.jclouds.cim.ResourceAllocationSettingData.ResourceType; |
| 42 | import org.jclouds.http.HttpRequest; |
| 43 | import org.jclouds.rest.MapBinder; |
| 44 | import org.jclouds.rest.binders.BindToStringPayload; |
| 45 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 46 | import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType; |
| 47 | import org.jclouds.trmk.vcloud_0_8.endpoints.Network; |
| 48 | import org.jclouds.trmk.vcloud_0_8.options.InstantiateVAppTemplateOptions; |
| 49 | import org.jclouds.trmk.vcloud_0_8.options.InstantiateVAppTemplateOptions.NetworkConfig; |
| 50 | |
| 51 | import com.google.common.collect.ImmutableMap; |
| 52 | import com.google.common.collect.Iterables; |
| 53 | import com.google.common.collect.Maps; |
| 54 | import com.google.inject.Inject; |
| 55 | import com.jamesmurty.utils.XMLBuilder; |
| 56 | |
| 57 | /** |
| 58 | * |
| 59 | * @author Adrian Cole |
| 60 | * |
| 61 | */ |
| 62 | @Singleton |
| 63 | public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder { |
| 64 | |
| 65 | protected final String ns; |
| 66 | protected final String schema; |
| 67 | private final BindToStringPayload stringBinder; |
| 68 | protected final Map<ResourceType, String> virtualHardwareToInstanceId = ImmutableMap.of(ResourceType.PROCESSOR, "1", |
| 69 | ResourceType.MEMORY, "2", ResourceType.DISK_DRIVE, "9"); |
| 70 | private final ReferenceType defaultNetwork; |
| 71 | private final String defaultFenceMode; |
| 72 | |
| 73 | @Inject |
| 74 | public BindInstantiateVAppTemplateParamsToXmlPayload(BindToStringPayload stringBinder, |
| 75 | @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema, |
| 76 | @Network ReferenceType network, @Named(PROPERTY_VCLOUD_DEFAULT_FENCEMODE) String fenceMode) { |
| 77 | this.ns = ns; |
| 78 | this.schema = schema; |
| 79 | this.stringBinder = stringBinder; |
| 80 | this.defaultNetwork = network; |
| 81 | this.defaultFenceMode = fenceMode; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) { |
| 86 | checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest<?>, |
| 87 | "this binder is only valid for GeneratedHttpRequests!"); |
| 88 | GeneratedHttpRequest<?> gRequest = (GeneratedHttpRequest<?>) request; |
| 89 | checkState(gRequest.getArgs() != null, "args should be initialized at this point"); |
| 90 | String name = checkNotNull(postParams.remove("name"), "name"); |
| 91 | String template = checkNotNull(postParams.remove("template"), "template"); |
| 92 | |
| 93 | SortedMap<ResourceType, String> virtualHardwareQuantity = Maps.newTreeMap(); |
| 94 | |
| 95 | InstantiateVAppTemplateOptions options = findOptionsInArgsOrNull(gRequest); |
| 96 | String network = (defaultNetwork != null) ? defaultNetwork.getHref().toASCIIString() : null; |
| 97 | String fenceMode = defaultFenceMode; |
| 98 | String networkName = name; |
| 99 | if (options != null) { |
| 100 | if (options.getNetworkConfig().size() > 0) { |
| 101 | NetworkConfig config = Iterables.get(options.getNetworkConfig(), 0); |
| 102 | network = ifNullDefaultTo(config.getParentNetwork(), network); |
| 103 | fenceMode = ifNullDefaultTo(config.getFenceMode(), defaultFenceMode); |
| 104 | networkName = ifNullDefaultTo(config.getNetworkName(), networkName); |
| 105 | } |
| 106 | addQuantity(options, virtualHardwareQuantity); |
| 107 | } |
| 108 | try { |
| 109 | return stringBinder.bindToRequest(request, |
| 110 | generateXml(name, template, virtualHardwareQuantity, networkName, fenceMode, URI.create(network))); |
| 111 | } catch (ParserConfigurationException e) { |
| 112 | throw new RuntimeException(e); |
| 113 | } catch (FactoryConfigurationError e) { |
| 114 | throw new RuntimeException(e); |
| 115 | } catch (TransformerException e) { |
| 116 | throw new RuntimeException(e); |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | |
| 121 | protected String generateXml(String name, String template, SortedMap<ResourceType, String> virtualHardwareQuantity, |
| 122 | String networkName, @Nullable String fenceMode, URI network) throws ParserConfigurationException, |
| 123 | FactoryConfigurationError, TransformerException { |
| 124 | XMLBuilder rootBuilder = buildRoot(name); |
| 125 | |
| 126 | rootBuilder.e("VAppTemplate").a("href", template); |
| 127 | |
| 128 | XMLBuilder instantiationParamsBuilder = rootBuilder.e("InstantiationParams"); |
| 129 | addVirtualQuantityIfPresent(instantiationParamsBuilder, virtualHardwareQuantity); |
| 130 | addNetworkConfig(instantiationParamsBuilder, networkName, fenceMode, network); |
| 131 | Properties outputProperties = new Properties(); |
| 132 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 133 | return rootBuilder.asString(outputProperties); |
| 134 | } |
| 135 | |
| 136 | protected void addNetworkConfig(XMLBuilder instantiationParamsBuilder, String name, @Nullable String fenceMode, |
| 137 | URI network) { |
| 138 | XMLBuilder networkConfigBuilder = instantiationParamsBuilder.e("NetworkConfigSection").e("NetworkConfig") |
| 139 | .a("name", name); |
| 140 | if (fenceMode != null) { |
| 141 | XMLBuilder featuresBuilder = networkConfigBuilder.e("Features"); |
| 142 | featuresBuilder.e("FenceMode").t(fenceMode); |
| 143 | } |
| 144 | networkConfigBuilder.e("NetworkAssociation").a("href", network.toASCIIString()); |
| 145 | } |
| 146 | |
| 147 | protected XMLBuilder buildRoot(String name) throws ParserConfigurationException, FactoryConfigurationError { |
| 148 | XMLBuilder rootBuilder = XMLBuilder.create("InstantiateVAppTemplateParams").a("name", name).a("xmlns", ns) |
| 149 | .a("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").a("xsi:schemaLocation", ns + " " + schema) |
| 150 | .a("xmlns:ovf", "http://schemas.dmtf.org/ovf/envelope/1"); |
| 151 | return rootBuilder; |
| 152 | } |
| 153 | |
| 154 | private void addQuantity(InstantiateVAppTemplateOptions options, Map<ResourceType, String> virtualHardwareQuantity) { |
| 155 | if (options.getCpuCount() != null) { |
| 156 | virtualHardwareQuantity.put(ResourceType.PROCESSOR, options.getCpuCount()); |
| 157 | } |
| 158 | if (options.getMemorySizeMegabytes() != null) { |
| 159 | virtualHardwareQuantity.put(ResourceType.MEMORY, options.getMemorySizeMegabytes()); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 165 | throw new IllegalStateException("InstantiateVAppTemplateParams is needs parameters"); |
| 166 | } |
| 167 | |
| 168 | protected String ifNullDefaultTo(Object value, String defaultValue) { |
| 169 | return value != null ? value.toString() : checkNotNull(defaultValue, "defaultValue"); |
| 170 | } |
| 171 | |
| 172 | ThreadLocal<Map<String, String>> propLocal = new ThreadLocal<Map<String, String>>(); |
| 173 | |
| 174 | protected InstantiateVAppTemplateOptions findOptionsInArgsOrNull(GeneratedHttpRequest<?> gRequest) { |
| 175 | InstantiateVAppTemplateOptions options = null; |
| 176 | for (Object arg : gRequest.getArgs()) { |
| 177 | if (arg instanceof InstantiateVAppTemplateOptions) { |
| 178 | options = (InstantiateVAppTemplateOptions) arg; |
| 179 | } else if (arg instanceof InstantiateVAppTemplateOptions[]) { |
| 180 | InstantiateVAppTemplateOptions[] optionsA = (InstantiateVAppTemplateOptions[]) arg; |
| 181 | options = (optionsA.length > 0) ? optionsA[0] : null; |
| 182 | } |
| 183 | } |
| 184 | if (options != null) |
| 185 | propLocal.set(options.getProperties()); |
| 186 | return options; |
| 187 | } |
| 188 | |
| 189 | protected void addVirtualQuantityIfPresent(XMLBuilder instantiationParamsBuilder, |
| 190 | SortedMap<ResourceType, String> virtualHardwareQuantity) { |
| 191 | XMLBuilder productSectionBuilder = instantiationParamsBuilder.e("ProductSection").a("xmlns:q1", ns) |
| 192 | .a("xmlns:ovf", "http://schemas.dmtf.org/ovf/envelope/1"); |
| 193 | if (propLocal.get() != null) { |
| 194 | for (Entry<String, String> entry : propLocal.get().entrySet()) { |
| 195 | productSectionBuilder.e("Property").a("xmlns", "http://schemas.dmtf.org/ovf/envelope/1") |
| 196 | .a("ovf:key", entry.getKey()).a("ovf:value", entry.getValue()); |
| 197 | } |
| 198 | propLocal.set(null); |
| 199 | } |
| 200 | if (virtualHardwareQuantity.size() > 0) { |
| 201 | XMLBuilder virtualHardwareSectionBuilder = instantiationParamsBuilder.e("VirtualHardwareSection").a( |
| 202 | "xmlns:q1", ns); |
| 203 | for (Entry<ResourceType, String> entry : virtualHardwareQuantity.entrySet()) { |
| 204 | XMLBuilder itemBuilder = virtualHardwareSectionBuilder.e("Item").a("xmlns", |
| 205 | "http://schemas.dmtf.org/ovf/envelope/1"); |
| 206 | itemBuilder.e("InstanceID") |
| 207 | .a("xmlns", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData") |
| 208 | .t(virtualHardwareToInstanceId.get(entry.getKey())); |
| 209 | itemBuilder.e("ResourceType") |
| 210 | .a("xmlns", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData") |
| 211 | .t(entry.getKey().value()); |
| 212 | itemBuilder.e("VirtualQuantity") |
| 213 | .a("xmlns", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData") |
| 214 | .t(entry.getValue()); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |