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

COVERAGE SUMMARY FOR SOURCE FILE [ElasticStackComputeServiceAdapter.java]

nameclass, %method, %block, %line, %
ElasticStackComputeServiceAdapter.java0%   (0/3)0%   (0/18)0%   (0/521)0%   (0/66)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ElasticStackComputeServiceAdapter0%   (0/1)0%   (0/12)0%   (0/430)0%   (0/54)
ElasticStackComputeServiceAdapter (ElasticStackClient, Predicate, JustProvide... 0%   (0/1)0%   (0/48)0%   (0/10)
access$000 (ElasticStackComputeServiceAdapter): Cache 0%   (0/1)0%   (0/3)0%   (0/1)
createNodeWithGroupEncodedIntoNameThenStoreCredentials (String, String, Templ... 0%   (0/1)0%   (0/162)0%   (0/17)
destroyNode (String): void 0%   (0/1)0%   (0/37)0%   (0/8)
getNode (String): ServerInfo 0%   (0/1)0%   (0/5)0%   (0/1)
listHardwareProfiles (): Iterable 0%   (0/1)0%   (0/133)0%   (0/7)
listImages (): Iterable 0%   (0/1)0%   (0/19)0%   (0/2)
listLocations (): Iterable 0%   (0/1)0%   (0/4)0%   (0/1)
listNodes (): Iterable 0%   (0/1)0%   (0/4)0%   (0/1)
rebootNode (String): void 0%   (0/1)0%   (0/5)0%   (0/2)
resumeNode (String): void 0%   (0/1)0%   (0/5)0%   (0/2)
suspendNode (String): void 0%   (0/1)0%   (0/5)0%   (0/2)
     
class ElasticStackComputeServiceAdapter$10%   (0/1)0%   (0/3)0%   (0/42)0%   (0/4)
ElasticStackComputeServiceAdapter$1 (ElasticStackComputeServiceAdapter, float... 0%   (0/1)0%   (0/9)0%   (0/1)
apply (Image): boolean 0%   (0/1)0%   (0/21)0%   (0/2)
toString (): String 0%   (0/1)0%   (0/12)0%   (0/1)
     
class ElasticStackComputeServiceAdapter$20%   (0/1)0%   (0/3)0%   (0/49)0%   (0/9)
ElasticStackComputeServiceAdapter$2 (ElasticStackComputeServiceAdapter): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (String): Future 0%   (0/1)0%   (0/41)0%   (0/7)
toString (): String 0%   (0/1)0%   (0/2)0%   (0/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.elasticstack.compute;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static com.google.common.base.Predicates.notNull;
23import static com.google.common.collect.Iterables.filter;
24import static org.jclouds.concurrent.FutureIterables.transformParallel;
25import static org.jclouds.elasticstack.util.Servers.small;
26 
27import java.util.Map;
28import java.util.concurrent.ExecutorService;
29import java.util.concurrent.Future;
30 
31import javax.annotation.Resource;
32import javax.inject.Inject;
33import javax.inject.Named;
34import javax.inject.Singleton;
35 
36import org.jclouds.Constants;
37import org.jclouds.compute.ComputeService;
38import org.jclouds.compute.ComputeServiceAdapter;
39import org.jclouds.compute.domain.Hardware;
40import org.jclouds.compute.domain.HardwareBuilder;
41import org.jclouds.compute.domain.Image;
42import org.jclouds.compute.domain.Processor;
43import org.jclouds.compute.domain.Template;
44import org.jclouds.compute.domain.Volume;
45import org.jclouds.compute.domain.internal.VolumeImpl;
46import org.jclouds.compute.reference.ComputeServiceConstants;
47import org.jclouds.domain.Credentials;
48import org.jclouds.domain.Location;
49import org.jclouds.elasticstack.ElasticStackClient;
50import org.jclouds.elasticstack.domain.Device;
51import org.jclouds.elasticstack.domain.Drive;
52import org.jclouds.elasticstack.domain.DriveInfo;
53import org.jclouds.elasticstack.domain.ImageConversionType;
54import org.jclouds.elasticstack.domain.Server;
55import org.jclouds.elasticstack.domain.ServerInfo;
56import org.jclouds.elasticstack.domain.ServerStatus;
57import org.jclouds.elasticstack.domain.WellKnownImage;
58import org.jclouds.elasticstack.reference.ElasticStackConstants;
59import org.jclouds.location.suppliers.JustProvider;
60import org.jclouds.logging.Logger;
61 
62import com.google.common.base.Function;
63import com.google.common.base.Predicate;
64import com.google.common.cache.Cache;
65import com.google.common.collect.ImmutableList;
66import com.google.common.collect.ImmutableSet;
67import com.google.common.collect.ImmutableSet.Builder;
68import com.google.common.util.concurrent.Futures;
69import com.google.common.util.concurrent.UncheckedExecutionException;
70 
71/**
72 * defines the connection between the {@link ElasticStackClient} implementation
73 * and the jclouds {@link ComputeService}
74 * 
75 */
76@Singleton
77public class ElasticStackComputeServiceAdapter implements
78      ComputeServiceAdapter<ServerInfo, Hardware, DriveInfo, Location> {
79   private final ElasticStackClient client;
80   private final Predicate<DriveInfo> driveNotClaimed;
81   private final Map<String, WellKnownImage> preinstalledImages;
82   private final Cache<String, DriveInfo> cache;
83   private final JustProvider locationSupplier;
84   private final String defaultVncPassword;
85   private final ExecutorService executor;
86 
87   @Resource
88   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
89   protected Logger logger = Logger.NULL;
90 
91   @Inject
92   public ElasticStackComputeServiceAdapter(ElasticStackClient client, Predicate<DriveInfo> driveNotClaimed,
93         JustProvider locationSupplier, Map<String, WellKnownImage> preinstalledImages, Cache<String, DriveInfo> cache,
94         @Named(ElasticStackConstants.PROPERTY_VNC_PASSWORD) String defaultVncPassword,
95         @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
96      this.client = checkNotNull(client, "client");
97      this.driveNotClaimed = checkNotNull(driveNotClaimed, "driveNotClaimed");
98      this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier");
99      this.preinstalledImages = checkNotNull(preinstalledImages, "preinstalledImages");
100      this.cache = checkNotNull(cache, "cache");
101      this.defaultVncPassword = checkNotNull(defaultVncPassword, "defaultVncPassword");
102      this.executor = checkNotNull(executor, "executor");
103   }
104 
105   @Override
106   public ServerInfo createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, Template template,
107         Map<String, Credentials> credentialStore) {
108      long bootSize = (long) (template.getHardware().getVolumes().get(0).getSize() * 1024 * 1024 * 1024l);
109 
110      logger.debug(">> creating boot drive bytes(%d)", bootSize);
111      DriveInfo drive = client
112            .createDrive(new Drive.Builder().name(template.getImage().getId()).size(bootSize).build());
113      logger.debug("<< drive(%s)", drive.getUuid());
114 
115      logger.debug(">> imaging boot drive source(%s)", template.getImage().getId());
116      client.imageDrive(template.getImage().getId(), drive.getUuid(), ImageConversionType.GUNZIP);
117      boolean success = driveNotClaimed.apply(drive);
118      logger.debug("<< imaged (%s)", success);
119      if (!success) {
120         client.destroyDrive(drive.getUuid());
121         throw new IllegalStateException("could not image drive in time!");
122      }
123 
124      Server toCreate = small(name, drive.getUuid(), defaultVncPassword).mem(template.getHardware().getRam())
125            .cpu((int) (template.getHardware().getProcessors().get(0).getSpeed())).build();
126 
127      ServerInfo from = client.createServer(toCreate);
128      client.startServer(from.getUuid());
129      from = client.getServerInfo(from.getUuid());
130      // store the credentials so that later functions can use them
131      credentialStore.put("node#" + from.getUuid(), new Credentials(
132            template.getImage().getDefaultCredentials().identity, from.getVnc().getPassword()));
133      return from;
134   }
135 
136   @Override
137   public Iterable<Hardware> listHardwareProfiles() {
138      Builder<Hardware> hardware = ImmutableSet.<Hardware> builder();
139      for (double cpu : new double[] { 1000, 5000, 10000, 20000 })
140         for (int ram : new int[] { 512, 1024, 2048, 4096, 8192 }) {
141            final float size = (float) cpu / 1000;
142            String id = String.format("cpu=%f,ram=%s,disk=%f", cpu, ram, size);
143            hardware.add(new HardwareBuilder().supportsImage(new Predicate<Image>() {
144 
145               @Override
146               public boolean apply(Image input) {
147                  String toParse = input.getUserMetadata().get("size");
148                  return (toParse != null && new Float(toParse) <= size);
149               }
150 
151               @Override
152               public String toString() {
153                  return "sizeLessThanOrEqual(" + size + ")";
154               }
155 
156            }).ids(id).ram(ram).processors(ImmutableList.of(new Processor(1, cpu)))
157                  .volumes(ImmutableList.<Volume> of(new VolumeImpl(size, true, true))).build());
158         }
159      return hardware.build();
160   }
161 
162   /**
163    * look up the current standard images and do not error out, if they are not
164    * found.
165    */
166   @Override
167   public Iterable<DriveInfo> listImages() {
168      Iterable<DriveInfo> drives = transformParallel(preinstalledImages.keySet(),
169            new Function<String, Future<DriveInfo>>() {
170 
171               @Override
172               public Future<DriveInfo> apply(String input) {
173                  try {
174                     return Futures.immediateFuture(cache.getUnchecked(input));
175                  } catch (NullPointerException e) {
176                     logger.debug("drive %s not found", input);
177                  } catch (UncheckedExecutionException e) {
178                     logger.warn(e, "error finding drive %s: %s", input, e.getMessage());
179                  }
180                  return Futures.immediateFuture(null);
181               }
182 
183               @Override
184               public String toString() {
185                  return "seedDriveCache()";
186               }
187 
188            }, executor, null, logger, "drives");
189      return filter(drives, notNull());
190   }
191 
192   @SuppressWarnings("unchecked")
193   @Override
194   public Iterable<ServerInfo> listNodes() {
195      return (Iterable<ServerInfo>) client.listServerInfo();
196   }
197 
198   @SuppressWarnings("unchecked")
199   @Override
200   public Iterable<Location> listLocations() {
201      return (Iterable<Location>) locationSupplier.get();
202   }
203 
204   @Override
205   public ServerInfo getNode(String id) {
206      return client.getServerInfo(id);
207   }
208 
209   @Override
210   public void destroyNode(String id) {
211      ServerInfo server = getNode(id);
212      if (server != null) {
213         if (server.getStatus() != ServerStatus.STOPPED)
214            client.stopServer(id);
215         client.destroyServer(id);
216         for (Device dev : server.getDevices().values())
217            client.destroyDrive(dev.getDriveUuid());
218      }
219   }
220 
221   @Override
222   public void rebootNode(String id) {
223      client.resetServer(id);
224   }
225 
226   @Override
227   public void resumeNode(String id) {
228      client.startServer(id);
229   }
230 
231   @Override
232   public void suspendNode(String id) {
233      client.stopServer(id);
234   }
235}

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