EMMA Coverage Report (generated Wed Aug 10 12:30:04 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/17)0%   (0/506)0%   (0/62)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ElasticStackComputeServiceAdapter0%   (0/1)0%   (0/12)0%   (0/452)0%   (0/57)
ElasticStackComputeServiceAdapter (ElasticStackClient, ElasticStackAsyncClien... 0%   (0/1)0%   (0/54)0%   (0/11)
access$000 (ElasticStackComputeServiceAdapter): ElasticStackAsyncClient 0%   (0/1)0%   (0/3)0%   (0/1)
createNodeWithGroupEncodedIntoNameThenStoreCredentials (String, String, Templ... 0%   (0/1)0%   (0/158)0%   (0/16)
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/39)0%   (0/5)
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/2)0%   (0/12)0%   (0/2)
ElasticStackComputeServiceAdapter$2 (ElasticStackComputeServiceAdapter): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (String): Future 0%   (0/1)0%   (0/6)0%   (0/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.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.ElasticStackAsyncClient;
50import org.jclouds.elasticstack.ElasticStackClient;
51import org.jclouds.elasticstack.domain.Device;
52import org.jclouds.elasticstack.domain.Drive;
53import org.jclouds.elasticstack.domain.DriveInfo;
54import org.jclouds.elasticstack.domain.ImageConversionType;
55import org.jclouds.elasticstack.domain.Server;
56import org.jclouds.elasticstack.domain.ServerInfo;
57import org.jclouds.elasticstack.domain.ServerStatus;
58import org.jclouds.elasticstack.domain.WellKnownImage;
59import org.jclouds.elasticstack.reference.ElasticStackConstants;
60import org.jclouds.location.suppliers.JustProvider;
61import org.jclouds.logging.Logger;
62 
63import com.google.common.base.Function;
64import com.google.common.base.Predicate;
65import com.google.common.collect.ImmutableList;
66import com.google.common.collect.ImmutableSet;
67import com.google.common.collect.ImmutableSet.Builder;
68 
69/**
70 * defines the connection between the {@link ElasticStackClient} implementation and the jclouds
71 * {@link ComputeService}
72 * 
73 */
74@Singleton
75public class ElasticStackComputeServiceAdapter implements
76         ComputeServiceAdapter<ServerInfo, Hardware, DriveInfo, Location> {
77   private final ElasticStackClient client;
78   private final ElasticStackAsyncClient aclient;
79   private final Predicate<DriveInfo> driveNotClaimed;
80   private final Map<String, WellKnownImage> preinstalledImages;
81   private final Map<String, DriveInfo> cache;
82   private final JustProvider locationSupplier;
83   private final String defaultVncPassword;
84   private final ExecutorService executor;
85 
86   @Resource
87   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
88   protected Logger logger = Logger.NULL;
89 
90   @Inject
91   public ElasticStackComputeServiceAdapter(ElasticStackClient client, ElasticStackAsyncClient aclient,
92            Predicate<DriveInfo> driveNotClaimed,  JustProvider locationSupplier, 
93            Map<String, WellKnownImage> preinstalledImages, Map<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.aclient = checkNotNull(aclient, "aclient");
98      this.driveNotClaimed = checkNotNull(driveNotClaimed, "driveNotClaimed");
99      this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier");
100      this.preinstalledImages = checkNotNull(preinstalledImages, "preinstalledImages");
101      this.cache = checkNotNull(cache, "cache");
102      this.defaultVncPassword = checkNotNull(defaultVncPassword, "defaultVncPassword");
103      this.executor = checkNotNull(executor, "executor");
104   }
105 
106   @Override
107   public ServerInfo createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, Template template,
108            Map<String, Credentials> credentialStore) {
109      long bootSize = (long) (template.getHardware().getVolumes().get(0).getSize() * 1024 * 1024 * 1024l);
110      
111      logger.debug(">> creating boot drive bytes(%d)", bootSize);
112      DriveInfo drive = client.createDrive(new Drive.Builder().name(template.getImage().getId()).size(bootSize)
113               .build());
114      logger.debug("<< drive(%s)", drive.getUuid());
115 
116      logger.debug(">> imaging boot drive source(%s)", template.getImage().getId());
117      client.imageDrive(template.getImage().getId(), drive.getUuid(), ImageConversionType.GUNZIP);
118      boolean success = driveNotClaimed.apply(drive);
119      logger.debug("<< imaged (%s)", success);
120      if (!success) {
121         client.destroyDrive(drive.getUuid());
122         throw new IllegalStateException("could not image drive in time!");
123      }
124      cache.put(drive.getUuid(), drive);
125 
126      Server toCreate = small(name, drive.getUuid(), defaultVncPassword).mem(template.getHardware().getRam()).cpu(
127               (int) (template.getHardware().getProcessors().get(0).getSpeed())).build();
128 
129      ServerInfo from = client.createAndStartServer(toCreate);
130      // store the credentials so that later functions can use them
131      credentialStore.put("node#"+ from.getUuid(), new Credentials(template.getImage().getDefaultCredentials().identity,
132               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))).volumes(
157                     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 found.
164    */
165   @Override
166   public Iterable<DriveInfo> listImages() {
167      Iterable<DriveInfo> drives = transformParallel(preinstalledImages.keySet(),
168               new Function<String, Future<DriveInfo>>() {
169 
170                  @Override
171                  public Future<DriveInfo> apply(String input) {
172                     return aclient.getDriveInfo(input);
173                  }
174 
175               }, executor, null, logger, "drives");
176      Iterable<DriveInfo> returnVal = filter(drives, notNull());
177      for (DriveInfo drive : returnVal)
178         cache.put(drive.getUuid(), drive);
179      return returnVal;
180   }
181 
182   @SuppressWarnings("unchecked")
183   @Override
184   public Iterable<ServerInfo> listNodes() {
185      return (Iterable<ServerInfo>) client.listServerInfo();
186   }
187   
188   @SuppressWarnings("unchecked")
189   @Override
190   public Iterable<Location> listLocations() {
191      return (Iterable<Location>) locationSupplier.get();
192   }
193 
194   @Override
195   public ServerInfo getNode(String id) {
196      return client.getServerInfo(id);
197   }
198 
199   @Override
200   public void destroyNode(String id) {
201      ServerInfo server = getNode(id);
202      if (server != null) {
203         if (server.getStatus() != ServerStatus.STOPPED)
204            client.stopServer(id);
205         client.destroyServer(id);
206         for (Device dev : server.getDevices().values())
207            client.destroyDrive(dev.getDriveUuid());
208      }
209   }
210 
211   @Override
212   public void rebootNode(String id) {
213      client.resetServer(id);
214   }
215 
216   @Override
217   public void resumeNode(String id) {
218      client.startServer(id);
219   }
220 
221   @Override
222   public void suspendNode(String id) {
223      client.stopServer(id);
224   }
225}

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