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.savvis.vpdc.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  
25  import java.net.URI;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import javax.inject.Singleton;
30  import javax.ws.rs.core.MediaType;
31  import javax.xml.parsers.FactoryConfigurationError;
32  import javax.xml.parsers.ParserConfigurationException;
33  
34  import org.jclouds.http.HttpRequest;
35  import org.jclouds.rest.MapBinder;
36  import org.jclouds.rest.binders.BindToStringPayload;
37  import org.jclouds.rest.internal.GeneratedHttpRequest;
38  import org.jclouds.savvis.vpdc.domain.FirewallRule;
39  
40  import com.jamesmurty.utils.XMLBuilder;
41  
42  /**
43   * 
44   * @author Kedar Dave
45   * 
46   */
47  @Singleton
48  public class BindCloneVMToXmlPayload extends BindToStringPayload implements MapBinder {
49     @Override
50     public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
51        throw new IllegalStateException("BindFirewallRuleToXmlPayload needs parameters");
52  
53     }
54  
55     protected URI findVAppURIInArgsOrNull(GeneratedHttpRequest<?> gRequest) {
56        for (Object arg : gRequest.getArgs()) {
57           if (arg instanceof URI) {
58              return (URI) arg;
59           } else if (arg instanceof FirewallRule[]) {
60          	 URI[] rules = (URI[]) arg;
61              return (rules.length > 0) ? rules[0] : null;
62           }
63        }
64        return null;
65     }
66  
67     @Override
68     public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
69        checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest<?>,
70              "this binder is only valid for GeneratedHttpRequests!");
71        GeneratedHttpRequest<?> gRequest = (GeneratedHttpRequest<?>) request;
72        checkState(gRequest.getArgs() != null, "args should be initialized at this point");
73  
74        request = super.bindToRequest(request,
75              generateXml(findVAppURIInArgsOrNull(gRequest), postParams.get("name"), postParams.get("networkTierName")));
76        request.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_XML);
77        return request;
78     }
79  
80     public String generateXml(URI vAppURI, String newVAppName, String networkTierName) {
81        checkNotNull(vAppURI, "vAppURI");
82  
83        try {
84           XMLBuilder rootBuilder = buildRoot(newVAppName);
85           addVAppSection(rootBuilder, vAppURI, networkTierName);
86           Properties outputProperties = new Properties();
87           return rootBuilder.asString(outputProperties);
88        } catch (Exception e) {
89           return null;
90        }
91     }
92  
93     void addVAppSection(XMLBuilder rootBuilder, URI vAppURI, String networkTierName) {
94  	  String vAppStr = "vApp/";
95  	  // TODO: Savvis returns network names with a - instead of space on getNetworkInVDC call,
96  	  // fix this once savvis api starts returning correctly
97        rootBuilder.e("Description").t(networkTierName.replace("-", " "));
98        String genericVAppURI = vAppURI.toString().substring(0, vAppURI.toString().indexOf("vApp") + vAppStr.length());
99        rootBuilder.e("VApp").a("href", genericVAppURI).a("type", "application/vnd.vmware.vcloud.vApp+xml");
100    }
101 
102    protected XMLBuilder buildRoot(String newVAppName) throws ParserConfigurationException, FactoryConfigurationError {
103 	      XMLBuilder rootBuilder = XMLBuilder.create("CloneVAppParams")
104 	      		.a("xmlns", "http://www.vmware.com/vcloud/v0.8")
105 	      		.a("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
106 	      		.a("name", newVAppName)
107 	      		.a("deploy" , "true")
108 	      		.a("powerOn" , "true")
109 	            .a("xsi:schemaLocation", "http://www.vmware.com/vcloud/v0.8 https://api.symphonyvpdc.savvis.net/ns/vcloud.xsd");
110 	      return rootBuilder;
111 	   }
112 
113    protected String ifNullDefaultTo(String value, String defaultValue) {
114       return value != null ? value : checkNotNull(defaultValue, "defaultValue");
115    }
116 
117 }