| 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.predicates; | 
| 20 |   | 
| 21 | import static com.google.common.base.Preconditions.checkNotNull; | 
| 22 |   | 
| 23 | import java.util.Set; | 
| 24 |   | 
| 25 | import org.jclouds.compute.domain.ComputeMetadata; | 
| 26 | import org.jclouds.compute.domain.NodeMetadata; | 
| 27 | import org.jclouds.compute.domain.NodeState; | 
| 28 | import org.jclouds.util.Preconditions2; | 
| 29 |   | 
| 30 | import com.google.common.base.Predicate; | 
| 31 | import com.google.common.base.Predicates; | 
| 32 | import com.google.common.collect.Sets; | 
| 33 |   | 
| 34 | /** | 
| 35 |  * Container for node filters (predicates). | 
| 36 |  *  | 
| 37 |  * This class has static methods that create customized predicates to use with | 
| 38 |  * {@link org.jclouds.compute.ComputeService}. | 
| 39 |  *  | 
| 40 |  * @author Oleksiy Yarmula | 
| 41 |  */ | 
| 42 | public class NodePredicates { | 
| 43 |   | 
| 44 |    private static class ParentLocationId implements Predicate<ComputeMetadata> { | 
| 45 |       private final String id; | 
| 46 |   | 
| 47 |       private ParentLocationId(String id) { | 
| 48 |          this.id = id; | 
| 49 |       } | 
| 50 |   | 
| 51 |       @Override | 
| 52 |       public int hashCode() { | 
| 53 |          final int prime = 31; | 
| 54 |          int result = 1; | 
| 55 |          result = prime * result + ((id == null) ? 0 : id.hashCode()); | 
| 56 |          return result; | 
| 57 |       } | 
| 58 |   | 
| 59 |       @Override | 
| 60 |       public boolean equals(Object obj) { | 
| 61 |          if (this == obj) | 
| 62 |             return true; | 
| 63 |          if (obj == null) | 
| 64 |             return false; | 
| 65 |          if (getClass() != obj.getClass()) | 
| 66 |             return false; | 
| 67 |          ParentLocationId other = (ParentLocationId) obj; | 
| 68 |          if (id == null) { | 
| 69 |             if (other.id != null) | 
| 70 |                return false; | 
| 71 |          } else if (!id.equals(other.id)) | 
| 72 |             return false; | 
| 73 |          return true; | 
| 74 |       } | 
| 75 |   | 
| 76 |       @Override | 
| 77 |       public boolean apply(ComputeMetadata nodeMetadata) { | 
| 78 |          if (nodeMetadata.getLocation().getParent() == null) | 
| 79 |             return false; | 
| 80 |          return id.equals(nodeMetadata.getLocation().getParent().getId()); | 
| 81 |       } | 
| 82 |   | 
| 83 |       @Override | 
| 84 |       public String toString() { | 
| 85 |          return "ParentLocationId [id=" + id + "]"; | 
| 86 |       } | 
| 87 |    } | 
| 88 |   | 
| 89 |    private static class LocationId implements Predicate<ComputeMetadata> { | 
| 90 |       @Override | 
| 91 |       public int hashCode() { | 
| 92 |          final int prime = 31; | 
| 93 |          int result = 1; | 
| 94 |          result = prime * result + ((id == null) ? 0 : id.hashCode()); | 
| 95 |          return result; | 
| 96 |       } | 
| 97 |   | 
| 98 |       @Override | 
| 99 |       public boolean equals(Object obj) { | 
| 100 |          if (this == obj) | 
| 101 |             return true; | 
| 102 |          if (obj == null) | 
| 103 |             return false; | 
| 104 |          if (getClass() != obj.getClass()) | 
| 105 |             return false; | 
| 106 |          LocationId other = (LocationId) obj; | 
| 107 |          if (id == null) { | 
| 108 |             if (other.id != null) | 
| 109 |                return false; | 
| 110 |          } else if (!id.equals(other.id)) | 
| 111 |             return false; | 
| 112 |          return true; | 
| 113 |       } | 
| 114 |   | 
| 115 |       private final String id; | 
| 116 |   | 
| 117 |       private LocationId(String id) { | 
| 118 |          this.id = id; | 
| 119 |       } | 
| 120 |   | 
| 121 |       @Override | 
| 122 |       public boolean apply(ComputeMetadata nodeMetadata) { | 
| 123 |          return id.equals(nodeMetadata.getLocation().getId()); | 
| 124 |       } | 
| 125 |   | 
| 126 |       @Override | 
| 127 |       public String toString() { | 
| 128 |          return "locationId(" + id + ")"; | 
| 129 |       } | 
| 130 |    } | 
| 131 |   | 
| 132 |    /** | 
| 133 |     * Return nodes in the specified location. | 
| 134 |     *  | 
| 135 |     * @param id | 
| 136 |     *           id of the location | 
| 137 |     * @return predicate | 
| 138 |     */ | 
| 139 |    public static Predicate<ComputeMetadata> locationId(final String id) { | 
| 140 |       checkNotNull(id, "id must be defined"); | 
| 141 |       return new LocationId(id); | 
| 142 |    } | 
| 143 |   | 
| 144 |    /** | 
| 145 |     * Return nodes in the specified parent location. | 
| 146 |     *  | 
| 147 |     * @param id | 
| 148 |     *           id of the location | 
| 149 |     * @return predicate | 
| 150 |     */ | 
| 151 |    public static Predicate<ComputeMetadata> parentLocationId(final String id) { | 
| 152 |       checkNotNull(id, "id must be defined"); | 
| 153 |       return new ParentLocationId(id); | 
| 154 |    } | 
| 155 |   | 
| 156 |    /** | 
| 157 |     * Return nodes with the specific ids Note: returns all nodes, regardless of the state. | 
| 158 |     *  | 
| 159 |     * @param ids | 
| 160 |     *           ids of the resources | 
| 161 |     * @return predicate | 
| 162 |     */ | 
| 163 |    public static Predicate<ComputeMetadata> withIds(String... ids) { | 
| 164 |       checkNotNull(ids, "ids must be defined"); | 
| 165 |       final Set<String> search = Sets.newHashSet(ids); | 
| 166 |       return new Predicate<ComputeMetadata>() { | 
| 167 |          @Override | 
| 168 |          public boolean apply(ComputeMetadata nodeMetadata) { | 
| 169 |             return search.contains(nodeMetadata.getProviderId()); | 
| 170 |          } | 
| 171 |   | 
| 172 |          @Override | 
| 173 |          public String toString() { | 
| 174 |             return "withIds(" + search + ")"; | 
| 175 |          } | 
| 176 |       }; | 
| 177 |    } | 
| 178 |   | 
| 179 |    /** | 
| 180 |     * return everything. | 
| 181 |     */ | 
| 182 |    public static Predicate<ComputeMetadata> all() { | 
| 183 |       return Predicates.<ComputeMetadata> alwaysTrue(); | 
| 184 |    } | 
| 185 |   | 
| 186 |    /** | 
| 187 |     * Return nodes in the specified group. Note: returns all nodes, regardless of the state. | 
| 188 |     *  | 
| 189 |     * @param group | 
| 190 |     *           group to match the items | 
| 191 |     * @return predicate | 
| 192 |     */ | 
| 193 |    public static Predicate<NodeMetadata> inGroup(final String group) { | 
| 194 |       Preconditions2.checkNotEmpty(group, "group must be defined"); | 
| 195 |       return new Predicate<NodeMetadata>() { | 
| 196 |          @Override | 
| 197 |          public boolean apply(NodeMetadata nodeMetadata) { | 
| 198 |             return group.equals(nodeMetadata.getGroup()); | 
| 199 |          } | 
| 200 |   | 
| 201 |          @Override | 
| 202 |          public String toString() { | 
| 203 |             return "inGroup(" + group + ")"; | 
| 204 |          } | 
| 205 |       }; | 
| 206 |    } | 
| 207 |   | 
| 208 |    /** | 
| 209 |     *  | 
| 210 |     * @see #inGroup(String) | 
| 211 |     */ | 
| 212 |    @Deprecated | 
| 213 |    public static Predicate<NodeMetadata> withTag(final String tag) { | 
| 214 |       return inGroup(tag); | 
| 215 |    } | 
| 216 |   | 
| 217 |    /** | 
| 218 |     * Return nodes with specified group that are in the NODE_RUNNING state. | 
| 219 |     *  | 
| 220 |     * @param group | 
| 221 |     *           group to match the items | 
| 222 |     * @return predicate | 
| 223 |     */ | 
| 224 |    public static Predicate<NodeMetadata> runningInGroup(final String group) { | 
| 225 |       Preconditions2.checkNotEmpty(group, "Tag must be defined"); | 
| 226 |       return new Predicate<NodeMetadata>() { | 
| 227 |          @Override | 
| 228 |          public boolean apply(NodeMetadata nodeMetadata) { | 
| 229 |             return group.equals(nodeMetadata.getGroup()) && nodeMetadata.getState() == NodeState.RUNNING; | 
| 230 |          } | 
| 231 |   | 
| 232 |          @Override | 
| 233 |          public String toString() { | 
| 234 |             return "runningInGroup(" + group + ")"; | 
| 235 |          } | 
| 236 |       }; | 
| 237 |    } | 
| 238 |   | 
| 239 |    /** | 
| 240 |     *  | 
| 241 |     * @see #inGroup(String) | 
| 242 |     */ | 
| 243 |    @Deprecated | 
| 244 |    public static Predicate<NodeMetadata> runningWithTag(final String tag) { | 
| 245 |       return runningInGroup(tag); | 
| 246 |    } | 
| 247 |   | 
| 248 |    /** | 
| 249 |     * Match nodes with State == RUNNING | 
| 250 |     */ | 
| 251 |    public static final Predicate<NodeMetadata> RUNNING = new Predicate<NodeMetadata>() { | 
| 252 |       @Override | 
| 253 |       public boolean apply(NodeMetadata nodeMetadata) { | 
| 254 |          return nodeMetadata.getState() == NodeState.RUNNING; | 
| 255 |       } | 
| 256 |   | 
| 257 |       @Override | 
| 258 |       public String toString() { | 
| 259 |          return "RUNNING"; | 
| 260 |       } | 
| 261 |    }; | 
| 262 |   | 
| 263 |    /** | 
| 264 |     * Match nodes with State == NODE_TERMINATED | 
| 265 |     */ | 
| 266 |    public static final Predicate<NodeMetadata> TERMINATED = new Predicate<NodeMetadata>() { | 
| 267 |       @Override | 
| 268 |       public boolean apply(NodeMetadata nodeMetadata) { | 
| 269 |          return nodeMetadata.getState() == NodeState.TERMINATED; | 
| 270 |       } | 
| 271 |   | 
| 272 |       @Override | 
| 273 |       public String toString() { | 
| 274 |          return "TERMINATED"; | 
| 275 |       } | 
| 276 |    }; | 
| 277 |   | 
| 278 | } |