EMMA Coverage Report (generated Tue Jun 21 05:51:52 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/505)14%  (11/81)

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

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