| 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 | import java.util.Map.Entry; |
| 23 | |
| 24 | import javax.annotation.Resource; |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.elasticstack.domain.ClaimType; |
| 28 | import org.jclouds.elasticstack.domain.DriveInfo; |
| 29 | import org.jclouds.elasticstack.domain.DriveMetrics; |
| 30 | import org.jclouds.elasticstack.domain.DriveStatus; |
| 31 | import org.jclouds.logging.Logger; |
| 32 | |
| 33 | import com.google.common.base.Function; |
| 34 | import com.google.common.base.Splitter; |
| 35 | import com.google.common.collect.Maps; |
| 36 | |
| 37 | /** |
| 38 | * |
| 39 | * @author Adrian Cole |
| 40 | */ |
| 41 | @Singleton |
| 42 | public class MapToDriveInfo implements Function<Map<String, String>, DriveInfo> { |
| 43 | |
| 44 | @Resource |
| 45 | protected Logger logger = Logger.NULL; |
| 46 | |
| 47 | @Override |
| 48 | public DriveInfo apply(Map<String, String> from) { |
| 49 | if (from.size() == 0) |
| 50 | return null; |
| 51 | DriveInfo.Builder builder = new DriveInfo.Builder(); |
| 52 | builder.name(from.get("name")); |
| 53 | if (from.containsKey("tags")) |
| 54 | builder.tags(Splitter.on(' ').split(from.get("tags"))); |
| 55 | if (from.containsKey("status")) |
| 56 | builder.status(DriveStatus.fromValue(from.get("status"))); |
| 57 | builder.metrics(buildMetrics(from)); |
| 58 | builder.user(from.get("user")); |
| 59 | builder.encryptionCipher(from.get("encryption:cipher")); |
| 60 | builder.uuid(from.get("drive")); |
| 61 | if (from.containsKey("claim:type")) |
| 62 | builder.claimType(ClaimType.fromValue(from.get("claim:type"))); |
| 63 | if (from.containsKey("claimed")) |
| 64 | builder.claimed(Splitter.on(' ').split(from.get("claimed"))); |
| 65 | if (from.containsKey("readers")) |
| 66 | builder.readers(Splitter.on(' ').split(from.get("readers"))); |
| 67 | if (from.containsKey("size")) |
| 68 | builder.size(new Long(from.get("size"))); |
| 69 | Map<String, String> metadata = Maps.newLinkedHashMap(); |
| 70 | for (Entry<String, String> entry : from.entrySet()) { |
| 71 | if (entry.getKey().startsWith("user:")) |
| 72 | metadata.put(entry.getKey().substring(entry.getKey().indexOf(':') + 1), entry.getValue()); |
| 73 | } |
| 74 | builder.userMetadata(metadata); |
| 75 | try { |
| 76 | return builder.build(); |
| 77 | } catch (NullPointerException e) { |
| 78 | logger.trace("entry missing data: %s; %s", e.getMessage(), from); |
| 79 | return null; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | protected DriveMetrics buildMetrics(Map<String, String> from) { |
| 84 | DriveMetrics.Builder metricsBuilder = new DriveMetrics.Builder(); |
| 85 | if (from.containsKey("read:bytes")) |
| 86 | metricsBuilder.readBytes(new Long(from.get("read:bytes"))); |
| 87 | if (from.containsKey("read:requests")) |
| 88 | metricsBuilder.readRequests(new Long(from.get("read:requests"))); |
| 89 | if (from.containsKey("write:bytes")) |
| 90 | metricsBuilder.writeBytes(new Long(from.get("write:bytes"))); |
| 91 | if (from.containsKey("write:requests")) |
| 92 | metricsBuilder.writeRequests(new Long(from.get("write:requests"))); |
| 93 | return metricsBuilder.build(); |
| 94 | } |
| 95 | } |