| 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.Singleton; |
| 24 | |
| 25 | import org.jclouds.elasticstack.domain.DriveMetrics; |
| 26 | |
| 27 | import com.google.common.base.Function; |
| 28 | import com.google.common.collect.ImmutableMap; |
| 29 | import com.google.common.collect.ImmutableMap.Builder; |
| 30 | |
| 31 | /** |
| 32 | * |
| 33 | * @author Adrian Cole |
| 34 | */ |
| 35 | @Singleton |
| 36 | public class MapToDriveMetrics implements Function<Map<String, String>, Map<String, ? extends DriveMetrics>> { |
| 37 | |
| 38 | public Map<String, ? extends DriveMetrics> apply(Map<String, String> from) { |
| 39 | Builder<String, DriveMetrics> builder = ImmutableMap.<String, DriveMetrics> builder(); |
| 40 | addIDEDevices(from, builder); |
| 41 | addSCSIDevices(from, builder); |
| 42 | addBlockDevices(from, builder); |
| 43 | return builder.build(); |
| 44 | } |
| 45 | |
| 46 | protected void addBlockDevices(Map<String, String> from, Builder<String, DriveMetrics> devices) { |
| 47 | BLOCK: for (int index : new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }) { |
| 48 | String key = String.format("block:0:%d", index); |
| 49 | if (!from.containsKey(key)) |
| 50 | break BLOCK; |
| 51 | devices.put(key, buildMetrics(key, from)); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | protected void addSCSIDevices(Map<String, String> from, Builder<String, DriveMetrics> devices) { |
| 56 | SCSI: for (int unit : new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }) { |
| 57 | String key = String.format("scsi:0:%d", unit); |
| 58 | if (!from.containsKey(key)) |
| 59 | break SCSI; |
| 60 | devices.put(key, buildMetrics(key, from)); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | protected void addIDEDevices(Map<String, String> from, Builder<String, DriveMetrics> devices) { |
| 65 | IDE: for (int bus : new int[] { 0, 1 }) |
| 66 | for (int unit : new int[] { 0, 1 }) { |
| 67 | String key = String.format("ide:%d:%d", bus, unit); |
| 68 | if (!from.containsKey(key)) |
| 69 | break IDE; |
| 70 | devices.put(key, buildMetrics(key, from)); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | protected DriveMetrics buildMetrics(String key, Map<String, String> from) { |
| 75 | DriveMetrics.Builder builder = new DriveMetrics.Builder(); |
| 76 | if (from.containsKey(key + ":read:bytes")) |
| 77 | builder.readBytes(new Long(from.get(key + ":read:bytes"))); |
| 78 | if (from.containsKey(key + ":read:requests")) |
| 79 | builder.readRequests(new Long(from.get(key + ":read:requests"))); |
| 80 | if (from.containsKey(key + ":write:bytes")) |
| 81 | builder.writeBytes(new Long(from.get(key + ":write:bytes"))); |
| 82 | if (from.containsKey(key + ":write:requests")) |
| 83 | builder.writeRequests(new Long(from.get(key + ":write:requests"))); |
| 84 | return builder.build(); |
| 85 | } |
| 86 | } |