| 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 | */ |
| 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.ImmutableSet; |
| 49 | import com.google.common.collect.Sets; |
| 50 | |
| 51 | /** |
| 52 | * |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | @Singleton |
| 56 | public 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 | } |