EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.compute.stub.config]

COVERAGE SUMMARY FOR SOURCE FILE [StubComputeServiceAdapter.java]

nameclass, %method, %block, %line, %
StubComputeServiceAdapter.java50%  (1/2)29%  (4/14)30%  (163/541)27%  (23/85)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class StubComputeServiceAdapter$10%   (0/1)0%   (0/2)0%   (0/42)0%   (0/7)
StubComputeServiceAdapter$1 (StubComputeServiceAdapter, String): void 0%   (0/1)0%   (0/9)0%   (0/1)
run (): void 0%   (0/1)0%   (0/33)0%   (0/6)
     
class StubComputeServiceAdapter100% (1/1)33%  (4/12)33%  (163/499)29%  (23/79)
access$000 (StubComputeServiceAdapter): ConcurrentMap 0%   (0/1)0%   (0/3)0%   (0/1)
createNodeWithGroupEncodedIntoNameThenStoreCredentials (String, String, Templ... 0%   (0/1)0%   (0/132)0%   (0/19)
destroyNode (String): void 0%   (0/1)0%   (0/25)0%   (0/7)
getNode (String): NodeMetadata 0%   (0/1)0%   (0/28)0%   (0/2)
listHardwareProfiles (): Iterable 0%   (0/1)0%   (0/17)0%   (0/1)
rebootNode (String): void 0%   (0/1)0%   (0/29)0%   (0/6)
resumeNode (String): void 0%   (0/1)0%   (0/51)0%   (0/10)
suspendNode (String): void 0%   (0/1)0%   (0/51)0%   (0/10)
StubComputeServiceAdapter (ConcurrentMap, Supplier, Provider, String, String,... 100% (1/1)100% (30/30)100% (11/11)
listImages (): Iterable 100% (1/1)100% (124/124)100% (10/10)
listLocations (): Iterable 100% (1/1)100% (5/5)100% (1/1)
listNodes (): Iterable 100% (1/1)100% (4/4)100% (1/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.compute.stub.config;
20 
21import java.util.Map;
22import java.util.Set;
23import java.util.Map.Entry;
24import java.util.concurrent.ConcurrentMap;
25 
26import javax.inject.Inject;
27import javax.inject.Named;
28import javax.inject.Provider;
29import javax.inject.Singleton;
30 
31import org.jclouds.compute.JCloudsNativeComputeServiceAdapter;
32import org.jclouds.compute.domain.Hardware;
33import org.jclouds.compute.domain.Image;
34import org.jclouds.compute.domain.ImageBuilder;
35import org.jclouds.compute.domain.NodeMetadata;
36import org.jclouds.compute.domain.NodeMetadataBuilder;
37import org.jclouds.compute.domain.NodeState;
38import org.jclouds.compute.domain.OperatingSystem;
39import org.jclouds.compute.domain.OsFamily;
40import org.jclouds.compute.domain.Template;
41import org.jclouds.domain.Credentials;
42import org.jclouds.domain.Location;
43import org.jclouds.location.suppliers.JustProvider;
44import org.jclouds.rest.ResourceNotFoundException;
45 
46import com.google.common.base.Supplier;
47import com.google.common.base.Throwables;
48import com.google.common.collect.ImmutableList;
49import com.google.common.collect.ImmutableSet;
50import com.google.common.collect.ImmutableList.Builder;
51 
52/**
53 * 
54 * @author Adrian Cole
55 */
56@Singleton
57public class StubComputeServiceAdapter implements JCloudsNativeComputeServiceAdapter {
58   private final Supplier<Location> location;
59   private final ConcurrentMap<String, NodeMetadata> nodes;
60   private final Provider<Integer> idProvider;
61   private final String publicIpPrefix;
62   private final String privateIpPrefix;
63   private final String passwordPrefix;
64   private final Supplier<Set<? extends Location>> locationSupplier;
65   private final Map<OsFamily, Map<String, String>> osToVersionMap;
66   private final Map<String, Credentials> credentialStore;
67 
68   @Inject
69   public StubComputeServiceAdapter(ConcurrentMap<String, NodeMetadata> nodes, Supplier<Location> location,
70            @Named("NODE_ID") Provider<Integer> idProvider, @Named("PUBLIC_IP_PREFIX") String publicIpPrefix,
71            @Named("PRIVATE_IP_PREFIX") String privateIpPrefix, @Named("PASSWORD_PREFIX") String passwordPrefix,
72            JustProvider locationSupplier, Map<OsFamily, Map<String, String>> osToVersionMap,
73            Map<String, Credentials> credentialStore) {
74      this.nodes = nodes;
75      this.location = location;
76      this.idProvider = idProvider;
77      this.publicIpPrefix = publicIpPrefix;
78      this.privateIpPrefix = privateIpPrefix;
79      this.passwordPrefix = passwordPrefix;
80      this.locationSupplier = locationSupplier;
81      this.osToVersionMap = osToVersionMap;
82      this.credentialStore = credentialStore;
83   }
84 
85   @Override
86   public NodeMetadata createNodeWithGroupEncodedIntoNameThenStoreCredentials(String group, String name, Template template,
87            Map<String, Credentials> credentialStore) {
88      NodeMetadataBuilder builder = new NodeMetadataBuilder();
89      String id = idProvider.get() + "";
90      builder.ids(id);
91      builder.name(name);
92      // using a predictable name so tests will pass
93      builder.hostname(group);
94      builder.tags(template.getOptions().getTags());
95      builder.group(group);
96      builder.location(location.get());
97      builder.imageId(template.getImage().getId());
98      builder.operatingSystem(template.getImage().getOperatingSystem());
99      builder.state(NodeState.PENDING);
100      builder.publicAddresses(ImmutableSet.<String> of(publicIpPrefix + id));
101      builder.privateAddresses(ImmutableSet.<String> of(privateIpPrefix + id));
102      builder.credentials(new Credentials("root", passwordPrefix + id));
103      NodeMetadata node = builder.build();
104      credentialStore.put("node#" + node.getId(), node.getCredentials());
105      nodes.put(node.getId(), node);
106      StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 100);
107      return node;
108   }
109 
110   @Override
111   public Iterable<Hardware> listHardwareProfiles() {
112      return ImmutableSet.<Hardware> of(StubComputeServiceDependenciesModule.stub("small", 1, 1740, 160),
113               StubComputeServiceDependenciesModule.stub("medium", 4, 7680, 850), StubComputeServiceDependenciesModule
114                        .stub("large", 8, 15360, 1690));
115   }
116 
117   @Override
118   public Iterable<Image> listImages() {
119      Credentials defaultCredentials = new Credentials("root", null);
120      // initializing as a List, as ImmutableSet does not allow you to put duplicates
121      Builder<Image> images = ImmutableList.<Image>builder();
122      int id = 1;
123      for (boolean is64Bit : new boolean[] { true, false })
124         for (Entry<OsFamily, Map<String, String>> osVersions : this.osToVersionMap.entrySet()) {
125            for (String version : ImmutableSet.copyOf(osVersions.getValue().values())) {
126               String desc = String.format("stub %s %s", osVersions.getKey(), is64Bit);
127               images.add(new ImageBuilder().ids(id++ + "").name(osVersions.getKey().name()).location(location.get())
128                        .operatingSystem(new OperatingSystem(osVersions.getKey(), desc, version, null, desc, is64Bit))
129                        .description(desc).defaultCredentials(defaultCredentials).build());
130            }
131         }
132      return images.build();
133   }
134 
135   @Override
136   public Iterable<NodeMetadata> listNodes() {
137      return nodes.values();
138   }
139 
140   @SuppressWarnings("unchecked")
141   @Override
142   public Iterable<Location> listLocations() {
143      return (Iterable<Location>) locationSupplier.get();
144   }
145 
146   @Override
147   public NodeMetadata getNode(String id) {
148      NodeMetadata node = nodes.get(id);
149      return node == null ? null : NodeMetadataBuilder.fromNodeMetadata(node).credentials(
150               credentialStore.get("node#" + node.getId())).build();
151   }
152 
153   @Override
154   public void destroyNode(final String id) {
155      NodeMetadata node = nodes.get(id);
156      if (node == null)
157         return;
158      StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0);
159      StubComputeServiceDependenciesModule.setState(node, NodeState.TERMINATED, 50);
160      StubComputeServiceDependenciesModule.service.execute(new Runnable() {
161 
162         @Override
163         public void run() {
164            try {
165               Thread.sleep(200);
166            } catch (InterruptedException e) {
167               Throwables.propagate(e);
168            } finally {
169               nodes.remove(id);
170            }
171         }
172 
173      });
174   }
175 
176   @Override
177   public void rebootNode(String id) {
178      NodeMetadata node = nodes.get(id);
179      if (node == null)
180         throw new ResourceNotFoundException("node not found: " + id);
181      StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0);
182      StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 50);
183   }
184 
185   @Override
186   public void resumeNode(String id) {
187      NodeMetadata node = nodes.get(id);
188      if (node == null)
189         throw new ResourceNotFoundException("node not found: " + id);
190      if (node.getState() == NodeState.RUNNING)
191         return;
192      if (node.getState() != NodeState.SUSPENDED)
193         throw new IllegalStateException("to resume a node, it must be in suspended state, not: " + node.getState());
194      StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0);
195      StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 50);
196   }
197 
198   @Override
199   public void suspendNode(String id) {
200      NodeMetadata node = nodes.get(id);
201      if (node == null)
202         throw new ResourceNotFoundException("node not found: " + id);
203      if (node.getState() == NodeState.SUSPENDED)
204         return;
205      if (node.getState() != NodeState.RUNNING)
206         throw new IllegalStateException("to suspend a node, it must be in running state, not: " + node.getState());
207      StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0);
208      StubComputeServiceDependenciesModule.setState(node, NodeState.SUSPENDED, 50);
209   }
210}

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