| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.vcloud.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.Constants.PROPERTY_API_VERSION; |
| 25 | import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_DEFAULT_FENCEMODE; |
| 26 | import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; |
| 27 | import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA; |
| 28 | |
| 29 | import java.net.URI; |
| 30 | import java.util.Map; |
| 31 | import java.util.Properties; |
| 32 | import java.util.SortedMap; |
| 33 | import java.util.Map.Entry; |
| 34 | |
| 35 | import javax.annotation.Nullable; |
| 36 | import javax.inject.Named; |
| 37 | import javax.inject.Singleton; |
| 38 | import javax.xml.parsers.FactoryConfigurationError; |
| 39 | import javax.xml.parsers.ParserConfigurationException; |
| 40 | import javax.xml.transform.TransformerException; |
| 41 | |
| 42 | import org.jclouds.cim.ResourceAllocationSettingData.ResourceType; |
| 43 | import org.jclouds.http.HttpRequest; |
| 44 | import org.jclouds.rest.MapBinder; |
| 45 | import org.jclouds.rest.binders.BindToStringPayload; |
| 46 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 47 | import org.jclouds.vcloud.domain.network.NetworkConfig; |
| 48 | import org.jclouds.vcloud.endpoints.Network; |
| 49 | import org.jclouds.vcloud.options.InstantiateVAppTemplateOptions; |
| 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 BindInstantiateVCloudExpressVAppTemplateParamsToXmlPayload 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 URI defaultNetwork; |
| 71 | private final String defaultFenceMode; |
| 72 | private final String apiVersion; |
| 73 | |
| 74 | @Inject |
| 75 | public BindInstantiateVCloudExpressVAppTemplateParamsToXmlPayload(BindToStringPayload stringBinder, |
| 76 | @Named(PROPERTY_API_VERSION) String apiVersion, @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, |
| 77 | @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema, @Network URI network, |
| 78 | @Named(PROPERTY_VCLOUD_DEFAULT_FENCEMODE) String fenceMode) { |
| 79 | this.ns = ns; |
| 80 | this.apiVersion = apiVersion; |
| 81 | this.schema = schema; |
| 82 | this.stringBinder = stringBinder; |
| 83 | this.defaultNetwork = network; |
| 84 | this.defaultFenceMode = fenceMode; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) { |
| 89 | checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest<?>, |
| 90 | "this binder is only valid for GeneratedHttpRequests!"); |
| 91 | GeneratedHttpRequest<?> gRequest = (GeneratedHttpRequest<?>) request; |
| 92 | checkState(gRequest.getArgs() != null, "args should be initialized at this point"); |
| 93 | String name = checkNotNull(postParams.remove("name"), "name"); |
| 94 | String template = checkNotNull(postParams.remove("template"), "template"); |
| 95 | |
| 96 | SortedMap<ResourceType, String> virtualHardwareQuantity = Maps.newTreeMap(); |
| 97 | |
| 98 | InstantiateVAppTemplateOptions options = findOptionsInArgsOrNull(gRequest); |
| 99 | String network = (defaultNetwork != null) ? defaultNetwork.toASCIIString() : null; |
| 100 | String fenceMode = defaultFenceMode; |
| 101 | String networkName = name; |
| 102 | if (options != null) { |
| 103 | if (options.getNetworkConfig().size() > 0) { |
| 104 | NetworkConfig config = Iterables.get(options.getNetworkConfig(), 0); |
| 105 | network = ifNullDefaultTo(config.getParentNetwork(), network); |
| 106 | fenceMode = ifNullDefaultTo(config.getFenceMode(), defaultFenceMode); |
| 107 | if (apiVersion.indexOf("0.8") != -1 && fenceMode.equals("bridged")) |
| 108 | fenceMode = "allowInOut"; |
| 109 | networkName = ifNullDefaultTo(config.getNetworkName(), networkName); |
| 110 | } |
| 111 | addQuantity(options, virtualHardwareQuantity); |
| 112 | } |
| 113 | try { |
| 114 | return stringBinder.bindToRequest(request, generateXml(name, template, virtualHardwareQuantity, networkName, |
| 115 | fenceMode, URI.create(network))); |
| 116 | } catch (ParserConfigurationException e) { |
| 117 | throw new RuntimeException(e); |
| 118 | } catch (FactoryConfigurationError e) { |
| 119 | throw new RuntimeException(e); |
| 120 | } catch (TransformerException e) { |
| 121 | throw new RuntimeException(e); |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |
| 126 | protected String generateXml(String name, String template, SortedMap<ResourceType, String> virtualHardwareQuantity, |
| 127 | String networkName, @Nullable String fenceMode, URI network) throws ParserConfigurationException, |
| 128 | FactoryConfigurationError, TransformerException { |
| 129 | XMLBuilder rootBuilder = buildRoot(name); |
| 130 | |
| 131 | rootBuilder.e("VAppTemplate").a("href", template); |
| 132 | |
| 133 | XMLBuilder instantiationParamsBuilder = rootBuilder.e("InstantiationParams"); |
| 134 | addVirtualQuantityIfPresent(instantiationParamsBuilder, virtualHardwareQuantity); |
| 135 | addNetworkConfig(instantiationParamsBuilder, networkName, fenceMode, network); |
| 136 | Properties outputProperties = new Properties(); |
| 137 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 138 | return rootBuilder.asString(outputProperties); |
| 139 | } |
| 140 | |
| 141 | protected void addNetworkConfig(XMLBuilder instantiationParamsBuilder, String name, @Nullable String fenceMode, |
| 142 | URI network) { |
| 143 | XMLBuilder networkConfigBuilder = instantiationParamsBuilder.e("NetworkConfigSection").e("NetworkConfig").a( |
| 144 | "name", name); |
| 145 | if (fenceMode != null) { |
| 146 | XMLBuilder featuresBuilder = networkConfigBuilder.e("Features"); |
| 147 | featuresBuilder.e("FenceMode").t(fenceMode); |
| 148 | } |
| 149 | networkConfigBuilder.e("NetworkAssociation").a("href", network.toASCIIString()); |
| 150 | } |
| 151 | |
| 152 | protected void addVirtualQuantityIfPresent(XMLBuilder instantiationParamsBuilder, |
| 153 | SortedMap<ResourceType, String> virtualHardwareQuantity) { |
| 154 | if (virtualHardwareQuantity.size() > 0) { |
| 155 | XMLBuilder virtualHardwareSectionBuilder = instantiationParamsBuilder.e("VirtualHardwareSection").a( |
| 156 | "xmlns:q1", ns); |
| 157 | for (Entry<ResourceType, String> entry : virtualHardwareQuantity.entrySet()) { |
| 158 | XMLBuilder itemBuilder = virtualHardwareSectionBuilder.e("Item").a("xmlns", |
| 159 | "http://schemas.dmtf.org/ovf/envelope/1"); |
| 160 | itemBuilder.e("InstanceID").a("xmlns", |
| 161 | "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData").t( |
| 162 | virtualHardwareToInstanceId.get(entry.getKey())); |
| 163 | itemBuilder.e("ResourceType").a("xmlns", |
| 164 | "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData").t( |
| 165 | entry.getKey().value()); |
| 166 | itemBuilder.e("VirtualQuantity").a("xmlns", |
| 167 | "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData").t( |
| 168 | entry.getValue()); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | protected XMLBuilder buildRoot(String name) throws ParserConfigurationException, FactoryConfigurationError { |
| 174 | XMLBuilder rootBuilder = XMLBuilder.create("InstantiateVAppTemplateParams").a("name", name).a("xmlns", ns).a( |
| 175 | "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").a("xsi:schemaLocation", ns + " " + schema).a( |
| 176 | "xmlns:ovf", "http://schemas.dmtf.org/ovf/envelope/1"); |
| 177 | return rootBuilder; |
| 178 | } |
| 179 | |
| 180 | protected InstantiateVAppTemplateOptions findOptionsInArgsOrNull(GeneratedHttpRequest<?> gRequest) { |
| 181 | for (Object arg : gRequest.getArgs()) { |
| 182 | if (arg instanceof InstantiateVAppTemplateOptions) { |
| 183 | return (InstantiateVAppTemplateOptions) arg; |
| 184 | } else if (arg instanceof InstantiateVAppTemplateOptions[]) { |
| 185 | InstantiateVAppTemplateOptions[] options = (InstantiateVAppTemplateOptions[]) arg; |
| 186 | return (options.length > 0) ? options[0] : null; |
| 187 | } |
| 188 | } |
| 189 | return null; |
| 190 | } |
| 191 | |
| 192 | private void addQuantity(InstantiateVAppTemplateOptions options, Map<ResourceType, String> virtualHardwareQuantity) { |
| 193 | if (options.getCpuCount() != null) { |
| 194 | virtualHardwareQuantity.put(ResourceType.PROCESSOR, options.getCpuCount()); |
| 195 | } |
| 196 | if (options.getMemorySizeMegabytes() != null) { |
| 197 | virtualHardwareQuantity.put(ResourceType.MEMORY, options.getMemorySizeMegabytes()); |
| 198 | } |
| 199 | if (options.getDiskSizeKilobytes() != null) { |
| 200 | virtualHardwareQuantity.put(ResourceType.DISK_DRIVE, options.getDiskSizeKilobytes()); |
| 201 | } |
| 202 | } |
| 203 | @Override |
| 204 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 205 | throw new IllegalStateException("InstantiateVAppTemplateParams is needs parameters"); |
| 206 | } |
| 207 | |
| 208 | protected String ifNullDefaultTo(Object value, String defaultValue) { |
| 209 | return value != null ? value.toString() : checkNotNull(defaultValue, "defaultValue"); |
| 210 | } |
| 211 | } |