EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.ec2.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [RunningInstanceToNodeMetadata.java]

nameclass, %method, %block, %line, %
RunningInstanceToNodeMetadata.java83%  (5/6)85%  (17/20)76%  (398/526)84%  (63.9/76)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class RunningInstanceToNodeMetadata$20%   (0/1)0%   (0/2)0%   (0/10)0%   (0/2)
RunningInstanceToNodeMetadata$2 (): void 0%   (0/1)0%   (0/3)0%   (0/1)
apply (Volume): boolean 0%   (0/1)0%   (0/7)0%   (0/1)
     
class RunningInstanceToNodeMetadata$1100% (1/1)50%  (1/2)18%  (6/34)50%  (1/2)
apply (Map$Entry): Volume 0%   (0/1)0%   (0/28)0%   (0/1)
RunningInstanceToNodeMetadata$1 (RunningInstance): void 100% (1/1)100% (6/6)100% (1/1)
     
class RunningInstanceToNodeMetadata$3100% (1/1)100% (2/2)50%  (15/30)64%  (1.3/2)
apply (String): boolean 100% (1/1)29%  (6/21)28%  (0.3/1)
RunningInstanceToNodeMetadata$3 (RunningInstanceToNodeMetadata, RunningInstan... 100% (1/1)100% (9/9)100% (1/1)
     
class RunningInstanceToNodeMetadata100% (1/1)100% (10/10)82%  (346/421)86%  (60.4/70)
getGroupForInstance (RunningInstance): String 100% (1/1)29%  (20/70)42%  (3.4/8)
addEBS (RunningInstance, Iterable): List 100% (1/1)75%  (18/24)75%  (3/4)
apply (RunningInstance): NodeMetadata 100% (1/1)89%  (17/19)80%  (4/5)
buildInstance (RunningInstance, NodeMetadataBuilder): NodeMetadataBuilder 100% (1/1)90%  (135/150)92%  (22/24)
findLocationWithId (String): Location 100% (1/1)95%  (35/37)86%  (6/7)
RunningInstanceToNodeMetadata (Map, Map, Supplier, Supplier, Supplier): void 100% (1/1)100% (36/36)100% (8/8)
addCredentialsForInstance (NodeMetadataBuilder, RunningInstance): void 100% (1/1)100% (22/22)100% (2/2)
getHardwareForInstance (RunningInstance): Hardware 100% (1/1)100% (32/32)100% (4/4)
getLocationForAvailabilityZoneOrRegion (RunningInstance): Location 100% (1/1)100% (14/14)100% (4/4)
parseHardware (RunningInstance): Hardware 100% (1/1)100% (17/17)100% (4/4)
     
class RunningInstanceToNodeMetadata$4100% (1/1)100% (2/2)100% (16/16)100% (2/2)
RunningInstanceToNodeMetadata$4 (RunningInstanceToNodeMetadata, RunningInstan... 100% (1/1)100% (9/9)100% (1/1)
apply (Hardware): boolean 100% (1/1)100% (7/7)100% (1/1)
     
class RunningInstanceToNodeMetadata$5100% (1/1)100% (2/2)100% (15/15)100% (2/2)
RunningInstanceToNodeMetadata$5 (RunningInstanceToNodeMetadata, String): void 100% (1/1)100% (9/9)100% (1/1)
apply (Location): boolean 100% (1/1)100% (6/6)100% (1/1)

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 */
19package org.jclouds.ec2.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.util.List;
24import java.util.Map;
25import java.util.Map.Entry;
26import java.util.NoSuchElementException;
27import java.util.Set;
28 
29import javax.annotation.Resource;
30import javax.inject.Inject;
31import javax.inject.Singleton;
32 
33import org.jclouds.collect.Memoized;
34import org.jclouds.compute.domain.Hardware;
35import org.jclouds.compute.domain.HardwareBuilder;
36import org.jclouds.compute.domain.Image;
37import org.jclouds.compute.domain.NodeMetadata;
38import org.jclouds.compute.domain.NodeMetadataBuilder;
39import org.jclouds.compute.domain.NodeState;
40import org.jclouds.compute.domain.Volume;
41import org.jclouds.compute.domain.internal.VolumeImpl;
42import org.jclouds.domain.Credentials;
43import org.jclouds.domain.Location;
44import org.jclouds.ec2.compute.domain.RegionAndName;
45import org.jclouds.ec2.domain.BlockDevice;
46import org.jclouds.ec2.domain.InstanceState;
47import org.jclouds.ec2.domain.RootDeviceType;
48import org.jclouds.ec2.domain.RunningInstance;
49import org.jclouds.logging.Logger;
50import org.jclouds.util.NullSafeCollections;
51 
52import com.google.common.annotations.VisibleForTesting;
53import com.google.common.base.Function;
54import com.google.common.base.Predicate;
55import com.google.common.base.Supplier;
56import com.google.common.cache.Cache;
57import com.google.common.collect.Iterables;
58import com.google.common.collect.Lists;
59import com.google.common.util.concurrent.UncheckedExecutionException;
60 
61/**
62 * @author Adrian Cole
63 */
64@Singleton
65public class RunningInstanceToNodeMetadata implements Function<RunningInstance, NodeMetadata> {
66 
67   @Resource
68   protected Logger logger = Logger.NULL;
69 
70   protected final Supplier<Set<? extends Location>> locations;
71   protected final Supplier<Set<? extends Hardware>> hardware;
72   protected final Supplier<Cache<RegionAndName, ? extends Image>> imageMap;
73   protected final Map<String, Credentials> credentialStore;
74   protected final Map<InstanceState, NodeState> instanceToNodeState;
75 
76   @Inject
77   protected RunningInstanceToNodeMetadata(Map<InstanceState, NodeState> instanceToNodeState,
78            Map<String, Credentials> credentialStore, Supplier<Cache<RegionAndName, ? extends Image>> imageMap,
79            @Memoized Supplier<Set<? extends Location>> locations, @Memoized Supplier<Set<? extends Hardware>> hardware) {
80      this.locations = checkNotNull(locations, "locations");
81      this.hardware = checkNotNull(hardware, "hardware");
82      this.imageMap = checkNotNull(imageMap, "imageMap");
83      this.instanceToNodeState = checkNotNull(instanceToNodeState, "instanceToNodeState");
84      this.credentialStore = checkNotNull(credentialStore, "credentialStore");
85   }
86 
87   @Override
88   public NodeMetadata apply(RunningInstance instance) {
89      if (instance == null || instance.getId() == null)
90         return null;
91      NodeMetadataBuilder builder = new NodeMetadataBuilder();
92      builder = buildInstance(instance, builder);
93      return builder.build();
94   }
95 
96   protected NodeMetadataBuilder buildInstance(RunningInstance instance, NodeMetadataBuilder builder) {
97      builder.providerId(instance.getId());
98      builder.id(instance.getRegion() + "/" + instance.getId());
99      String group = getGroupForInstance(instance);
100      builder.group(group);
101      // standard convention from aws-ec2, which might not be re-used outside.
102      if (instance.getPrivateDnsName() != null)
103         builder.hostname(instance.getPrivateDnsName().replaceAll("\\..*", ""));
104      addCredentialsForInstance(builder, instance);
105      builder.state(instanceToNodeState.get(instance.getInstanceState()));
106      builder.publicAddresses(NullSafeCollections.nullSafeSet(instance.getIpAddress()));
107      builder.privateAddresses(NullSafeCollections.nullSafeSet(instance.getPrivateIpAddress()));
108      builder.hardware(parseHardware(instance));
109      Location location = getLocationForAvailabilityZoneOrRegion(instance);
110      builder.location(location);
111      builder.imageId(instance.getRegion() + "/" + instance.getImageId());
112 
113      // extract the operating system from the image
114      RegionAndName regionAndName = new RegionAndName(instance.getRegion(), instance.getImageId());
115      try {
116         Image image = imageMap.get().getUnchecked(regionAndName);
117         if (image != null)
118            builder.operatingSystem(image.getOperatingSystem());
119      } catch (NullPointerException e) {
120         logger.debug("image not found for %s: %s", regionAndName, e);
121      } catch (UncheckedExecutionException e) {
122         logger.debug("error getting image for %s: %s", regionAndName, e);
123      }
124      return builder;
125   }
126 
127   protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) {
128      builder.credentials(credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId()));
129   }
130 
131   protected Hardware parseHardware(final RunningInstance instance) {
132      Hardware hardware = getHardwareForInstance(instance);
133 
134      if (hardware != null) {
135         hardware = HardwareBuilder.fromHardware(hardware).volumes(addEBS(instance, hardware.getVolumes())).build();
136      }
137      return hardware;
138   }
139 
140   @VisibleForTesting
141   static List<Volume> addEBS(final RunningInstance instance, Iterable<? extends Volume> volumes) {
142      Iterable<Volume> ebsVolumes = Iterables.transform(instance.getEbsBlockDevices().entrySet(),
143               new Function<Entry<String, BlockDevice>, Volume>() {
144 
145                  @Override
146                  public Volume apply(Entry<String, BlockDevice> from) {
147                     return new VolumeImpl(from.getValue().getVolumeId(), Volume.Type.SAN, null, from.getKey(),
148                              instance.getRootDeviceName() != null
149                                       && instance.getRootDeviceName().equals(from.getKey()), true);
150                  }
151               });
152 
153      if (instance.getRootDeviceType() == RootDeviceType.EBS) {
154         volumes = Iterables.filter(volumes, new Predicate<Volume>() {
155 
156            @Override
157            public boolean apply(Volume input) {
158               return !input.isBootDevice();
159            }
160 
161         });
162 
163      }
164      return Lists.newArrayList(Iterables.concat(volumes, ebsVolumes));
165 
166   }
167 
168   @VisibleForTesting
169   String getGroupForInstance(final RunningInstance instance) {
170      String group = null;
171      try {
172         group = Iterables.getOnlyElement(Iterables.filter(instance.getGroupIds(), new Predicate<String>() {
173 
174            @Override
175            public boolean apply(String input) {
176               return input.startsWith("jclouds#") && input.endsWith("#" + instance.getRegion());
177            }
178 
179         })).substring(8).replaceAll("#" + instance.getRegion() + "$", "");
180      } catch (NoSuchElementException e) {
181         logger.debug("no group parsed from %s's security groups: %s", instance.getId(), instance.getGroupIds());
182      } catch (IllegalArgumentException e) {
183         logger.debug("too many groups match %s; %s's security groups: %s", "jclouds#", instance.getId(), instance
184                  .getGroupIds());
185      }
186      return group;
187   }
188 
189   @VisibleForTesting
190   Hardware getHardwareForInstance(final RunningInstance instance) {
191      try {
192         return Iterables.find(hardware.get(), new Predicate<Hardware>() {
193 
194            @Override
195            public boolean apply(Hardware input) {
196               return input.getId().equals(instance.getInstanceType());
197            }
198 
199         });
200      } catch (NoSuchElementException e) {
201         logger.debug("couldn't match instance type %s in: %s", instance.getInstanceType(), hardware.get());
202         return null;
203      }
204   }
205 
206   private Location getLocationForAvailabilityZoneOrRegion(final RunningInstance instance) {
207      Location location = findLocationWithId(instance.getAvailabilityZone());
208      if (location == null)
209         location = findLocationWithId(instance.getRegion());
210      return location;
211   }
212 
213   private Location findLocationWithId(final String locationId) {
214      if (locationId == null)
215         return null;
216      try {
217         Location location = Iterables.find(locations.get(), new Predicate<Location>() {
218 
219            @Override
220            public boolean apply(Location input) {
221               return input.getId().equals(locationId);
222            }
223 
224         });
225         return location;
226 
227      } catch (NoSuchElementException e) {
228         logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
229         return null;
230      }
231   }
232 
233}

[all classes][org.jclouds.ec2.compute.functions]
EMMA 2.0.5312 (C) Vladimir Roubtsov