Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 77 additions & 49 deletions api/src/main/java/com/cloud/hypervisor/Hypervisor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,45 @@
package com.cloud.hypervisor;

import com.cloud.storage.Storage.ImageFormat;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

public class Hypervisor {
public static class HypervisorType {
private static final Map<String, HypervisorType> hypervisorTypeMap = new LinkedHashMap<>();
public static final HypervisorType None = new HypervisorType("None"); //for storage hosts
public static final HypervisorType XenServer = new HypervisorType("XenServer", ImageFormat.VHD);
public static final HypervisorType KVM = new HypervisorType("KVM", ImageFormat.QCOW2);
public static final HypervisorType VMware = new HypervisorType("VMware", ImageFormat.OVA);
public static final HypervisorType Hyperv = new HypervisorType("Hyperv");
public static final HypervisorType VirtualBox = new HypervisorType("VirtualBox");
public static final HypervisorType Parralels = new HypervisorType("Parralels");
public static final HypervisorType BareMetal = new HypervisorType("BareMetal");
public static final HypervisorType Simulator = new HypervisorType("Simulator");
public static final HypervisorType Ovm = new HypervisorType("Ovm", ImageFormat.RAW);
public static final HypervisorType Ovm3 = new HypervisorType("Ovm3", ImageFormat.RAW);
public static final HypervisorType LXC = new HypervisorType("LXC");
public static final HypervisorType Custom = new HypervisorType("Custom");
public static final HypervisorType Any = new HypervisorType("Any"); /*If you don't care about the hypervisor type*/
private final String name;
private final ImageFormat imageFormat;

static Map<String, HypervisorType> hypervisorTypeMap;
static Map<HypervisorType, ImageFormat> supportedImageFormatMap;

public enum HypervisorType {
None, //for storage hosts
XenServer,
KVM,
VMware,
Hyperv,
VirtualBox,
Parralels,
BareMetal,
Simulator,
Ovm,
Ovm3,
LXC,
Custom,

Any; /*If you don't care about the hypervisor type*/

static {
hypervisorTypeMap = new HashMap<>();
hypervisorTypeMap.put("xenserver", HypervisorType.XenServer);
hypervisorTypeMap.put("kvm", HypervisorType.KVM);
hypervisorTypeMap.put("vmware", HypervisorType.VMware);
hypervisorTypeMap.put("hyperv", HypervisorType.Hyperv);
hypervisorTypeMap.put("virtualbox", HypervisorType.VirtualBox);
hypervisorTypeMap.put("parallels", HypervisorType.Parralels);
hypervisorTypeMap.put("baremetal", HypervisorType.BareMetal);
hypervisorTypeMap.put("simulator", HypervisorType.Simulator);
hypervisorTypeMap.put("ovm", HypervisorType.Ovm);
hypervisorTypeMap.put("lxc", HypervisorType.LXC);
hypervisorTypeMap.put("any", HypervisorType.Any);
hypervisorTypeMap.put("ovm3", HypervisorType.Ovm3);
hypervisorTypeMap.put("custom", HypervisorType.Custom);

supportedImageFormatMap = new HashMap<>();
supportedImageFormatMap.put(HypervisorType.XenServer, ImageFormat.VHD);
supportedImageFormatMap.put(HypervisorType.KVM, ImageFormat.QCOW2);
supportedImageFormatMap.put(HypervisorType.VMware, ImageFormat.OVA);
supportedImageFormatMap.put(HypervisorType.Ovm, ImageFormat.RAW);
supportedImageFormatMap.put(HypervisorType.Ovm3, ImageFormat.RAW);
public HypervisorType(String name) {
this(name, null);
}

public HypervisorType(String name, ImageFormat imageFormat) {
this.name = name;
this.imageFormat = imageFormat;
if (name.equals("Parralels")){ // typo in the original code
hypervisorTypeMap.put("parallels", this);
} else {
hypervisorTypeMap.putIfAbsent(name.toLowerCase(Locale.ROOT), this);
}
}

public static HypervisorType getType(String hypervisor) {
Expand All @@ -75,24 +65,62 @@ public static HypervisorType getType(String hypervisor) {
hypervisorTypeMap.getOrDefault(hypervisor.toLowerCase(Locale.ROOT), HypervisorType.None));
}

public static HypervisorType[] values() {
return hypervisorTypeMap.values().toArray(HypervisorType[]::new).clone();
}

public static HypervisorType valueOf(String name) {
if (StringUtils.isBlank(name)) {
return null;
}

HypervisorType hypervisorType = hypervisorTypeMap.get(name.toLowerCase(Locale.ROOT));
if (hypervisorType == null) {
throw new IllegalArgumentException("HypervisorType '" + name + "' not found");
}
return hypervisorType;
}

/**
* Returns the display name of a hypervisor type in case the custom hypervisor is used,
* using the 'hypervisor.custom.display.name' setting. Otherwise, returns hypervisor name
*/
public String getHypervisorDisplayName() {
return !Hypervisor.HypervisorType.Custom.equals(this) ?
this.toString() :
HypervisorGuru.HypervisorCustomDisplayName.value();
return HypervisorType.Custom.equals(this) ? HypervisorGuru.HypervisorCustomDisplayName.value() : name;
}

/**
* This method really needs to be part of the properties of the hypervisor type itself.
*
* @param hyperType
* @return
*/
public static ImageFormat getSupportedImageFormat(HypervisorType hyperType) {
return supportedImageFormatMap.getOrDefault(hyperType, null);
public ImageFormat getSupportedImageFormat() {
return imageFormat;
}

public String name() {
return name;
}

@Override
public int hashCode() {
return Objects.hash(name);
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (o == null || getClass() != o.getClass()) {
return false;
}
HypervisorType that = (HypervisorType) o;
return Objects.equals(name, that.name);
}

@Override
public String toString() {
return name;
}
}

Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/com/cloud/serializer/GsonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.util.List;

import com.cloud.hypervisor.Hypervisor;
import org.apache.cloudstack.transport.HypervisorTypeAdaptor;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

Expand Down Expand Up @@ -75,6 +77,7 @@ static Gson setDefaultGsonConfig(GsonBuilder builder) {
builder.registerTypeAdapter(new TypeToken<Pair<Long, Long>>() {
}.getType(), new NwGroupsCommandTypeAdaptor());
builder.registerTypeAdapter(Storage.StoragePoolType.class, new StoragePoolTypeAdaptor());
builder.registerTypeAdapter(Hypervisor.HypervisorType.class, new HypervisorTypeAdaptor());
Gson gson = builder.create();
dsAdaptor.initGson(gson);
dtAdaptor.initGson(gson);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.transport;

import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

/**
* {@link HypervisorType} acts as extendable set of singleton objects and should return same result when used "=="
* or {@link Object#equals(Object)}.
* To support that, need to return existing object for a given name instead of creating new.
*/
public class HypervisorTypeAdaptor implements JsonDeserializer<HypervisorType>, JsonSerializer<HypervisorType> {
@Override
public HypervisorType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isString()) {
return HypervisorType.valueOf(json.getAsString());
}
return null;
}

@Override
public JsonElement serialize(HypervisorType src, Type typeOfSrc, JsonSerializationContext context) {
String name = src.name();
if (name == null) {
return new JsonNull();
}
return new JsonPrimitive(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1735,17 +1735,12 @@ public boolean getExecuteInSequence(final HypervisorType hypervisorType) {
return ExecuteInSequence.value();
}

switch (hypervisorType) {
case KVM:
case XenServer:
case Hyperv:
case LXC:
return false;
case VMware:
return StorageManager.shouldExecuteInSequenceOnVmware();
default:
return ExecuteInSequence.value();
if (Set.of(HypervisorType.KVM, HypervisorType.XenServer, HypervisorType.Hyperv, HypervisorType.LXC).contains(hypervisorType)) {
return false;
} else if (hypervisorType.equals(HypervisorType.VMware)) {
return StorageManager.shouldExecuteInSequenceOnVmware();
}
return ExecuteInSequence.value();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import org.apache.cloudstack.api.Identity;
import org.apache.cloudstack.engine.datacenter.entity.api.DataCenterResourceEntity.State;
import org.apache.cloudstack.engine.datacenter.entity.api.DataCenterResourceEntity.State.Event;
import org.apache.cloudstack.util.HypervisorTypeConverter;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
Expand Down Expand Up @@ -62,6 +64,7 @@ public class EngineClusterVO implements EngineCluster, Identity {
long podId;

@Column(name = "hypervisor_type")
@Convert(converter = HypervisorTypeConverter.class)
String hypervisorType;

@Column(name = "cluster_type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.db.GenericDao;
import com.cloud.utils.db.StateMachine;
import org.apache.cloudstack.util.HypervisorTypeConverter;

@Entity
@Table(name = "host")
Expand Down Expand Up @@ -118,7 +119,7 @@ public class EngineHostVO implements EngineHost, Identity {
private String storageMacAddressDeux;

@Column(name = "hypervisor_type", updatable = true, nullable = false)
@Enumerated(value = EnumType.STRING)
@Convert(converter = HypervisorTypeConverter.class)
private HypervisorType hypervisorType;

@Column(name = "proxy_port")
Expand Down
3 changes: 3 additions & 0 deletions engine/schema/src/main/java/com/cloud/dc/ClusterVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import com.cloud.org.Managed.ManagedState;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.db.GenericDao;
import org.apache.cloudstack.util.HypervisorTypeConverter;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
Expand Down Expand Up @@ -56,6 +58,7 @@ public class ClusterVO implements Cluster {
long podId;

@Column(name = "hypervisor_type")
@Convert(converter = HypervisorTypeConverter.class)
String hypervisorType;

@Column(name = "cluster_type")
Expand Down
3 changes: 2 additions & 1 deletion engine/schema/src/main/java/com/cloud/host/HostVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.apache.cloudstack.util.HypervisorTypeConverter;
import org.apache.cloudstack.utils.jsinterpreter.TagAsRuleHelper;
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
import org.apache.commons.lang.BooleanUtils;
Expand Down Expand Up @@ -125,7 +126,7 @@ public class HostVO implements Host {
private String storageMacAddressDeux;

@Column(name = "hypervisor_type", updatable = true, nullable = false)
@Enumerated(value = EnumType.STRING)
@Convert(converter = HypervisorTypeConverter.class)
private HypervisorType hypervisorType;

@Column(name = "proxy_port")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.utils.NumbersUtil;
import org.apache.cloudstack.util.HypervisorTypeConverter;

@Entity
@Table(name = "hypervisor_capabilities")
Expand All @@ -39,7 +39,7 @@ public class HypervisorCapabilitiesVO implements HypervisorCapabilities {
private long id;

@Column(name = "hypervisor_type")
@Enumerated(value = EnumType.STRING)
@Convert(converter = HypervisorTypeConverter.class)
private HypervisorType hypervisorType;

@Column(name = "hypervisor_version")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
Expand All @@ -28,6 +29,7 @@

import com.cloud.hypervisor.Hypervisor;
import com.cloud.utils.db.GenericDao;
import org.apache.cloudstack.util.HypervisorTypeConverter;

@Entity
@Table(name = "guest_os_hypervisor")
Expand All @@ -44,6 +46,7 @@ public class GuestOSHypervisorVO implements GuestOSHypervisor {
String guestOsName;

@Column(name = "hypervisor_type")
@Convert(converter = HypervisorTypeConverter.class)
String hypervisorType;

@Column(name = "hypervisor_version")
Expand Down
Loading