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

COVERAGE SUMMARY FOR SOURCE FILE [ServerInfoToNodeMetadata.java]

nameclass, %method, %block, %line, %
ServerInfoToNodeMetadata.java0%   (0/4)0%   (0/9)0%   (0/334)0%   (0/58)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ServerInfoToNodeMetadata0%   (0/1)0%   (0/3)0%   (0/176)0%   (0/25)
<static initializer> 0%   (0/1)0%   (0/22)0%   (0/1)
ServerInfoToNodeMetadata (Map, Function, Function, Function, Supplier): void 0%   (0/1)0%   (0/33)0%   (0/7)
apply (ServerInfo): NodeMetadata 0%   (0/1)0%   (0/121)0%   (0/17)
     
class ServerInfoToNodeMetadata$DeviceToVolume0%   (0/1)0%   (0/2)0%   (0/80)0%   (0/14)
ServerInfoToNodeMetadata$DeviceToVolume (Cache): void 0%   (0/1)0%   (0/12)0%   (0/4)
apply (Device): Volume 0%   (0/1)0%   (0/68)0%   (0/10)
     
class ServerInfoToNodeMetadata$FindImageForId0%   (0/1)0%   (0/2)0%   (0/9)0%   (0/3)
ServerInfoToNodeMetadata$FindImageForId (Supplier): void 0%   (0/1)0%   (0/4)0%   (0/2)
matches (String, Image): boolean 0%   (0/1)0%   (0/5)0%   (0/1)
     
class ServerInfoToNodeMetadata$GetImageIdFromServer0%   (0/1)0%   (0/2)0%   (0/69)0%   (0/16)
ServerInfoToNodeMetadata$GetImageIdFromServer (Cache): void 0%   (0/1)0%   (0/9)0%   (0/4)
apply (Server): String 0%   (0/1)0%   (0/60)0%   (0/12)

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.cloudsigma.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName;
23 
24import java.util.List;
25import java.util.Map;
26import java.util.Set;
27 
28import javax.annotation.Resource;
29import javax.inject.Inject;
30import javax.inject.Singleton;
31 
32import org.jclouds.cloudsigma.domain.Device;
33import org.jclouds.cloudsigma.domain.DriveInfo;
34import org.jclouds.cloudsigma.domain.Server;
35import org.jclouds.cloudsigma.domain.ServerInfo;
36import org.jclouds.cloudsigma.domain.ServerStatus;
37import org.jclouds.collect.FindResourceInSet;
38import org.jclouds.collect.Memoized;
39import org.jclouds.compute.domain.HardwareBuilder;
40import org.jclouds.compute.domain.Image;
41import org.jclouds.compute.domain.NodeMetadata;
42import org.jclouds.compute.domain.NodeMetadataBuilder;
43import org.jclouds.compute.domain.NodeState;
44import org.jclouds.compute.domain.Processor;
45import org.jclouds.compute.domain.Volume;
46import org.jclouds.compute.domain.VolumeBuilder;
47import org.jclouds.domain.Credentials;
48import org.jclouds.domain.Location;
49import org.jclouds.logging.Logger;
50 
51import com.google.common.base.Function;
52import com.google.common.base.Supplier;
53import com.google.common.cache.Cache;
54import com.google.common.collect.ImmutableList;
55import com.google.common.collect.ImmutableMap;
56import com.google.common.collect.ImmutableSet;
57import com.google.common.collect.Iterables;
58import com.google.common.util.concurrent.UncheckedExecutionException;
59 
60/**
61 * @author Adrian Cole
62 */
63@Singleton
64public class ServerInfoToNodeMetadata implements Function<ServerInfo, NodeMetadata> {
65   public static final Map<ServerStatus, NodeState> serverStatusToNodeState = ImmutableMap
66         .<ServerStatus, NodeState> builder().put(ServerStatus.ACTIVE, NodeState.RUNNING)//
67         .put(ServerStatus.STOPPED, NodeState.SUSPENDED)//
68         .put(ServerStatus.PAUSED, NodeState.SUSPENDED)//
69         .put(ServerStatus.DUMPED, NodeState.PENDING)//
70         .put(ServerStatus.DEAD, NodeState.TERMINATED)//
71         .put(ServerStatus.UNRECOGNIZED, NodeState.UNRECOGNIZED)//
72         .build();
73 
74   private final Function<Server, String> getImageIdFromServer;
75   private final Function<String, Image> findImageForId;
76   private final Map<String, Credentials> credentialStore;
77   private final Supplier<Location> locationSupplier;
78   private final Function<Device, Volume> deviceToVolume;
79 
80   @Inject
81   ServerInfoToNodeMetadata(Map<String, Credentials> credentialStore, Function<Server, String> getImageIdFromServer,
82         Function<String, Image> findImageForId, Function<Device, Volume> deviceToVolume,
83         Supplier<Location> locationSupplier) {
84      this.credentialStore = checkNotNull(credentialStore, "credentialStore");
85      this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier");
86      this.deviceToVolume = checkNotNull(deviceToVolume, "deviceToVolume");
87      this.findImageForId = checkNotNull(findImageForId, "findImageForId");
88      this.getImageIdFromServer = checkNotNull(getImageIdFromServer, "getImageIdFromServer");
89   }
90 
91   @SuppressWarnings({ "unchecked", "rawtypes" })
92   @Override
93   public NodeMetadata apply(ServerInfo from) {
94      NodeMetadataBuilder builder = new NodeMetadataBuilder();
95      builder.ids(from.getUuid());
96      builder.name(from.getName());
97      builder.location(locationSupplier.get());
98      builder.group(parseGroupFromName(from.getName()));
99 
100      String imageId = getImageIdFromServer.apply(from);
101      if (imageId != null) {
102         Image image = findImageForId.apply(imageId);
103         if (image != null) {
104            builder.operatingSystem(image.getOperatingSystem());
105            builder.adminPassword(image.getAdminPassword());
106         }
107      }
108      builder.hardware(new HardwareBuilder().ids(from.getUuid())
109            .processors(ImmutableList.of(new Processor(1, from.getCpu()))).ram(from.getMem())
110            .volumes((List) ImmutableList.of(Iterables.transform(from.getDevices().values(), deviceToVolume))).build());
111      builder.state(serverStatusToNodeState.get(from.getStatus()));
112      builder.publicAddresses(ImmutableSet.<String> of(from.getVnc().getIp()));
113      builder.privateAddresses(ImmutableSet.<String> of());
114      builder.credentials(credentialStore.get("node#"+ from.getUuid()));
115      return builder.build();
116   }
117 
118   @Singleton
119   public static final class DeviceToVolume implements Function<Device, Volume> {
120      @Resource
121      protected Logger logger = Logger.NULL;
122 
123      private final Cache<String, DriveInfo> cache;
124 
125      @Inject
126      public DeviceToVolume(Cache<String, DriveInfo> cache) {
127         this.cache = checkNotNull(cache, "cache");
128      }
129 
130      @Override
131      public Volume apply(Device input) {
132         VolumeBuilder builder = new VolumeBuilder();
133         builder.id(input.getId());
134         try {
135            DriveInfo drive = cache.getUnchecked(input.getDriveUuid());
136            builder.size(drive.getSize() / 1024 / 1024f);
137         } catch (NullPointerException e) {
138            logger.debug("drive %s not found", input.getDriveUuid());
139         } catch (UncheckedExecutionException e) {
140            logger.warn(e, "error finding drive %s: %s", input.getDriveUuid(), e.getMessage());
141         }
142         return new VolumeBuilder().durable(true).type(Volume.Type.NAS).build();
143      }
144   }
145 
146   /**
147    * When we create the boot drive of the server, by convention we set the name to the image it
148    * came from.
149    * 
150    * @author Adrian Cole
151    * 
152    */
153   @Singleton
154   public static class GetImageIdFromServer implements Function<Server, String> {
155      @Resource
156      protected Logger logger = Logger.NULL;
157 
158      private final Cache<String, DriveInfo> cache;
159 
160      @Inject
161      public GetImageIdFromServer(Cache<String, DriveInfo> cache) {
162         this.cache = cache;
163      }
164 
165      @Override
166      public String apply(Server from) {
167         String imageId = null;
168         String bootDeviceId = Iterables.get(from.getBootDeviceIds(), 0);
169         Device bootDevice = from.getDevices().get(bootDeviceId);
170         if (bootDevice != null) {
171            try {
172               DriveInfo drive = cache.getUnchecked(bootDevice.getDriveUuid());
173               imageId = drive.getName();
174            } catch (NullPointerException e) {
175               logger.debug("drive %s not found", bootDevice.getDriveUuid());
176            } catch (UncheckedExecutionException e) {
177               logger.warn(e, "error finding drive %s: %s", bootDevice.getDriveUuid(), e.getMessage());
178            }
179         }
180         return imageId;
181      }
182   }
183 
184   @Singleton
185   public static class FindImageForId extends FindResourceInSet<String, Image> {
186 
187      @Inject
188      public FindImageForId(@Memoized Supplier<Set<? extends Image>> images) {
189         super(images);
190      }
191 
192      @Override
193      public boolean matches(String from, Image input) {
194         return input.getProviderId().equals(from);
195      }
196   }
197 
198}

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