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 org.jclouds.util.SaxUtils.currentOrNull; |
22 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
23 | |
24 | import java.util.Date; |
25 | import java.util.Set; |
26 | |
27 | import javax.annotation.Resource; |
28 | import javax.inject.Inject; |
29 | |
30 | import org.jclouds.aws.util.AWSUtils; |
31 | import org.jclouds.date.DateService; |
32 | import org.jclouds.ec2.domain.Attachment; |
33 | import org.jclouds.ec2.domain.BlockDevice; |
34 | import org.jclouds.ec2.domain.InstanceState; |
35 | import org.jclouds.ec2.domain.Reservation; |
36 | import org.jclouds.ec2.domain.RootDeviceType; |
37 | import org.jclouds.ec2.domain.RunningInstance; |
38 | import org.jclouds.ec2.domain.RunningInstance.Builder; |
39 | import org.jclouds.http.functions.ParseSax.HandlerForGeneratedRequestWithResult; |
40 | import org.jclouds.location.Region; |
41 | import org.jclouds.logging.Logger; |
42 | import org.xml.sax.Attributes; |
43 | |
44 | import com.google.common.base.Supplier; |
45 | import com.google.common.collect.Sets; |
46 | import com.google.inject.Provider; |
47 | |
48 | /** |
49 | * |
50 | * @author Adrian Cole |
51 | */ |
52 | public abstract class BaseReservationHandler<T> extends HandlerForGeneratedRequestWithResult<T> { |
53 | |
54 | @Resource |
55 | protected Logger logger = Logger.NULL; |
56 | |
57 | protected final DateService dateService; |
58 | protected final Supplier<String> defaultRegion; |
59 | protected final Provider<Builder> builderProvider; |
60 | |
61 | @Inject |
62 | public BaseReservationHandler(DateService dateService, @Region Supplier<String> defaultRegion, |
63 | Provider<RunningInstance.Builder> builderProvider) { |
64 | this.dateService = dateService; |
65 | this.defaultRegion = defaultRegion; |
66 | this.builderProvider = builderProvider; |
67 | this.builder = builderProvider.get(); |
68 | } |
69 | |
70 | protected StringBuilder currentText = new StringBuilder(); |
71 | |
72 | protected Builder builder; |
73 | |
74 | protected int itemDepth; |
75 | boolean inInstancesSet; |
76 | // attachments |
77 | private String volumeId; |
78 | private Attachment.Status attachmentStatus; |
79 | private Date attachTime; |
80 | private boolean deleteOnTermination; |
81 | private String deviceName; |
82 | |
83 | // reservation stuff |
84 | private Set<String> groupIds = Sets.newLinkedHashSet(); |
85 | private String ownerId; |
86 | private String requesterId; |
87 | private String reservationId; |
88 | |
89 | private Set<RunningInstance> instances = Sets.newLinkedHashSet(); |
90 | |
91 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
92 | if (equalsOrSuffix(qName, "item")) { |
93 | itemDepth++; |
94 | } else if (equalsOrSuffix(qName, "instancesSet")) { |
95 | inInstancesSet = true; |
96 | } |
97 | } |
98 | |
99 | public void endElement(String uri, String name, String qName) { |
100 | if (equalsOrSuffix(qName, "item")) { |
101 | inItem(); |
102 | itemDepth--; |
103 | } else if (equalsOrSuffix(qName, "instancesSet")) { |
104 | inInstancesSet = false; |
105 | } else if (equalsOrSuffix(qName, "groupId")) { |
106 | groupIds.add(currentOrNull(currentText)); |
107 | } else if (equalsOrSuffix(qName, "ownerId")) { |
108 | ownerId = currentOrNull(currentText); |
109 | } else if (equalsOrSuffix(qName, "requesterId")) { |
110 | requesterId = currentOrNull(currentText); |
111 | } else if (equalsOrSuffix(qName, "reservationId")) { |
112 | reservationId = currentOrNull(currentText); |
113 | } else if (equalsOrSuffix(qName, "amiLaunchIndex")) { |
114 | builder.amiLaunchIndex(currentOrNull(currentText)); |
115 | } else if (equalsOrSuffix(qName, "dnsName")) { |
116 | String dnsName = currentOrNull(currentText); |
117 | // Eucalyptus |
118 | if (!"0.0.0.0".equals(dnsName)) |
119 | builder.dnsName(dnsName); |
120 | } else if (equalsOrSuffix(qName, "imageId")) { |
121 | builder.imageId(currentOrNull(currentText)); |
122 | } else if (equalsOrSuffix(qName, "instanceId")) { |
123 | builder.instanceId(currentOrNull(currentText)); |
124 | } else if (equalsOrSuffix(qName, "name")) { |
125 | builder.instanceState(InstanceState.fromValue(currentOrNull(currentText))); |
126 | } else if (equalsOrSuffix(qName, "instanceType")) { |
127 | builder.instanceType(currentOrNull(currentText)); |
128 | } else if (equalsOrSuffix(qName, "ipAddress")) { |
129 | builder.ipAddress(currentOrNull(currentText)); |
130 | } else if (equalsOrSuffix(qName, "kernelId")) { |
131 | builder.kernelId(currentOrNull(currentText)); |
132 | } else if (equalsOrSuffix(qName, "keyName")) { |
133 | builder.keyName(currentOrNull(currentText)); |
134 | } else if (equalsOrSuffix(qName, "launchTime")) { |
135 | builder.launchTime(parseDate()); |
136 | } else if (equalsOrSuffix(qName, "availabilityZone")) { |
137 | builder.availabilityZone(currentOrNull(currentText)); |
138 | } else if (equalsOrSuffix(qName, "virtualizationType")) { |
139 | builder.virtualizationType(currentOrNull(currentText)); |
140 | } else if (equalsOrSuffix(qName, "platform")) { |
141 | builder.platform(currentOrNull(currentText)); |
142 | } else if (equalsOrSuffix(qName, "privateDnsName")) { |
143 | String privateDnsName = currentOrNull(currentText); |
144 | // Eucalyptus |
145 | if (!"0.0.0.0".equals(privateDnsName)) |
146 | builder.privateDnsName(privateDnsName); |
147 | } else if (equalsOrSuffix(qName, "privateIpAddress")) { |
148 | builder.privateIpAddress(currentOrNull(currentText)); |
149 | } else if (equalsOrSuffix(qName, "ramdiskId")) { |
150 | builder.ramdiskId(currentOrNull(currentText)); |
151 | } else if (equalsOrSuffix(qName, "reason")) { |
152 | builder.reason(currentOrNull(currentText)); |
153 | } else if (equalsOrSuffix(qName, "rootDeviceType")) { |
154 | builder.rootDeviceType(RootDeviceType.fromValue(currentOrNull(currentText))); |
155 | } else if (equalsOrSuffix(qName, "rootDeviceName")) { |
156 | builder.rootDeviceName(currentOrNull(currentText)); |
157 | } else if (equalsOrSuffix(qName, "deviceName")) { |
158 | deviceName = currentOrNull(currentText); |
159 | } else if (equalsOrSuffix(qName, "volumeId")) { |
160 | volumeId = currentOrNull(currentText); |
161 | } else if (equalsOrSuffix(qName, "status")) { |
162 | attachmentStatus = Attachment.Status.fromValue(currentText.toString().trim()); |
163 | } else if (equalsOrSuffix(qName, "attachTime")) { |
164 | attachTime = dateService.iso8601DateParse(currentText.toString().trim()); |
165 | } else if (equalsOrSuffix(qName, "deleteOnTermination")) { |
166 | deleteOnTermination = Boolean.parseBoolean(currentText.toString().trim()); |
167 | } else if (equalsOrSuffix(qName, "ebs")) { |
168 | builder.device(deviceName, new BlockDevice(volumeId, attachmentStatus, attachTime, deleteOnTermination)); |
169 | this.deviceName = null; |
170 | this.volumeId = null; |
171 | this.attachmentStatus = null; |
172 | this.attachTime = null; |
173 | this.deleteOnTermination = true; |
174 | } |
175 | currentText = new StringBuilder(); |
176 | } |
177 | |
178 | protected Date parseDate() { |
179 | try { |
180 | return dateService.iso8601DateParse(currentOrNull(currentText)); |
181 | } catch (RuntimeException e) { |
182 | // Eucalyptus |
183 | return dateService.iso8601SecondsDateParse(currentOrNull(currentText)); |
184 | } |
185 | } |
186 | |
187 | protected void inItem() { |
188 | if (endOfInstanceItem()) { |
189 | refineBuilderBeforeAddingInstance(); |
190 | instances.add(builder.build()); |
191 | builder = builderProvider.get(); |
192 | } |
193 | } |
194 | |
195 | protected void refineBuilderBeforeAddingInstance() { |
196 | String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; |
197 | |
198 | // Eucalyptus |
199 | if (builder.getIpAddress() == null && builder.getDnsName() != null && builder.getDnsName().matches(".*[0-9]$")) { |
200 | builder.ipAddress(builder.getDnsName()); |
201 | builder.dnsName(null); |
202 | } |
203 | if (builder.getPrivateIpAddress() == null && builder.getPrivateDnsName() != null |
204 | && builder.getPrivateDnsName().matches(".*[0-9]$")) { |
205 | builder.privateIpAddress(builder.getPrivateDnsName()); |
206 | builder.privateDnsName(null); |
207 | } |
208 | |
209 | builder.region((region == null) ? defaultRegion.get() : region); |
210 | builder.groupIds(groupIds); |
211 | } |
212 | |
213 | protected Builder builder() { |
214 | return builder; |
215 | } |
216 | |
217 | protected boolean endOfInstanceItem() { |
218 | return itemDepth <= 2 && inInstancesSet; |
219 | } |
220 | |
221 | public void characters(char ch[], int start, int length) { |
222 | currentText.append(ch, start, length); |
223 | } |
224 | |
225 | protected Reservation<? extends RunningInstance> newReservation() { |
226 | String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; |
227 | if (region == null) |
228 | region = defaultRegion.get(); |
229 | Reservation<? extends RunningInstance> info = new Reservation<RunningInstance>(region, groupIds, instances, |
230 | ownerId, requesterId, reservationId); |
231 | this.groupIds = Sets.newLinkedHashSet(); |
232 | this.instances = Sets.newLinkedHashSet(); |
233 | this.ownerId = null; |
234 | this.requesterId = null; |
235 | this.reservationId = null; |
236 | return info; |
237 | } |
238 | |
239 | } |