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 | */ |
19 | package org.jclouds.compute.stub.config; |
20 | |
21 | import java.util.Map; |
22 | import java.util.Set; |
23 | import java.util.Map.Entry; |
24 | import java.util.concurrent.ConcurrentMap; |
25 | |
26 | import javax.inject.Inject; |
27 | import javax.inject.Named; |
28 | import javax.inject.Provider; |
29 | import javax.inject.Singleton; |
30 | |
31 | import org.jclouds.compute.JCloudsNativeComputeServiceAdapter; |
32 | import org.jclouds.compute.domain.Hardware; |
33 | import org.jclouds.compute.domain.Image; |
34 | import org.jclouds.compute.domain.ImageBuilder; |
35 | import org.jclouds.compute.domain.NodeMetadata; |
36 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
37 | import org.jclouds.compute.domain.NodeState; |
38 | import org.jclouds.compute.domain.OperatingSystem; |
39 | import org.jclouds.compute.domain.OsFamily; |
40 | import org.jclouds.compute.domain.Template; |
41 | import org.jclouds.domain.Credentials; |
42 | import org.jclouds.domain.Location; |
43 | import org.jclouds.location.suppliers.JustProvider; |
44 | import org.jclouds.rest.ResourceNotFoundException; |
45 | |
46 | import com.google.common.base.Supplier; |
47 | import com.google.common.base.Throwables; |
48 | import com.google.common.collect.ImmutableList; |
49 | import com.google.common.collect.ImmutableSet; |
50 | import com.google.common.collect.ImmutableList.Builder; |
51 | |
52 | /** |
53 | * |
54 | * @author Adrian Cole |
55 | */ |
56 | @Singleton |
57 | public 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.userMetadata(template.getOptions().getUserMetadata()); |
96 | builder.group(group); |
97 | builder.location(location.get()); |
98 | builder.imageId(template.getImage().getId()); |
99 | builder.operatingSystem(template.getImage().getOperatingSystem()); |
100 | builder.state(NodeState.PENDING); |
101 | builder.publicAddresses(ImmutableSet.<String> of(publicIpPrefix + id)); |
102 | builder.privateAddresses(ImmutableSet.<String> of(privateIpPrefix + id)); |
103 | Credentials creds = template.getOptions().getOverridingCredentials(); |
104 | if (creds == null) |
105 | creds = new Credentials(null, null); |
106 | if (creds.identity == null) |
107 | creds = creds.toBuilder().identity("root").build(); |
108 | if (creds.credential == null) |
109 | creds = creds.toBuilder().credential(passwordPrefix + id).build(); |
110 | builder.credentials(creds); |
111 | NodeMetadata node = builder.build(); |
112 | credentialStore.put("node#" + node.getId(), node.getCredentials()); |
113 | nodes.put(node.getId(), node); |
114 | StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 100); |
115 | return node; |
116 | } |
117 | |
118 | @Override |
119 | public Iterable<Hardware> listHardwareProfiles() { |
120 | return ImmutableSet.<Hardware> of(StubComputeServiceDependenciesModule.stub("small", 1, 1740, 160), |
121 | StubComputeServiceDependenciesModule.stub("medium", 4, 7680, 850), StubComputeServiceDependenciesModule |
122 | .stub("large", 8, 15360, 1690)); |
123 | } |
124 | |
125 | @Override |
126 | public Iterable<Image> listImages() { |
127 | Credentials defaultCredentials = new Credentials("root", null); |
128 | // initializing as a List, as ImmutableSet does not allow you to put duplicates |
129 | Builder<Image> images = ImmutableList.<Image>builder(); |
130 | int id = 1; |
131 | for (boolean is64Bit : new boolean[] { true, false }) |
132 | for (Entry<OsFamily, Map<String, String>> osVersions : this.osToVersionMap.entrySet()) { |
133 | for (String version : ImmutableSet.copyOf(osVersions.getValue().values())) { |
134 | String desc = String.format("stub %s %s", osVersions.getKey(), is64Bit); |
135 | images.add(new ImageBuilder().ids(id++ + "").name(osVersions.getKey().name()).location(location.get()) |
136 | .operatingSystem(new OperatingSystem(osVersions.getKey(), desc, version, null, desc, is64Bit)) |
137 | .description(desc).defaultCredentials(defaultCredentials).build()); |
138 | } |
139 | } |
140 | return images.build(); |
141 | } |
142 | |
143 | @Override |
144 | public Iterable<NodeMetadata> listNodes() { |
145 | return nodes.values(); |
146 | } |
147 | |
148 | @SuppressWarnings("unchecked") |
149 | @Override |
150 | public Iterable<Location> listLocations() { |
151 | return (Iterable<Location>) locationSupplier.get(); |
152 | } |
153 | |
154 | @Override |
155 | public NodeMetadata getNode(String id) { |
156 | NodeMetadata node = nodes.get(id); |
157 | return node == null ? null : NodeMetadataBuilder.fromNodeMetadata(node).credentials( |
158 | credentialStore.get("node#" + node.getId())).build(); |
159 | } |
160 | |
161 | @Override |
162 | public void destroyNode(final String id) { |
163 | NodeMetadata node = nodes.get(id); |
164 | if (node == null) |
165 | return; |
166 | StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0); |
167 | StubComputeServiceDependenciesModule.setState(node, NodeState.TERMINATED, 50); |
168 | StubComputeServiceDependenciesModule.service.execute(new Runnable() { |
169 | |
170 | @Override |
171 | public void run() { |
172 | try { |
173 | Thread.sleep(200); |
174 | } catch (InterruptedException e) { |
175 | Throwables.propagate(e); |
176 | } finally { |
177 | nodes.remove(id); |
178 | } |
179 | } |
180 | |
181 | }); |
182 | } |
183 | |
184 | @Override |
185 | public void rebootNode(String id) { |
186 | NodeMetadata node = nodes.get(id); |
187 | if (node == null) |
188 | throw new ResourceNotFoundException("node not found: " + id); |
189 | StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0); |
190 | StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 50); |
191 | } |
192 | |
193 | @Override |
194 | public void resumeNode(String id) { |
195 | NodeMetadata node = nodes.get(id); |
196 | if (node == null) |
197 | throw new ResourceNotFoundException("node not found: " + id); |
198 | if (node.getState() == NodeState.RUNNING) |
199 | return; |
200 | if (node.getState() != NodeState.SUSPENDED) |
201 | throw new IllegalStateException("to resume a node, it must be in suspended state, not: " + node.getState()); |
202 | StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0); |
203 | StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 50); |
204 | } |
205 | |
206 | @Override |
207 | public void suspendNode(String id) { |
208 | NodeMetadata node = nodes.get(id); |
209 | if (node == null) |
210 | throw new ResourceNotFoundException("node not found: " + id); |
211 | if (node.getState() == NodeState.SUSPENDED) |
212 | return; |
213 | if (node.getState() != NodeState.RUNNING) |
214 | throw new IllegalStateException("to suspend a node, it must be in running state, not: " + node.getState()); |
215 | StubComputeServiceDependenciesModule.setState(node, NodeState.PENDING, 0); |
216 | StubComputeServiceDependenciesModule.setState(node, NodeState.SUSPENDED, 50); |
217 | } |
218 | } |