EMMA Coverage Report (generated Wed Oct 26 13:47:17 EDT 2011)
[all classes][org.jclouds.compute.strategy.impl]

COVERAGE SUMMARY FOR SOURCE FILE [CreateNodesWithGroupEncodedIntoNameThenAddToSet.java]

nameclass, %method, %block, %line, %
CreateNodesWithGroupEncodedIntoNameThenAddToSet.java33%  (1/3)10%  (1/10)8%   (21/250)22%  (8/36)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateNodesWithGroupEncodedIntoNameThenAddToSet$10%   (0/1)0%   (0/2)0%   (0/15)0%   (0/2)
CreateNodesWithGroupEncodedIntoNameThenAddToSet$1 (CreateNodesWithGroupEncode... 0%   (0/1)0%   (0/9)0%   (0/1)
apply (ComputeMetadata): boolean 0%   (0/1)0%   (0/6)0%   (0/1)
     
class CreateNodesWithGroupEncodedIntoNameThenAddToSet$AddNode0%   (0/1)0%   (0/4)0%   (0/112)0%   (0/12)
CreateNodesWithGroupEncodedIntoNameThenAddToSet$AddNode (CreateNodesWithGroup... 0%   (0/1)0%   (0/24)0%   (0/5)
CreateNodesWithGroupEncodedIntoNameThenAddToSet$AddNode (CreateNodesWithGroup... 0%   (0/1)0%   (0/7)0%   (0/1)
call (): NodeMetadata 0%   (0/1)0%   (0/65)0%   (0/5)
toString (): String 0%   (0/1)0%   (0/16)0%   (0/1)
     
class CreateNodesWithGroupEncodedIntoNameThenAddToSet100% (1/1)25%  (1/4)17%  (21/123)35%  (8/23)
execute (String, int, Template, Set, Map, Multimap): Map 0%   (0/1)0%   (0/45)0%   (0/4)
getNextName (String, Template): String 0%   (0/1)0%   (0/19)0%   (0/1)
getNextNames (String, Template, int): Set 0%   (0/1)0%   (0/38)0%   (0/10)
CreateNodesWithGroupEncodedIntoNameThenAddToSet (CreateNodeWithGroupEncodedIn... 100% (1/1)100% (21/21)100% (8/8)

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.compute.strategy.impl;
20 
21import static com.google.common.base.Objects.toStringHelper;
22import static com.google.common.base.Preconditions.checkNotNull;
23import static com.google.common.collect.Iterables.any;
24import static com.google.common.collect.Maps.newLinkedHashMap;
25import static com.google.common.collect.Sets.newLinkedHashSet;
26import static org.jclouds.concurrent.Futures.compose;
27 
28import java.security.SecureRandom;
29import java.util.Map;
30import java.util.Set;
31import java.util.concurrent.Callable;
32import java.util.concurrent.ExecutorService;
33import java.util.concurrent.Future;
34 
35import javax.annotation.Resource;
36import javax.inject.Inject;
37import javax.inject.Named;
38import javax.inject.Singleton;
39 
40import org.jclouds.Constants;
41import org.jclouds.compute.config.CustomizationResponse;
42import org.jclouds.compute.domain.ComputeMetadata;
43import org.jclouds.compute.domain.NodeMetadata;
44import org.jclouds.compute.domain.Template;
45import org.jclouds.compute.reference.ComputeServiceConstants;
46import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName;
47import org.jclouds.compute.strategy.CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap;
48import org.jclouds.compute.strategy.ListNodesStrategy;
49import org.jclouds.compute.strategy.CreateNodesInGroupThenAddToSet;
50import org.jclouds.logging.Logger;
51 
52import com.google.common.base.Predicate;
53import com.google.common.collect.Multimap;
54 
55/**
56 * creates futures that correlate to
57 * 
58 * @author Adrian Cole
59 */
60@Singleton
61public class CreateNodesWithGroupEncodedIntoNameThenAddToSet implements CreateNodesInGroupThenAddToSet {
62 
63   private class AddNode implements Callable<NodeMetadata> {
64      private final String name;
65      private final String tag;
66      private final Template template;
67 
68      private AddNode(String name, String tag, Template template) {
69         this.name = checkNotNull(name, "name");
70         this.tag = checkNotNull(tag, "tag");
71         this.template = checkNotNull(template, "template");
72      }
73 
74      @Override
75      public NodeMetadata call() throws Exception {
76         NodeMetadata node = null;
77         logger.debug(">> adding node location(%s) name(%s) image(%s) hardware(%s)",
78                  template.getLocation().getId(), name, template.getImage().getProviderId(), template.getHardware()
79                           .getProviderId());
80         node = addNodeWithTagStrategy.createNodeWithGroupEncodedIntoName(tag, name, template);
81         logger.debug("<< %s node(%s)", node.getState(), node.getId());
82         return node;
83      }
84 
85      public String toString() {
86         return toStringHelper(this).add("name", name).add("tag", tag).add("template", template).toString();
87      }
88 
89   }
90 
91   @Resource
92   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
93   protected Logger logger = Logger.NULL;
94   protected final CreateNodeWithGroupEncodedIntoName addNodeWithTagStrategy;
95   protected final ListNodesStrategy listNodesStrategy;
96   protected final String nodeNamingConvention;
97   protected final ExecutorService executor;
98   protected final CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap.Factory customizeNodeAndAddToGoodMapOrPutExceptionIntoBadMapFactory;
99 
100   @Inject
101   protected CreateNodesWithGroupEncodedIntoNameThenAddToSet(
102            CreateNodeWithGroupEncodedIntoName addNodeWithTagStrategy,
103            ListNodesStrategy listNodesStrategy,
104            @Named("NAMING_CONVENTION") String nodeNamingConvention,
105            @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor,
106            CustomizeNodeAndAddToGoodMapOrPutExceptionIntoBadMap.Factory customizeNodeAndAddToGoodMapOrPutExceptionIntoBadMapFactory) {
107      this.addNodeWithTagStrategy = addNodeWithTagStrategy;
108      this.listNodesStrategy = listNodesStrategy;
109      this.nodeNamingConvention = nodeNamingConvention;
110      this.executor = executor;
111      this.customizeNodeAndAddToGoodMapOrPutExceptionIntoBadMapFactory = customizeNodeAndAddToGoodMapOrPutExceptionIntoBadMapFactory;
112   }
113 
114   /**
115    * This implementation gets a list of acceptable node names to encode the tag into, then it
116    * simultaneously runs the nodes and applies options to them.
117    */
118   @Override
119   public Map<?, Future<Void>> execute(String tag, int count, Template template, Set<NodeMetadata> goodNodes,
120            Map<NodeMetadata, Exception> badNodes, Multimap<NodeMetadata, CustomizationResponse> customizationResponses) {
121      Map<String, Future<Void>> responses = newLinkedHashMap();
122      for (String name : getNextNames(tag, template, count)) {
123         responses.put(name, compose(executor.submit(new AddNode(name, tag, template)),
124                  customizeNodeAndAddToGoodMapOrPutExceptionIntoBadMapFactory.create(template.getOptions(),
125                           goodNodes, badNodes, customizationResponses), executor));
126      }
127      return responses;
128   }
129 
130   /**
131    * Find the next node names that can be used. These will be derived from the tag and the
132    * template. We will pre-allocate a specified quantity, and attempt to verify that there is no
133    * name conflict with the current service.
134    * 
135    * @param tag
136    * @param count
137    * @param template
138    * @return
139    */
140   protected Set<String> getNextNames(final String tag, final Template template, int count) {
141      Set<String> names = newLinkedHashSet();
142      Iterable<? extends ComputeMetadata> currentNodes = listNodesStrategy.listNodes();
143      int maxTries = 100;
144      int currentTries = 0;
145      while (names.size() < count && currentTries++ < maxTries) {
146         final String name = getNextName(tag, template);
147         if (!any(currentNodes, new Predicate<ComputeMetadata>() {
148 
149            @Override
150            public boolean apply(ComputeMetadata input) {
151               return name.equals(input.getName());
152            }
153 
154         })) {
155            names.add(name);
156         }
157      }
158      return names;
159   }
160 
161   /**
162    * Get a name using a random mechanism that still ties all nodes in a tag together.
163    * 
164    * This implementation will pass the tag and a hex formatted random number to the configured
165    * naming convention.
166    * 
167    */
168   protected String getNextName(final String tag, final Template template) {
169      return String.format(nodeNamingConvention, tag, Integer.toHexString(new SecureRandom().nextInt(4095)));
170   }
171 
172}

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