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

COVERAGE SUMMARY FOR SOURCE FILE [VCloudListNodesStrategy.java]

nameclass, %method, %block, %line, %
VCloudListNodesStrategy.java0%   (0/1)0%   (0/7)0%   (0/252)0%   (0/41)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class VCloudListNodesStrategy0%   (0/1)0%   (0/7)0%   (0/252)0%   (0/41)
VCloudListNodesStrategy (VCloudClient, Supplier, VCloudGetNodeMetadataStrateg... 0%   (0/1)0%   (0/21)0%   (0/8)
addVAppToSetRetryingIfNotYetPresent (ImmutableSet$Builder, ReferenceType, Ref... 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/67)0%   (0/7)
listNodes (): Iterable 0%   (0/1)0%   (0/62)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.vcloud.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.vcloud.VCloudClient;
38import org.jclouds.vcloud.VCloudMediaType;
39import org.jclouds.vcloud.compute.functions.FindLocationForResource;
40import org.jclouds.vcloud.domain.Org;
41import org.jclouds.vcloud.domain.ReferenceType;
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.ImmutableSet.Builder;
49import com.google.inject.Inject;
50 
51/**
52 * @author Adrian Cole
53 */
54// TODO REFACTOR!!! needs to be parallel
55@Singleton
56public class VCloudListNodesStrategy implements ListNodesStrategy {
57   @Resource
58   @Named(COMPUTE_LOGGER)
59   public Logger logger = Logger.NULL;
60 
61   protected final VCloudClient client;
62   protected final Supplier<Map<String, ? extends Org>> nameToOrg;
63   protected final VCloudGetNodeMetadataStrategy getNodeMetadata;
64   protected final FindLocationForResource findLocationForResourceInVDC;
65 
66   Set<String> blackListVAppNames = ImmutableSet.<String> of();
67 
68   @Inject(optional = true)
69   void setBlackList(@Named(PROPERTY_BLACKLIST_NODES) String blackListNodes) {
70      if (blackListNodes != null && !"".equals(blackListNodes))
71         this.blackListVAppNames = ImmutableSet.copyOf(Splitter.on(',').split(blackListNodes));
72   }
73 
74   @Inject
75   protected VCloudListNodesStrategy(VCloudClient client, Supplier<Map<String, ? extends Org>> nameToOrg,
76         VCloudGetNodeMetadataStrategy getNodeMetadata, FindLocationForResource findLocationForResourceInVDC) {
77      this.client = client;
78      this.nameToOrg = nameToOrg;
79      this.getNodeMetadata = getNodeMetadata;
80      this.findLocationForResourceInVDC = findLocationForResourceInVDC;
81   }
82 
83   @Override
84   public Iterable<ComputeMetadata> listNodes() {
85      Builder<ComputeMetadata> nodes = ImmutableSet.<ComputeMetadata> builder();
86      for (Org org : nameToOrg.get().values()) {
87         for (ReferenceType vdc : org.getVDCs().values()) {
88            for (ReferenceType resource : client.getVDCClient().getVDC(vdc.getHref()).getResourceEntities().values()) {
89               if (validVApp(resource)) {
90                  nodes.add(convertVAppToComputeMetadata(vdc, resource));
91               }
92            }
93         }
94      }
95      return nodes.build();
96   }
97 
98   private boolean validVApp(ReferenceType resource) {
99      return resource.getType().equals(VCloudMediaType.VAPP_XML) && !blackListVAppNames.contains(resource.getName());
100   }
101 
102   private ComputeMetadata convertVAppToComputeMetadata(ReferenceType vdc, ReferenceType resource) {
103      ComputeMetadataBuilder builder = new ComputeMetadataBuilder(ComputeType.NODE);
104      builder.providerId(resource.getHref().toASCIIString());
105      builder.name(resource.getName());
106      builder.id(resource.getHref().toASCIIString());
107      builder.location(findLocationForResourceInVDC.apply(vdc));
108      return builder.build();
109   }
110 
111   @Override
112   public Iterable<NodeMetadata> listDetailsOnNodesMatching(Predicate<ComputeMetadata> filter) {
113      Builder<NodeMetadata> nodes = ImmutableSet.<NodeMetadata> builder();
114      for (Org org : nameToOrg.get().values()) {
115         for (ReferenceType vdc : org.getVDCs().values()) {
116            for (ReferenceType resource : client.getVDCClient().getVDC(vdc.getHref()).getResourceEntities().values()) {
117               if (validVApp(resource) && filter.apply(convertVAppToComputeMetadata(vdc, resource))) {
118                  addVAppToSetRetryingIfNotYetPresent(nodes, vdc, resource);
119               }
120            }
121         }
122      }
123      return nodes.build();
124   }
125 
126   @VisibleForTesting
127   void addVAppToSetRetryingIfNotYetPresent(Builder<NodeMetadata> nodes, ReferenceType vdc, ReferenceType resource) {
128      NodeMetadata node = null;
129      int i = 0;
130      while (node == null && i++ < 3) {
131         try {
132            node = getNodeMetadata.getNode(resource.getHref().toASCIIString());
133            nodes.add(node);
134         } catch (NullPointerException e) {
135            logger.warn("vApp %s not yet present in vdc %s", resource.getName(), vdc.getName());
136         }
137      }
138   }
139 
140}

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