| 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.trmk.vcloud_0_8.xml; |
| 20 | |
| 21 | import static org.jclouds.trmk.vcloud_0_8.util.Utils.newReferenceType; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import javax.inject.Inject; |
| 29 | |
| 30 | import org.jclouds.cim.ResourceAllocationSettingData; |
| 31 | import org.jclouds.cim.VirtualSystemSettingData; |
| 32 | import org.jclouds.cim.xml.ResourceAllocationSettingDataHandler; |
| 33 | import org.jclouds.cim.xml.VirtualSystemSettingDataHandler; |
| 34 | import org.jclouds.http.functions.ParseSax; |
| 35 | import org.jclouds.logging.Logger; |
| 36 | import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudMediaType; |
| 37 | import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType; |
| 38 | import org.jclouds.trmk.vcloud_0_8.domain.Status; |
| 39 | import org.jclouds.trmk.vcloud_0_8.domain.VApp; |
| 40 | import org.jclouds.trmk.vcloud_0_8.domain.internal.VAppImpl; |
| 41 | import org.jclouds.util.SaxUtils; |
| 42 | import org.xml.sax.Attributes; |
| 43 | import org.xml.sax.SAXException; |
| 44 | |
| 45 | import com.google.common.collect.ArrayListMultimap; |
| 46 | import com.google.common.collect.ListMultimap; |
| 47 | import com.google.common.collect.Sets; |
| 48 | |
| 49 | /** |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | public class VAppHandler extends ParseSax.HandlerWithResult<VApp> { |
| 53 | private final VirtualSystemSettingDataHandler systemHandler; |
| 54 | private final ResourceAllocationSettingDataHandler allocationHandler; |
| 55 | @Resource |
| 56 | protected Logger logger = Logger.NULL; |
| 57 | |
| 58 | @Inject |
| 59 | public VAppHandler(VirtualSystemSettingDataHandler systemHandler, |
| 60 | ResourceAllocationSettingDataHandler allocationHandler) { |
| 61 | this.systemHandler = systemHandler; |
| 62 | this.allocationHandler = allocationHandler; |
| 63 | } |
| 64 | |
| 65 | protected VirtualSystemSettingData system; |
| 66 | protected Set<ResourceAllocationSettingData> allocations = Sets.newLinkedHashSet(); |
| 67 | protected Status status; |
| 68 | protected final ListMultimap<String, String> networkToAddresses = ArrayListMultimap.create(); |
| 69 | protected StringBuilder currentText = new StringBuilder(); |
| 70 | protected String operatingSystemDescription; |
| 71 | protected boolean inOs; |
| 72 | protected String networkName; |
| 73 | protected String name; |
| 74 | protected Integer osType; |
| 75 | protected URI location; |
| 76 | protected Long size; |
| 77 | protected ReferenceType vDC; |
| 78 | protected Set<ReferenceType> extendedInfo = Sets.newLinkedHashSet(); |
| 79 | |
| 80 | public VApp getResult() { |
| 81 | return new VAppImpl(name, location, status, size, vDC, networkToAddresses, osType, operatingSystemDescription, |
| 82 | system, allocations, extendedInfo); |
| 83 | } |
| 84 | |
| 85 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 86 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 87 | if (qName.equals("VApp")) { |
| 88 | ReferenceType resource = newReferenceType(attributes); |
| 89 | name = resource.getName(); |
| 90 | location = resource.getHref(); |
| 91 | String statusString = attributes.get("status"); |
| 92 | status = Status.fromValue(statusString); |
| 93 | if (attributes.containsKey("size")) |
| 94 | size = new Long(attributes.get("size")); |
| 95 | } else if (qName.equals("Link")) { // type should never be missing |
| 96 | if (attributes.containsKey("type")) { |
| 97 | if (attributes.get("type").equals(TerremarkVCloudMediaType.VDC_XML)) { |
| 98 | vDC = newReferenceType(attributes); |
| 99 | } else { |
| 100 | extendedInfo.add(newReferenceType(attributes)); |
| 101 | } |
| 102 | } |
| 103 | } else if (qName.equals("OperatingSystemSection")) { |
| 104 | inOs = true; |
| 105 | if (attributes.containsKey("id")) |
| 106 | osType = Integer.parseInt(attributes.get("id")); |
| 107 | } else if (qName.endsWith("NetworkConnection")) { |
| 108 | networkName = attributes.get("Network"); |
| 109 | } else { |
| 110 | systemHandler.startElement(uri, localName, qName, attrs); |
| 111 | allocationHandler.startElement(uri, localName, qName, attrs); |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void endElement(String uri, String localName, String qName) throws SAXException { |
| 118 | if (qName.equals("OperatingSystemSection")) { |
| 119 | inOs = false; |
| 120 | } else if (inOs && qName.equals("Description")) { |
| 121 | operatingSystemDescription = currentText.toString().trim(); |
| 122 | } else if (qName.endsWith("IpAddress")) { |
| 123 | networkToAddresses.put(networkName, currentText.toString().trim()); |
| 124 | } else if (qName.equals("System")) { |
| 125 | systemHandler.endElement(uri, localName, qName); |
| 126 | system = systemHandler.getResult(); |
| 127 | } else if (qName.equals("Item")) { |
| 128 | allocationHandler.endElement(uri, localName, qName); |
| 129 | allocations.add(allocationHandler.getResult()); |
| 130 | } else { |
| 131 | systemHandler.endElement(uri, localName, qName); |
| 132 | allocationHandler.endElement(uri, localName, qName); |
| 133 | } |
| 134 | currentText = new StringBuilder(); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void characters(char ch[], int start, int length) { |
| 139 | currentText.append(ch, start, length); |
| 140 | systemHandler.characters(ch, start, length); |
| 141 | allocationHandler.characters(ch, start, length); |
| 142 | } |
| 143 | |
| 144 | } |