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

COVERAGE SUMMARY FOR SOURCE FILE [ServerInfoToNodeMetadata.java]

nameclass, %method, %block, %line, %
ServerInfoToNodeMetadata.java0%   (0/4)0%   (0/9)0%   (0/332)0%   (0/57)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ServerInfoToNodeMetadata0%   (0/1)0%   (0/3)0%   (0/174)0%   (0/24)
<static initializer> 0%   (0/1)0%   (0/22)0%   (0/1)
ServerInfoToNodeMetadata (Map, Function, Function, Function, Supplier): void 0%   (0/1)0%   (0/33)0%   (0/7)
apply (ServerInfo): NodeMetadata 0%   (0/1)0%   (0/119)0%   (0/16)
     
class ServerInfoToNodeMetadata$DeviceToVolume0%   (0/1)0%   (0/2)0%   (0/80)0%   (0/14)
ServerInfoToNodeMetadata$DeviceToVolume (Cache): void 0%   (0/1)0%   (0/12)0%   (0/4)
apply (Device): Volume 0%   (0/1)0%   (0/68)0%   (0/10)
     
class ServerInfoToNodeMetadata$FindImageForId0%   (0/1)0%   (0/2)0%   (0/9)0%   (0/3)
ServerInfoToNodeMetadata$FindImageForId (Supplier): void 0%   (0/1)0%   (0/4)0%   (0/2)
matches (String, Image): boolean 0%   (0/1)0%   (0/5)0%   (0/1)
     
class ServerInfoToNodeMetadata$GetImageIdFromServer0%   (0/1)0%   (0/2)0%   (0/69)0%   (0/16)
ServerInfoToNodeMetadata$GetImageIdFromServer (Cache): void 0%   (0/1)0%   (0/9)0%   (0/4)
apply (Server): String 0%   (0/1)0%   (0/60)0%   (0/12)

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.elasticstack.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName;
23 
24import java.util.List;
25import java.util.Map;
26import java.util.Set;
27 
28import javax.annotation.Resource;
29import javax.inject.Inject;
30import javax.inject.Singleton;
31 
32import org.jclouds.collect.FindResourceInSet;
33import org.jclouds.collect.Memoized;
34import org.jclouds.compute.domain.HardwareBuilder;
35import org.jclouds.compute.domain.Image;
36import org.jclouds.compute.domain.NodeMetadata;
37import org.jclouds.compute.domain.NodeMetadataBuilder;
38import org.jclouds.compute.domain.NodeState;
39import org.jclouds.compute.domain.Processor;
40import org.jclouds.compute.domain.Volume;
41import org.jclouds.compute.domain.VolumeBuilder;
42import org.jclouds.domain.Credentials;
43import org.jclouds.domain.Location;
44import org.jclouds.elasticstack.domain.Device;
45import org.jclouds.elasticstack.domain.DriveInfo;
46import org.jclouds.elasticstack.domain.Server;
47import org.jclouds.elasticstack.domain.ServerInfo;
48import org.jclouds.elasticstack.domain.ServerStatus;
49import org.jclouds.logging.Logger;
50 
51import com.google.common.base.Function;
52import com.google.common.base.Supplier;
53import com.google.common.cache.Cache;
54import com.google.common.collect.ImmutableList;
55import com.google.common.collect.ImmutableMap;
56import com.google.common.collect.ImmutableSet;
57import com.google.common.collect.Iterables;
58import com.google.common.util.concurrent.UncheckedExecutionException;
59 
60/**
61 * @author Adrian Cole
62 */
63@Singleton
64public class ServerInfoToNodeMetadata implements Function<ServerInfo, NodeMetadata> {
65   public static final Map<ServerStatus, NodeState> serverStatusToNodeState = ImmutableMap
66         .<ServerStatus, NodeState> builder().put(ServerStatus.ACTIVE, NodeState.RUNNING)//
67         .put(ServerStatus.STOPPED, NodeState.SUSPENDED)//
68         .put(ServerStatus.PAUSED, NodeState.SUSPENDED)//
69         .put(ServerStatus.DUMPED, NodeState.PENDING)//
70         .put(ServerStatus.DEAD, NodeState.TERMINATED)//
71         .put(ServerStatus.UNRECOGNIZED, NodeState.UNRECOGNIZED)//
72         .build();
73 
74   private final Function<Server, String> getImageIdFromServer;
75   private final Function<String, Image> findImageForId;
76   private final Map<String, Credentials> credentialStore;
77   private final Supplier<Location> locationSupplier;
78   private final Function<Device, Volume> deviceToVolume;
79 
80   @Inject
81   ServerInfoToNodeMetadata(Map<String, Credentials> credentialStore, Function<Server, String> getImageIdFromServer,
82         Function<String, Image> findImageForId, Function<Device, Volume> deviceToVolume,
83         Supplier<Location> locationSupplier) {
84      this.credentialStore = checkNotNull(credentialStore, "credentialStore");
85      this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier");
86      this.deviceToVolume = checkNotNull(deviceToVolume, "deviceToVolume");
87      this.findImageForId = checkNotNull(findImageForId, "findImageForId");
88      this.getImageIdFromServer = checkNotNull(getImageIdFromServer, "getImageIdFromServer");
89   }
90 
91   @SuppressWarnings({ "unchecked", "rawtypes" })
92   @Override
93   public NodeMetadata apply(ServerInfo from) {
94      NodeMetadataBuilder builder = new NodeMetadataBuilder();
95      builder.ids(from.getUuid());
96      builder.name(from.getName());
97      builder.location(locationSupplier.get());
98      builder.group(parseGroupFromName(from.getName()));
99 
100      String imageId = getImageIdFromServer.apply(from);
101      if (imageId != null) {
102         Image image = findImageForId.apply(imageId);
103         if (image != null) {
104            builder.operatingSystem(image.getOperatingSystem());
105         }
106      }
107      builder.hardware(new HardwareBuilder().ids(from.getUuid())
108            .processors(ImmutableList.of(new Processor(1, from.getCpu()))).ram(from.getMem())
109            .volumes((List) ImmutableList.of(Iterables.transform(from.getDevices().values(), deviceToVolume))).build());
110      builder.state(serverStatusToNodeState.get(from.getStatus()));
111      builder.publicAddresses(ImmutableSet.<String> of(from.getNics().get(0).getDhcp()));
112      builder.privateAddresses(ImmutableSet.<String> of());
113      builder.credentials(credentialStore.get("node#" + from.getUuid()));
114      return builder.build();
115   }
116 
117   @Singleton
118   public static final class DeviceToVolume implements Function<Device, Volume> {
119      @Resource
120      protected Logger logger = Logger.NULL;
121 
122      private final Cache<String, DriveInfo> cache;
123 
124      @Inject
125      public DeviceToVolume(Cache<String, DriveInfo> cache) {
126         this.cache = checkNotNull(cache, "cache");
127      }
128 
129      @Override
130      public Volume apply(Device input) {
131         VolumeBuilder builder = new VolumeBuilder();
132         builder.id(input.getId());
133         try {
134            DriveInfo drive = cache.getUnchecked(input.getDriveUuid());
135            builder.size(drive.getSize() / 1024 / 1024f);
136         } catch (NullPointerException e) {
137            logger.debug("drive %s not found", input.getDriveUuid());
138         } catch (UncheckedExecutionException e) {
139            logger.warn(e, "error finding drive %s: %s", input.getDriveUuid(), e.getMessage());
140         }
141         return new VolumeBuilder().durable(true).type(Volume.Type.NAS).build();
142      }
143   }
144 
145   /**
146    * When we create the boot drive of the server, by convention we set the name
147    * to the image it came from.
148    * 
149    * @author Adrian Cole
150    * 
151    */
152   @Singleton
153   public static class GetImageIdFromServer implements Function<Server, String> {
154      @Resource
155      protected Logger logger = Logger.NULL;
156 
157      private final Cache<String, DriveInfo> cache;
158 
159      @Inject
160      public GetImageIdFromServer(Cache<String, DriveInfo> cache) {
161         this.cache = cache;
162      }
163 
164      @Override
165      public String apply(Server from) {
166         String imageId = null;
167         String bootDeviceId = Iterables.get(from.getBootDeviceIds(), 0);
168         Device bootDevice = from.getDevices().get(bootDeviceId);
169         if (bootDevice != null) {
170            try {
171               DriveInfo drive = cache.getUnchecked(bootDevice.getDriveUuid());
172               imageId = drive.getName();
173            } catch (NullPointerException e) {
174               logger.debug("drive %s not found", bootDevice.getDriveUuid());
175            } catch (UncheckedExecutionException e) {
176               logger.warn(e, "error finding drive %s: %s", bootDevice.getDriveUuid(), e.getMessage());
177            }
178         }
179         return imageId;
180      }
181   }
182 
183   @Singleton
184   public static class FindImageForId extends FindResourceInSet<String, Image> {
185 
186      @Inject
187      public FindImageForId(@Memoized Supplier<Set<? extends Image>> images) {
188         super(images);
189      }
190 
191      @Override
192      public boolean matches(String from, Image input) {
193         return input.getProviderId().equals(from);
194      }
195   }
196 
197}

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