| 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.deltacloud.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.NoSuchElementException; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import javax.annotation.Resource; |
| 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Named; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.collect.Memoized; |
| 34 | import org.jclouds.compute.domain.Hardware; |
| 35 | import org.jclouds.compute.domain.Image; |
| 36 | import org.jclouds.compute.domain.NodeMetadata; |
| 37 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 38 | import org.jclouds.compute.domain.NodeState; |
| 39 | import org.jclouds.compute.domain.OperatingSystem; |
| 40 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 41 | import org.jclouds.deltacloud.domain.Instance; |
| 42 | import org.jclouds.domain.Credentials; |
| 43 | import org.jclouds.domain.Location; |
| 44 | import org.jclouds.logging.Logger; |
| 45 | |
| 46 | import com.google.common.base.Function; |
| 47 | import com.google.common.base.Predicate; |
| 48 | import com.google.common.base.Supplier; |
| 49 | import com.google.common.collect.ImmutableMap; |
| 50 | import com.google.common.collect.Iterables; |
| 51 | |
| 52 | /** |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | @Singleton |
| 56 | public class InstanceToNodeMetadata implements Function<Instance, NodeMetadata> { |
| 57 | |
| 58 | public static final Map<Instance.State, NodeState> instanceToNodeState = ImmutableMap |
| 59 | .<Instance.State, NodeState> builder().put(Instance.State.STOPPED, NodeState.SUSPENDED).put( |
| 60 | Instance.State.RUNNING, NodeState.RUNNING).put(Instance.State.PENDING, NodeState.PENDING).put( |
| 61 | Instance.State.UNRECOGNIZED, NodeState.UNRECOGNIZED).put(Instance.State.SHUTTING_DOWN, |
| 62 | NodeState.PENDING).put(Instance.State.START, NodeState.PENDING).build(); |
| 63 | |
| 64 | @Resource |
| 65 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 66 | protected Logger logger = Logger.NULL; |
| 67 | |
| 68 | protected final Map<String, Credentials> credentialStore; |
| 69 | protected final Supplier<Set<? extends Location>> locations; |
| 70 | protected final Supplier<Set<? extends Image>> images; |
| 71 | protected final Supplier<Set<? extends Hardware>> hardwares; |
| 72 | |
| 73 | private static class FindImageForInstance implements Predicate<Image> { |
| 74 | private final Instance instance; |
| 75 | |
| 76 | private FindImageForInstance(Instance instance) { |
| 77 | this.instance = instance; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean apply(Image input) { |
| 82 | return input.getUri().equals(instance.getImage()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private static class FindHardwareForInstance implements Predicate<Hardware> { |
| 87 | private final Instance instance; |
| 88 | |
| 89 | private FindHardwareForInstance(Instance instance) { |
| 90 | this.instance = instance; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean apply(Hardware input) { |
| 95 | return input.getUri().equals(instance.getHardwareProfile()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | protected Hardware parseHardware(Instance from) { |
| 100 | try { |
| 101 | return Iterables.find(hardwares.get(), new FindHardwareForInstance(from)); |
| 102 | } catch (NoSuchElementException e) { |
| 103 | logger.warn("could not find a matching hardware for instance %s", from); |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | protected OperatingSystem parseOperatingSystem(Instance from) { |
| 109 | try { |
| 110 | return Iterables.find(images.get(), new FindImageForInstance(from)).getOperatingSystem(); |
| 111 | } catch (NoSuchElementException e) { |
| 112 | logger.warn("could not find a matching image for instance %s", from); |
| 113 | } |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | private static class FindLocationForInstance implements Predicate<Location> { |
| 118 | private final Instance instance; |
| 119 | |
| 120 | private FindLocationForInstance(Instance instance) { |
| 121 | this.instance = instance; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public boolean apply(Location input) { |
| 126 | return input.getId().equals(instance.getRealm().toASCIIString()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | protected Location parseLocation(Instance from) { |
| 131 | try { |
| 132 | return Iterables.find(locations.get(), new FindLocationForInstance(from)); |
| 133 | } catch (NoSuchElementException e) { |
| 134 | logger.warn("could not find a matching realm for instance %s", from); |
| 135 | } |
| 136 | return null; |
| 137 | } |
| 138 | |
| 139 | @Inject |
| 140 | InstanceToNodeMetadata(Map<String, Credentials> credentialStore, |
| 141 | @Memoized Supplier<Set<? extends Location>> locations, @Memoized Supplier<Set<? extends Image>> images, |
| 142 | @Memoized Supplier<Set<? extends Hardware>> hardwares) { |
| 143 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 144 | this.images = checkNotNull(images, "images"); |
| 145 | this.locations = checkNotNull(locations, "locations"); |
| 146 | this.hardwares = checkNotNull(hardwares, "hardwares"); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public NodeMetadata apply(org.jclouds.deltacloud.domain.Instance from) { |
| 151 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 152 | builder.ids(from.getHref().toASCIIString()); |
| 153 | builder.name(from.getName()); |
| 154 | builder.location(parseLocation(from)); |
| 155 | builder.group(parseGroupFromName(from.getName())); |
| 156 | builder.imageId(from.getImage().toASCIIString()); |
| 157 | builder.operatingSystem(parseOperatingSystem(from)); |
| 158 | builder.hardware(parseHardware(from)); |
| 159 | builder.state(instanceToNodeState.get(from.getState())); |
| 160 | builder.publicAddresses(from.getPublicAddresses()); |
| 161 | builder.privateAddresses(from.getPrivateAddresses()); |
| 162 | builder.credentials(credentialStore.get(from.getHref().toASCIIString())); |
| 163 | return builder.build(); |
| 164 | } |
| 165 | } |