| 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.compute.domain; |
| 20 | |
| 21 | import org.jclouds.javax.annotation.Nullable; |
| 22 | |
| 23 | import org.jclouds.cim.OSType; |
| 24 | import org.jclouds.ovf.Envelope; |
| 25 | |
| 26 | import com.google.common.annotations.Beta; |
| 27 | |
| 28 | /** |
| 29 | * Operating system based on DMTF CIM model. |
| 30 | * |
| 31 | * @author Adrian Cole |
| 32 | * @see <a href="http://dmtf.org/standards/cim/cim_schema_v2260">DMTF CIM model</a> |
| 33 | */ |
| 34 | @Beta |
| 35 | public class CIMOperatingSystem extends OperatingSystem { |
| 36 | public static Builder builder() { |
| 37 | return new Builder(); |
| 38 | } |
| 39 | |
| 40 | public static class Builder extends OperatingSystem.Builder { |
| 41 | private OSType osType; |
| 42 | |
| 43 | /** |
| 44 | * @see CIMOperatingSystem#getOsType |
| 45 | */ |
| 46 | public Builder osType(@Nullable OSType osType) { |
| 47 | this.osType = osType; |
| 48 | return this; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * {@inheritDoc} |
| 53 | */ |
| 54 | @Override |
| 55 | public CIMOperatingSystem build() { |
| 56 | return new CIMOperatingSystem(family, name, version, arch, description, is64Bit, osType); |
| 57 | } |
| 58 | |
| 59 | public Builder fromCIMOperatingSystem(CIMOperatingSystem in) { |
| 60 | return fromOperatingSystem(in).osType(in.getOsType()); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritDoc} |
| 65 | */ |
| 66 | |
| 67 | @Override |
| 68 | public Builder arch(String arch) { |
| 69 | return Builder.class.cast(super.arch(arch)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * {@inheritDoc} |
| 74 | */ |
| 75 | @Override |
| 76 | public Builder description(String description) { |
| 77 | return Builder.class.cast(super.description(description)); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * {@inheritDoc} |
| 82 | */ |
| 83 | @Override |
| 84 | public Builder family(OsFamily family) { |
| 85 | return Builder.class.cast(super.family(family)); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * {@inheritDoc} |
| 90 | */ |
| 91 | @Override |
| 92 | public Builder fromOperatingSystem(OperatingSystem in) { |
| 93 | return Builder.class.cast(super.fromOperatingSystem(in)); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * {@inheritDoc} |
| 98 | */ |
| 99 | @Override |
| 100 | public Builder is64Bit(boolean is64Bit) { |
| 101 | return Builder.class.cast(super.is64Bit(is64Bit)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * {@inheritDoc} |
| 106 | */ |
| 107 | @Override |
| 108 | public Builder name(String name) { |
| 109 | return Builder.class.cast(super.name(name)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * {@inheritDoc} |
| 114 | */ |
| 115 | @Override |
| 116 | public Builder version(String version) { |
| 117 | return Builder.class.cast(super.version(version)); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public static CIMOperatingSystem toComputeOs(org.jclouds.ovf.OperatingSystemSection os) { |
| 122 | return new CIMOperatingSystem(OSType.fromValue(os.getId()), "", null, os.getDescription()); |
| 123 | } |
| 124 | |
| 125 | public static CIMOperatingSystem toComputeOs(Envelope ovf) { |
| 126 | return toComputeOs(ovf.getVirtualSystem().getOperatingSystemSection()); |
| 127 | } |
| 128 | |
| 129 | private OSType osType; |
| 130 | |
| 131 | protected CIMOperatingSystem() { |
| 132 | super(); |
| 133 | } |
| 134 | |
| 135 | public CIMOperatingSystem(OSType osType, String version, String arch, String description) { |
| 136 | this(osType.getFamily(), osType.getValue(), version, arch, description, osType.is64Bit(), osType); |
| 137 | } |
| 138 | |
| 139 | public CIMOperatingSystem(@Nullable OsFamily family, @Nullable String name, @Nullable String version, |
| 140 | @Nullable String arch, String description, boolean is64Bit, OSType osType) { |
| 141 | super(family, name, version, arch, description, is64Bit); |
| 142 | this.osType = osType; |
| 143 | |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * CIM OSType of the image |
| 148 | */ |
| 149 | public OSType getOsType() { |
| 150 | return osType; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public int hashCode() { |
| 155 | final int prime = 31; |
| 156 | int result = super.hashCode(); |
| 157 | result = prime * result + ((osType == null) ? 0 : osType.hashCode()); |
| 158 | return result; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public boolean equals(Object obj) { |
| 163 | if (this == obj) |
| 164 | return true; |
| 165 | if (!super.equals(obj)) |
| 166 | return false; |
| 167 | if (getClass() != obj.getClass()) |
| 168 | return false; |
| 169 | CIMOperatingSystem other = (CIMOperatingSystem) obj; |
| 170 | if (osType == null) { |
| 171 | if (other.osType != null) |
| 172 | return false; |
| 173 | } else if (!osType.equals(other.osType)) |
| 174 | return false; |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public String toString() { |
| 180 | return String.format("[osType=%s, arch=%s, description=%s, family=%s, is64Bit=%s, name=%s, version=%s]", osType, |
| 181 | arch, description, family, is64Bit, name, version); |
| 182 | } |
| 183 | } |