EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.rimuhosting.miro.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [ServerToNodeMetadata.java]

nameclass, %method, %block, %line, %
ServerToNodeMetadata.java0%   (0/3)0%   (0/9)0%   (0/268)0%   (0/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ServerToNodeMetadata0%   (0/1)0%   (0/4)0%   (0/212)0%   (0/32)
ServerToNodeMetadata (Function, Supplier, Map, Map, Supplier): void 0%   (0/1)0%   (0/36)0%   (0/8)
apply (Server): NodeMetadata 0%   (0/1)0%   (0/112)0%   (0/15)
findLocationWithId (String): Location 0%   (0/1)0%   (0/33)0%   (0/5)
parseOperatingSystem (Server, Location): OperatingSystem 0%   (0/1)0%   (0/31)0%   (0/4)
     
class ServerToNodeMetadata$10%   (0/1)0%   (0/2)0%   (0/15)0%   (0/2)
ServerToNodeMetadata$1 (ServerToNodeMetadata, String): void 0%   (0/1)0%   (0/9)0%   (0/1)
apply (Location): boolean 0%   (0/1)0%   (0/6)0%   (0/1)
     
class ServerToNodeMetadata$FindImageForServer0%   (0/1)0%   (0/3)0%   (0/41)0%   (0/6)
ServerToNodeMetadata$FindImageForServer (Location, Server): void 0%   (0/1)0%   (0/9)0%   (0/4)
ServerToNodeMetadata$FindImageForServer (Location, Server, ServerToNodeMetada... 0%   (0/1)0%   (0/5)0%   (0/1)
apply (Image): boolean 0%   (0/1)0%   (0/27)0%   (0/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.rimuhosting.miro.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.Image;
34import org.jclouds.compute.domain.NodeMetadata;
35import org.jclouds.compute.domain.NodeMetadataBuilder;
36import org.jclouds.compute.domain.NodeState;
37import org.jclouds.compute.domain.OperatingSystem;
38import org.jclouds.domain.Credentials;
39import org.jclouds.domain.Location;
40import org.jclouds.logging.Logger;
41import org.jclouds.rimuhosting.miro.domain.Server;
42import org.jclouds.rimuhosting.miro.domain.internal.RunningState;
43 
44import com.google.common.base.Function;
45import com.google.common.base.Predicate;
46import com.google.common.base.Supplier;
47import com.google.common.collect.Iterables;
48 
49/**
50 * 
51 * @author Adrian Cole
52 */
53@Singleton
54public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
55 
56   @Resource
57   protected Logger logger = Logger.NULL;
58   protected final Map<String, Credentials> credentialStore;
59   protected final Supplier<Set<? extends Location>> locations;
60   protected final Function<Server, Iterable<String>> getPublicAddresses;
61   protected final Map<RunningState, NodeState> runningStateToNodeState;
62   protected final Supplier<Set<? extends Image>> images;
63 
64   private static class FindImageForServer implements Predicate<Image> {
65      private final Location location;
66      private final Server instance;
67 
68      private FindImageForServer(Location location, Server instance) {
69         this.location = location;
70         this.instance = instance;
71      }
72 
73      @Override
74      public boolean apply(Image input) {
75         return input.getProviderId().equals(instance.getImageId())
76                  && (input.getLocation() == null || input.getLocation().equals(location) || input.getLocation()
77                           .equals(location.getParent()));
78      }
79   }
80 
81   @Inject
82   ServerToNodeMetadata(Function<Server, Iterable<String>> getPublicAddresses,
83            @Memoized Supplier<Set<? extends Location>> locations, Map<String, Credentials> credentialStore,
84            Map<RunningState, NodeState> runningStateToNodeState, @Memoized Supplier<Set<? extends Image>> images) {
85      this.getPublicAddresses = checkNotNull(getPublicAddresses, "serverStateToNodeState");
86      this.locations = checkNotNull(locations, "locations");
87      this.credentialStore = checkNotNull(credentialStore, "credentialStore");
88      this.runningStateToNodeState = checkNotNull(runningStateToNodeState, "serverStateToNodeState");
89      this.images = checkNotNull(images, "images");
90   }
91 
92   @Override
93   public NodeMetadata apply(Server from) {
94      NodeMetadataBuilder builder = new NodeMetadataBuilder();
95      builder.ids(from.getId() + "");
96      builder.name(from.getName());
97      Location location = findLocationWithId(from.getLocation().getId());
98      builder.location(location);
99      builder.group(parseGroupFromName(from.getName()));
100      builder.imageId(from.getImageId() + "");
101      builder.operatingSystem(parseOperatingSystem(from, location));
102      builder.hardware(null);// TODO
103      if (from.getBillingData() != null && from.getBillingData().getDateCancelled() != null
104               && RunningState.NOTRUNNING == from.getState())
105         builder.state(NodeState.TERMINATED);
106      else
107         builder.state(runningStateToNodeState.get(from.getState()));
108      builder.publicAddresses(getPublicAddresses.apply(from));
109      builder.credentials(credentialStore.get("node#" + from.getId()));
110      return builder.build();
111   }
112 
113   private Location findLocationWithId(final String locationId) {
114      try {
115         Location location = Iterables.find(locations.get(), new Predicate<Location>() {
116 
117            @Override
118            public boolean apply(Location input) {
119               return input.getId().equals(locationId);
120            }
121 
122         });
123         return location;
124 
125      } catch (NoSuchElementException e) {
126         logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
127         return null;
128      }
129   }
130 
131   protected OperatingSystem parseOperatingSystem(Server from, Location location) {
132      try {
133         return Iterables.find(images.get(), new FindImageForServer(location, from)).getOperatingSystem();
134      } catch (NoSuchElementException e) {
135         logger.warn("could not find a matching image for server %s in location %s", from, location);
136      }
137      return null;
138   }
139}

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