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

COVERAGE SUMMARY FOR SOURCE FILE [VirtualGuestToNodeMetadata.java]

nameclass, %method, %block, %line, %
VirtualGuestToNodeMetadata.java100% (4/4)78%  (7/9)75%  (194/260)71%  (39/55)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class VirtualGuestToNodeMetadata$GetImageForVirtualGuest100% (1/1)50%  (1/2)15%  (6/41)27%  (3/11)
getImage (VirtualGuest): Image 0%   (0/1)0%   (0/35)0%   (0/8)
VirtualGuestToNodeMetadata$GetImageForVirtualGuest (SoftLayerClient): void 100% (1/1)100% (6/6)100% (3/3)
     
class VirtualGuestToNodeMetadata$GetHardwareForVirtualGuest100% (1/1)50%  (1/2)34%  (15/44)36%  (4/11)
getHardware (VirtualGuest): Hardware 0%   (0/1)0%   (0/29)0%   (0/7)
VirtualGuestToNodeMetadata$GetHardwareForVirtualGuest (SoftLayerClient, Funct... 100% (1/1)100% (15/15)100% (4/4)
     
class VirtualGuestToNodeMetadata$FindLocationForVirtualGuest100% (1/1)100% (2/2)89%  (16/18)83%  (5/6)
matches (VirtualGuest, Location): boolean 100% (1/1)86%  (12/14)75%  (3/4)
VirtualGuestToNodeMetadata$FindLocationForVirtualGuest (Supplier): void 100% (1/1)100% (4/4)100% (2/2)
     
class VirtualGuestToNodeMetadata100% (1/1)100% (3/3)100% (157/157)100% (27/27)
<static initializer> 100% (1/1)100% (16/16)100% (1/1)
VirtualGuestToNodeMetadata (Map, VirtualGuestToNodeMetadata$FindLocationForVi... 100% (1/1)100% (27/27)100% (6/6)
apply (VirtualGuest): NodeMetadata 100% (1/1)100% (114/114)100% (20/20)

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.softlayer.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName;
23 
24import java.util.Map;
25import java.util.Set;
26 
27import javax.inject.Inject;
28import javax.inject.Singleton;
29 
30import org.jclouds.collect.FindResourceInSet;
31import org.jclouds.collect.Memoized;
32import org.jclouds.compute.domain.Hardware;
33import org.jclouds.compute.domain.Image;
34import org.jclouds.compute.domain.NodeMetadata;
35import org.jclouds.compute.domain.NodeMetadataBuilder;
36import org.jclouds.compute.domain.NodeState;
37import org.jclouds.domain.Credentials;
38import org.jclouds.domain.Location;
39import org.jclouds.softlayer.SoftLayerClient;
40import org.jclouds.softlayer.domain.Datacenter;
41import org.jclouds.softlayer.domain.ProductItem;
42import org.jclouds.softlayer.domain.ProductOrder;
43import org.jclouds.softlayer.domain.VirtualGuest;
44import org.jclouds.softlayer.predicates.ProductItemPredicates;
45 
46import com.google.common.base.Function;
47import com.google.common.base.Supplier;
48import com.google.common.collect.ImmutableMap;
49import com.google.common.collect.ImmutableSet;
50import com.google.common.collect.Iterables;
51 
52/**
53 * @author Adrian Cole
54 */
55@Singleton
56public class VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMetadata> {
57 
58   public static final Map<VirtualGuest.State, NodeState> serverStateToNodeState = ImmutableMap
59            .<VirtualGuest.State, NodeState> builder().put(VirtualGuest.State.HALTED, NodeState.PENDING).put(
60                     VirtualGuest.State.PAUSED, NodeState.SUSPENDED).put(VirtualGuest.State.RUNNING, NodeState.RUNNING)
61            .put(VirtualGuest.State.UNRECOGNIZED, NodeState.UNRECOGNIZED).build();
62 
63   private final Map<String, Credentials> credentialStore;
64   private final FindLocationForVirtualGuest findLocationForVirtualGuest;
65   private final GetHardwareForVirtualGuest getHardwareForVirtualGuest;
66   private final GetImageForVirtualGuest getImageForVirtualGuest;
67 
68   @Inject
69   VirtualGuestToNodeMetadata(Map<String, Credentials> credentialStore,
70            FindLocationForVirtualGuest findLocationForVirtualGuest,
71            GetHardwareForVirtualGuest getHardwareForVirtualGuest, GetImageForVirtualGuest getImageForVirtualGuest) {
72      this.credentialStore = checkNotNull(credentialStore, "credentialStore");
73      this.findLocationForVirtualGuest = checkNotNull(findLocationForVirtualGuest, "findLocationForVirtualGuest");
74      this.getHardwareForVirtualGuest = checkNotNull(getHardwareForVirtualGuest, "getHardwareForVirtualGuest");
75      this.getImageForVirtualGuest = checkNotNull(getImageForVirtualGuest, "getImageForVirtualGuest");
76   }
77 
78   @Override
79   public NodeMetadata apply(VirtualGuest from) {
80      // convert the result object to a jclouds NodeMetadata
81      NodeMetadataBuilder builder = new NodeMetadataBuilder();
82      builder.ids(from.getId() + "");
83      builder.name(from.getHostname());
84      builder.hostname(from.getHostname());
85      builder.location(findLocationForVirtualGuest.apply(from));
86      builder.group(parseGroupFromName(from.getHostname()));
87 
88      Image image = getImageForVirtualGuest.getImage(from);
89      if (image != null) {
90         builder.imageId(image.getId());
91         builder.operatingSystem(image.getOperatingSystem());
92      }
93 
94      Hardware hardware = getHardwareForVirtualGuest.getHardware(from);
95      if (hardware != null)
96         builder.hardware(hardware);
97 
98      builder.state(serverStateToNodeState.get(from.getPowerState().getKeyName()));
99 
100      // These are null for 'bad' guest orders in the HALTED state.
101      if (from.getPrimaryIpAddress() != null)
102         builder.publicAddresses(ImmutableSet.<String> of(from.getPrimaryIpAddress()));
103      if (from.getPrimaryBackendIpAddress() != null)
104         builder.privateAddresses(ImmutableSet.<String> of(from.getPrimaryBackendIpAddress()));
105 
106      builder.credentials(credentialStore.get("node#" + from.getId()));
107      return builder.build();
108   }
109 
110   @Singleton
111   public static class FindLocationForVirtualGuest extends FindResourceInSet<VirtualGuest, Location> {
112 
113      @Inject
114      public FindLocationForVirtualGuest(@Memoized Supplier<Set<? extends Location>> location) {
115         super(location);
116      }
117 
118      @Override
119      public boolean matches(VirtualGuest from, Location input) {
120         Datacenter dc = from.getDatacenter();
121         if (dc == null)
122            return false;
123         return input.getId().equals(Integer.toString(dc.getId()));
124      }
125   }
126 
127   @Singleton
128   public static class GetHardwareForVirtualGuest {
129 
130      private final SoftLayerClient client;
131      private final Function<Iterable<ProductItem>, Hardware> productItemsToHardware;
132 
133      @Inject
134      public GetHardwareForVirtualGuest(SoftLayerClient client,
135               Function<Iterable<ProductItem>, Hardware> productItemsToHardware) {
136         this.client = checkNotNull(client, "client");
137         this.productItemsToHardware = checkNotNull(productItemsToHardware, "productItemsToHardware");
138 
139      }
140 
141      public Hardware getHardware(VirtualGuest guest) {
142         // 'bad' orders have no start cpu's and cause the order lookup to fail.
143         if (guest.getStartCpus() < 1)
144            return null;
145         ProductOrder order = client.getVirtualGuestClient().getOrderTemplate(guest.getId());
146         if (order == null)
147            return null;
148         Iterable<ProductItem> items = Iterables.transform(order.getPrices(), ProductItems.item());
149         return productItemsToHardware.apply(items);
150      }
151   }
152 
153   @Singleton
154   public static class GetImageForVirtualGuest {
155 
156      private SoftLayerClient client;
157 
158      @Inject
159      public GetImageForVirtualGuest(SoftLayerClient client) {
160         this.client = client;
161      }
162 
163      public Image getImage(VirtualGuest guest) {
164         // 'bad' orders have no start cpu's and cause the order lookup to fail.
165         if (guest.getStartCpus() < 1)
166            return null;
167         ProductOrder order = client.getVirtualGuestClient().getOrderTemplate(guest.getId());
168         if (order == null)
169            return null;
170         Iterable<ProductItem> items = Iterables.transform(order.getPrices(), ProductItems.item());
171         ProductItem os = Iterables.find(items, ProductItemPredicates.categoryCode("os"));
172         return new ProductItemToImage().apply(os);
173      }
174   }
175 
176}

[all classes][org.jclouds.softlayer.compute.functions]
EMMA 2.0.5312 (C) Vladimir Roubtsov