EMMA Coverage Report (generated Tue Jun 21 05:51:52 EDT 2011)
[all classes][org.jclouds.cloudservers.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [ServerToNodeMetadata.java]

nameclass, %method, %block, %line, %
ServerToNodeMetadata.java75%  (3/4)100% (10/10)100% (256/256)100% (40/40)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ServerToNodeMetadata100% (1/1)100% (4/4)100% (208/208)100% (30/30)
ServerToNodeMetadata (Map, Map, Supplier, Supplier, Supplier): void 100% (1/1)100% (36/36)100% (8/8)
apply (Server): NodeMetadata 100% (1/1)100% (116/116)100% (14/14)
parseHardware (Server): Hardware 100% (1/1)100% (25/25)100% (4/4)
parseOperatingSystem (Server): OperatingSystem 100% (1/1)100% (31/31)100% (4/4)
     
class ServerToNodeMetadata$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
class ServerToNodeMetadata$FindHardwareForServer100% (1/1)100% (3/3)100% (24/24)100% (5/5)
ServerToNodeMetadata$FindHardwareForServer (Server): void 100% (1/1)100% (6/6)100% (3/3)
ServerToNodeMetadata$FindHardwareForServer (Server, ServerToNodeMetadata$1): ... 100% (1/1)100% (4/4)100% (1/1)
apply (Hardware): boolean 100% (1/1)100% (14/14)100% (1/1)
     
class ServerToNodeMetadata$FindImageForServer100% (1/1)100% (3/3)100% (24/24)100% (5/5)
ServerToNodeMetadata$FindImageForServer (Server): void 100% (1/1)100% (6/6)100% (3/3)
ServerToNodeMetadata$FindImageForServer (Server, ServerToNodeMetadata$1): void 100% (1/1)100% (4/4)100% (1/1)
apply (Image): boolean 100% (1/1)100% (14/14)100% (1/1)

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 */
19package org.jclouds.cloudservers.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.NoSuchElementException;
26import java.util.Set;
27 
28import javax.annotation.Resource;
29import javax.inject.Inject;
30import javax.inject.Named;
31import javax.inject.Singleton;
32 
33import org.jclouds.cloudservers.domain.Server;
34import org.jclouds.cloudservers.domain.ServerStatus;
35import org.jclouds.collect.Memoized;
36import org.jclouds.compute.domain.Hardware;
37import org.jclouds.compute.domain.Image;
38import org.jclouds.compute.domain.NodeMetadata;
39import org.jclouds.compute.domain.NodeMetadataBuilder;
40import org.jclouds.compute.domain.NodeState;
41import org.jclouds.compute.domain.OperatingSystem;
42import org.jclouds.compute.reference.ComputeServiceConstants;
43import org.jclouds.domain.Credentials;
44import org.jclouds.domain.Location;
45import org.jclouds.domain.LocationBuilder;
46import org.jclouds.domain.LocationScope;
47import org.jclouds.logging.Logger;
48 
49import com.google.common.base.Function;
50import com.google.common.base.Predicate;
51import com.google.common.base.Supplier;
52import com.google.common.collect.Iterables;
53 
54/**
55 * @author Adrian Cole
56 */
57@Singleton
58public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
59   @Resource
60   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
61   protected Logger logger = Logger.NULL;
62 
63   protected final Supplier<Location> location;
64   protected final Map<String, Credentials> credentialStore;
65   protected final Map<ServerStatus, NodeState> serverToNodeState;
66   protected final Supplier<Set<? extends Image>> images;
67   protected final Supplier<Set<? extends Hardware>> hardwares;
68 
69   private static class FindImageForServer implements Predicate<Image> {
70      private final Server instance;
71 
72      private FindImageForServer(Server instance) {
73         this.instance = instance;
74      }
75 
76      @Override
77      public boolean apply(Image input) {
78         return input.getProviderId().equals(instance.getImageId() + "");
79      }
80   }
81 
82   private static class FindHardwareForServer implements Predicate<Hardware> {
83      private final Server instance;
84 
85      private FindHardwareForServer(Server instance) {
86         this.instance = instance;
87      }
88 
89      @Override
90      public boolean apply(Hardware input) {
91         return input.getProviderId().equals(instance.getFlavorId() + "");
92      }
93   }
94 
95   @Inject
96   ServerToNodeMetadata(Map<ServerStatus, NodeState> serverStateToNodeState, Map<String, Credentials> credentialStore,
97            @Memoized Supplier<Set<? extends Image>> images, Supplier<Location> location,
98            @Memoized Supplier<Set<? extends Hardware>> hardwares) {
99      this.serverToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState");
100      this.credentialStore = checkNotNull(credentialStore, "credentialStore");
101      this.images = checkNotNull(images, "images");
102      this.location = checkNotNull(location, "location");
103      this.hardwares = checkNotNull(hardwares, "hardwares");
104   }
105 
106   @Override
107   public NodeMetadata apply(Server from) {
108      NodeMetadataBuilder builder = new NodeMetadataBuilder();
109      builder.ids(from.getId() + "");
110      builder.name(from.getName());
111      builder.location(new LocationBuilder().scope(LocationScope.HOST).id(from.getHostId()).description(
112               from.getHostId()).parent(location.get()).build());
113      builder.userMetadata(from.getMetadata());
114      builder.group(parseGroupFromName(from.getName()));
115      builder.imageId(from.getImageId() + "");
116      builder.operatingSystem(parseOperatingSystem(from));
117      builder.hardware(parseHardware(from));
118      builder.state(serverToNodeState.get(from.getStatus()));
119      builder.publicAddresses(from.getAddresses().getPublicAddresses());
120      builder.privateAddresses(from.getAddresses().getPrivateAddresses());
121      builder.credentials(credentialStore.get("node#" + from.getId()));
122      return builder.build();
123   }
124 
125   protected Hardware parseHardware(Server from) {
126      try {
127         return Iterables.find(hardwares.get(), new FindHardwareForServer(from));
128      } catch (NoSuchElementException e) {
129         logger.warn("could not find a matching hardware for server %s", from);
130      }
131      return null;
132   }
133 
134   protected OperatingSystem parseOperatingSystem(Server from) {
135      try {
136         return Iterables.find(images.get(), new FindImageForServer(from)).getOperatingSystem();
137      } catch (NoSuchElementException e) {
138         logger.warn("could not find a matching image for server %s in location %s", from, location);
139      }
140      return null;
141   }
142}

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