| 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 | */ |
| 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.annotation.Resource; |
| 29 | import javax.inject.Named; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.http.HttpRequest; |
| 33 | import org.jclouds.logging.Logger; |
| 34 | import org.jclouds.rest.binders.BindToStringPayload; |
| 35 | import org.jclouds.vcloud.domain.NetworkConnection; |
| 36 | import org.jclouds.vcloud.domain.NetworkConnectionSection; |
| 37 | |
| 38 | import com.google.common.base.Throwables; |
| 39 | import com.google.inject.Inject; |
| 40 | import com.jamesmurty.utils.XMLBuilder; |
| 41 | |
| 42 | /** |
| 43 | * |
| 44 | * @author Adrian Cole |
| 45 | * |
| 46 | */ |
| 47 | @Singleton |
| 48 | public class BindNetworkConnectionSectionToXmlPayload extends BindToStringPayload { |
| 49 | @Resource |
| 50 | protected Logger logger = Logger.NULL; |
| 51 | |
| 52 | protected final String ns; |
| 53 | protected final String schema; |
| 54 | |
| 55 | @Inject |
| 56 | public BindNetworkConnectionSectionToXmlPayload(BindToStringPayload stringBinder, |
| 57 | @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema) { |
| 58 | this.ns = ns; |
| 59 | this.schema = schema; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public <R extends HttpRequest> R bindToRequest(R request, Object payload) { |
| 64 | checkArgument(checkNotNull(payload, "NetworkConnectionSection") instanceof NetworkConnectionSection, |
| 65 | "this binder is only valid for NetworkConnectionSection!"); |
| 66 | NetworkConnectionSection net = NetworkConnectionSection.class.cast(payload); |
| 67 | XMLBuilder networkConnectionSection; |
| 68 | try { |
| 69 | networkConnectionSection = XMLBuilder.create("NetworkConnectionSection").a("xmlns", ns) |
| 70 | .a("xmlns:ovf", "http://schemas.dmtf.org/ovf/envelope/1").a("type", net.getType()) |
| 71 | .a("href", net.getHref().toASCIIString()).a("ovf:required", "false"); |
| 72 | networkConnectionSection.e("ovf:Info").t(net.getInfo()); |
| 73 | |
| 74 | if (net.getPrimaryNetworkConnectionIndex() != null) |
| 75 | networkConnectionSection.e("PrimaryNetworkConnectionIndex").t( |
| 76 | net.getPrimaryNetworkConnectionIndex().toString()); |
| 77 | for (NetworkConnection networkConnection : net.getConnections()) { |
| 78 | XMLBuilder networkConnectionSectionChild = networkConnectionSection.e("NetworkConnection").a("network", |
| 79 | networkConnection.getNetwork()); |
| 80 | networkConnectionSectionChild.e("NetworkConnectionIndex").t( |
| 81 | networkConnection.getNetworkConnectionIndex() + ""); |
| 82 | if (networkConnection.getExternalIpAddress() != null) |
| 83 | networkConnectionSectionChild.e("ExternalIpAddress").t(networkConnection.getExternalIpAddress()); |
| 84 | if (networkConnection.getIpAddress() != null) |
| 85 | networkConnectionSectionChild.e("IpAddress").t(networkConnection.getIpAddress()); |
| 86 | networkConnectionSectionChild.e("IsConnected").t(networkConnection.isConnected() + ""); |
| 87 | if (networkConnection.getMACAddress() != null) |
| 88 | networkConnectionSectionChild.e("MACAddress").t(networkConnection.getMACAddress()); |
| 89 | if (networkConnection.getIpAddressAllocationMode() != null) |
| 90 | networkConnectionSectionChild.e("IpAddressAllocationMode").t( |
| 91 | networkConnection.getIpAddressAllocationMode().toString()); |
| 92 | } |
| 93 | |
| 94 | if (net.getEdit() != null) |
| 95 | networkConnectionSection.e("Link").a("rel", "edit").a("type", net.getType()) |
| 96 | .a("href", net.getHref().toASCIIString()); |
| 97 | |
| 98 | Properties outputProperties = new Properties(); |
| 99 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 100 | request = super.bindToRequest(request, networkConnectionSection.asString(outputProperties)); |
| 101 | request.getPayload().getContentMetadata().setContentType(net.getType()); |
| 102 | } catch (Exception e) { |
| 103 | Throwables.propagate(e); |
| 104 | } |
| 105 | return request; |
| 106 | } |
| 107 | |
| 108 | } |