| 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.slicehost.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.Singleton; |
| 31 | |
| 32 | import org.jclouds.collect.Memoized; |
| 33 | import org.jclouds.compute.domain.Hardware; |
| 34 | import org.jclouds.compute.domain.Image; |
| 35 | import org.jclouds.compute.domain.NodeMetadata; |
| 36 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 37 | import org.jclouds.compute.domain.NodeState; |
| 38 | import org.jclouds.compute.domain.OperatingSystem; |
| 39 | import org.jclouds.domain.Credentials; |
| 40 | import org.jclouds.domain.Location; |
| 41 | import org.jclouds.logging.Logger; |
| 42 | import org.jclouds.slicehost.domain.Slice; |
| 43 | |
| 44 | import com.google.common.base.Function; |
| 45 | import com.google.common.base.Predicate; |
| 46 | import com.google.common.base.Supplier; |
| 47 | import com.google.common.collect.Iterables; |
| 48 | |
| 49 | /** |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | @Singleton |
| 53 | public class SliceToNodeMetadata implements Function<Slice, NodeMetadata> { |
| 54 | protected final Supplier<Location> location; |
| 55 | protected final Map<Slice.Status, NodeState> sliceToNodeState; |
| 56 | protected final Supplier<Set<? extends Image>> images; |
| 57 | protected final Supplier<Set<? extends Hardware>> hardwares; |
| 58 | protected final Map<String, Credentials> credentialStore; |
| 59 | |
| 60 | @Resource |
| 61 | protected Logger logger = Logger.NULL; |
| 62 | |
| 63 | private static class FindImageForSlice implements Predicate<Image> { |
| 64 | private final Slice slice; |
| 65 | |
| 66 | private FindImageForSlice(Slice slice) { |
| 67 | this.slice = slice; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean apply(Image input) { |
| 72 | return input.getProviderId().equals(slice.getImageId() + ""); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private static class FindHardwareForSlice implements Predicate<Hardware> { |
| 77 | private final Slice slice; |
| 78 | |
| 79 | private FindHardwareForSlice(Slice slice) { |
| 80 | this.slice = slice; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean apply(Hardware input) { |
| 85 | return input.getProviderId().equals(slice.getFlavorId() + ""); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Inject |
| 90 | SliceToNodeMetadata(Map<Slice.Status, NodeState> sliceStateToNodeState, Map<String, Credentials> credentialStore, |
| 91 | @Memoized Supplier<Set<? extends Image>> images, Supplier<Location> location, |
| 92 | @Memoized Supplier<Set<? extends Hardware>> hardwares) { |
| 93 | this.sliceToNodeState = checkNotNull(sliceStateToNodeState, "sliceStateToNodeState"); |
| 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(Slice from) { |
| 102 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 103 | builder.ids(from.getId() + ""); |
| 104 | builder.name(from.getName()); |
| 105 | builder.hostname(from.getName()); |
| 106 | builder.location(location.get()); |
| 107 | builder.group(parseGroupFromName(from.getName())); |
| 108 | builder.imageId(from.getImageId() + ""); |
| 109 | builder.operatingSystem(parseOperatingSystem(from)); |
| 110 | builder.hardware(parseHardware(from)); |
| 111 | builder.state(sliceToNodeState.get(from.getStatus())); |
| 112 | builder.publicAddresses(Iterables.filter(from.getAddresses(), new Predicate<String>() { |
| 113 | |
| 114 | @Override |
| 115 | public boolean apply(String input) { |
| 116 | return !input.startsWith("10."); |
| 117 | } |
| 118 | |
| 119 | })); |
| 120 | builder.privateAddresses(Iterables.filter(from.getAddresses(), new Predicate<String>() { |
| 121 | |
| 122 | @Override |
| 123 | public boolean apply(String input) { |
| 124 | return input.startsWith("10."); |
| 125 | } |
| 126 | |
| 127 | })); |
| 128 | builder.credentials(credentialStore.get("node#" + from.getId())); |
| 129 | return builder.build(); |
| 130 | } |
| 131 | |
| 132 | protected Hardware parseHardware(Slice from) { |
| 133 | try { |
| 134 | return Iterables.find(hardwares.get(), new FindHardwareForSlice(from)); |
| 135 | } catch (NoSuchElementException e) { |
| 136 | logger.warn("could not find a matching hardware for slice %s", from); |
| 137 | } |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | protected OperatingSystem parseOperatingSystem(Slice from) { |
| 142 | try { |
| 143 | return Iterables.find(images.get(), new FindImageForSlice(from)).getOperatingSystem(); |
| 144 | } catch (NoSuchElementException e) { |
| 145 | logger.warn("could not find a matching image for slice %s in location %s", from, location); |
| 146 | } |
| 147 | return null; |
| 148 | } |
| 149 | } |