| 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 | */ |
| 19 | package org.jclouds.openstack.nova.compute.functions; |
| 20 | |
| 21 | import com.google.common.base.Function; |
| 22 | import com.google.common.base.Predicate; |
| 23 | import com.google.common.base.Supplier; |
| 24 | import com.google.common.collect.Iterables; |
| 25 | import org.jclouds.collect.Memoized; |
| 26 | import org.jclouds.compute.domain.*; |
| 27 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 28 | import org.jclouds.domain.Credentials; |
| 29 | import org.jclouds.domain.Location; |
| 30 | import org.jclouds.domain.LocationBuilder; |
| 31 | import org.jclouds.domain.LocationScope; |
| 32 | import org.jclouds.logging.Logger; |
| 33 | import org.jclouds.openstack.nova.domain.Address; |
| 34 | import org.jclouds.openstack.nova.domain.Server; |
| 35 | import org.jclouds.openstack.nova.domain.ServerStatus; |
| 36 | |
| 37 | import javax.annotation.Resource; |
| 38 | import javax.inject.Inject; |
| 39 | import javax.inject.Named; |
| 40 | import javax.inject.Singleton; |
| 41 | import java.util.Map; |
| 42 | import java.util.NoSuchElementException; |
| 43 | import java.util.Set; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName; |
| 47 | |
| 48 | /** |
| 49 | * @author Adrian Cole |
| 50 | */ |
| 51 | @Singleton |
| 52 | public 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 | } |