| 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.savvis.vpdc.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Predicates.not; |
| 23 | import static com.google.common.collect.Iterables.filter; |
| 24 | import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.collect.FindResourceInSet; |
| 33 | import org.jclouds.collect.Memoized; |
| 34 | import org.jclouds.compute.domain.CIMOperatingSystem; |
| 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.domain.Credentials; |
| 39 | import org.jclouds.domain.Location; |
| 40 | import org.jclouds.savvis.vpdc.domain.VM; |
| 41 | import org.jclouds.savvis.vpdc.util.Utils; |
| 42 | import org.jclouds.util.InetAddresses2.IsPrivateIPAddress; |
| 43 | |
| 44 | import com.google.common.base.Function; |
| 45 | import com.google.common.base.Supplier; |
| 46 | import com.google.common.collect.ImmutableMap; |
| 47 | import com.google.common.collect.Iterables; |
| 48 | |
| 49 | /** |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | @Singleton |
| 53 | public class VMToNodeMetadata implements Function<VM, NodeMetadata> { |
| 54 | |
| 55 | public static final Map<VM.Status, NodeState> VAPPSTATUS_TO_NODESTATE = ImmutableMap |
| 56 | .<VM.Status, NodeState> builder().put(VM.Status.OFF, NodeState.SUSPENDED).put(VM.Status.ON, |
| 57 | NodeState.RUNNING).put(VM.Status.RESOLVED, NodeState.PENDING).put(VM.Status.UNRECOGNIZED, |
| 58 | NodeState.UNRECOGNIZED).put(VM.Status.UNKNOWN, NodeState.UNRECOGNIZED).put(VM.Status.SUSPENDED, |
| 59 | NodeState.SUSPENDED).put(VM.Status.UNRESOLVED, NodeState.PENDING).build(); |
| 60 | |
| 61 | private final FindLocationForVM findLocationForVM; |
| 62 | private final Map<String, Credentials> credentialStore; |
| 63 | |
| 64 | @Inject |
| 65 | VMToNodeMetadata(Map<String, Credentials> credentialStore, FindLocationForVM findLocationForVM) { |
| 66 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
| 67 | this.findLocationForVM = checkNotNull(findLocationForVM, "findLocationForVM"); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public NodeMetadata apply(VM from) { |
| 72 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
| 73 | builder.ids(from.getHref().toASCIIString()); |
| 74 | builder.name(from.getName()); |
| 75 | builder.location(findLocationForVM.apply(from)); |
| 76 | builder.group(parseGroupFromName(from.getName())); |
| 77 | try { |
| 78 | builder.operatingSystem(CIMOperatingSystem.toComputeOs(from.getOperatingSystemSection())); |
| 79 | } catch (NullPointerException e) { |
| 80 | // os section was null |
| 81 | } |
| 82 | // TODO build from resource allocation section |
| 83 | // builder.hardware(findHardwareForVM.apply(from)); |
| 84 | builder.state(VAPPSTATUS_TO_NODESTATE.get(from.getStatus())); |
| 85 | Set<String> addresses = Utils.getIpsFromVM(from); |
| 86 | builder.publicAddresses(filter(addresses, not(IsPrivateIPAddress.INSTANCE))); |
| 87 | builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE)); |
| 88 | builder.credentials(credentialStore.get(from.getHref().toASCIIString())); |
| 89 | return builder.build(); |
| 90 | } |
| 91 | |
| 92 | @Singleton |
| 93 | public static class FindLocationForVM extends FindResourceInSet<VM, Location> { |
| 94 | |
| 95 | @Inject |
| 96 | public FindLocationForVM(@Memoized Supplier<Set<? extends Location>> hardware) { |
| 97 | super(hardware); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean matches(VM from, Location input) { |
| 102 | return input.getId().equals(Iterables.get(from.getNetworkSection().getNetworks(), 0).getName()); |
| 103 | } |
| 104 | } |
| 105 | } |