/providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/internal/OperatingSystems.java

http://github.com/jclouds/jclouds · Java · 93 lines · 67 code · 10 blank · 16 comment · 25 complexity · b930c9b1cba5b674f5561a780586f801 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.softlayer.compute.functions.internal;
  18. import static com.google.common.collect.Iterables.getLast;
  19. import org.jclouds.compute.domain.OsFamily;
  20. import com.google.common.base.Function;
  21. import com.google.common.base.Splitter;
  22. import com.google.common.collect.Iterables;
  23. import com.google.common.primitives.Ints;
  24. public class OperatingSystems {
  25. protected static final String CENTOS = "CENTOS";
  26. protected static final String DEBIAN = "DEBIAN";
  27. protected static final String RHEL = "REDHAT";
  28. protected static final String UBUNTU = "UBUNTU";
  29. protected static final String WINDOWS = "WIN_";
  30. protected static final String CLOUD_LINUX = "CLOUDLINUX";
  31. protected static final String VYATTACE = "VYATTACE";
  32. public static Function<String, OsFamily> osFamily() {
  33. return new Function<String, OsFamily>() {
  34. @Override
  35. public OsFamily apply(final String description) {
  36. if (description != null) {
  37. if (description.startsWith(CENTOS)) return OsFamily.CENTOS;
  38. else if (description.startsWith(DEBIAN)) return OsFamily.DEBIAN;
  39. else if (description.startsWith(RHEL)) return OsFamily.RHEL;
  40. else if (description.startsWith(UBUNTU)) return OsFamily.UBUNTU;
  41. else if (description.startsWith(WINDOWS)) return OsFamily.WINDOWS;
  42. else if (description.startsWith(CLOUD_LINUX)) return OsFamily.CLOUD_LINUX;
  43. else if (description.startsWith(VYATTACE)) return OsFamily.LINUX;
  44. }
  45. return OsFamily.UNRECOGNIZED;
  46. }
  47. };
  48. }
  49. public static Function<String, Integer> bits() {
  50. return new Function<String, Integer>() {
  51. @Override
  52. public Integer apply(String operatingSystemReferenceCode) {
  53. if (operatingSystemReferenceCode != null) {
  54. return Ints.tryParse(getLast(Splitter.on("_").split(operatingSystemReferenceCode)));
  55. }
  56. return null;
  57. }
  58. };
  59. }
  60. public static Function<String, String> version() {
  61. return new Function<String, String>() {
  62. @Override
  63. public String apply(final String version) {
  64. return parseVersion(version);
  65. }
  66. };
  67. }
  68. private static String parseVersion(String version) {
  69. if (version.contains("-")) {
  70. String rawVersion = version.substring(0, version.lastIndexOf("-"));
  71. if (Iterables.size(Splitter.on(".").split(rawVersion)) == 3) {
  72. return rawVersion.substring(0, rawVersion.lastIndexOf("."));
  73. } else {
  74. return rawVersion;
  75. }
  76. } else if (version.contains(" ")) {
  77. return version.substring(0, version.indexOf(" "));
  78. } else if (version.matches("^(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$")) {
  79. return version;
  80. }
  81. return null;
  82. }
  83. }