| 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 com.google.common.base.Function; |
| 22 | import org.jclouds.compute.domain.Image; |
| 23 | import org.jclouds.compute.domain.ImageBuilder; |
| 24 | import org.jclouds.compute.domain.OperatingSystem; |
| 25 | import org.jclouds.compute.domain.OsFamily; |
| 26 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 27 | import org.jclouds.logging.Logger; |
| 28 | import org.jclouds.softlayer.domain.ProductItem; |
| 29 | import org.jclouds.softlayer.domain.ProductItemPrice; |
| 30 | |
| 31 | import javax.annotation.Resource; |
| 32 | import javax.inject.Named; |
| 33 | import javax.inject.Singleton; |
| 34 | import java.util.NoSuchElementException; |
| 35 | import java.util.regex.Matcher; |
| 36 | import java.util.regex.Pattern; |
| 37 | |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | |
| 40 | /** |
| 41 | * @author Jason King |
| 42 | */ |
| 43 | @Singleton |
| 44 | public class ProductItemToImage implements Function<ProductItem, Image> { |
| 45 | |
| 46 | /** Pattern to capture the number of bits e.g. "a (32 bit) os" */ |
| 47 | private static final Pattern OS_BITS_PATTERN = Pattern.compile(".*\\((\\d+) ?bit\\).*"); |
| 48 | |
| 49 | private static final String CENTOS = "CentOS"; |
| 50 | private static final String DEBIAN = "Debian GNU/Linux"; |
| 51 | private static final String FEDORA = "Fedora Release"; |
| 52 | private static final String RHEL = "Red Hat Enterprise Linux"; |
| 53 | private static final String UBUNTU = "Ubuntu Linux"; |
| 54 | private static final String WINDOWS = "Windows Server"; |
| 55 | |
| 56 | @Resource |
| 57 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 58 | protected Logger logger = Logger.NULL; |
| 59 | |
| 60 | @Override |
| 61 | public Image apply(ProductItem productItem) { |
| 62 | checkNotNull(productItem,"productItem"); |
| 63 | |
| 64 | OsFamily family = osFamily().apply(productItem); |
| 65 | Integer bits = osBits().apply(productItem); |
| 66 | OperatingSystem os = OperatingSystem.builder() |
| 67 | .description(productItem.getDescription()) |
| 68 | .family(family) |
| 69 | .version(osVersion().apply(productItem)) |
| 70 | .is64Bit(bits.equals(64)) |
| 71 | .build(); |
| 72 | |
| 73 | return new ImageBuilder() |
| 74 | .ids(imageId().apply(productItem)) |
| 75 | .description(productItem.getDescription()) |
| 76 | .operatingSystem(os) |
| 77 | .build(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Parses the item description to determine the OSFamily |
| 82 | * @return the @see OsFamily or OsFamily.UNRECOGNIZED |
| 83 | */ |
| 84 | public static Function<ProductItem,OsFamily> osFamily() { |
| 85 | return new Function<ProductItem,OsFamily>() { |
| 86 | @Override |
| 87 | public OsFamily apply(ProductItem productItem) { |
| 88 | checkNotNull(productItem,"productItem"); |
| 89 | |
| 90 | final String description = productItem.getDescription(); |
| 91 | if(description.startsWith(CENTOS)) return OsFamily.CENTOS; |
| 92 | else if(description.startsWith(DEBIAN)) return OsFamily.DEBIAN; |
| 93 | else if(description.startsWith(FEDORA)) return OsFamily.FEDORA; |
| 94 | else if(description.startsWith(RHEL)) return OsFamily.RHEL; |
| 95 | else if(description.startsWith(UBUNTU)) return OsFamily.UBUNTU; |
| 96 | else if(description.startsWith(WINDOWS)) return OsFamily.WINDOWS; |
| 97 | return OsFamily.UNRECOGNIZED; |
| 98 | } |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Parses the item description to determine the os version |
| 104 | * @return the version |
| 105 | * @throws java.util.NoSuchElementException if the version cannot be determined |
| 106 | */ |
| 107 | public static Function<ProductItem,String> osVersion() { |
| 108 | return new Function<ProductItem,String>() { |
| 109 | @Override |
| 110 | public String apply(ProductItem productItem) { |
| 111 | checkNotNull(productItem,"productItem"); |
| 112 | |
| 113 | final String description = productItem.getDescription(); |
| 114 | OsFamily family = osFamily().apply(productItem); |
| 115 | if (family.equals(OsFamily.CENTOS)) return parseVersion(description, CENTOS); |
| 116 | else if(family.equals(OsFamily.DEBIAN)) return parseVersion(description, DEBIAN); |
| 117 | else if(family.equals(OsFamily.FEDORA)) return parseVersion(description, FEDORA); |
| 118 | else if(family.equals(OsFamily.RHEL)) return parseVersion(description, RHEL); |
| 119 | else if(family.equals(OsFamily.UBUNTU)) return parseVersion(description, UBUNTU); |
| 120 | else if(family.equals(OsFamily.WINDOWS)) return parseVersion(description, WINDOWS); |
| 121 | else throw new NoSuchElementException("No os version for item:"+productItem); |
| 122 | } |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | private static String parseVersion(String description, String os) { |
| 127 | String noOsName = description.replaceFirst(os,"").trim(); |
| 128 | return noOsName.split(" ")[0]; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Parses the item description to determine the number of OS bits |
| 133 | * Expects the number to be in parenthesis and to contain the word "bit". |
| 134 | * The following return 64: "A (64 bit) OS", "A (64bit) OS" |
| 135 | * @return the number of bits |
| 136 | * @throws java.util.NoSuchElementException if the number of bits cannot be determined |
| 137 | */ |
| 138 | public static Function<ProductItem,Integer> osBits() { |
| 139 | return new Function<ProductItem,Integer>() { |
| 140 | @Override |
| 141 | public Integer apply(ProductItem productItem) { |
| 142 | checkNotNull(productItem,"productItem"); |
| 143 | |
| 144 | Matcher m = OS_BITS_PATTERN.matcher(productItem.getDescription()); |
| 145 | if (m.matches()) { |
| 146 | return Integer.parseInt(m.group(1)); |
| 147 | } else { |
| 148 | throw new NoSuchElementException("Cannot determine os-bits for item:"+productItem); |
| 149 | } |
| 150 | } |
| 151 | }; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Generates an id for an Image. |
| 156 | * @return the generated id |
| 157 | */ |
| 158 | public static Function<ProductItem,String> imageId() { |
| 159 | return new Function<ProductItem,String>() { |
| 160 | @Override |
| 161 | public String apply(ProductItem productItem) { |
| 162 | checkNotNull(productItem,"productItem"); |
| 163 | ProductItemPrice price = ProductItems.price().apply(productItem); |
| 164 | return ""+price.getId(); |
| 165 | } |
| 166 | }; |
| 167 | } |
| 168 | |
| 169 | } |