View Javadoc

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