EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.cloudsigma.compute]

COVERAGE SUMMARY FOR SOURCE FILE [CloudSigmaComputeServiceAdapter.java]

nameclass, %method, %block, %line, %
CloudSigmaComputeServiceAdapter.java0%   (0/4)0%   (0/20)0%   (0/526)0%   (0/63)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CloudSigmaComputeServiceAdapter0%   (0/1)0%   (0/13)0%   (0/455)0%   (0/56)
<static initializer> 0%   (0/1)0%   (0/7)0%   (0/1)
CloudSigmaComputeServiceAdapter (CloudSigmaClient, CloudSigmaAsyncClient, Pre... 0%   (0/1)0%   (0/48)0%   (0/10)
access$000 (CloudSigmaComputeServiceAdapter): CloudSigmaAsyncClient 0%   (0/1)0%   (0/3)0%   (0/1)
createNodeWithGroupEncodedIntoNameThenStoreCredentials (String, String, Templ... 0%   (0/1)0%   (0/164)0%   (0/16)
destroyNode (String): void 0%   (0/1)0%   (0/33)0%   (0/7)
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 CloudSigmaComputeServiceAdapter$10%   (0/1)0%   (0/2)0%   (0/17)0%   (0/2)
CloudSigmaComputeServiceAdapter$1 (): void 0%   (0/1)0%   (0/3)0%   (0/1)
apply (DriveInfo): boolean 0%   (0/1)0%   (0/14)0%   (0/1)
     
class CloudSigmaComputeServiceAdapter$20%   (0/1)0%   (0/3)0%   (0/42)0%   (0/4)
CloudSigmaComputeServiceAdapter$2 (CloudSigmaComputeServiceAdapter, float): void 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 CloudSigmaComputeServiceAdapter$30%   (0/1)0%   (0/2)0%   (0/12)0%   (0/2)
CloudSigmaComputeServiceAdapter$3 (CloudSigmaComputeServiceAdapter): 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.cloudsigma.compute;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static com.google.common.collect.Iterables.filter;
23import static org.jclouds.concurrent.FutureIterables.transformParallel;
24 
25import java.util.Map;
26import java.util.concurrent.ExecutorService;
27import java.util.concurrent.Future;
28 
29import javax.annotation.Resource;
30import javax.inject.Inject;
31import javax.inject.Named;
32import javax.inject.Singleton;
33 
34import org.jclouds.Constants;
35import org.jclouds.cloudsigma.CloudSigmaAsyncClient;
36import org.jclouds.cloudsigma.CloudSigmaClient;
37import org.jclouds.cloudsigma.domain.Device;
38import org.jclouds.cloudsigma.domain.DriveInfo;
39import org.jclouds.cloudsigma.domain.DriveType;
40import org.jclouds.cloudsigma.domain.Server;
41import org.jclouds.cloudsigma.domain.ServerInfo;
42import org.jclouds.cloudsigma.options.CloneDriveOptions;
43import org.jclouds.cloudsigma.reference.CloudSigmaConstants;
44import org.jclouds.cloudsigma.util.Servers;
45import org.jclouds.compute.ComputeService;
46import org.jclouds.compute.ComputeServiceAdapter;
47import org.jclouds.compute.domain.Hardware;
48import org.jclouds.compute.domain.HardwareBuilder;
49import org.jclouds.compute.domain.Image;
50import org.jclouds.compute.domain.Processor;
51import org.jclouds.compute.domain.Template;
52import org.jclouds.compute.domain.Volume;
53import org.jclouds.compute.domain.internal.VolumeImpl;
54import org.jclouds.compute.reference.ComputeServiceConstants;
55import org.jclouds.domain.Credentials;
56import org.jclouds.domain.Location;
57import org.jclouds.location.suppliers.JustProvider;
58import org.jclouds.logging.Logger;
59 
60import com.google.common.base.Function;
61import com.google.common.base.Predicate;
62import com.google.common.base.Predicates;
63import com.google.common.collect.ImmutableList;
64import com.google.common.collect.ImmutableSet;
65import com.google.common.collect.ImmutableSet.Builder;
66 
67/**
68 * defines the connection between the {@link CloudSigmaClient} implementation and the jclouds
69 * {@link ComputeService}
70 * 
71 */
72@Singleton
73public class CloudSigmaComputeServiceAdapter implements
74         ComputeServiceAdapter<ServerInfo, Hardware, DriveInfo, Location> {
75   private static final Predicate<DriveInfo> PREINSTALLED_DISK = Predicates.and(Predicates.notNull(),
76            new Predicate<DriveInfo>() {
77 
78               @Override
79               public boolean apply(DriveInfo drive) {
80                  return drive.getType().equals(DriveType.DISK) && drive.getDriveType().contains("preinstalled");
81               }
82 
83            });
84   private final CloudSigmaClient client;
85   private final CloudSigmaAsyncClient aclient;
86   private final Predicate<DriveInfo> driveNotClaimed;
87   private final JustProvider locationSupplier;
88   private final String defaultVncPassword;
89   private final Map<String, DriveInfo> cache;
90   private final ExecutorService executor;
91 
92   @Resource
93   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
94   protected Logger logger = Logger.NULL;
95 
96   @Inject
97   public CloudSigmaComputeServiceAdapter(CloudSigmaClient client, CloudSigmaAsyncClient aclient,
98            Predicate<DriveInfo> driveNotClaimed, JustProvider locationSupplier,
99            @Named(CloudSigmaConstants.PROPERTY_VNC_PASSWORD) String defaultVncPassword, Map<String, DriveInfo> cache,
100            @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
101      this.client = checkNotNull(client, "client");
102      this.aclient = checkNotNull(aclient, "aclient");
103      this.driveNotClaimed = checkNotNull(driveNotClaimed, "driveNotClaimed");
104      this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier");
105      this.defaultVncPassword = checkNotNull(defaultVncPassword, "defaultVncPassword");
106      this.cache = checkNotNull(cache, "cache");
107      this.executor = checkNotNull(executor, "executor");
108   }
109 
110   @Override
111   public ServerInfo createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, Template template,
112            Map<String, Credentials> credentialStore) {
113      long bootSize = (long) (template.getHardware().getVolumes().get(0).getSize() * 1024 * 1024 * 1024l);
114      logger.debug(">> imaging boot drive source(%s) bytes(%d)", template.getImage().getId(), bootSize);
115      DriveInfo drive = client.cloneDrive(template.getImage().getId(), template.getImage().getId(),
116               new CloneDriveOptions().size(bootSize));
117      boolean success = driveNotClaimed.apply(drive);
118      logger.debug("<< image(%s) complete(%s)", drive.getUuid(), success);
119      if (!success) {
120         client.destroyDrive(drive.getUuid());
121         throw new IllegalStateException("could not image drive in time!");
122      }
123      Server toCreate = Servers.small(name, drive.getUuid(), defaultVncPassword).mem(template.getHardware().getRam())
124               .cpu((int) (template.getHardware().getProcessors().get(0).getSpeed())).build();
125 
126      logger.debug(">> creating server");
127      ServerInfo from = client.createServer(toCreate);
128      logger.debug("<< created server(%s)", from.getUuid());
129      logger.debug(">> starting server(%s)", from.getUuid());
130      client.startServer(from.getUuid());
131      // store the credentials so that later functions can use them
132      credentialStore.put(from.getUuid() + "", new Credentials("cloudsigma", "cloudsigma"));
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, 4 * 1024, 16 * 1024, 32 * 1024 }) {
141            final float size = (float) cpu / 100;
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(client.listStandardDrives(),
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, PREINSTALLED_DISK);
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         client.stopServer(id);
204         client.destroyServer(id);
205         for (Device dev : server.getDevices().values())
206            client.destroyDrive(dev.getDriveUuid());
207      }
208   }
209 
210   @Override
211   public void rebootNode(String id) {
212      client.resetServer(id);
213   }
214 
215   @Override
216   public void resumeNode(String id) {
217      client.startServer(id);
218 
219   }
220 
221   @Override
222   public void suspendNode(String id) {
223      client.stopServer(id);
224   }
225}

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