| 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.softlayer.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.collect.Iterables.filter; |
| 23 | import static com.google.common.collect.Iterables.get; |
| 24 | import static com.google.common.collect.Iterables.getOnlyElement; |
| 25 | import static org.jclouds.softlayer.predicates.ProductItemPredicates.categoryCode; |
| 26 | import static org.jclouds.softlayer.predicates.ProductItemPredicates.categoryCodeMatches; |
| 27 | import static org.jclouds.softlayer.predicates.ProductItemPredicates.matches; |
| 28 | |
| 29 | import java.util.List; |
| 30 | import java.util.regex.Matcher; |
| 31 | import java.util.regex.Pattern; |
| 32 | |
| 33 | import javax.inject.Inject; |
| 34 | import javax.inject.Singleton; |
| 35 | |
| 36 | import org.jclouds.compute.domain.Hardware; |
| 37 | import org.jclouds.compute.domain.HardwareBuilder; |
| 38 | import org.jclouds.compute.domain.Processor; |
| 39 | import org.jclouds.compute.domain.Volume; |
| 40 | import org.jclouds.compute.domain.internal.VolumeImpl; |
| 41 | import org.jclouds.softlayer.domain.ProductItem; |
| 42 | import org.jclouds.softlayer.domain.ProductItemPrice; |
| 43 | |
| 44 | import com.google.common.base.Function; |
| 45 | import com.google.common.collect.ImmutableList; |
| 46 | import com.google.common.collect.Iterables; |
| 47 | |
| 48 | /** |
| 49 | * Converts a set of ProductItems to Hardware. All cores have a speed of 2.0Ghz The Hardware Id will |
| 50 | * be a comma separated list containing the price ids: cpus,ram,volume |
| 51 | * |
| 52 | * @author Jason King |
| 53 | */ |
| 54 | @Singleton |
| 55 | public class ProductItemsToHardware implements Function<Iterable<ProductItem>, Hardware> { |
| 56 | |
| 57 | private static final String GUEST_DISK_CATEGORY_REGEX = "guest_disk[0-9]"; |
| 58 | private static final String FIRST_GUEST_DISK = "guest_disk0"; |
| 59 | private static final String STORAGE_AREA_NETWORK = "SAN"; |
| 60 | |
| 61 | private static final String RAM_CATEGORY = "ram"; |
| 62 | |
| 63 | private static final String CPU_DESCRIPTION_REGEX = "(Private )?[0-9]+ x ([.0-9]+) GHz Core[s]?"; |
| 64 | private static final double DEFAULT_CORE_SPEED = 2.0; |
| 65 | |
| 66 | private final Pattern cpuDescriptionRegex; |
| 67 | private final Pattern diskCategoryRegex; |
| 68 | |
| 69 | @Inject |
| 70 | public ProductItemsToHardware() { |
| 71 | this(Pattern.compile(CPU_DESCRIPTION_REGEX), Pattern.compile(GUEST_DISK_CATEGORY_REGEX)); |
| 72 | } |
| 73 | |
| 74 | public ProductItemsToHardware(Pattern cpuDescriptionRegex, Pattern diskCategoryRegex) { |
| 75 | this.cpuDescriptionRegex = checkNotNull(cpuDescriptionRegex, "cpuDescriptionRegex"); |
| 76 | this.diskCategoryRegex = checkNotNull(diskCategoryRegex, "diskCategoryRegex"); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Hardware apply(Iterable<ProductItem> items) { |
| 81 | |
| 82 | ProductItem coresItem = getOnlyElement(filter(items, matches(cpuDescriptionRegex))); |
| 83 | ProductItem ramItem = getOnlyElement(filter(items, categoryCode(RAM_CATEGORY))); |
| 84 | ProductItem volumeItem = get(filter(items, categoryCode(FIRST_GUEST_DISK)), 0); |
| 85 | |
| 86 | String hardwareId = hardwareId().apply(ImmutableList.of(coresItem, ramItem, volumeItem)); |
| 87 | double cores = ProductItems.capacity().apply(coresItem).doubleValue(); |
| 88 | Matcher cpuMatcher = cpuDescriptionRegex.matcher(coresItem.getDescription()); |
| 89 | double coreSpeed = (cpuMatcher.matches()) ? Double.parseDouble(cpuMatcher.group(cpuMatcher.groupCount())) : DEFAULT_CORE_SPEED; |
| 90 | int ram = ProductItems.capacity().apply(ramItem).intValue(); |
| 91 | |
| 92 | return new HardwareBuilder().ids(hardwareId).processors(ImmutableList.of(new Processor(cores, coreSpeed))).ram( |
| 93 | ram).volumes( |
| 94 | Iterables.transform(filter(items, categoryCodeMatches(diskCategoryRegex)), |
| 95 | new Function<ProductItem, Volume>() { |
| 96 | @Override |
| 97 | public Volume apply(ProductItem item) { |
| 98 | float volumeSize = ProductItems.capacity().apply(item); |
| 99 | return new VolumeImpl( |
| 100 | item.getId() + "", |
| 101 | item.getDescription().indexOf(STORAGE_AREA_NETWORK) != -1 ? Volume.Type.SAN : Volume.Type.LOCAL, |
| 102 | volumeSize, null, categoryCode(FIRST_GUEST_DISK).apply(item), false); |
| 103 | } |
| 104 | })).build(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Generates a hardwareId based on the priceId's of the items in the list |
| 109 | * |
| 110 | * @return comma separated list of priceid's |
| 111 | */ |
| 112 | public static Function<List<ProductItem>, String> hardwareId() { |
| 113 | return new Function<List<ProductItem>, String>() { |
| 114 | @Override |
| 115 | public String apply(List<ProductItem> productItems) { |
| 116 | StringBuilder builder = new StringBuilder(); |
| 117 | for (ProductItem item : productItems) { |
| 118 | ProductItemPrice price = ProductItems.price().apply(item); |
| 119 | builder.append(price.getId()).append(","); |
| 120 | } |
| 121 | return builder.toString().substring(0, builder.lastIndexOf(",")); |
| 122 | } |
| 123 | }; |
| 124 | } |
| 125 | } |