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

COVERAGE BREAKDOWN BY CLASS AND METHOD

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

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