EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.trmk.vcloud_0_8.compute.strategy]

COVERAGE SUMMARY FOR SOURCE FILE [TerremarkVCloudListNodesStrategy.java]

nameclass, %method, %block, %line, %
TerremarkVCloudListNodesStrategy.java0%   (0/1)0%   (0/7)0%   (0/254)0%   (0/41)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TerremarkVCloudListNodesStrategy0%   (0/1)0%   (0/7)0%   (0/254)0%   (0/41)
TerremarkVCloudListNodesStrategy (TerremarkVCloudClient, Supplier, TerremarkV... 0%   (0/1)0%   (0/21)0%   (0/8)
addVAppToSetRetryingIfNotYetPresent (Set, ReferenceType, ReferenceType): void 0%   (0/1)0%   (0/41)0%   (0/9)
convertVAppToComputeMetadata (ReferenceType, ReferenceType): ComputeMetadata 0%   (0/1)0%   (0/32)0%   (0/6)
listDetailsOnNodesMatching (Predicate): Iterable 0%   (0/1)0%   (0/68)0%   (0/7)
listNodes (): Iterable 0%   (0/1)0%   (0/63)0%   (0/7)
setBlackList (String): void 0%   (0/1)0%   (0/14)0%   (0/3)
validVApp (ReferenceType): boolean 0%   (0/1)0%   (0/15)0%   (0/1)

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 */
19package org.jclouds.trmk.vcloud_0_8.compute.strategy;
20 
21import static org.jclouds.compute.reference.ComputeServiceConstants.COMPUTE_LOGGER;
22import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_BLACKLIST_NODES;
23 
24import java.util.Map;
25import java.util.Set;
26 
27import javax.annotation.Resource;
28import javax.inject.Named;
29import javax.inject.Singleton;
30 
31import org.jclouds.compute.domain.ComputeMetadata;
32import org.jclouds.compute.domain.ComputeMetadataBuilder;
33import org.jclouds.compute.domain.ComputeType;
34import org.jclouds.compute.domain.NodeMetadata;
35import org.jclouds.compute.strategy.ListNodesStrategy;
36import org.jclouds.logging.Logger;
37import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudClient;
38import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudMediaType;
39import org.jclouds.trmk.vcloud_0_8.compute.functions.FindLocationForResource;
40import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType;
41import org.jclouds.trmk.vcloud_0_8.endpoints.Org;
42 
43import com.google.common.annotations.VisibleForTesting;
44import com.google.common.base.Predicate;
45import com.google.common.base.Splitter;
46import com.google.common.base.Supplier;
47import com.google.common.collect.ImmutableSet;
48import com.google.common.collect.Sets;
49import com.google.inject.Inject;
50 
51/**
52 * @author Adrian Cole
53 */
54// TODO REFACTOR!!! needs to be parallel
55@Singleton
56public class TerremarkVCloudListNodesStrategy implements ListNodesStrategy {
57   @Resource
58   @Named(COMPUTE_LOGGER)
59   public Logger logger = Logger.NULL;
60   protected final TerremarkVCloudGetNodeMetadataStrategy getNodeMetadata;
61   protected final TerremarkVCloudClient client;
62   protected final FindLocationForResource findLocationForResourceInVDC;
63   Set<String> blackListVAppNames = ImmutableSet.<String> of();
64 
65   @Inject(optional = true)
66   void setBlackList(@Named(PROPERTY_BLACKLIST_NODES) String blackListNodes) {
67      if (blackListNodes != null && !"".equals(blackListNodes))
68         this.blackListVAppNames = ImmutableSet.copyOf(Splitter.on(',').split(blackListNodes));
69   }
70 
71   private final Supplier<Map<String, ReferenceType>> orgNameToEndpoint;
72 
73   @Inject
74   protected TerremarkVCloudListNodesStrategy(TerremarkVCloudClient client,
75         @Org Supplier<Map<String, ReferenceType>> orgNameToEndpoint,
76         TerremarkVCloudGetNodeMetadataStrategy getNodeMetadata, FindLocationForResource findLocationForResourceInVDC) {
77      this.client = client;
78      this.orgNameToEndpoint = orgNameToEndpoint;
79      this.getNodeMetadata = getNodeMetadata;
80      this.findLocationForResourceInVDC = findLocationForResourceInVDC;
81   }
82 
83   @Override
84   public Iterable<ComputeMetadata> listNodes() {
85      Set<ComputeMetadata> nodes = Sets.newHashSet();
86      for (String org : orgNameToEndpoint.get().keySet()) {
87         for (ReferenceType vdc : client.findOrgNamed(org).getVDCs().values()) {
88            for (ReferenceType resource : client.getVDC(vdc.getHref()).getResourceEntities().values()) {
89               if (validVApp(resource)) {
90                  nodes.add(convertVAppToComputeMetadata(vdc, resource));
91               }
92            }
93         }
94      }
95      return nodes;
96   }
97 
98   private boolean validVApp(ReferenceType resource) {
99      return resource.getType().equals(TerremarkVCloudMediaType.VAPP_XML)
100            && !blackListVAppNames.contains(resource.getName());
101   }
102 
103   private ComputeMetadata convertVAppToComputeMetadata(ReferenceType vdc, ReferenceType resource) {
104      ComputeMetadataBuilder builder = new ComputeMetadataBuilder(ComputeType.NODE);
105      builder.providerId(resource.getHref().toASCIIString());
106      builder.name(resource.getName());
107      builder.id(resource.getHref().toASCIIString());
108      builder.location(findLocationForResourceInVDC.apply(vdc));
109      return builder.build();
110   }
111 
112   @Override
113   public Iterable<NodeMetadata> listDetailsOnNodesMatching(Predicate<ComputeMetadata> filter) {
114      Set<NodeMetadata> nodes = Sets.newHashSet();
115      for (String org : orgNameToEndpoint.get().keySet()) {
116         for (ReferenceType vdc : client.findOrgNamed(org).getVDCs().values()) {
117            for (ReferenceType resource : client.getVDC(vdc.getHref()).getResourceEntities().values()) {
118               if (validVApp(resource) && filter.apply(convertVAppToComputeMetadata(vdc, resource))) {
119                  addVAppToSetRetryingIfNotYetPresent(nodes, vdc, resource);
120               }
121            }
122         }
123      }
124      return nodes;
125   }
126 
127   @VisibleForTesting
128   void addVAppToSetRetryingIfNotYetPresent(Set<NodeMetadata> nodes, ReferenceType vdc, ReferenceType resource) {
129      NodeMetadata node = null;
130      int i = 0;
131      while (node == null && i++ < 3) {
132         try {
133            node = getNodeMetadata.getNode(resource.getHref().toASCIIString());
134            nodes.add(node);
135         } catch (NullPointerException e) {
136            logger.warn("vApp %s not yet present in vdc %s", resource.getName(), vdc.getName());
137         }
138      }
139   }
140 
141}

[all classes][org.jclouds.trmk.vcloud_0_8.compute.strategy]
EMMA 2.0.5312 (C) Vladimir Roubtsov