EMMA Coverage Report (generated Mon Oct 17 05:41:20 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/273)0%   (0/40)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ServerToNodeMetadata0%   (0/1)0%   (0/4)0%   (0/217)0%   (0/33)
ServerToNodeMetadata (Function, Supplier, Map, Map, Supplier): void 0%   (0/1)0%   (0/36)0%   (0/8)
apply (Server): NodeMetadata 0%   (0/1)0%   (0/117)0%   (0/16)
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 * 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.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      builder.hostname(from.getName());
98      Location location = findLocationWithId(from.getLocation().getId());
99      builder.location(location);
100      builder.group(parseGroupFromName(from.getName()));
101      builder.imageId(from.getImageId() + "");
102      builder.operatingSystem(parseOperatingSystem(from, location));
103      builder.hardware(null);// TODO
104      if (from.getBillingData() != null && from.getBillingData().getDateCancelled() != null
105               && RunningState.NOTRUNNING == from.getState())
106         builder.state(NodeState.TERMINATED);
107      else
108         builder.state(runningStateToNodeState.get(from.getState()));
109      builder.publicAddresses(getPublicAddresses.apply(from));
110      builder.credentials(credentialStore.get("node#" + from.getId()));
111      return builder.build();
112   }
113 
114   private Location findLocationWithId(final String locationId) {
115      try {
116         Location location = Iterables.find(locations.get(), new Predicate<Location>() {
117 
118            @Override
119            public boolean apply(Location input) {
120               return input.getId().equals(locationId);
121            }
122 
123         });
124         return location;
125 
126      } catch (NoSuchElementException e) {
127         logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
128         return null;
129      }
130   }
131 
132   protected OperatingSystem parseOperatingSystem(Server from, Location location) {
133      try {
134         return Iterables.find(images.get(), new FindImageForServer(location, from)).getOperatingSystem();
135      } catch (NoSuchElementException e) {
136         logger.warn("could not find a matching image for server %s in location %s", from, location);
137      }
138      return null;
139   }
140}

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