EMMA Coverage Report (generated Mon Dec 09 15:12:29 EST 2013)
[all classes][org.jclouds.glesys.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [ServerDetailsToNodeMetadata.java]

nameclass, %method, %block, %line, %
ServerDetailsToNodeMetadata.java100% (3/3)100% (9/9)88%  (243/276)86%  (30/35)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ServerDetailsToNodeMetadata100% (1/1)100% (4/4)87%  (216/249)83%  (24/29)
parseOperatingSystem (ServerDetails): OperatingSystem 100% (1/1)50%  (13/26)25%  (1/4)
apply (ServerDetails): NodeMetadata 100% (1/1)89%  (162/182)89%  (16/18)
<static initializer> 100% (1/1)100% (16/16)100% (1/1)
ServerDetailsToNodeMetadata (Supplier, Supplier, GroupNamingConvention$Factor... 100% (1/1)100% (25/25)100% (6/6)
     
class ServerDetailsToNodeMetadata$1100% (1/1)100% (2/2)100% (10/10)100% (2/2)
ServerDetailsToNodeMetadata$1 (ServerDetailsToNodeMetadata): void 100% (1/1)100% (6/6)100% (1/1)
apply (Ip): String 100% (1/1)100% (4/4)100% (1/1)
     
class ServerDetailsToNodeMetadata$FindImageForServer100% (1/1)100% (3/3)100% (17/17)100% (5/5)
ServerDetailsToNodeMetadata$FindImageForServer (ServerDetails): void 100% (1/1)100% (6/6)100% (3/3)
ServerDetailsToNodeMetadata$FindImageForServer (ServerDetails, ServerDetailsT... 100% (1/1)100% (4/4)100% (1/1)
apply (Image): boolean 100% (1/1)100% (7/7)100% (1/1)

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package org.jclouds.glesys.compute.functions;
18 
19import static com.google.common.base.Charsets.UTF_8;
20import static com.google.common.base.Preconditions.checkNotNull;
21import static com.google.common.base.Preconditions.checkState;
22import static com.google.common.base.Strings.isNullOrEmpty;
23import static com.google.common.io.BaseEncoding.base16;
24import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTagsFromCommaDelimitedValue;
25import static org.jclouds.location.predicates.LocationPredicates.idEquals;
26 
27import java.util.Map;
28import java.util.NoSuchElementException;
29import java.util.Set;
30 
31import javax.annotation.Resource;
32import javax.inject.Inject;
33import javax.inject.Named;
34import javax.inject.Singleton;
35 
36import org.jclouds.collect.Memoized;
37import org.jclouds.compute.domain.HardwareBuilder;
38import org.jclouds.compute.domain.Image;
39import org.jclouds.compute.domain.NodeMetadata;
40import org.jclouds.compute.domain.NodeMetadata.Status;
41import org.jclouds.compute.domain.NodeMetadataBuilder;
42import org.jclouds.compute.domain.OperatingSystem;
43import org.jclouds.compute.domain.Processor;
44import org.jclouds.compute.domain.Volume;
45import org.jclouds.compute.domain.internal.VolumeImpl;
46import org.jclouds.compute.functions.GroupNamingConvention;
47import org.jclouds.compute.reference.ComputeServiceConstants;
48import org.jclouds.domain.Location;
49import org.jclouds.glesys.domain.Ip;
50import org.jclouds.glesys.domain.ServerDetails;
51import org.jclouds.logging.Logger;
52import org.jclouds.util.InetAddresses2.IsPrivateIPAddress;
53 
54import com.google.common.base.Function;
55import com.google.common.base.Predicate;
56import com.google.common.base.Predicates;
57import com.google.common.base.Splitter;
58import com.google.common.base.Strings;
59import com.google.common.base.Supplier;
60import com.google.common.collect.FluentIterable;
61import com.google.common.collect.ImmutableList;
62import com.google.common.collect.ImmutableMap;
63import com.google.common.collect.Iterables;
64 
65/**
66 * @author Adrian Cole
67 */
68@Singleton
69public class ServerDetailsToNodeMetadata implements Function<ServerDetails, NodeMetadata> {
70   @Resource
71   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
72   protected Logger logger = Logger.NULL;
73   public static final Map<ServerDetails.State, Status> serverStateToNodeStatus = ImmutableMap
74         .<ServerDetails.State, Status> builder().put(ServerDetails.State.STOPPED, Status.SUSPENDED)
75         .put(ServerDetails.State.LOCKED, Status.PENDING)
76         .put(ServerDetails.State.RUNNING, Status.RUNNING)
77         .put(ServerDetails.State.UNRECOGNIZED, Status.UNRECOGNIZED).build();
78 
79   protected final Supplier<Set<? extends Image>> images;
80   protected final Supplier<Set<? extends Location>> locations;
81   protected final GroupNamingConvention nodeNamingConvention;
82 
83   private static class FindImageForServer implements Predicate<Image> {
84      private final ServerDetails instance;
85 
86      private FindImageForServer(ServerDetails instance) {
87         this.instance = instance;
88      }
89 
90      @Override
91      public boolean apply(Image input) {
92         return input.getProviderId().equals(instance.getTemplateName());
93      }
94   }
95 
96   @Inject
97   ServerDetailsToNodeMetadata(@Memoized Supplier<Set<? extends Location>> locations,
98         @Memoized Supplier<Set<? extends Image>> images, GroupNamingConvention.Factory namingConvention) {
99      this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
100      this.locations = checkNotNull(locations, "locations");
101      this.images = checkNotNull(images, "images");
102   }
103 
104   @Override
105   public NodeMetadata apply(ServerDetails from) {
106      NodeMetadataBuilder builder = new NodeMetadataBuilder();
107      builder.ids(from.getId() + "");
108      builder.name(from.getHostname());
109      builder.hostname(from.getHostname());
110      Location location = FluentIterable.from(locations.get()).firstMatch(idEquals(from.getDatacenter())).orNull();
111      checkState(location != null, "no location matched ServerDetails %s", from);
112      builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getHostname()));
113      
114      // TODO: get glesys to stop stripping out equals and commas!
115      if (!isNullOrEmpty(from.getDescription()) && from.getDescription().matches("^[0-9A-Fa-f]+$")) {
116         String decoded = new String(base16().lowerCase().decode(from.getDescription()), UTF_8);
117         addMetadataAndParseTagsFromCommaDelimitedValue(builder,
118                  Splitter.on('\n').withKeyValueSeparator("=").split(decoded));
119      }
120 
121      builder.imageId(from.getTemplateName() + "");
122      builder.operatingSystem(parseOperatingSystem(from));
123      builder.hardware(new HardwareBuilder().ids(from.getId() + "").ram(from.getMemorySizeMB())
124            .processors(ImmutableList.of(new Processor(from.getCpuCores(), 1.0)))
125            .volumes(ImmutableList.<Volume> of(new VolumeImpl((float) from.getDiskSizeGB(), true, true)))
126            .hypervisor(from.getPlatform()).build());
127      builder.status(serverStateToNodeStatus.get(from.getState()));
128      Iterable<String> addresses = Iterables.filter(Iterables.transform(from.getIps(), new Function<Ip, String>() {
129 
130         @Override
131         public String apply(Ip arg0) {
132            return Strings.emptyToNull(arg0.getIp());
133         }
134 
135      }), Predicates.notNull());
136      builder.publicAddresses(Iterables.filter(addresses, Predicates.not(IsPrivateIPAddress.INSTANCE)));
137      builder.privateAddresses(Iterables.filter(addresses, IsPrivateIPAddress.INSTANCE));
138      return builder.build();
139   }
140 
141   protected OperatingSystem parseOperatingSystem(ServerDetails from) {
142      try {
143         return Iterables.find(images.get(), new FindImageForServer(from)).getOperatingSystem();
144      } catch (NoSuchElementException e) {
145         logger.debug("could not find a matching image for server %s", from);
146      }
147      return null;
148   }
149}

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