EMMA Coverage Report (generated Mon Dec 09 15:12:29 EST 2013)
[all classes][org.jclouds.glesys.compute]

COVERAGE SUMMARY FOR SOURCE FILE [GleSYSComputeServiceAdapter.java]

nameclass, %method, %block, %line, %
GleSYSComputeServiceAdapter.java33%  (2/6)19%  (5/26)11%  (64/600)16%  (12/74)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class GleSYSComputeServiceAdapter$10%   (0/1)0%   (0/2)0%   (0/15)0%   (0/2)
GleSYSComputeServiceAdapter$1 (GleSYSComputeServiceAdapter, String): void 0%   (0/1)0%   (0/9)0%   (0/1)
apply (OSTemplate): boolean 0%   (0/1)0%   (0/6)0%   (0/1)
     
class GleSYSComputeServiceAdapter$20%   (0/1)0%   (0/2)0%   (0/14)0%   (0/2)
GleSYSComputeServiceAdapter$2 (GleSYSComputeServiceAdapter): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (Server): ListenableFuture 0%   (0/1)0%   (0/8)0%   (0/1)
     
class GleSYSComputeServiceAdapter$30%   (0/1)0%   (0/2)0%   (0/15)0%   (0/2)
GleSYSComputeServiceAdapter$3 (GleSYSComputeServiceAdapter, Iterable): void 0%   (0/1)0%   (0/9)0%   (0/1)
apply (ServerDetails): boolean 0%   (0/1)0%   (0/6)0%   (0/1)
     
class GleSYSComputeServiceAdapter$50%   (0/1)0%   (0/2)0%   (0/19)0%   (0/5)
GleSYSComputeServiceAdapter$5 (GleSYSComputeServiceAdapter): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (String): boolean 0%   (0/1)0%   (0/13)0%   (0/4)
     
class GleSYSComputeServiceAdapter100% (1/1)19%  (3/16)10%  (55/528)15%  (10/65)
access$000 (GleSYSComputeServiceAdapter): GleSYSAsyncApi 0%   (0/1)0%   (0/3)0%   (0/1)
access$100 (GleSYSComputeServiceAdapter): GleSYSApi 0%   (0/1)0%   (0/3)0%   (0/1)
createNodeWithGroupEncodedIntoName (String, String, Template): ComputeService... 0%   (0/1)0%   (0/189)0%   (0/24)
destroyNode (String): void 0%   (0/1)0%   (0/12)0%   (0/2)
getImage (String): OSTemplate 0%   (0/1)0%   (0/11)0%   (0/1)
getNode (String): ServerDetails 0%   (0/1)0%   (0/6)0%   (0/1)
getRandomPassword (): String 0%   (0/1)0%   (0/6)0%   (0/1)
listHardwareProfiles (): Iterable 0%   (0/1)0%   (0/197)0%   (0/17)
listNodes (): Iterable 0%   (0/1)0%   (0/16)0%   (0/1)
listNodesByIds (Iterable): Iterable 0%   (0/1)0%   (0/9)0%   (0/1)
rebootNode (String): void 0%   (0/1)0%   (0/7)0%   (0/2)
resumeNode (String): void 0%   (0/1)0%   (0/7)0%   (0/2)
suspendNode (String): void 0%   (0/1)0%   (0/7)0%   (0/2)
GleSYSComputeServiceAdapter (GleSYSApi, GleSYSAsyncApi, ListeningExecutorServ... 100% (1/1)100% (36/36)100% (8/8)
listImages (): Set 100% (1/1)100% (6/6)100% (1/1)
listLocations (): Set 100% (1/1)100% (13/13)100% (1/1)
     
class GleSYSComputeServiceAdapter$4100% (1/1)100% (2/2)100% (9/9)100% (2/2)
GleSYSComputeServiceAdapter$4 (GleSYSComputeServiceAdapter): void 100% (1/1)100% (6/6)100% (1/1)
apply (AllowedArgumentsForCreateServer): Set 100% (1/1)100% (3/3)100% (1/1)

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package org.jclouds.glesys.compute;
18 
19import static com.google.common.base.Charsets.UTF_8;
20import static com.google.common.base.Preconditions.checkArgument;
21import static com.google.common.base.Preconditions.checkNotNull;
22import static com.google.common.collect.Iterables.contains;
23import static com.google.common.collect.Iterables.filter;
24import static com.google.common.collect.Iterables.find;
25import static com.google.common.io.BaseEncoding.base16;
26import static org.jclouds.compute.util.ComputeServiceUtils.metadataAndTagsAsCommaDelimitedValue;
27import static org.jclouds.concurrent.FutureIterables.transformParallel;
28import static org.jclouds.util.Predicates2.retry;
29 
30import java.util.Map;
31import java.util.Map.Entry;
32import java.util.Set;
33import java.util.UUID;
34 
35import javax.annotation.Resource;
36import javax.inject.Inject;
37import javax.inject.Named;
38import javax.inject.Singleton;
39 
40import org.jclouds.Constants;
41import org.jclouds.collect.Memoized;
42import org.jclouds.compute.ComputeService;
43import org.jclouds.compute.ComputeServiceAdapter;
44import org.jclouds.compute.domain.Hardware;
45import org.jclouds.compute.domain.HardwareBuilder;
46import org.jclouds.compute.domain.Processor;
47import org.jclouds.compute.domain.Template;
48import org.jclouds.compute.domain.Volume;
49import org.jclouds.compute.domain.internal.VolumeImpl;
50import org.jclouds.compute.predicates.ImagePredicates;
51import org.jclouds.compute.reference.ComputeServiceConstants;
52import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts;
53import org.jclouds.domain.Location;
54import org.jclouds.domain.LoginCredentials;
55import org.jclouds.glesys.GleSYSApi;
56import org.jclouds.glesys.GleSYSAsyncApi;
57import org.jclouds.glesys.compute.options.GleSYSTemplateOptions;
58import org.jclouds.glesys.domain.AllowedArgumentsForCreateServer;
59import org.jclouds.glesys.domain.OSTemplate;
60import org.jclouds.glesys.domain.Server;
61import org.jclouds.glesys.domain.ServerDetails;
62import org.jclouds.glesys.domain.ServerSpec;
63import org.jclouds.glesys.options.CreateServerOptions;
64import org.jclouds.glesys.options.DestroyServerOptions;
65import org.jclouds.location.predicates.LocationPredicates;
66import org.jclouds.logging.Logger;
67 
68import com.google.common.base.Function;
69import com.google.common.base.Joiner;
70import com.google.common.base.Predicate;
71import com.google.common.base.Supplier;
72import com.google.common.collect.FluentIterable;
73import com.google.common.collect.ImmutableList;
74import com.google.common.collect.ImmutableSet;
75import com.google.common.collect.Iterables;
76import com.google.common.util.concurrent.ListenableFuture;
77import com.google.common.util.concurrent.ListeningExecutorService;
78 
79/**
80 * defines the connection between the {@link GleSYSApi} implementation and
81 * the jclouds {@link ComputeService}
82 * 
83 */
84@Singleton
85public class GleSYSComputeServiceAdapter implements ComputeServiceAdapter<ServerDetails, Hardware, OSTemplate, String> {
86 
87   @Resource
88   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
89   protected Logger logger = Logger.NULL;
90 
91   private final GleSYSApi api;
92   private final GleSYSAsyncApi aapi;
93   private final ListeningExecutorService userExecutor;
94   private final Timeouts timeouts;
95   private final Supplier<Set<? extends Location>> locations;
96 
97   @Inject
98   public GleSYSComputeServiceAdapter(GleSYSApi api, GleSYSAsyncApi aapi,
99         @Named(Constants.PROPERTY_USER_THREADS) ListeningExecutorService userExecutor, Timeouts timeouts,
100         @Memoized Supplier<Set<? extends Location>> locations) {
101      this.api = checkNotNull(api, "api");
102      this.aapi = checkNotNull(aapi, "aapi");
103      this.userExecutor = checkNotNull(userExecutor, "userExecutor");
104      this.timeouts = checkNotNull(timeouts, "timeouts");
105      this.locations = checkNotNull(locations, "locations");
106   }
107 
108   @Override
109   public NodeAndInitialCredentials<ServerDetails> createNodeWithGroupEncodedIntoName(String group, String name,
110         Template template) {
111      checkNotNull(template, "template was null");
112      checkNotNull(template.getOptions(), "template options was null");
113      checkArgument(template.getOptions().getClass().isAssignableFrom(GleSYSTemplateOptions.class),
114            "options class %s should have been assignable from GleSYSTemplateOptions", template.getOptions().getClass());
115 
116      GleSYSTemplateOptions templateOptions = template.getOptions().as(GleSYSTemplateOptions.class);
117 
118      CreateServerOptions createServerOptions = new CreateServerOptions();
119      createServerOptions.ip(templateOptions.getIp());
120 
121      Map<String, String> md = metadataAndTagsAsCommaDelimitedValue(template.getOptions());
122      if (md.size() > 0) {
123         String description = Joiner.on('\n').withKeyValueSeparator("=").join(md);
124         // TODO: get glesys to stop stripping out equals and commas!
125         createServerOptions.description(base16().lowerCase().encode(description.getBytes(UTF_8)));
126      }
127      ServerSpec.Builder<?> builder = ServerSpec.builder();
128      builder.datacenter(template.getLocation().getId());
129      builder.templateName(template.getImage().getId());
130      builder.platform(template.getHardware().getHypervisor());
131      builder.memorySizeMB(template.getHardware().getRam());
132      builder.diskSizeGB(Math.round(template.getHardware().getVolumes().get(0).getSize()));
133      builder.cpuCores((int) template.getHardware().getProcessors().get(0).getCores());
134      builder.transferGB(templateOptions.getTransferGB());
135      ServerSpec spec = builder.build();
136 
137      
138      // use random root password unless one was provided via template options
139      String password = templateOptions.hasRootPassword() ? templateOptions.getRootPassword() : getRandomPassword();
140 
141      logger.debug(">> creating new Server spec(%s) name(%s) options(%s)", spec, name, createServerOptions);
142      ServerDetails result = api.getServerApi().createWithHostnameAndRootPassword(spec, name, password,
143            createServerOptions);
144      logger.trace("<< server(%s)", result.getId());
145 
146      return new NodeAndInitialCredentials<ServerDetails>(result, result.getId() + "", LoginCredentials.builder()
147            .password(password).build());
148   }
149 
150   /**
151    * @return a generated random password string
152    */
153   private String getRandomPassword() {
154      return UUID.randomUUID().toString().replace("-","");
155   }
156 
157   @Override
158   public Iterable<Hardware> listHardwareProfiles() {
159      Set<? extends Location> locationsSet = locations.get();
160      ImmutableSet.Builder<Hardware> hardwareToReturn = ImmutableSet.builder();
161 
162      // do this loop after dupes are filtered, else OOM
163      Set<OSTemplate> images = listImages();
164 
165      for (Entry<String, AllowedArgumentsForCreateServer> platformToArgs : api.getServerApi()
166            .getAllowedArgumentsForCreateByPlatform().entrySet())
167         for (String datacenter : platformToArgs.getValue().getDataCenters())
168            for (int diskSizeGB : platformToArgs.getValue().getDiskSizesInGB().getAllowedUnits())
169               for (int cpuCores : platformToArgs.getValue().getCpuCoreOptions().getAllowedUnits())
170                  for (int memorySizeMB : platformToArgs.getValue().getMemorySizesInMB().getAllowedUnits()) {
171                     ImmutableSet.Builder<String> templatesSupportedBuilder = ImmutableSet.builder();
172                     for (OSTemplate template : images) {
173                        if (template.getPlatform().equals(platformToArgs.getKey())
174                              && diskSizeGB >= template.getMinDiskSize() && memorySizeMB >= template.getMinMemSize())
175                           templatesSupportedBuilder.add(template.getName());
176                     }
177                     ImmutableSet<String> templatesSupported = templatesSupportedBuilder.build();
178                     if (templatesSupported.size() > 0)
179                        hardwareToReturn.add(new HardwareBuilder()
180                              .ids(String.format(
181                                    "datacenter(%s)platform(%s)cpuCores(%d)memorySizeMB(%d)diskSizeGB(%d)", datacenter,
182                                    platformToArgs.getKey(), cpuCores, memorySizeMB, diskSizeGB)).ram(memorySizeMB)
183                              .processors(ImmutableList.of(new Processor(cpuCores, 1.0)))
184                              .volumes(ImmutableList.<Volume> of(new VolumeImpl((float) diskSizeGB, true, true)))
185                              .hypervisor(platformToArgs.getKey())
186                              .location(find(locationsSet, LocationPredicates.idEquals(datacenter)))
187                              .supportsImage(ImagePredicates.idIn(templatesSupported)).build());
188                  }
189 
190      return hardwareToReturn.build();
191   }
192 
193   @Override
194   public Set<OSTemplate> listImages() {
195      return api.getServerApi().listTemplates().toSet();
196   }
197   
198   // cheat until we have a getTemplate command
199   @Override
200   public OSTemplate getImage(final String id) {
201      return find(listImages(), new Predicate<OSTemplate>(){
202 
203         @Override
204         public boolean apply(OSTemplate input) {
205            return input.getName().equals(id);
206         }
207         
208      }, null);
209   }
210   
211   @Override
212   public Iterable<ServerDetails> listNodes() {
213      return transformParallel(api.getServerApi().list(), new Function<Server, ListenableFuture<? extends ServerDetails>>() {
214         public ListenableFuture<ServerDetails> apply(Server from) {
215            return aapi.getServerApi().get(from.getId());
216         }
217      }, userExecutor, null, logger, "server details");
218   }
219 
220   @Override
221   public Iterable<ServerDetails> listNodesByIds(final Iterable<String> ids) {
222      return filter(listNodes(), new Predicate<ServerDetails>() {
223 
224            @Override
225            public boolean apply(ServerDetails server) {
226               return contains(ids, server.getId());
227            }
228         });
229   }
230 
231   @Override
232   public Set<String> listLocations() {
233      return FluentIterable.from(api.getServerApi().getAllowedArgumentsForCreateByPlatform().values())
234            .transformAndConcat(new Function<AllowedArgumentsForCreateServer, Set<String>>() {
235               @Override
236               public Set<String> apply(AllowedArgumentsForCreateServer arg0) {
237                  return arg0.getDataCenters();
238               }
239            }).toSet();
240   }
241 
242   @Override
243   public ServerDetails getNode(String id) {
244      return api.getServerApi().get(id);
245   }
246 
247   @Override
248   public void destroyNode(String id) {
249      retry(new Predicate<String>() {
250         public boolean apply(String arg0) {
251            try {
252               api.getServerApi().destroy(arg0, DestroyServerOptions.Builder.discardIp());
253               return true;
254            } catch (IllegalStateException e) {
255               return false;
256            }
257         }
258      }, timeouts.nodeTerminated).apply(id);
259   }
260 
261   @Override
262   public void rebootNode(String id) {
263      api.getServerApi().reboot(id);
264   }
265 
266   @Override
267   public void resumeNode(String id) {
268      api.getServerApi().start(id);
269   }
270 
271   @Override
272   public void suspendNode(String id) {
273      api.getServerApi().stop(id);
274   }
275}

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