| 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.xml.ovf; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.jclouds.cim.ResourceAllocationSettingData; |
| 24 | import org.jclouds.cim.xml.ResourceAllocationSettingDataHandler; |
| 25 | import org.jclouds.util.SaxUtils; |
| 26 | import org.jclouds.vcloud.domain.ReferenceType; |
| 27 | import org.jclouds.vcloud.domain.ovf.EditableResourceAllocationSettingData; |
| 28 | import org.jclouds.vcloud.domain.ovf.VCloudHardDisk; |
| 29 | import org.jclouds.vcloud.domain.ovf.VCloudNetworkAdapter; |
| 30 | import org.jclouds.vcloud.util.Utils; |
| 31 | import org.xml.sax.Attributes; |
| 32 | |
| 33 | /** |
| 34 | * @author Adrian Cole |
| 35 | */ |
| 36 | public class VCloudResourceAllocationSettingDataHandler extends ResourceAllocationSettingDataHandler { |
| 37 | |
| 38 | private ReferenceType edit; |
| 39 | |
| 40 | private long capacity; |
| 41 | private int busType; |
| 42 | private String busSubType; |
| 43 | |
| 44 | private String ipAddress; |
| 45 | private boolean primaryNetworkConnection; |
| 46 | private String ipAddressingMode; |
| 47 | |
| 48 | public ResourceAllocationSettingData getResult() { |
| 49 | try { |
| 50 | ResourceAllocationSettingData from = super.getResult(); |
| 51 | if (edit != null) { |
| 52 | return EditableResourceAllocationSettingData.builder().fromResourceAllocationSettingData(from).edit(edit) |
| 53 | .build(); |
| 54 | } else if (busSubType != null) { |
| 55 | return VCloudHardDisk.builder().fromResourceAllocationSettingData(from).capacity(capacity).busType(busType) |
| 56 | .busSubType(busSubType).build(); |
| 57 | } else if (ipAddress != null) { |
| 58 | return VCloudNetworkAdapter.builder().fromResourceAllocationSettingData(from).ipAddress(ipAddress) |
| 59 | .primaryNetworkConnection(primaryNetworkConnection).ipAddressingMode(ipAddressingMode).build(); |
| 60 | } else { |
| 61 | return from; |
| 62 | } |
| 63 | } finally { |
| 64 | ipAddress = null; |
| 65 | primaryNetworkConnection = false; |
| 66 | ipAddressingMode = null; |
| 67 | capacity = -1; |
| 68 | busType = -1; |
| 69 | busSubType = null; |
| 70 | edit = null; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void startElement(String uri, String localName, String qName, Attributes attrs) { |
| 76 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 77 | if (qName.endsWith("Link")) { |
| 78 | this.edit = Utils.newReferenceType(attributes); |
| 79 | } else if (qName.endsWith("HostResource") && attributes.size() > 0) { |
| 80 | capacity = Long.parseLong(attributes.get("capacity")); |
| 81 | busType = Integer.parseInt(attributes.get("busType")); |
| 82 | busSubType = attributes.get("busSubType"); |
| 83 | } else if (qName.endsWith("Connection") && attributes.size() > 0) { |
| 84 | ipAddress = attributes.get("ipAddress"); |
| 85 | primaryNetworkConnection = Boolean.parseBoolean(attributes.get("primaryNetworkConnection")); |
| 86 | ipAddressingMode = attributes.get("ipAddressingMode"); |
| 87 | } |
| 88 | super.startElement(uri, localName, qName, attrs); |
| 89 | } |
| 90 | |
| 91 | } |