EMMA Coverage Report (generated Wed Jun 22 19:47:49 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)76%  (363/476)85%  (58.9/69)

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%  (311/371)88%  (55.4/63)
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%  (117/119)95%  (21/22)
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      addCredentialsForInstance(builder, instance);
95      builder.state(instanceToNodeState.get(instance.getInstanceState()));
96      builder.publicAddresses(NullSafeCollections.nullSafeSet(instance.getIpAddress()));
97      builder.privateAddresses(NullSafeCollections.nullSafeSet(instance.getPrivateIpAddress()));
98      builder.hardware(parseHardware(instance));
99      Location location = getLocationForAvailabilityZoneOrRegion(instance);
100      builder.location(location);
101      builder.imageId(instance.getRegion() + "/" + instance.getImageId());
102 
103      // extract the operating system from the image
104      RegionAndName regionAndName = new RegionAndName(instance.getRegion(), instance.getImageId());
105      try {
106         Image image = instanceToImage.get(regionAndName);
107         if (image != null)
108            builder.operatingSystem(image.getOperatingSystem());
109      } catch (NullPointerException e) {
110         // The instanceToImage Map may throw NullPointerException (actually subclass
111         // NullOutputException) if the
112         // computing Function returns a null value.
113         //
114         // See the following for more information:
115         // MapMaker.makeComputingMap()
116         // RegionAndIdToImage.apply()
117      }
118 
119      return builder.build();
120   }
121 
122   protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) {
123      builder.credentials(credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId()));
124   }
125 
126   protected Hardware parseHardware(final RunningInstance instance) {
127      Hardware hardware = getHardwareForInstance(instance);
128 
129      if (hardware != null) {
130         hardware = HardwareBuilder.fromHardware(hardware).volumes(addEBS(instance, hardware.getVolumes())).build();
131      }
132      return hardware;
133   }
134 
135   @VisibleForTesting
136   static List<Volume> addEBS(final RunningInstance instance, Iterable<? extends Volume> volumes) {
137      Iterable<Volume> ebsVolumes = Iterables.transform(instance.getEbsBlockDevices().entrySet(),
138               new Function<Entry<String, BlockDevice>, Volume>() {
139 
140                  @Override
141                  public Volume apply(Entry<String, BlockDevice> from) {
142                     return new VolumeImpl(from.getValue().getVolumeId(), Volume.Type.SAN, null, from.getKey(),
143                              instance.getRootDeviceName() != null
144                                       && instance.getRootDeviceName().equals(from.getKey()), true);
145                  }
146               });
147 
148      if (instance.getRootDeviceType() == RootDeviceType.EBS) {
149         volumes = Iterables.filter(volumes, new Predicate<Volume>() {
150 
151            @Override
152            public boolean apply(Volume input) {
153               return !input.isBootDevice();
154            }
155 
156         });
157 
158      }
159      return Lists.newArrayList(Iterables.concat(volumes, ebsVolumes));
160 
161   }
162 
163   @VisibleForTesting
164   String getGroupForInstance(final RunningInstance instance) {
165      String group = null;
166      try {
167         group = Iterables.getOnlyElement(Iterables.filter(instance.getGroupIds(), new Predicate<String>() {
168 
169            @Override
170            public boolean apply(String input) {
171               return input.startsWith("jclouds#") && input.endsWith("#" + instance.getRegion());
172            }
173 
174         })).substring(8).replaceAll("#" + instance.getRegion() + "$", "");
175      } catch (NoSuchElementException e) {
176         logger.debug("no group parsed from %s's security groups: %s", instance.getId(), instance.getGroupIds());
177      } catch (IllegalArgumentException e) {
178         logger.debug("too many groups match %s; %s's security groups: %s", "jclouds#", instance.getId(), instance
179                  .getGroupIds());
180      }
181      return group;
182   }
183 
184   @VisibleForTesting
185   Hardware getHardwareForInstance(final RunningInstance instance) {
186      try {
187         return Iterables.find(hardware.get(), new Predicate<Hardware>() {
188 
189            @Override
190            public boolean apply(Hardware input) {
191               return input.getId().equals(instance.getInstanceType());
192            }
193 
194         });
195      } catch (NoSuchElementException e) {
196         logger.debug("couldn't match instance type %s in: %s", instance.getInstanceType(), hardware.get());
197         return null;
198      }
199   }
200 
201   private Location getLocationForAvailabilityZoneOrRegion(final RunningInstance instance) {
202      Location location = findLocationWithId(instance.getAvailabilityZone());
203      if (location == null)
204         location = findLocationWithId(instance.getRegion());
205      return location;
206   }
207 
208   private Location findLocationWithId(final String locationId) {
209      if (locationId == null)
210         return null;
211      try {
212         Location location = Iterables.find(locations.get(), new Predicate<Location>() {
213 
214            @Override
215            public boolean apply(Location input) {
216               return input.getId().equals(locationId);
217            }
218 
219         });
220         return location;
221 
222      } catch (NoSuchElementException e) {
223         logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
224         return null;
225      }
226   }
227 
228}

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