EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.trmk.vcloud_0_8.binders]

COVERAGE SUMMARY FOR SOURCE FILE [BindInstantiateVAppTemplateParamsToXmlPayload.java]

nameclass, %method, %block, %line, %
BindInstantiateVAppTemplateParamsToXmlPayload.java100% (1/1)90%  (9/10)91%  (408/449)88%  (70.9/81)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindInstantiateVAppTemplateParamsToXmlPayload100% (1/1)90%  (9/10)91%  (408/449)88%  (70.9/81)
bindToRequest (HttpRequest, Object): HttpRequest 0%   (0/1)0%   (0/5)0%   (0/1)
findOptionsInArgsOrNull (GeneratedHttpRequest): InstantiateVAppTemplateOptions 100% (1/1)64%  (29/45)73%  (8/11)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)83%  (99/119)75%  (17.9/24)
BindInstantiateVAppTemplateParamsToXmlPayload (BindToStringPayload, String, S... 100% (1/1)100% (32/32)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% (19/19)100% (5/5)
addVirtualQuantityIfPresent (XMLBuilder, SortedMap): void 100% (1/1)100% (119/119)100% (14/14)
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 * 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 */
19package org.jclouds.trmk.vcloud_0_8.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.trmk.vcloud_0_8.reference.VCloudConstants.PROPERTY_VCLOUD_DEFAULT_FENCEMODE;
25import static org.jclouds.trmk.vcloud_0_8.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE;
26import static org.jclouds.trmk.vcloud_0_8.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA;
27 
28import java.net.URI;
29import java.util.Map;
30import java.util.Map.Entry;
31import java.util.Properties;
32import java.util.SortedMap;
33 
34import org.jclouds.javax.annotation.Nullable;
35import javax.inject.Named;
36import javax.inject.Singleton;
37import javax.xml.parsers.FactoryConfigurationError;
38import javax.xml.parsers.ParserConfigurationException;
39import javax.xml.transform.TransformerException;
40 
41import org.jclouds.cim.ResourceAllocationSettingData.ResourceType;
42import org.jclouds.http.HttpRequest;
43import org.jclouds.rest.MapBinder;
44import org.jclouds.rest.binders.BindToStringPayload;
45import org.jclouds.rest.internal.GeneratedHttpRequest;
46import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType;
47import org.jclouds.trmk.vcloud_0_8.endpoints.Network;
48import org.jclouds.trmk.vcloud_0_8.options.InstantiateVAppTemplateOptions;
49import org.jclouds.trmk.vcloud_0_8.options.InstantiateVAppTemplateOptions.NetworkConfig;
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 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}

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