| 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.cim.functions; |
| 20 | |
| 21 | import static com.google.common.collect.Iterables.filter; |
| 22 | import static com.google.common.collect.Iterables.find; |
| 23 | import static com.google.common.collect.Iterables.transform; |
| 24 | |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.cim.CIMPredicates; |
| 28 | import org.jclouds.cim.ResourceAllocationSettingData; |
| 29 | import org.jclouds.cim.ResourceAllocationSettingData.ResourceType; |
| 30 | import org.jclouds.compute.domain.HardwareBuilder; |
| 31 | import org.jclouds.compute.domain.Processor; |
| 32 | import org.jclouds.compute.domain.Volume; |
| 33 | import org.jclouds.compute.domain.internal.VolumeImpl; |
| 34 | |
| 35 | import com.google.common.base.Function; |
| 36 | |
| 37 | /** |
| 38 | * @author Adrian Cole |
| 39 | */ |
| 40 | @Singleton |
| 41 | public class HardwareBuilderFromResourceAllocations implements |
| 42 | Function<Iterable<? extends ResourceAllocationSettingData>, HardwareBuilder> { |
| 43 | @Override |
| 44 | public HardwareBuilder apply(Iterable<? extends ResourceAllocationSettingData> from) { |
| 45 | HardwareBuilder builder = new HardwareBuilder(); |
| 46 | builder.volumes(transform(filter(from, CIMPredicates.resourceTypeIn(ResourceType.DISK_DRIVE, |
| 47 | ResourceType.BASE_PARTITIONABLE_UNIT, ResourceType.PARTITIONABLE_UNIT)), |
| 48 | new Function<ResourceAllocationSettingData, Volume>() { |
| 49 | |
| 50 | @Override |
| 51 | public Volume apply(ResourceAllocationSettingData from) { |
| 52 | return HardwareBuilderFromResourceAllocations.this.apply(from); |
| 53 | } |
| 54 | |
| 55 | })); |
| 56 | |
| 57 | builder.ram((int) find(from, CIMPredicates.resourceTypeIn(ResourceType.MEMORY)).getVirtualQuantity().longValue()); |
| 58 | |
| 59 | builder.processors(transform(filter(from, CIMPredicates.resourceTypeIn(ResourceType.PROCESSOR)), |
| 60 | new Function<ResourceAllocationSettingData, Processor>() { |
| 61 | |
| 62 | @Override |
| 63 | public Processor apply(ResourceAllocationSettingData arg0) { |
| 64 | return new Processor(arg0.getVirtualQuantity(), 1); |
| 65 | } |
| 66 | })); |
| 67 | return builder; |
| 68 | } |
| 69 | |
| 70 | public Volume apply(ResourceAllocationSettingData from) { |
| 71 | return new VolumeImpl(from.getAddressOnParent() + "", Volume.Type.LOCAL, from.getVirtualQuantity() == null ? null |
| 72 | : from.getVirtualQuantity() / 1024 / 1024f, null, "0".equals(from.getAddressOnParent()) |
| 73 | || ResourceType.BASE_PARTITIONABLE_UNIT.equals(from.getResourceType()), true); |
| 74 | } |
| 75 | } |