| 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.ec2.xml; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import javax.annotation.Resource; |
| 25 | import javax.inject.Inject; |
| 26 | |
| 27 | import org.jclouds.aws.util.AWSUtils; |
| 28 | import org.jclouds.ec2.domain.Hypervisor; |
| 29 | import org.jclouds.ec2.domain.Image; |
| 30 | import org.jclouds.ec2.domain.Image.Architecture; |
| 31 | import org.jclouds.ec2.domain.Image.EbsBlockDevice; |
| 32 | import org.jclouds.ec2.domain.Image.ImageState; |
| 33 | import org.jclouds.ec2.domain.Image.ImageType; |
| 34 | import org.jclouds.ec2.domain.RootDeviceType; |
| 35 | import org.jclouds.ec2.domain.VirtualizationType; |
| 36 | import org.jclouds.http.functions.ParseSax; |
| 37 | import org.jclouds.location.Region; |
| 38 | import org.jclouds.logging.Logger; |
| 39 | import org.xml.sax.Attributes; |
| 40 | |
| 41 | import com.google.common.base.Supplier; |
| 42 | import com.google.common.collect.Maps; |
| 43 | import com.google.common.collect.Sets; |
| 44 | |
| 45 | /** |
| 46 | * Parses the following XML document: |
| 47 | * <p/> |
| 48 | * DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2010-06-15/" |
| 49 | * |
| 50 | * @author Adrian Cole |
| 51 | * @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeImages.html" |
| 52 | * /> |
| 53 | */ |
| 54 | public class DescribeImagesResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Set<Image>> { |
| 55 | |
| 56 | @Inject |
| 57 | public DescribeImagesResponseHandler(@Region Supplier<String> defaultRegion) { |
| 58 | this.defaultRegion = defaultRegion; |
| 59 | } |
| 60 | |
| 61 | @Resource |
| 62 | protected Logger logger = Logger.NULL; |
| 63 | |
| 64 | protected Set<Image> contents = Sets.newLinkedHashSet(); |
| 65 | private StringBuilder currentText = new StringBuilder(); |
| 66 | private final Supplier<String> defaultRegion; |
| 67 | |
| 68 | private Architecture architecture; |
| 69 | private String name; |
| 70 | private String description; |
| 71 | private String imageId; |
| 72 | private String imageLocation; |
| 73 | private String imageOwnerId; |
| 74 | private ImageState imageState; |
| 75 | private ImageType imageType; |
| 76 | private boolean isPublic; |
| 77 | private String kernelId; |
| 78 | private String platform; |
| 79 | private Set<String> productCodes = Sets.newHashSet(); |
| 80 | private String ramdiskId; |
| 81 | private boolean inProductCodes; |
| 82 | private boolean inBlockDeviceMapping; |
| 83 | /** |
| 84 | * Eucalyptus 1.6 doesn't set rootDeviceType |
| 85 | */ |
| 86 | private RootDeviceType rootDeviceType = RootDeviceType.INSTANCE_STORE; |
| 87 | private Map<String, EbsBlockDevice> ebsBlockDevices = Maps.newHashMap(); |
| 88 | private String deviceName; |
| 89 | private String snapshotId; |
| 90 | private VirtualizationType virtualizationType = VirtualizationType.PARAVIRTUAL; |
| 91 | private Hypervisor hypervisor = Hypervisor.XEN; |
| 92 | |
| 93 | private int volumeSize; |
| 94 | private boolean deleteOnTermination = true;// correct default is true. |
| 95 | |
| 96 | private String rootDeviceName; |
| 97 | |
| 98 | public Set<Image> getResult() { |
| 99 | return contents; |
| 100 | } |
| 101 | |
| 102 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
| 103 | if (qName.equals("productCodes")) { |
| 104 | inProductCodes = true; |
| 105 | } else if (qName.equals("blockDeviceMapping")) { |
| 106 | inBlockDeviceMapping = true; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public void endElement(String uri, String name, String qName) { |
| 111 | if (qName.equals("architecture")) { |
| 112 | architecture = Architecture.fromValue(currentText.toString().trim()); |
| 113 | // Nova Diablo uses the wrong name for this field |
| 114 | } else if (qName.equals("name") || qName.equals("displayName")) { |
| 115 | this.name = currentText.toString().trim(); |
| 116 | } else if (qName.equals("description")) { |
| 117 | description = currentText.toString().trim(); |
| 118 | } else if (qName.equals("imageId")) { |
| 119 | imageId = currentText.toString().trim(); |
| 120 | } else if (qName.equals("deviceName")) { |
| 121 | deviceName = currentText.toString().trim(); |
| 122 | } else if (qName.equals("imageLocation")) { |
| 123 | imageLocation = currentText.toString().trim(); |
| 124 | } else if (qName.equals("imageOwnerId")) { |
| 125 | imageOwnerId = currentText.toString().trim(); |
| 126 | } else if (qName.equals("imageState")) { |
| 127 | imageState = ImageState.fromValue(currentText.toString().trim()); |
| 128 | // eucalyptus |
| 129 | } else if (qName.equals("imageType") || qName.equals("type")) { |
| 130 | imageType = ImageType.fromValue(currentText.toString().trim()); |
| 131 | } else if (qName.equals("isPublic")) { |
| 132 | isPublic = Boolean.parseBoolean(currentText.toString().trim()); |
| 133 | } else if (qName.equals("kernelId")) { |
| 134 | kernelId = currentText.toString().trim(); |
| 135 | } else if (qName.equals("platform")) { |
| 136 | platform = currentText.toString().trim(); |
| 137 | } else if (qName.equals("productCode")) { |
| 138 | productCodes.add(currentText.toString().trim()); |
| 139 | } else if (qName.equals("productCodes")) { |
| 140 | inProductCodes = false; |
| 141 | } else if (qName.equals("blockDeviceMapping")) { |
| 142 | inBlockDeviceMapping = false; |
| 143 | } else if (qName.equals("snapshotId")) { |
| 144 | snapshotId = currentText.toString().trim(); |
| 145 | } else if (qName.equals("volumeSize")) { |
| 146 | volumeSize = Integer.parseInt(currentText.toString().trim()); |
| 147 | } else if (qName.equals("deleteOnTermination")) { |
| 148 | deleteOnTermination = Boolean.parseBoolean(currentText.toString().trim()); |
| 149 | } else if (qName.equals("ramdiskId")) { |
| 150 | ramdiskId = currentText.toString().trim(); |
| 151 | } else if (qName.equals("rootDeviceType")) { |
| 152 | rootDeviceType = RootDeviceType.fromValue(currentText.toString().trim()); |
| 153 | } else if (qName.equals("rootDeviceName")) { |
| 154 | rootDeviceName = currentText.toString().trim(); |
| 155 | } else if (qName.equals("virtualizationType")) { |
| 156 | virtualizationType = VirtualizationType.fromValue(currentText.toString().trim()); |
| 157 | } else if (qName.equals("hypervisor")) { |
| 158 | hypervisor = Hypervisor.fromValue(currentText.toString().trim()); |
| 159 | } else if (qName.equals("item")) { |
| 160 | if (inBlockDeviceMapping) { |
| 161 | ebsBlockDevices.put(deviceName, new Image.EbsBlockDevice(snapshotId, volumeSize, deleteOnTermination)); |
| 162 | this.deviceName = null; |
| 163 | this.snapshotId = null; |
| 164 | this.volumeSize = 0; |
| 165 | this.deleteOnTermination = true; |
| 166 | } else if (!inProductCodes) { |
| 167 | try { |
| 168 | String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; |
| 169 | if (region == null) |
| 170 | region = defaultRegion.get(); |
| 171 | contents.add(new Image(region, architecture, this.name, description, imageId, imageLocation, |
| 172 | imageOwnerId, imageState, imageType, isPublic, productCodes, kernelId, platform, ramdiskId, |
| 173 | rootDeviceType, rootDeviceName, ebsBlockDevices, virtualizationType, hypervisor)); |
| 174 | } catch (NullPointerException e) { |
| 175 | logger.warn(e, "malformed image: %s", imageId); |
| 176 | } |
| 177 | this.name = null; |
| 178 | this.description = null; |
| 179 | this.architecture = null; |
| 180 | this.imageId = null; |
| 181 | this.imageLocation = null; |
| 182 | this.imageOwnerId = null; |
| 183 | this.imageState = null; |
| 184 | this.imageType = null; |
| 185 | this.isPublic = false; |
| 186 | this.kernelId = null; |
| 187 | this.platform = null; |
| 188 | this.productCodes = Sets.newHashSet(); |
| 189 | this.ramdiskId = null; |
| 190 | this.rootDeviceType = RootDeviceType.INSTANCE_STORE; |
| 191 | this.rootDeviceName = null; |
| 192 | this.ebsBlockDevices = Maps.newHashMap(); |
| 193 | this.virtualizationType = VirtualizationType.PARAVIRTUAL; |
| 194 | this.hypervisor = Hypervisor.XEN; |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | currentText = new StringBuilder(); |
| 199 | } |
| 200 | |
| 201 | public void characters(char ch[], int start, int length) { |
| 202 | currentText.append(ch, start, length); |
| 203 | } |
| 204 | } |