| 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 static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Date; |
| 24 | import java.util.Map; |
| 25 | import java.util.Map.Entry; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import javax.annotation.Resource; |
| 29 | import javax.inject.Inject; |
| 30 | |
| 31 | import org.jclouds.aws.util.AWSUtils; |
| 32 | import org.jclouds.date.DateService; |
| 33 | import org.jclouds.ec2.domain.Attachment; |
| 34 | import org.jclouds.ec2.domain.Volume; |
| 35 | import org.jclouds.http.HttpRequest; |
| 36 | import org.jclouds.http.functions.ParseSax; |
| 37 | import org.jclouds.location.Region; |
| 38 | import org.jclouds.location.Zone; |
| 39 | import org.jclouds.logging.Logger; |
| 40 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 41 | import org.xml.sax.Attributes; |
| 42 | |
| 43 | import com.google.common.base.Supplier; |
| 44 | import com.google.common.base.Suppliers; |
| 45 | import com.google.common.collect.Maps; |
| 46 | import com.google.common.collect.Sets; |
| 47 | |
| 48 | /** |
| 49 | * |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | public class CreateVolumeResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Volume> { |
| 53 | protected StringBuilder currentText = new StringBuilder(); |
| 54 | |
| 55 | @Resource |
| 56 | protected Logger logger = Logger.NULL; |
| 57 | @Inject |
| 58 | protected DateService dateService; |
| 59 | @Inject |
| 60 | @Region |
| 61 | protected Supplier<String> defaultRegion; |
| 62 | @Inject |
| 63 | @Zone |
| 64 | protected Supplier<Map<String, Supplier<Set<String>>>> regionToZonesSupplier; |
| 65 | @Inject |
| 66 | @Zone |
| 67 | protected Supplier<Set<String>> zonesSupplier; |
| 68 | |
| 69 | protected String id; |
| 70 | protected int size; |
| 71 | protected String snapshotId; |
| 72 | protected String availabilityZone; |
| 73 | protected Volume.Status volumeStatus; |
| 74 | protected Date createTime; |
| 75 | protected Set<Attachment> attachments = Sets.newLinkedHashSet(); |
| 76 | |
| 77 | protected String volumeId; |
| 78 | protected String instanceId; |
| 79 | protected String device; |
| 80 | protected Attachment.Status attachmentStatus; |
| 81 | protected Date attachTime; |
| 82 | |
| 83 | protected boolean inAttachmentSet; |
| 84 | |
| 85 | protected String region; |
| 86 | |
| 87 | public Volume getResult() { |
| 88 | return newVolume(); |
| 89 | } |
| 90 | |
| 91 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
| 92 | if (qName.equals("attachmentSet")) { |
| 93 | inAttachmentSet = true; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public void endElement(String uri, String name, String qName) { |
| 98 | if (qName.equals("volumeId")) { |
| 99 | if (inAttachmentSet) { |
| 100 | volumeId = currentText.toString().trim(); |
| 101 | } else { |
| 102 | id = currentText.toString().trim(); |
| 103 | } |
| 104 | } else if (qName.equals("size")) { |
| 105 | size = Integer.parseInt(currentText.toString().trim()); |
| 106 | } else if (qName.equals("availabilityZone")) { |
| 107 | availabilityZone = currentText.toString().trim(); |
| 108 | } else if (qName.equals("volumeId")) { |
| 109 | if (inAttachmentSet) { |
| 110 | volumeId = currentText.toString().trim(); |
| 111 | } else { |
| 112 | id = currentText.toString().trim(); |
| 113 | } |
| 114 | } else if (qName.equals("status")) { |
| 115 | if (inAttachmentSet) { |
| 116 | attachmentStatus = Attachment.Status.fromValue(currentText.toString().trim()); |
| 117 | } else { |
| 118 | volumeStatus = Volume.Status.fromValue(currentText.toString().trim()); |
| 119 | } |
| 120 | } else if (qName.equals("createTime")) { |
| 121 | createTime = dateService.iso8601DateParse(currentText.toString().trim()); |
| 122 | } else if (qName.equals("attachmentSet")) { |
| 123 | inAttachmentSet = false; |
| 124 | } else if (qName.equals("instanceId")) { |
| 125 | instanceId = currentText.toString().trim(); |
| 126 | } else if (qName.equals("snapshotId")) { |
| 127 | snapshotId = currentText.toString().trim(); |
| 128 | if (snapshotId.equals("")) |
| 129 | snapshotId = null; |
| 130 | } else if (qName.equals("device")) { |
| 131 | device = currentText.toString().trim(); |
| 132 | } else if (qName.equals("attachTime")) { |
| 133 | attachTime = dateService.iso8601DateParse(currentText.toString().trim()); |
| 134 | } else if (qName.equals("item")) { |
| 135 | if (inAttachmentSet) { |
| 136 | attachments.add(new Attachment(region, volumeId, instanceId, device, attachmentStatus, attachTime)); |
| 137 | volumeId = null; |
| 138 | instanceId = null; |
| 139 | device = null; |
| 140 | attachmentStatus = null; |
| 141 | attachTime = null; |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | currentText = new StringBuilder(); |
| 146 | } |
| 147 | |
| 148 | private Volume newVolume() { |
| 149 | Volume volume = new Volume(region, id, size, snapshotId, availabilityZone, volumeStatus, createTime, attachments); |
| 150 | id = null; |
| 151 | size = 0; |
| 152 | snapshotId = null; |
| 153 | availabilityZone = null; |
| 154 | volumeStatus = null; |
| 155 | createTime = null; |
| 156 | attachments = Sets.newLinkedHashSet(); |
| 157 | return volume; |
| 158 | } |
| 159 | |
| 160 | public void characters(char ch[], int start, int length) { |
| 161 | currentText.append(ch, start, length); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public CreateVolumeResponseHandler setContext(HttpRequest request) { |
| 166 | super.setContext(request); |
| 167 | region = AWSUtils.findRegionInArgsOrNull(getRequest()); |
| 168 | if (region == null) { |
| 169 | Set<String> zones = zonesSupplier.get(); |
| 170 | String zone = findAvailabilityZoneInArgsOrNull(getRequest(), zones); |
| 171 | if (zone != null) { |
| 172 | Map<String, Set<String>> regionToZones = Maps.transformValues(regionToZonesSupplier.get(), Suppliers |
| 173 | .<Set<String>> supplierFunction()); |
| 174 | for (Entry<String, Set<String>> entry : regionToZones.entrySet()) { |
| 175 | if (entry.getValue().contains(zone)) { |
| 176 | region = entry.getKey(); |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | checkNotNull(regionToZones, String.format("zone %s not in %s", zone, regionToZones)); |
| 182 | } else { |
| 183 | region = defaultRegion.get(); |
| 184 | } |
| 185 | } |
| 186 | return this; |
| 187 | } |
| 188 | |
| 189 | public static String findAvailabilityZoneInArgsOrNull(GeneratedHttpRequest<?> gRequest, Set<String> zones) { |
| 190 | for (Object arg : gRequest.getArgs()) { |
| 191 | if (arg instanceof String) { |
| 192 | String zone = (String) arg; |
| 193 | if (zones.contains(zone)) |
| 194 | return zone; |
| 195 | } |
| 196 | } |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | } |