EMMA Coverage Report (generated Wed Jun 22 19:47:49 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/499)0%   (0/61)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ElasticStackComputeServiceAdapter0%   (0/1)0%   (0/12)0%   (0/445)0%   (0/56)
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/151)0%   (0/15)
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      logger.debug(">> creating boot drive bytes(%d)", bootSize);
111      DriveInfo drive = client.createDrive(new Drive.Builder().name(template.getImage().getName()).size(bootSize)
112               .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      Server toCreate = small(name, drive.getUuid(), defaultVncPassword).mem(template.getHardware().getRam()).cpu(
124               (int) (template.getHardware().getProcessors().get(0).getSpeed())).build();
125 
126      ServerInfo from = client.createAndStartServer(toCreate);
127      // store the credentials so that later functions can use them
128      credentialStore.put(from.getUuid() + "", new Credentials(template.getImage().getDefaultCredentials().identity,
129               from.getVnc().getPassword()));
130      return from;
131   }
132 
133   @Override
134   public Iterable<Hardware> listHardwareProfiles() {
135      Builder<Hardware> hardware = ImmutableSet.<Hardware> builder();
136      for (double cpu : new double[] { 1000, 5000, 10000, 20000 })
137         for (int ram : new int[] { 512, 1024, 2048, 4096, 8192 }) {
138            final float size = (float) cpu / 1000;
139            String id = String.format("cpu=%f,ram=%s,disk=%f", cpu, ram, size);
140            hardware.add(new HardwareBuilder().supportsImage(new Predicate<Image>() {
141 
142               @Override
143               public boolean apply(Image input) {
144                  String toParse = input.getUserMetadata().get("size");
145                  return (toParse != null && new Float(toParse) <= size);
146               }
147 
148               @Override
149               public String toString() {
150                  return "sizeLessThanOrEqual(" + size + ")";
151               }
152 
153            }).ids(id).ram(ram).processors(ImmutableList.of(new Processor(1, cpu))).volumes(
154                     ImmutableList.<Volume> of(new VolumeImpl(size, true, true))).build());
155         }
156      return hardware.build();
157   }
158 
159   /**
160    * look up the current standard images and do not error out, if they are not found.
161    */
162   @Override
163   public Iterable<DriveInfo> listImages() {
164      Iterable<DriveInfo> drives = transformParallel(preinstalledImages.keySet(),
165               new Function<String, Future<DriveInfo>>() {
166 
167                  @Override
168                  public Future<DriveInfo> apply(String input) {
169                     return aclient.getDriveInfo(input);
170                  }
171 
172               }, executor, null, logger, "drives");
173      Iterable<DriveInfo> returnVal = filter(drives, notNull());
174      for (DriveInfo drive : returnVal)
175         cache.put(drive.getUuid(), drive);
176      return returnVal;
177   }
178 
179   @SuppressWarnings("unchecked")
180   @Override
181   public Iterable<ServerInfo> listNodes() {
182      return (Iterable<ServerInfo>) client.listServerInfo();
183   }
184   
185   @SuppressWarnings("unchecked")
186   @Override
187   public Iterable<Location> listLocations() {
188      return (Iterable<Location>) locationSupplier.get();
189   }
190 
191   @Override
192   public ServerInfo getNode(String id) {
193      return client.getServerInfo(id);
194   }
195 
196   @Override
197   public void destroyNode(String id) {
198      ServerInfo server = getNode(id);
199      if (server != null) {
200         if (server.getStatus() != ServerStatus.STOPPED)
201            client.stopServer(id);
202         client.destroyServer(id);
203         for (Device dev : server.getDevices().values())
204            client.destroyDrive(dev.getDriveUuid());
205      }
206   }
207 
208   @Override
209   public void rebootNode(String id) {
210      client.resetServer(id);
211   }
212 
213   @Override
214   public void resumeNode(String id) {
215      client.startServer(id);
216 
217   }
218 
219   @Override
220   public void suspendNode(String id) {
221      client.stopServer(id);
222   }
223}

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