| 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 | */ |
| 17 | package org.jclouds.glesys.compute.functions; |
| 18 | |
| 19 | import static com.google.common.base.Charsets.UTF_8; |
| 20 | import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | import static com.google.common.base.Preconditions.checkState; |
| 22 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 23 | import static com.google.common.io.BaseEncoding.base16; |
| 24 | import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTagsFromCommaDelimitedValue; |
| 25 | import static org.jclouds.location.predicates.LocationPredicates.idEquals; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | import java.util.NoSuchElementException; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import javax.annotation.Resource; |
| 32 | import javax.inject.Inject; |
| 33 | import javax.inject.Named; |
| 34 | import javax.inject.Singleton; |
| 35 | |
| 36 | import org.jclouds.collect.Memoized; |
| 37 | import org.jclouds.compute.domain.HardwareBuilder; |
| 38 | import org.jclouds.compute.domain.Image; |
| 39 | import org.jclouds.compute.domain.NodeMetadata; |
| 40 | import org.jclouds.compute.domain.NodeMetadata.Status; |
| 41 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 42 | import org.jclouds.compute.domain.OperatingSystem; |
| 43 | import org.jclouds.compute.domain.Processor; |
| 44 | import org.jclouds.compute.domain.Volume; |
| 45 | import org.jclouds.compute.domain.internal.VolumeImpl; |
| 46 | import org.jclouds.compute.functions.GroupNamingConvention; |
| 47 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 48 | import org.jclouds.domain.Location; |
| 49 | import org.jclouds.glesys.domain.Ip; |
| 50 | import org.jclouds.glesys.domain.ServerDetails; |
| 51 | import org.jclouds.logging.Logger; |
| 52 | import org.jclouds.util.InetAddresses2.IsPrivateIPAddress; |
| 53 | |
| 54 | import com.google.common.base.Function; |
| 55 | import com.google.common.base.Predicate; |
| 56 | import com.google.common.base.Predicates; |
| 57 | import com.google.common.base.Splitter; |
| 58 | import com.google.common.base.Strings; |
| 59 | import com.google.common.base.Supplier; |
| 60 | import com.google.common.collect.FluentIterable; |
| 61 | import com.google.common.collect.ImmutableList; |
| 62 | import com.google.common.collect.ImmutableMap; |
| 63 | import com.google.common.collect.Iterables; |
| 64 | |
| 65 | /** |
| 66 | * @author Adrian Cole |
| 67 | */ |
| 68 | @Singleton |
| 69 | public 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 | } |