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

COVERAGE SUMMARY FOR SOURCE FILE [ServerToNodeMetadata.java]

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

COVERAGE BREAKDOWN BY CLASS AND METHOD

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