View Javadoc

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 org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE;
24  import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA;
25  
26  import java.util.Properties;
27  
28  import javax.inject.Named;
29  import javax.inject.Singleton;
30  
31  import org.jclouds.cim.ResourceAllocationSettingData.ResourceType;
32  import org.jclouds.http.HttpRequest;
33  import org.jclouds.rest.binders.BindToStringPayload;
34  
35  import com.google.common.base.Throwables;
36  import com.google.inject.Inject;
37  import com.jamesmurty.utils.XMLBuilder;
38  
39  /**
40   * 
41   * @author Adrian Cole
42   * 
43   */
44  @Singleton
45  public class BindCPUCountToXmlPayload extends BindToStringPayload {
46     protected final String ns;
47     protected final String schema;
48  
49     @Inject
50     public BindCPUCountToXmlPayload(BindToStringPayload stringBinder, @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns,
51           @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema) {
52        this.ns = ns;
53        this.schema = schema;
54     }
55  
56     private static final String RESOURCE_ALLOCATION_NS = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData";
57  
58     @Override
59     public <R extends HttpRequest> R bindToRequest(R request, Object payload) {
60        checkArgument(checkNotNull(payload, "cpuCount") instanceof Integer, "this binder is only valid for Integers!");
61        Integer cpuCount = Integer.class.cast(payload);
62        XMLBuilder cpuItem;
63        try {
64           cpuItem = XMLBuilder.create("Item").a("xmlns", ns).a("xmlns:rasd", RESOURCE_ALLOCATION_NS);
65           cpuItem.e("rasd:AllocationUnits").t("hertz * 10^6");
66           cpuItem.e("rasd:Description").t("Number of Virtual CPUs");
67           cpuItem.e("rasd:ElementName").t(cpuCount.toString() + " virtual CPU(s)");
68           cpuItem.e("rasd:InstanceID").t("4");
69           cpuItem.e("rasd:ResourceType").t(ResourceType.PROCESSOR.value());
70           cpuItem.e("rasd:VirtualQuantity").t(cpuCount.toString());
71           cpuItem.e("rasd:Weight").t("0");
72           Properties outputProperties = new Properties();
73           outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
74           request = super.bindToRequest(request, cpuItem.asString(outputProperties));
75        } catch (Exception e) {
76           Throwables.propagate(e);
77        }
78        return request;
79     }
80  
81  }