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.predicates; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.util.Set; |
24 | |
25 | import javax.annotation.Resource; |
26 | import javax.inject.Singleton; |
27 | |
28 | import org.jclouds.compute.ComputeService; |
29 | import org.jclouds.compute.domain.NodeMetadata; |
30 | import org.jclouds.compute.domain.NodeState; |
31 | import org.jclouds.logging.Logger; |
32 | |
33 | import com.google.common.base.Predicate; |
34 | import com.google.common.collect.ImmutableSet; |
35 | import com.google.inject.Inject; |
36 | |
37 | /** |
38 | * |
39 | * Tests to see if a node is active. |
40 | * |
41 | * @author Adrian Cole |
42 | */ |
43 | @Singleton |
44 | public class NodePresentAndInIntendedState implements Predicate<NodeMetadata> { |
45 | |
46 | private final ComputeService client; |
47 | private final NodeState intended; |
48 | private final Set<NodeState> invalids; |
49 | @Resource |
50 | protected Logger logger = Logger.NULL; |
51 | |
52 | @Inject |
53 | public NodePresentAndInIntendedState(NodeState intended, ComputeService client) { |
54 | this(intended, ImmutableSet.of(NodeState.ERROR), client); |
55 | } |
56 | |
57 | public NodePresentAndInIntendedState(NodeState intended, Set<NodeState> invalids, ComputeService client) { |
58 | this.intended = intended; |
59 | this.client = client; |
60 | this.invalids = invalids; |
61 | } |
62 | |
63 | public boolean apply(NodeMetadata node) { |
64 | logger.trace("looking for state on node %s", checkNotNull(node, "node")); |
65 | node = refresh(node); |
66 | if (node == null) |
67 | return false; |
68 | logger.trace("%s: looking for node state %s: currently: %s", node.getId(), intended, node.getState()); |
69 | if (invalids.contains(node.getState())) |
70 | throw new IllegalStateException("node " + node.getId() + " in location " + node.getLocation() |
71 | + " is in invalid state "+node.getState()); |
72 | return node.getState() == intended; |
73 | } |
74 | |
75 | private NodeMetadata refresh(NodeMetadata node) { |
76 | if (node == null || node.getId() == null) |
77 | return null; |
78 | return client.getNodeMetadata(node.getId()); |
79 | } |
80 | } |