| 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.cloudsigma.functions; |
| 20 | |
| 21 | import java.util.Date; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Map.Entry; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.cloudsigma.domain.Device; |
| 30 | import org.jclouds.cloudsigma.domain.NIC; |
| 31 | import org.jclouds.cloudsigma.domain.ServerInfo; |
| 32 | import org.jclouds.cloudsigma.domain.ServerMetrics; |
| 33 | import org.jclouds.cloudsigma.domain.ServerStatus; |
| 34 | import org.jclouds.cloudsigma.domain.VNC; |
| 35 | |
| 36 | import com.google.common.base.Function; |
| 37 | import com.google.common.base.Splitter; |
| 38 | import com.google.common.collect.Maps; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @author Adrian Cole |
| 43 | */ |
| 44 | @Singleton |
| 45 | public class MapToServerInfo implements Function<Map<String, String>, ServerInfo> { |
| 46 | private final Function<Map<String, String>, Map<String, ? extends Device>> mapToDevices; |
| 47 | private final Function<Map<String, String>, ServerMetrics> mapToMetrics; |
| 48 | private final Function<Map<String, String>, List<NIC>> mapToNICs; |
| 49 | |
| 50 | @Inject |
| 51 | public MapToServerInfo(Function<Map<String, String>, Map<String, ? extends Device>> mapToDevices, |
| 52 | Function<Map<String, String>, ServerMetrics> mapToMetrics, Function<Map<String, String>, List<NIC>> mapToNICs) { |
| 53 | this.mapToDevices = mapToDevices; |
| 54 | this.mapToMetrics = mapToMetrics; |
| 55 | this.mapToNICs = mapToNICs; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public ServerInfo apply(Map<String, String> from) { |
| 60 | if (from.size() == 0) |
| 61 | return null; |
| 62 | ServerInfo.Builder builder = new ServerInfo.Builder(); |
| 63 | builder.name(from.get("name")); |
| 64 | builder.description(from.get("description")); |
| 65 | builder.persistent(Boolean.parseBoolean(from.get("persistent"))); |
| 66 | if (from.containsKey("use")) |
| 67 | builder.use(Splitter.on(' ').split(from.get("use"))); |
| 68 | if (from.containsKey("status")) |
| 69 | builder.status(ServerStatus.fromValue(from.get("status"))); |
| 70 | if (from.containsKey("smp") && !"auto".equals(from.get("smp"))) |
| 71 | builder.smp(new Integer(from.get("smp"))); |
| 72 | builder.cpu(Integer.parseInt(from.get("cpu"))); |
| 73 | builder.mem(Integer.parseInt(from.get("mem"))); |
| 74 | builder.user(from.get("user")); |
| 75 | if (from.containsKey("started")) |
| 76 | builder.started(new Date(new Long(from.get("started")))); |
| 77 | builder.uuid(from.get("server")); |
| 78 | builder.vnc(new VNC(from.get("vnc:ip"), from.get("vnc:password"), from.containsKey("vnc:tls") |
| 79 | && Boolean.valueOf(from.get("vnc:tls")))); |
| 80 | if (from.containsKey("boot")) |
| 81 | builder.bootDeviceIds(Splitter.on(' ').split(from.get("boot"))); |
| 82 | |
| 83 | Map<String, String> metadata = Maps.newLinkedHashMap(); |
| 84 | for (Entry<String, String> entry : from.entrySet()) { |
| 85 | if (entry.getKey().startsWith("user:")) |
| 86 | metadata.put(entry.getKey().substring(entry.getKey().indexOf(':') + 1), entry.getValue()); |
| 87 | } |
| 88 | builder.nics(mapToNICs.apply(from)); |
| 89 | builder.devices(mapToDevices.apply(from)); |
| 90 | builder.metrics(mapToMetrics.apply(from)); |
| 91 | return builder.build(); |
| 92 | } |
| 93 | } |