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

COVERAGE SUMMARY FOR SOURCE FILE [StubComputeServiceAdapter.java]

nameclass, %method, %block, %line, %
StubComputeServiceAdapter.java50%  (1/2)14%  (2/14)6%   (31/512)13%  (11/82)

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)17%  (2/12)7%   (31/470)14%  (11/76)
access$000 (StubComputeServiceAdapter): ConcurrentMap 0%   (0/1)0%   (0/3)0%   (0/1)
createNodeWithGroupEncodedIntoNameThenStoreCredentials (String, String, Templ... 0%   (0/1)0%   (0/128)0%   (0/18)
destroyNode (String): void 0%   (0/1)0%   (0/25)0%   (0/7)
getNode (String): NodeMetadata 0%   (0/1)0%   (0/6)0%   (0/1)
listHardwareProfiles (): Iterable 0%   (0/1)0%   (0/17)0%   (0/1)
listImages (): Iterable 0%   (0/1)0%   (0/124)0%   (0/10)
listLocations (): Iterable 0%   (0/1)0%   (0/5)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% (27/27)100% (10/10)
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 
67   @Inject
68   public StubComputeServiceAdapter(ConcurrentMap<String, NodeMetadata> nodes, Supplier<Location> location,
69            @Named("NODE_ID") Provider<Integer> idProvider, @Named("PUBLIC_IP_PREFIX") String publicIpPrefix,
70            @Named("PRIVATE_IP_PREFIX") String privateIpPrefix, @Named("PASSWORD_PREFIX") String passwordPrefix,
71            JustProvider locationSupplier, Map<OsFamily, Map<String, String>> osToVersionMap) {
72      this.nodes = nodes;
73      this.location = location;
74      this.idProvider = idProvider;
75      this.publicIpPrefix = publicIpPrefix;
76      this.privateIpPrefix = privateIpPrefix;
77      this.passwordPrefix = passwordPrefix;
78      this.locationSupplier = locationSupplier;
79      this.osToVersionMap = osToVersionMap;
80   }
81 
82   @Override
83   public NodeMetadata createNodeWithGroupEncodedIntoNameThenStoreCredentials(String group, String name, Template template,
84            Map<String, Credentials> credentialStore) {
85      NodeMetadataBuilder builder = new NodeMetadataBuilder();
86      String id = idProvider.get() + "";
87      builder.ids(id);
88      builder.name(name);
89      builder.tags(template.getOptions().getTags());
90      builder.group(group);
91      builder.location(location.get());
92      builder.imageId(template.getImage().getId());
93      builder.operatingSystem(template.getImage().getOperatingSystem());
94      builder.state(NodeState.PENDING);
95      builder.publicAddresses(ImmutableSet.<String> of(publicIpPrefix + id));
96      builder.privateAddresses(ImmutableSet.<String> of(privateIpPrefix + id));
97      builder.credentials(new Credentials("root", passwordPrefix + id));
98      NodeMetadata node = builder.build();
99      credentialStore.put("node#" + node.getId(), node.getCredentials());
100      nodes.put(node.getId(), node);
101      StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 100);
102      return node;
103   }
104 
105   @Override
106   public Iterable<Hardware> listHardwareProfiles() {
107      return ImmutableSet.<Hardware> of(StubComputeServiceDependenciesModule.stub("small", 1, 1740, 160),
108               StubComputeServiceDependenciesModule.stub("medium", 4, 7680, 850), StubComputeServiceDependenciesModule
109                        .stub("large", 8, 15360, 1690));
110   }
111 
112   @Override
113   public Iterable<Image> listImages() {
114      Credentials defaultCredentials = new Credentials("root", null);
115      // initializing as a List, as ImmutableSet does not allow you to put duplicates
116      Builder<Image> images = ImmutableList.<Image>builder();
117      int id = 1;
118      for (boolean is64Bit : new boolean[] { true, false })
119         for (Entry<OsFamily, Map<String, String>> osVersions : this.osToVersionMap.entrySet()) {
120            for (String version : ImmutableSet.copyOf(osVersions.getValue().values())) {
121               String desc = String.format("stub %s %s", osVersions.getKey(), is64Bit);
122               images.add(new ImageBuilder().ids(id++ + "").name(osVersions.getKey().name()).location(location.get())
123                        .operatingSystem(new OperatingSystem(osVersions.getKey(), desc, version, null, desc, is64Bit))
124                        .description(desc).defaultCredentials(defaultCredentials).build());
125            }
126         }
127      return images.build();
128   }
129 
130   @Override
131   public Iterable<NodeMetadata> listNodes() {
132      return nodes.values();
133   }
134 
135   @SuppressWarnings("unchecked")
136   @Override
137   public Iterable<Location> listLocations() {
138      return (Iterable<Location>) locationSupplier.get();
139   }
140 
141   @Override
142   public NodeMetadata getNode(String id) {
143      return nodes.get(id);
144   }
145 
146   @Override
147   public void destroyNode(final String id) {
148      NodeMetadata node = nodes.get(id);
149      if (node == null)
150         return;
151      StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0);
152      StubComputeServiceDependenciesModule.setState(node, NodeState.TERMINATED, 50);
153      StubComputeServiceDependenciesModule.service.execute(new Runnable() {
154 
155         @Override
156         public void run() {
157            try {
158               Thread.sleep(200);
159            } catch (InterruptedException e) {
160               Throwables.propagate(e);
161            } finally {
162               nodes.remove(id);
163            }
164         }
165 
166      });
167   }
168 
169   @Override
170   public void rebootNode(String id) {
171      NodeMetadata node = nodes.get(id);
172      if (node == null)
173         throw new ResourceNotFoundException("node not found: " + id);
174      StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0);
175      StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 50);
176   }
177 
178   @Override
179   public void resumeNode(String id) {
180      NodeMetadata node = nodes.get(id);
181      if (node == null)
182         throw new ResourceNotFoundException("node not found: " + id);
183      if (node.getState() == NodeState.RUNNING)
184         return;
185      if (node.getState() != NodeState.SUSPENDED)
186         throw new IllegalStateException("to resume a node, it must be in suspended state, not: " + node.getState());
187      StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0);
188      StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 50);
189   }
190 
191   @Override
192   public void suspendNode(String id) {
193      NodeMetadata node = nodes.get(id);
194      if (node == null)
195         throw new ResourceNotFoundException("node not found: " + id);
196      if (node.getState() == NodeState.SUSPENDED)
197         return;
198      if (node.getState() != NodeState.RUNNING)
199         throw new IllegalStateException("to suspend a node, it must be in running state, not: " + node.getState());
200      StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0);
201      StubComputeServiceDependenciesModule.setState(node, NodeState.SUSPENDED, 50);
202   }
203}

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