View Javadoc

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.ec2.compute.strategy;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  import static com.google.common.base.Predicates.and;
23  import static com.google.common.base.Predicates.notNull;
24  import static com.google.common.collect.Iterables.concat;
25  import static com.google.common.collect.Iterables.filter;
26  import static com.google.common.collect.Iterables.transform;
27  import static org.jclouds.concurrent.FutureIterables.transformParallel;
28  
29  import java.util.Set;
30  import java.util.concurrent.ExecutorService;
31  import java.util.concurrent.Future;
32  
33  import javax.annotation.Resource;
34  import javax.inject.Inject;
35  import javax.inject.Named;
36  import javax.inject.Singleton;
37  
38  import org.jclouds.Constants;
39  import org.jclouds.compute.domain.ComputeMetadata;
40  import org.jclouds.compute.domain.NodeMetadata;
41  import org.jclouds.compute.predicates.NodePredicates;
42  import org.jclouds.compute.reference.ComputeServiceConstants;
43  import org.jclouds.compute.strategy.ListNodesStrategy;
44  import org.jclouds.ec2.EC2AsyncClient;
45  import org.jclouds.ec2.domain.Reservation;
46  import org.jclouds.ec2.domain.RunningInstance;
47  import org.jclouds.location.Region;
48  import org.jclouds.logging.Logger;
49  
50  import com.google.common.base.Function;
51  import com.google.common.base.Predicate;
52  import com.google.common.collect.ImmutableSet;
53  
54  /**
55   * 
56   * @author Adrian Cole
57   */
58  @Singleton
59  public class EC2ListNodesStrategy implements ListNodesStrategy {
60     @Resource
61     @Named(ComputeServiceConstants.COMPUTE_LOGGER)
62     protected Logger logger = Logger.NULL;
63  
64     protected final EC2AsyncClient client;
65     protected final Set<String> regions;
66     protected final Function<RunningInstance, NodeMetadata> runningInstanceToNodeMetadata;
67     protected final ExecutorService executor;
68  
69     @Inject
70     protected EC2ListNodesStrategy(EC2AsyncClient client, @Region Set<String> regions,
71              Function<RunningInstance, NodeMetadata> runningInstanceToNodeMetadata,
72              @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
73        this.client =  checkNotNull(client, "client");
74        this.regions =  checkNotNull(regions, "regions");
75        this.runningInstanceToNodeMetadata = checkNotNull(runningInstanceToNodeMetadata, "runningInstanceToNodeMetadata");
76        this.executor =  checkNotNull(executor, "executor");
77     }
78  
79     @Override
80     public Set<? extends ComputeMetadata> listNodes() {
81        return listDetailsOnNodesMatching(NodePredicates.all());
82     }
83  
84     @Override
85     public Set<? extends NodeMetadata> listDetailsOnNodesMatching(Predicate<ComputeMetadata> filter) {
86        Iterable<? extends RunningInstance> instances = pollRunningInstances();
87        Iterable<? extends NodeMetadata> nodes = filter(transform(filter(instances, notNull()),
88                 runningInstanceToNodeMetadata), and(notNull(), filter));
89        return ImmutableSet.copyOf(nodes);
90     }
91  
92     protected Iterable<? extends RunningInstance> pollRunningInstances() {
93        Iterable<? extends Set<? extends Reservation<? extends RunningInstance>>> reservations = transformParallel(
94                 regions, new Function<String, Future<Set<? extends Reservation<? extends RunningInstance>>>>() {
95  
96                    @SuppressWarnings("unchecked")
97                    @Override
98                    public Future<Set<? extends Reservation<? extends RunningInstance>>> apply(String from) {
99                       return (Future<Set<? extends Reservation<? extends RunningInstance>>>) client
100                               .getInstanceServices().describeInstancesInRegion(from);
101                   }
102 
103                }, executor, null, logger, "reservations");
104 
105       return concat(concat(reservations));
106    }
107 }