EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.ec2.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [RunningInstanceToNodeMetadata.java]

nameclass, %method, %block, %line, %
RunningInstanceToNodeMetadata.java83%  (5/6)84%  (16/19)77%  (374/487)86%  (60.9/71)

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% (9/9)84%  (322/382)88%  (57.4/65)
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)
findLocationWithId (String): Location 100% (1/1)95%  (35/37)86%  (6/7)
apply (RunningInstance): NodeMetadata 100% (1/1)98%  (128/130)96%  (23/24)
RunningInstanceToNodeMetadata (Map, Map, Map, 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 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
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.ec2.compute.domain.RegionAndName;
34import org.jclouds.ec2.domain.BlockDevice;
35import org.jclouds.ec2.domain.InstanceState;
36import org.jclouds.ec2.domain.RootDeviceType;
37import org.jclouds.ec2.domain.RunningInstance;
38import org.jclouds.collect.Memoized;
39import org.jclouds.compute.domain.Hardware;
40import org.jclouds.compute.domain.HardwareBuilder;
41import org.jclouds.compute.domain.Image;
42import org.jclouds.compute.domain.NodeMetadata;
43import org.jclouds.compute.domain.NodeMetadataBuilder;
44import org.jclouds.compute.domain.NodeState;
45import org.jclouds.compute.domain.Volume;
46import org.jclouds.compute.domain.internal.VolumeImpl;
47import org.jclouds.domain.Credentials;
48import org.jclouds.domain.Location;
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.collect.Iterables;
57import com.google.common.collect.Lists;
58 
59/**
60 * @author Adrian Cole
61 */
62@Singleton
63public class RunningInstanceToNodeMetadata implements Function<RunningInstance, NodeMetadata> {
64 
65   @Resource
66   protected Logger logger = Logger.NULL;
67 
68   protected final Supplier<Set<? extends Location>> locations;
69   protected final Supplier<Set<? extends Hardware>> hardware;
70   protected final Map<RegionAndName, Image> instanceToImage;
71   protected final Map<String, Credentials> credentialStore;
72   protected final Map<InstanceState, NodeState> instanceToNodeState;
73 
74   @Inject
75   protected RunningInstanceToNodeMetadata(Map<InstanceState, NodeState> instanceToNodeState,
76            Map<String, Credentials> credentialStore, Map<RegionAndName, Image> instanceToImage,
77            @Memoized Supplier<Set<? extends Location>> locations, @Memoized Supplier<Set<? extends Hardware>> hardware) {
78      this.locations = checkNotNull(locations, "locations");
79      this.hardware = checkNotNull(hardware, "hardware");
80      this.instanceToImage = checkNotNull(instanceToImage, "instanceToImage");
81      this.instanceToNodeState = checkNotNull(instanceToNodeState, "instanceToNodeState");
82      this.credentialStore = checkNotNull(credentialStore, "credentialStore");
83   }
84 
85   @Override
86   public NodeMetadata apply(RunningInstance instance) {
87      if (instance == null || instance.getId() == null)
88         return null;
89      NodeMetadataBuilder builder = new NodeMetadataBuilder();
90      builder.providerId(instance.getId());
91      builder.id(instance.getRegion() + "/" + instance.getId());
92      String group = getGroupForInstance(instance);
93      builder.group(group);
94      // standard convention from aws-ec2, which might not be re-used outside.
95      if (instance.getPrivateDnsName() != null)
96         builder.hostname(instance.getPrivateDnsName().replaceAll("\\..*", ""));
97      addCredentialsForInstance(builder, instance);
98      builder.state(instanceToNodeState.get(instance.getInstanceState()));
99      builder.publicAddresses(NullSafeCollections.nullSafeSet(instance.getIpAddress()));
100      builder.privateAddresses(NullSafeCollections.nullSafeSet(instance.getPrivateIpAddress()));
101      builder.hardware(parseHardware(instance));
102      Location location = getLocationForAvailabilityZoneOrRegion(instance);
103      builder.location(location);
104      builder.imageId(instance.getRegion() + "/" + instance.getImageId());
105 
106      // extract the operating system from the image
107      RegionAndName regionAndName = new RegionAndName(instance.getRegion(), instance.getImageId());
108      try {
109         Image image = instanceToImage.get(regionAndName);
110         if (image != null)
111            builder.operatingSystem(image.getOperatingSystem());
112      } catch (NullPointerException e) {
113         // The instanceToImage Map may throw NullPointerException (actually subclass
114         // NullOutputException) if the
115         // computing Function returns a null value.
116         //
117         // See the following for more information:
118         // MapMaker.makeComputingMap()
119         // RegionAndIdToImage.apply()
120      }
121 
122      return builder.build();
123   }
124 
125   protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) {
126      builder.credentials(credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId()));
127   }
128 
129   protected Hardware parseHardware(final RunningInstance instance) {
130      Hardware hardware = getHardwareForInstance(instance);
131 
132      if (hardware != null) {
133         hardware = HardwareBuilder.fromHardware(hardware).volumes(addEBS(instance, hardware.getVolumes())).build();
134      }
135      return hardware;
136   }
137 
138   @VisibleForTesting
139   static List<Volume> addEBS(final RunningInstance instance, Iterable<? extends Volume> volumes) {
140      Iterable<Volume> ebsVolumes = Iterables.transform(instance.getEbsBlockDevices().entrySet(),
141               new Function<Entry<String, BlockDevice>, Volume>() {
142 
143                  @Override
144                  public Volume apply(Entry<String, BlockDevice> from) {
145                     return new VolumeImpl(from.getValue().getVolumeId(), Volume.Type.SAN, null, from.getKey(),
146                              instance.getRootDeviceName() != null
147                                       && instance.getRootDeviceName().equals(from.getKey()), true);
148                  }
149               });
150 
151      if (instance.getRootDeviceType() == RootDeviceType.EBS) {
152         volumes = Iterables.filter(volumes, new Predicate<Volume>() {
153 
154            @Override
155            public boolean apply(Volume input) {
156               return !input.isBootDevice();
157            }
158 
159         });
160 
161      }
162      return Lists.newArrayList(Iterables.concat(volumes, ebsVolumes));
163 
164   }
165 
166   @VisibleForTesting
167   String getGroupForInstance(final RunningInstance instance) {
168      String group = null;
169      try {
170         group = Iterables.getOnlyElement(Iterables.filter(instance.getGroupIds(), new Predicate<String>() {
171 
172            @Override
173            public boolean apply(String input) {
174               return input.startsWith("jclouds#") && input.endsWith("#" + instance.getRegion());
175            }
176 
177         })).substring(8).replaceAll("#" + instance.getRegion() + "$", "");
178      } catch (NoSuchElementException e) {
179         logger.debug("no group parsed from %s's security groups: %s", instance.getId(), instance.getGroupIds());
180      } catch (IllegalArgumentException e) {
181         logger.debug("too many groups match %s; %s's security groups: %s", "jclouds#", instance.getId(), instance
182                  .getGroupIds());
183      }
184      return group;
185   }
186 
187   @VisibleForTesting
188   Hardware getHardwareForInstance(final RunningInstance instance) {
189      try {
190         return Iterables.find(hardware.get(), new Predicate<Hardware>() {
191 
192            @Override
193            public boolean apply(Hardware input) {
194               return input.getId().equals(instance.getInstanceType());
195            }
196 
197         });
198      } catch (NoSuchElementException e) {
199         logger.debug("couldn't match instance type %s in: %s", instance.getInstanceType(), hardware.get());
200         return null;
201      }
202   }
203 
204   private Location getLocationForAvailabilityZoneOrRegion(final RunningInstance instance) {
205      Location location = findLocationWithId(instance.getAvailabilityZone());
206      if (location == null)
207         location = findLocationWithId(instance.getRegion());
208      return location;
209   }
210 
211   private Location findLocationWithId(final String locationId) {
212      if (locationId == null)
213         return null;
214      try {
215         Location location = Iterables.find(locations.get(), new Predicate<Location>() {
216 
217            @Override
218            public boolean apply(Location input) {
219               return input.getId().equals(locationId);
220            }
221 
222         });
223         return location;
224 
225      } catch (NoSuchElementException e) {
226         logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
227         return null;
228      }
229   }
230 
231}

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