| 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.elasticstack.functions; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | import javax.inject.Singleton; |
| 25 | |
| 26 | import org.jclouds.elasticstack.domain.BlockDevice; |
| 27 | import org.jclouds.elasticstack.domain.Device; |
| 28 | import org.jclouds.elasticstack.domain.IDEDevice; |
| 29 | import org.jclouds.elasticstack.domain.MediaType; |
| 30 | import org.jclouds.elasticstack.domain.SCSIDevice; |
| 31 | |
| 32 | import com.google.common.base.Function; |
| 33 | import com.google.common.collect.ImmutableSet; |
| 34 | import com.google.common.collect.ImmutableSet.Builder; |
| 35 | import com.google.common.collect.Maps; |
| 36 | |
| 37 | /** |
| 38 | * |
| 39 | * @author Adrian Cole |
| 40 | */ |
| 41 | @Singleton |
| 42 | public class MapToDevices implements Function<Map<String, String>, Map<String, ? extends Device>> { |
| 43 | @Singleton |
| 44 | public static class DeviceToId implements Function<Device, String> { |
| 45 | @Override |
| 46 | public String apply(Device input) { |
| 47 | return input.getId(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | private final Function<Device, String> deviceToId; |
| 52 | |
| 53 | @Inject |
| 54 | public MapToDevices(Function<Device, String> deviceToId) { |
| 55 | this.deviceToId = deviceToId; |
| 56 | } |
| 57 | |
| 58 | public Map<String, ? extends Device> apply(Map<String, String> from) { |
| 59 | Builder<Device> devices = ImmutableSet.builder(); |
| 60 | addIDEDevices(from, devices); |
| 61 | addSCSIDevices(from, devices); |
| 62 | addBlockDevices(from, devices); |
| 63 | |
| 64 | return Maps.uniqueIndex(devices.build(), deviceToId); |
| 65 | } |
| 66 | |
| 67 | protected void addBlockDevices(Map<String, String> from, Builder<Device> devices) { |
| 68 | BLOCK: for (int index : new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }) { |
| 69 | String key = String.format("block:0:%d", index); |
| 70 | if (!from.containsKey(key)) |
| 71 | break BLOCK; |
| 72 | devices.add(populateBuilder(new BlockDevice.Builder(index), key, from).build()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | protected void addSCSIDevices(Map<String, String> from, Builder<Device> devices) { |
| 77 | SCSI: for (int unit : new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }) { |
| 78 | String key = String.format("scsi:0:%d", unit); |
| 79 | if (!from.containsKey(key)) |
| 80 | break SCSI; |
| 81 | devices.add(populateBuilder(new SCSIDevice.Builder(unit), key, from).build()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | protected void addIDEDevices(Map<String, String> from, Builder<Device> devices) { |
| 86 | IDE: for (int bus : new int[] { 0, 1 }) |
| 87 | for (int unit : new int[] { 0, 1 }) { |
| 88 | String key = String.format("ide:%d:%d", bus, unit); |
| 89 | if (!from.containsKey(key)) |
| 90 | break IDE; |
| 91 | devices.add(populateBuilder(new IDEDevice.Builder(bus, unit), key, from).build()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | protected Device.Builder populateBuilder(Device.Builder deviceBuilder, String key, Map<String, String> from) { |
| 96 | deviceBuilder.uuid(from.get(key)); |
| 97 | if (from.containsKey(key + ":media")) |
| 98 | deviceBuilder.mediaType(MediaType.fromValue(from.get(key + ":media"))); |
| 99 | return deviceBuilder; |
| 100 | } |
| 101 | } |