1 | /** |
2 | * |
3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
4 | * |
5 | * ==================================================================== |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * ==================================================================== |
18 | */ |
19 | package org.jclouds.cloudservers.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.cloudservers.domain.Server; |
34 | import org.jclouds.cloudservers.domain.ServerStatus; |
35 | import org.jclouds.collect.Memoized; |
36 | import org.jclouds.compute.domain.Hardware; |
37 | import org.jclouds.compute.domain.Image; |
38 | import org.jclouds.compute.domain.NodeMetadata; |
39 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
40 | import org.jclouds.compute.domain.NodeState; |
41 | import org.jclouds.compute.domain.OperatingSystem; |
42 | import org.jclouds.compute.reference.ComputeServiceConstants; |
43 | import org.jclouds.domain.Credentials; |
44 | import org.jclouds.domain.Location; |
45 | import org.jclouds.domain.LocationBuilder; |
46 | import org.jclouds.domain.LocationScope; |
47 | import org.jclouds.logging.Logger; |
48 | |
49 | import com.google.common.base.Function; |
50 | import com.google.common.base.Predicate; |
51 | import com.google.common.base.Supplier; |
52 | import com.google.common.collect.Iterables; |
53 | |
54 | /** |
55 | * @author Adrian Cole |
56 | */ |
57 | @Singleton |
58 | public class ServerToNodeMetadata implements Function<Server, NodeMetadata> { |
59 | @Resource |
60 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
61 | protected Logger logger = Logger.NULL; |
62 | |
63 | protected final Supplier<Location> location; |
64 | protected final Map<String, Credentials> credentialStore; |
65 | protected final Map<ServerStatus, NodeState> serverToNodeState; |
66 | protected final Supplier<Set<? extends Image>> images; |
67 | protected final Supplier<Set<? extends Hardware>> hardwares; |
68 | |
69 | private static class FindImageForServer implements Predicate<Image> { |
70 | private final Server instance; |
71 | |
72 | private FindImageForServer(Server instance) { |
73 | this.instance = instance; |
74 | } |
75 | |
76 | @Override |
77 | public boolean apply(Image input) { |
78 | return input.getProviderId().equals(instance.getImageId() + ""); |
79 | } |
80 | } |
81 | |
82 | private static class FindHardwareForServer implements Predicate<Hardware> { |
83 | private final Server instance; |
84 | |
85 | private FindHardwareForServer(Server instance) { |
86 | this.instance = instance; |
87 | } |
88 | |
89 | @Override |
90 | public boolean apply(Hardware input) { |
91 | return input.getProviderId().equals(instance.getFlavorId() + ""); |
92 | } |
93 | } |
94 | |
95 | @Inject |
96 | ServerToNodeMetadata(Map<ServerStatus, NodeState> serverStateToNodeState, Map<String, Credentials> credentialStore, |
97 | @Memoized Supplier<Set<? extends Image>> images, Supplier<Location> location, |
98 | @Memoized Supplier<Set<? extends Hardware>> hardwares) { |
99 | this.serverToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState"); |
100 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
101 | this.images = checkNotNull(images, "images"); |
102 | this.location = checkNotNull(location, "location"); |
103 | this.hardwares = checkNotNull(hardwares, "hardwares"); |
104 | } |
105 | |
106 | @Override |
107 | public NodeMetadata apply(Server from) { |
108 | NodeMetadataBuilder builder = new NodeMetadataBuilder(); |
109 | builder.ids(from.getId() + ""); |
110 | builder.name(from.getName()); |
111 | builder.location(new LocationBuilder().scope(LocationScope.HOST).id(from.getHostId()).description( |
112 | from.getHostId()).parent(location.get()).build()); |
113 | builder.userMetadata(from.getMetadata()); |
114 | builder.group(parseGroupFromName(from.getName())); |
115 | builder.imageId(from.getImageId() + ""); |
116 | builder.operatingSystem(parseOperatingSystem(from)); |
117 | builder.hardware(parseHardware(from)); |
118 | builder.state(serverToNodeState.get(from.getStatus())); |
119 | builder.publicAddresses(from.getAddresses().getPublicAddresses()); |
120 | builder.privateAddresses(from.getAddresses().getPrivateAddresses()); |
121 | builder.credentials(credentialStore.get("node#" + from.getId())); |
122 | return builder.build(); |
123 | } |
124 | |
125 | protected Hardware parseHardware(Server from) { |
126 | try { |
127 | return Iterables.find(hardwares.get(), new FindHardwareForServer(from)); |
128 | } catch (NoSuchElementException e) { |
129 | logger.warn("could not find a matching hardware for server %s", from); |
130 | } |
131 | return null; |
132 | } |
133 | |
134 | protected OperatingSystem parseOperatingSystem(Server from) { |
135 | try { |
136 | return Iterables.find(images.get(), new FindImageForServer(from)).getOperatingSystem(); |
137 | } catch (NoSuchElementException e) { |
138 | logger.warn("could not find a matching image for server %s in location %s", from, location); |
139 | } |
140 | return null; |
141 | } |
142 | } |