1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
81 }
82
83
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 }