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.concurrent.ConcurrentHashMap; |
22 | import java.util.concurrent.ConcurrentMap; |
23 | import java.util.concurrent.ExecutorService; |
24 | import java.util.concurrent.Executors; |
25 | import java.util.concurrent.atomic.AtomicInteger; |
26 | |
27 | import javax.inject.Inject; |
28 | import javax.inject.Named; |
29 | import javax.inject.Singleton; |
30 | |
31 | import org.jclouds.compute.domain.Hardware; |
32 | import org.jclouds.compute.domain.NodeMetadata; |
33 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
34 | import org.jclouds.compute.domain.NodeState; |
35 | import org.jclouds.compute.domain.Processor; |
36 | import org.jclouds.compute.domain.Volume; |
37 | import org.jclouds.compute.domain.internal.VolumeImpl; |
38 | import org.jclouds.net.IPSocket; |
39 | import org.jclouds.predicates.SocketOpen; |
40 | |
41 | import com.google.common.base.Throwables; |
42 | import com.google.common.collect.ImmutableList; |
43 | import com.google.inject.AbstractModule; |
44 | import com.google.inject.Provides; |
45 | |
46 | /** |
47 | * |
48 | * @author Adrian Cole |
49 | */ |
50 | public class StubComputeServiceDependenciesModule extends AbstractModule { |
51 | |
52 | @Override |
53 | protected void configure() { |
54 | |
55 | } |
56 | |
57 | protected static final ConcurrentMap<String, NodeMetadata> backing = new ConcurrentHashMap<String, NodeMetadata>(); |
58 | |
59 | // implementation details below |
60 | @Provides |
61 | @Singleton |
62 | ConcurrentMap<String, NodeMetadata> provideNodes() { |
63 | return backing; |
64 | } |
65 | |
66 | // STUB STUFF STATIC SO MULTIPLE CONTEXTS CAN SEE IT |
67 | private static final AtomicInteger nodeIds = new AtomicInteger(0); |
68 | static final ExecutorService service = Executors.newCachedThreadPool(); |
69 | |
70 | @Provides |
71 | @Named("NODE_ID") |
72 | Integer provideNodeId() { |
73 | return nodeIds.incrementAndGet(); |
74 | } |
75 | |
76 | @Singleton |
77 | @Provides |
78 | @Named("PUBLIC_IP_PREFIX") |
79 | String publicIpPrefix() { |
80 | return "144.175.1."; |
81 | } |
82 | |
83 | @Singleton |
84 | @Provides |
85 | @Named("PRIVATE_IP_PREFIX") |
86 | String privateIpPrefix() { |
87 | return "10.1.1."; |
88 | } |
89 | |
90 | @Singleton |
91 | @Provides |
92 | @Named("PASSWORD_PREFIX") |
93 | String passwordPrefix() { |
94 | return "password"; |
95 | } |
96 | |
97 | @Singleton |
98 | @Provides |
99 | SocketOpen socketOpen(StubSocketOpen in) { |
100 | return in; |
101 | } |
102 | |
103 | @Singleton |
104 | public static class StubSocketOpen implements SocketOpen { |
105 | private final ConcurrentMap<String, NodeMetadata> nodes; |
106 | private final String publicIpPrefix; |
107 | |
108 | @Inject |
109 | public StubSocketOpen(ConcurrentMap<String, NodeMetadata> nodes, @Named("PUBLIC_IP_PREFIX") String publicIpPrefix) { |
110 | this.nodes = nodes; |
111 | this.publicIpPrefix = publicIpPrefix; |
112 | } |
113 | |
114 | @Override |
115 | public boolean apply(IPSocket input) { |
116 | if (input.getAddress().indexOf(publicIpPrefix) == -1) |
117 | return false; |
118 | String id = input.getAddress().replace(publicIpPrefix, ""); |
119 | NodeMetadata node = nodes.get(id); |
120 | return node != null && node.getState() == NodeState.RUNNING; |
121 | } |
122 | |
123 | } |
124 | |
125 | protected static void nodeWithState(NodeMetadata node, NodeState state) { |
126 | backing.put(node.getId(), NodeMetadataBuilder.fromNodeMetadata(node).state(state).build()); |
127 | } |
128 | |
129 | public static void setState(final NodeMetadata node, final NodeState state, final long millis) { |
130 | if (millis == 0l) |
131 | nodeWithState(node, state); |
132 | else |
133 | service.execute(new Runnable() { |
134 | |
135 | @Override |
136 | public void run() { |
137 | try { |
138 | Thread.sleep(millis); |
139 | } catch (InterruptedException e) { |
140 | Throwables.propagate(e); |
141 | } |
142 | nodeWithState(node, state); |
143 | } |
144 | |
145 | }); |
146 | } |
147 | |
148 | static Hardware stub(String type, int cores, int ram, float disk) { |
149 | return new org.jclouds.compute.domain.HardwareBuilder().ids(type).name(type) |
150 | .processors(ImmutableList.of(new Processor(cores, 1.0))).ram(ram) |
151 | .volumes(ImmutableList.<Volume> of(new VolumeImpl(disk, true, false))).build(); |
152 | } |
153 | |
154 | } |