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

COVERAGE SUMMARY FOR SOURCE FILE [ServerToNodeMetadata.java]

nameclass, %method, %block, %line, %
ServerToNodeMetadata.java75%  (3/4)100% (10/10)91%  (252/276)91%  (39.9/44)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ServerToNodeMetadata100% (1/1)100% (4/4)89%  (181/203)88%  (30/34)
parseHardware (Server): Hardware 100% (1/1)61%  (17/28)67%  (4/6)
parseImage (Server): Image 100% (1/1)61%  (17/28)67%  (4/6)
ServerToNodeMetadata (Map, Map, Supplier, Supplier, Supplier): void 100% (1/1)100% (36/36)100% (8/8)
apply (Server): NodeMetadata 100% (1/1)100% (111/111)100% (14/14)
     
class ServerToNodeMetadata$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
class ServerToNodeMetadata$FindHardwareForServer100% (1/1)100% (3/3)96%  (24/25)99%  (4.9/5)
apply (Hardware): boolean 100% (1/1)93%  (14/15)93%  (0.9/1)
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)
     
class ServerToNodeMetadata$FindImageForServer100% (1/1)100% (3/3)98%  (47/48)99%  (5/5)
apply (Image): boolean 100% (1/1)97%  (37/38)97%  (1/1)
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)

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.gogrid.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.Singleton;
31 
32import org.jclouds.collect.Memoized;
33import org.jclouds.compute.domain.Hardware;
34import org.jclouds.compute.domain.Image;
35import org.jclouds.compute.domain.NodeMetadata;
36import org.jclouds.compute.domain.NodeMetadataBuilder;
37import org.jclouds.compute.domain.NodeState;
38import org.jclouds.domain.Credentials;
39import org.jclouds.domain.Location;
40import org.jclouds.gogrid.domain.Server;
41import org.jclouds.gogrid.domain.ServerState;
42import org.jclouds.logging.Logger;
43 
44import com.google.common.base.Function;
45import com.google.common.base.Predicate;
46import com.google.common.base.Supplier;
47import com.google.common.collect.ImmutableSet;
48import com.google.common.collect.Iterables;
49 
50/**
51 * @author Oleksiy Yarmula
52 */
53@Singleton
54public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
55 
56   @Resource
57   protected Logger logger = Logger.NULL;
58   private final Map<ServerState, NodeState> serverStateToNodeState;
59   private final Supplier<Set<? extends Image>> images;
60   private final Supplier<Set<? extends Hardware>> hardwares;
61   private final Supplier<Map<String, ? extends Location>> locations;
62   private final Map<String, Credentials> credentialStore;
63 
64   static class FindImageForServer implements Predicate<Image> {
65      private final Server instance;
66 
67      @Inject
68      private FindImageForServer(Server instance) {
69         this.instance = instance;
70      }
71 
72      @Override
73      public boolean apply(Image input) {
74         return input.getProviderId().equals(instance.getImage().getId() + "")
75                  && (input.getLocation() == null || input.getLocation().getId().equals(
76                           instance.getDatacenter().getId() + ""));
77      }
78   }
79 
80   static class FindHardwareForServer implements Predicate<Hardware> {
81      private final Server instance;
82 
83      @Inject
84      private FindHardwareForServer(Server instance) {
85         this.instance = instance;
86      }
87 
88      @Override
89      public boolean apply(Hardware input) {
90         return input.getRam() == Integer.parseInt(instance.getRam().getName().replaceAll("[^0-9]", ""));
91      }
92   }
93 
94   @Inject
95   ServerToNodeMetadata(Map<ServerState, NodeState> serverStateToNodeState,  Map<String, Credentials> credentialStore,
96            @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> hardwares,
97            Supplier<Map<String, ? extends Location>> locations) {
98      this.serverStateToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState");
99      this.credentialStore = checkNotNull(credentialStore, "credentialStore");
100      this.images = checkNotNull(images, "images");
101      this.hardwares = checkNotNull(hardwares, "hardwares");
102      this.locations = checkNotNull(locations, "locations");
103   }
104 
105   @Override
106   public NodeMetadata apply(Server from) {
107      NodeMetadataBuilder builder = new NodeMetadataBuilder();
108      builder.ids(from.getId() + "");
109      builder.name(from.getName());
110      builder.location(locations.get().get(from.getDatacenter().getId() + ""));
111      builder.group(parseGroupFromName(from.getName()));
112      builder.hardware(parseHardware(from));
113      builder.imageId(from.getImage().getId() + "");
114 
115      Image image = parseImage(from);
116      if (image != null)
117         builder.operatingSystem(image.getOperatingSystem());
118 
119      builder.state(serverStateToNodeState.get(from.getState()));
120      builder.publicAddresses(ImmutableSet.of(from.getIp().getIp()));
121      builder.credentials(credentialStore.get("node#" + from.getId()));
122      return builder.build();
123   }
124 
125   protected Image parseImage(Server from) {
126      Image image = null;
127      try {
128         image = Iterables.find(images.get(), new FindImageForServer(from));
129      } catch (NoSuchElementException e) {
130         logger.warn("could not find a matching image for server %s", from);
131      }
132      return image;
133   }
134 
135   protected Hardware parseHardware(Server from) {
136      Hardware hardware = null;
137      try {
138         hardware = Iterables.find(hardwares.get(), new FindHardwareForServer(from));
139      } catch (NoSuchElementException e) {
140         logger.warn("could not find a matching hardware for server %s", from);
141      }
142      return hardware;
143   }
144}

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