EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.vcloud.binders]

COVERAGE SUMMARY FOR SOURCE FILE [BindInstantiateVCloudExpressVAppTemplateParamsToXmlPayload.java]

nameclass, %method, %block, %line, %
BindInstantiateVCloudExpressVAppTemplateParamsToXmlPayload.java100% (1/1)90%  (9/10)91%  (368/406)88%  (67/76)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindInstantiateVCloudExpressVAppTemplateParamsToXmlPayload100% (1/1)90%  (9/10)91%  (368/406)88%  (67/76)
bindToRequest (HttpRequest, Object): HttpRequest 0%   (0/1)0%   (0/5)0%   (0/1)
findOptionsInArgsOrNull (GeneratedHttpRequest): InstantiateVAppTemplateOptions 100% (1/1)63%  (22/35)72%  (5.1/7)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)85%  (110/130)77%  (19.9/26)
BindInstantiateVCloudExpressVAppTemplateParamsToXmlPayload (BindToStringPaylo... 100% (1/1)100% (30/30)100% (9/9)
addNetworkConfig (XMLBuilder, String, String, URI): void 100% (1/1)100% (30/30)100% (6/6)
addQuantity (InstantiateVAppTemplateOptions, Map): void 100% (1/1)100% (28/28)100% (7/7)
addVirtualQuantityIfPresent (XMLBuilder, SortedMap): void 100% (1/1)100% (68/68)100% (9/9)
buildRoot (String): XMLBuilder 100% (1/1)100% (32/32)100% (2/2)
generateXml (String, String, SortedMap, String, String, URI): String 100% (1/1)100% (38/38)100% (8/8)
ifNullDefaultTo (Object, String): String 100% (1/1)100% (10/10)100% (1/1)

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 */
19package org.jclouds.vcloud.binders;
20 
21import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkNotNull;
23import static com.google.common.base.Preconditions.checkState;
24import static org.jclouds.Constants.PROPERTY_API_VERSION;
25import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_DEFAULT_FENCEMODE;
26import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE;
27import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA;
28 
29import java.net.URI;
30import java.util.Map;
31import java.util.Properties;
32import java.util.SortedMap;
33import java.util.Map.Entry;
34 
35import javax.annotation.Nullable;
36import javax.inject.Named;
37import javax.inject.Singleton;
38import javax.xml.parsers.FactoryConfigurationError;
39import javax.xml.parsers.ParserConfigurationException;
40import javax.xml.transform.TransformerException;
41 
42import org.jclouds.cim.ResourceAllocationSettingData.ResourceType;
43import org.jclouds.http.HttpRequest;
44import org.jclouds.rest.MapBinder;
45import org.jclouds.rest.binders.BindToStringPayload;
46import org.jclouds.rest.internal.GeneratedHttpRequest;
47import org.jclouds.vcloud.domain.network.NetworkConfig;
48import org.jclouds.vcloud.endpoints.Network;
49import org.jclouds.vcloud.options.InstantiateVAppTemplateOptions;
50 
51import com.google.common.collect.ImmutableMap;
52import com.google.common.collect.Iterables;
53import com.google.common.collect.Maps;
54import com.google.inject.Inject;
55import com.jamesmurty.utils.XMLBuilder;
56 
57/**
58 * 
59 * @author Adrian Cole
60 * 
61 */
62@Singleton
63public 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}

[all classes][org.jclouds.vcloud.binders]
EMMA 2.0.5312 (C) Vladimir Roubtsov