EMMA Coverage Report (generated Mon Oct 17 05:41:20 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/21)0%   (0/538)0%   (0/66)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CloudSigmaComputeServiceAdapter0%   (0/1)0%   (0/13)0%   (0/430)0%   (0/52)
<static initializer> 0%   (0/1)0%   (0/7)0%   (0/1)
CloudSigmaComputeServiceAdapter (CloudSigmaClient, Predicate, JustProvider, S... 0%   (0/1)0%   (0/42)0%   (0/9)
access$000 (CloudSigmaComputeServiceAdapter): Cache 0%   (0/1)0%   (0/3)0%   (0/1)
createNodeWithGroupEncodedIntoNameThenStoreCredentials (String, String, Templ... 0%   (0/1)0%   (0/165)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/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 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/3)0%   (0/49)0%   (0/9)
CloudSigmaComputeServiceAdapter$3 (CloudSigmaComputeServiceAdapter): 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.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.CloudSigmaClient;
36import org.jclouds.cloudsigma.domain.Device;
37import org.jclouds.cloudsigma.domain.DriveInfo;
38import org.jclouds.cloudsigma.domain.DriveType;
39import org.jclouds.cloudsigma.domain.Server;
40import org.jclouds.cloudsigma.domain.ServerInfo;
41import org.jclouds.cloudsigma.options.CloneDriveOptions;
42import org.jclouds.cloudsigma.reference.CloudSigmaConstants;
43import org.jclouds.cloudsigma.util.Servers;
44import org.jclouds.compute.ComputeService;
45import org.jclouds.compute.ComputeServiceAdapter;
46import org.jclouds.compute.domain.Hardware;
47import org.jclouds.compute.domain.HardwareBuilder;
48import org.jclouds.compute.domain.Image;
49import org.jclouds.compute.domain.Processor;
50import org.jclouds.compute.domain.Template;
51import org.jclouds.compute.domain.Volume;
52import org.jclouds.compute.domain.internal.VolumeImpl;
53import org.jclouds.compute.reference.ComputeServiceConstants;
54import org.jclouds.domain.Credentials;
55import org.jclouds.domain.Location;
56import org.jclouds.location.suppliers.JustProvider;
57import org.jclouds.logging.Logger;
58 
59import com.google.common.base.Function;
60import com.google.common.base.Predicate;
61import com.google.common.base.Predicates;
62import com.google.common.cache.Cache;
63import com.google.common.collect.ImmutableList;
64import com.google.common.collect.ImmutableSet;
65import com.google.common.collect.ImmutableSet.Builder;
66import com.google.common.util.concurrent.Futures;
67import com.google.common.util.concurrent.UncheckedExecutionException;
68 
69/**
70 * defines the connection between the {@link CloudSigmaClient} implementation
71 * and the jclouds {@link ComputeService}
72 * 
73 */
74@Singleton
75public class CloudSigmaComputeServiceAdapter implements
76      ComputeServiceAdapter<ServerInfo, Hardware, DriveInfo, Location> {
77   private static final Predicate<DriveInfo> PREINSTALLED_DISK = Predicates.and(Predicates.notNull(),
78         new Predicate<DriveInfo>() {
79 
80            @Override
81            public boolean apply(DriveInfo drive) {
82               return drive.getType().equals(DriveType.DISK) && drive.getDriveType().contains("preinstalled");
83            }
84 
85         });
86   private final CloudSigmaClient client;
87   private final Predicate<DriveInfo> driveNotClaimed;
88   private final JustProvider locationSupplier;
89   private final String defaultVncPassword;
90   private final Cache<String, DriveInfo> cache;
91   private final ExecutorService executor;
92 
93   @Resource
94   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
95   protected Logger logger = Logger.NULL;
96 
97   @Inject
98   public CloudSigmaComputeServiceAdapter(CloudSigmaClient client, Predicate<DriveInfo> driveNotClaimed,
99         JustProvider locationSupplier, @Named(CloudSigmaConstants.PROPERTY_VNC_PASSWORD) String defaultVncPassword,
100         Cache<String, DriveInfo> cache, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
101      this.client = checkNotNull(client, "client");
102      this.driveNotClaimed = checkNotNull(driveNotClaimed, "driveNotClaimed");
103      this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier");
104      this.defaultVncPassword = checkNotNull(defaultVncPassword, "defaultVncPassword");
105      this.cache = checkNotNull(cache, "cache");
106      this.executor = checkNotNull(executor, "executor");
107   }
108 
109   @Override
110   public ServerInfo createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, Template template,
111         Map<String, Credentials> credentialStore) {
112      long bootSize = (long) (template.getHardware().getVolumes().get(0).getSize() * 1024 * 1024 * 1024l);
113      logger.debug(">> imaging boot drive source(%s) bytes(%d)", template.getImage().getId(), bootSize);
114      DriveInfo drive = client.cloneDrive(template.getImage().getId(), template.getImage().getId(),
115            new CloneDriveOptions().size(bootSize));
116      boolean success = driveNotClaimed.apply(drive);
117      logger.debug("<< image(%s) complete(%s)", drive.getUuid(), success);
118      if (!success) {
119         client.destroyDrive(drive.getUuid());
120         throw new IllegalStateException("could not image drive in time!");
121      }
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("node#" + from.getUuid(), new Credentials("root", defaultVncPassword));
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)))
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(client.listStandardDrives(),
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            }, executor, null, logger, "drives");
188      return filter(drives, PREINSTALLED_DISK);
189   }
190 
191   @SuppressWarnings("unchecked")
192   @Override
193   public Iterable<ServerInfo> listNodes() {
194      return (Iterable<ServerInfo>) client.listServerInfo();
195   }
196 
197   @SuppressWarnings("unchecked")
198   @Override
199   public Iterable<Location> listLocations() {
200      return (Iterable<Location>) locationSupplier.get();
201   }
202 
203   @Override
204   public ServerInfo getNode(String id) {
205      return client.getServerInfo(id);
206   }
207 
208   @Override
209   public void destroyNode(String id) {
210      ServerInfo server = getNode(id);
211      if (server != null) {
212         client.stopServer(id);
213         client.destroyServer(id);
214         for (Device dev : server.getDevices().values())
215            client.destroyDrive(dev.getDriveUuid());
216      }
217   }
218 
219   @Override
220   public void rebootNode(String id) {
221      client.resetServer(id);
222   }
223 
224   @Override
225   public void resumeNode(String id) {
226      client.startServer(id);
227 
228   }
229 
230   @Override
231   public void suspendNode(String id) {
232      client.stopServer(id);
233   }
234}

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