From fa2dd8e93ae1a88ff94bcb28c53b56dfc240792a Mon Sep 17 00:00:00 2001 From: Kelven Yang Date: Thu, 27 Oct 2011 17:00:01 -0700 Subject: [PATCH] bug 6745: Using UUID instead of integers - for VM instances --- api/src/com/cloud/api/Identity.java | 22 + api/src/com/cloud/api/IdentityMapper.java | 30 + .../com/cloud/api/commands/DestroyVMCmd.java | 2 + .../cloud/api/commands/GetVMPasswordCmd.java | 2 + .../com/cloud/api/commands/ListVMsCmd.java | 2 + .../com/cloud/api/commands/MigrateVMCmd.java | 2 + .../com/cloud/api/commands/RebootVMCmd.java | 2 + .../com/cloud/api/commands/RecoverVMCmd.java | 2 + .../api/commands/ResetVMPasswordCmd.java | 2 + .../com/cloud/api/commands/RestoreVMCmd.java | 2 + .../com/cloud/api/commands/StartVMCmd.java | 2 + api/src/com/cloud/api/commands/StopVMCmd.java | 2 + .../com/cloud/api/commands/UpdateVMCmd.java | 2 + .../cloud/api/commands/UpdateVMGroupCmd.java | 180 +-- .../com/cloud/api/commands/UpgradeVMCmd.java | 2 + .../cloud/api/response/UserVmResponse.java | 6 +- api/src/com/cloud/vm/VirtualMachine.java | 3 +- core/src/com/cloud/vm/VMInstanceVO.java | 14 + .../com/cloud/Identity/dao/IdentityDao.java | 26 + .../cloud/Identity/dao/IdentityDaoImpl.java | 66 + .../com/cloud/Identity/dao/IdentityVO.java | 27 + server/src/com/cloud/api/ApiDispatcher.java | 15 +- .../src/com/cloud/api/ApiResponseHelper.java | 8 +- .../DefaultComponentLibrary.java | 2 + server/src/com/cloud/vm/dao/UserVmDao.java | 2 - .../src/com/cloud/vm/dao/UserVmDaoImpl.java | 1164 +++++++++-------- server/src/com/cloud/vm/dao/UserVmData.java | 9 + setup/db/create-schema.sql | 4 +- setup/db/db/schema-2213to30.sql | 6 +- 29 files changed, 935 insertions(+), 673 deletions(-) create mode 100644 api/src/com/cloud/api/Identity.java create mode 100644 api/src/com/cloud/api/IdentityMapper.java create mode 100644 server/src/com/cloud/Identity/dao/IdentityDao.java create mode 100644 server/src/com/cloud/Identity/dao/IdentityDaoImpl.java create mode 100644 server/src/com/cloud/Identity/dao/IdentityVO.java diff --git a/api/src/com/cloud/api/Identity.java b/api/src/com/cloud/api/Identity.java new file mode 100644 index 00000000000..3545fd6d1a4 --- /dev/null +++ b/api/src/com/cloud/api/Identity.java @@ -0,0 +1,22 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.api; + +public interface Identity { + String getUuid(); +} diff --git a/api/src/com/cloud/api/IdentityMapper.java b/api/src/com/cloud/api/IdentityMapper.java new file mode 100644 index 00000000000..f2b03215fb1 --- /dev/null +++ b/api/src/com/cloud/api/IdentityMapper.java @@ -0,0 +1,30 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.api; + +import static java.lang.annotation.ElementType.FIELD; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({FIELD}) +public @interface IdentityMapper { + String entityTableName(); +} diff --git a/api/src/com/cloud/api/commands/DestroyVMCmd.java b/api/src/com/cloud/api/commands/DestroyVMCmd.java index 864ae0b0353..354f9facc12 100644 --- a/api/src/com/cloud/api/commands/DestroyVMCmd.java +++ b/api/src/com/cloud/api/commands/DestroyVMCmd.java @@ -22,6 +22,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseAsyncCmd; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; @@ -45,6 +46,7 @@ public class DestroyVMCmd extends BaseAsyncCmd { //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="The ID of the virtual machine") private Long id; diff --git a/api/src/com/cloud/api/commands/GetVMPasswordCmd.java b/api/src/com/cloud/api/commands/GetVMPasswordCmd.java index b80f303ffed..64e550930d7 100644 --- a/api/src/com/cloud/api/commands/GetVMPasswordCmd.java +++ b/api/src/com/cloud/api/commands/GetVMPasswordCmd.java @@ -24,6 +24,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.response.GetVMPasswordResponse; @@ -40,6 +41,7 @@ public class GetVMPasswordCmd extends BaseCmd { //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="The ID of the virtual machine") private Long id; diff --git a/api/src/com/cloud/api/commands/ListVMsCmd.java b/api/src/com/cloud/api/commands/ListVMsCmd.java index bd332e41434..dcebdbf9d32 100755 --- a/api/src/com/cloud/api/commands/ListVMsCmd.java +++ b/api/src/com/cloud/api/commands/ListVMsCmd.java @@ -23,6 +23,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseListCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.response.ListResponse; @@ -55,6 +56,7 @@ public class ListVMsCmd extends BaseListCmd { @Parameter(name=ApiConstants.HOST_ID, type=CommandType.LONG, description="the host ID") private Long hostId; + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.ID, type=CommandType.LONG, description="the ID of the virtual machine") private Long id; diff --git a/api/src/com/cloud/api/commands/MigrateVMCmd.java b/api/src/com/cloud/api/commands/MigrateVMCmd.java index f502e169644..47560c1990d 100644 --- a/api/src/com/cloud/api/commands/MigrateVMCmd.java +++ b/api/src/com/cloud/api/commands/MigrateVMCmd.java @@ -22,6 +22,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseAsyncCmd; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; @@ -52,6 +53,7 @@ public class MigrateVMCmd extends BaseAsyncCmd { @Parameter(name=ApiConstants.HOST_ID, type=CommandType.LONG, required=false, description="destination Host ID to migrate VM to") private Long hostId; + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.VIRTUAL_MACHINE_ID, type=CommandType.LONG, required=true, description="the ID of the virtual machine") private Long virtualMachineId; diff --git a/api/src/com/cloud/api/commands/RebootVMCmd.java b/api/src/com/cloud/api/commands/RebootVMCmd.java index 755c840f154..8411b35bb99 100644 --- a/api/src/com/cloud/api/commands/RebootVMCmd.java +++ b/api/src/com/cloud/api/commands/RebootVMCmd.java @@ -22,6 +22,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseAsyncCmd; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; @@ -44,6 +45,7 @@ public class RebootVMCmd extends BaseAsyncCmd { //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="The ID of the virtual machine") private Long id; diff --git a/api/src/com/cloud/api/commands/RecoverVMCmd.java b/api/src/com/cloud/api/commands/RecoverVMCmd.java index 4b895721192..f3f0c44f91f 100644 --- a/api/src/com/cloud/api/commands/RecoverVMCmd.java +++ b/api/src/com/cloud/api/commands/RecoverVMCmd.java @@ -22,6 +22,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; @@ -40,6 +41,7 @@ public class RecoverVMCmd extends BaseCmd { //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="The ID of the virtual machine") private Long id; diff --git a/api/src/com/cloud/api/commands/ResetVMPasswordCmd.java b/api/src/com/cloud/api/commands/ResetVMPasswordCmd.java index 049b6900714..b6ab662e307 100644 --- a/api/src/com/cloud/api/commands/ResetVMPasswordCmd.java +++ b/api/src/com/cloud/api/commands/ResetVMPasswordCmd.java @@ -22,6 +22,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseAsyncCmd; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; @@ -46,6 +47,7 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd { //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="The ID of the virtual machine") private Long id; diff --git a/api/src/com/cloud/api/commands/RestoreVMCmd.java b/api/src/com/cloud/api/commands/RestoreVMCmd.java index 965f031db7e..0ee6b1af84f 100755 --- a/api/src/com/cloud/api/commands/RestoreVMCmd.java +++ b/api/src/com/cloud/api/commands/RestoreVMCmd.java @@ -5,6 +5,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseAsyncCmd; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; @@ -25,6 +26,7 @@ public class RestoreVMCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(RestoreVMCmd.class); private static final String s_name = "restorevmresponse"; + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.VIRTUAL_MACHINE_ID, type=CommandType.LONG, required=true, description="Virtual Machine ID") private Long vmId; diff --git a/api/src/com/cloud/api/commands/StartVMCmd.java b/api/src/com/cloud/api/commands/StartVMCmd.java index a4ba5c1f331..c1969e1ae9d 100644 --- a/api/src/com/cloud/api/commands/StartVMCmd.java +++ b/api/src/com/cloud/api/commands/StartVMCmd.java @@ -22,6 +22,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseAsyncCmd; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; @@ -49,6 +50,7 @@ public class StartVMCmd extends BaseAsyncCmd { //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="The ID of the virtual machine") private Long id; diff --git a/api/src/com/cloud/api/commands/StopVMCmd.java b/api/src/com/cloud/api/commands/StopVMCmd.java index 8230ec44414..534d0df97c5 100644 --- a/api/src/com/cloud/api/commands/StopVMCmd.java +++ b/api/src/com/cloud/api/commands/StopVMCmd.java @@ -22,6 +22,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseAsyncCmd; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; @@ -44,6 +45,7 @@ public class StopVMCmd extends BaseAsyncCmd { //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="The ID of the virtual machine") private Long id; diff --git a/api/src/com/cloud/api/commands/UpdateVMCmd.java b/api/src/com/cloud/api/commands/UpdateVMCmd.java index 7a281ab5f33..59d3fa41572 100644 --- a/api/src/com/cloud/api/commands/UpdateVMCmd.java +++ b/api/src/com/cloud/api/commands/UpdateVMCmd.java @@ -21,6 +21,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; @@ -47,6 +48,7 @@ public class UpdateVMCmd extends BaseCmd{ @Parameter(name=ApiConstants.HA_ENABLE, type=CommandType.BOOLEAN, description="true if high-availability is enabled for the virtual machine, false otherwise") private Boolean haEnable; + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="The ID of the virtual machine") private Long id; diff --git a/api/src/com/cloud/api/commands/UpdateVMGroupCmd.java b/api/src/com/cloud/api/commands/UpdateVMGroupCmd.java index 5d2523ac6e7..78458ea2cac 100644 --- a/api/src/com/cloud/api/commands/UpdateVMGroupCmd.java +++ b/api/src/com/cloud/api/commands/UpdateVMGroupCmd.java @@ -1,89 +1,91 @@ -/** - * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. - * - * This software is licensed under the GNU General Public License v3 or later. - * - * It is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or any later version. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ -package com.cloud.api.commands; - -import org.apache.log4j.Logger; - -import com.cloud.api.ApiConstants; -import com.cloud.api.BaseCmd; -import com.cloud.api.Implementation; -import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; -import com.cloud.api.response.InstanceGroupResponse; -import com.cloud.user.Account; -import com.cloud.vm.InstanceGroup; - -@Implementation(description="Updates a vm group", responseObject=InstanceGroupResponse.class) -public class UpdateVMGroupCmd extends BaseCmd{ - - private static final String s_name = "updateinstancegroupresponse"; - public static final Logger s_logger = Logger.getLogger(UpdateVMGroupCmd.class.getName()); - - ///////////////////////////////////////////////////// - //////////////// API parameters ///////////////////// - ///////////////////////////////////////////////////// - - @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="Instance group ID") - private Long id; - - @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="new instance group name") - private String groupName; - - ///////////////////////////////////////////////////// - /////////////////// Accessors /////////////////////// - ///////////////////////////////////////////////////// - - public Long getId() { - return id; - } - - public String getGroupName() { - return groupName; - } - - ///////////////////////////////////////////////////// - /////////////// API Implementation/////////////////// - ///////////////////////////////////////////////////// - - @Override - public String getCommandName() { - return s_name; - } - - @Override - public long getEntityOwnerId() { - InstanceGroup group = _entityMgr.findById(InstanceGroup.class, getId()); - if (group != null) { - return group.getAccountId(); - } - - return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked - } - - @Override - public void execute(){ - InstanceGroup result = _mgr.updateVmGroup(this); - if (result != null){ - InstanceGroupResponse response = _responseGenerator.createInstanceGroupResponse(result); - response.setResponseName(getCommandName()); - this.setResponseObject(response); - } else { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update vm instance group"); - } - } -} +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.api.commands; + +import org.apache.log4j.Logger; + +import com.cloud.api.ApiConstants; +import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; +import com.cloud.api.Implementation; +import com.cloud.api.Parameter; +import com.cloud.api.ServerApiException; +import com.cloud.api.response.InstanceGroupResponse; +import com.cloud.user.Account; +import com.cloud.vm.InstanceGroup; + +@Implementation(description="Updates a vm group", responseObject=InstanceGroupResponse.class) +public class UpdateVMGroupCmd extends BaseCmd{ + + private static final String s_name = "updateinstancegroupresponse"; + public static final Logger s_logger = Logger.getLogger(UpdateVMGroupCmd.class.getName()); + + ///////////////////////////////////////////////////// + //////////////// API parameters ///////////////////// + ///////////////////////////////////////////////////// + + @IdentityMapper(entityTableName="instance_group") + @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="Instance group ID") + private Long id; + + @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="new instance group name") + private String groupName; + + ///////////////////////////////////////////////////// + /////////////////// Accessors /////////////////////// + ///////////////////////////////////////////////////// + + public Long getId() { + return id; + } + + public String getGroupName() { + return groupName; + } + + ///////////////////////////////////////////////////// + /////////////// API Implementation/////////////////// + ///////////////////////////////////////////////////// + + @Override + public String getCommandName() { + return s_name; + } + + @Override + public long getEntityOwnerId() { + InstanceGroup group = _entityMgr.findById(InstanceGroup.class, getId()); + if (group != null) { + return group.getAccountId(); + } + + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked + } + + @Override + public void execute(){ + InstanceGroup result = _mgr.updateVmGroup(this); + if (result != null){ + InstanceGroupResponse response = _responseGenerator.createInstanceGroupResponse(result); + response.setResponseName(getCommandName()); + this.setResponseObject(response); + } else { + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update vm instance group"); + } + } +} diff --git a/api/src/com/cloud/api/commands/UpgradeVMCmd.java b/api/src/com/cloud/api/commands/UpgradeVMCmd.java index 3af7967b128..4299edb7042 100644 --- a/api/src/com/cloud/api/commands/UpgradeVMCmd.java +++ b/api/src/com/cloud/api/commands/UpgradeVMCmd.java @@ -21,6 +21,7 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseCmd; +import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; @@ -42,6 +43,7 @@ public class UpgradeVMCmd extends BaseCmd { //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// + @IdentityMapper(entityTableName="vm_instance") @Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="The ID of the virtual machine") private Long id; diff --git a/api/src/com/cloud/api/response/UserVmResponse.java b/api/src/com/cloud/api/response/UserVmResponse.java index c5ad1c2113d..1c67796a0e5 100755 --- a/api/src/com/cloud/api/response/UserVmResponse.java +++ b/api/src/com/cloud/api/response/UserVmResponse.java @@ -27,7 +27,7 @@ import com.google.gson.annotations.SerializedName; @SuppressWarnings("unused") public class UserVmResponse extends BaseResponse implements ControlledEntityResponse { @SerializedName(ApiConstants.ID) @Param(description="the ID of the virtual machine") - private Long id; + private String id; @SerializedName(ApiConstants.NAME) @Param(description="the name of the virtual machine") private String name; @@ -160,11 +160,11 @@ public class UserVmResponse extends BaseResponse implements ControlledEntityResp this.hypervisor = hypervisor; } - public void setId(Long id) { + public void setId(String id) { this.id = id; } - public Long getId() { + public String getId() { return id; } diff --git a/api/src/com/cloud/vm/VirtualMachine.java b/api/src/com/cloud/vm/VirtualMachine.java index c416c07f1fb..e29d02e739f 100755 --- a/api/src/com/cloud/vm/VirtualMachine.java +++ b/api/src/com/cloud/vm/VirtualMachine.java @@ -21,6 +21,7 @@ import java.util.Date; import java.util.Map; import com.cloud.acl.ControlledEntity; +import com.cloud.api.Identity; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.utils.fsm.StateMachine2; import com.cloud.utils.fsm.StateObject; @@ -30,7 +31,7 @@ import com.cloud.utils.fsm.StateObject; * VirtualMachine describes the properties held by a virtual machine * */ -public interface VirtualMachine extends RunningOn, ControlledEntity, StateObject { +public interface VirtualMachine extends RunningOn, ControlledEntity, Identity, StateObject { public enum State { Starting(true, "VM is being started. At this state, you should find host id filled which means it's being started on that host."), diff --git a/core/src/com/cloud/vm/VMInstanceVO.java b/core/src/com/cloud/vm/VMInstanceVO.java index 8c28a31d892..5d88f28fdb9 100644 --- a/core/src/com/cloud/vm/VMInstanceVO.java +++ b/core/src/com/cloud/vm/VMInstanceVO.java @@ -21,6 +21,7 @@ package com.cloud.vm; import java.util.Date; import java.util.Map; import java.util.Random; +import java.util.UUID; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; @@ -144,6 +145,9 @@ public class VMInstanceVO implements VirtualMachine, FiniteStateObject details; + + @Column(name="uuid") + protected String uuid; public VMInstanceVO(long id, long serviceOfferingId, @@ -172,6 +176,7 @@ public class VMInstanceVO implements VirtualMachine, FiniteStateObject. + * + */ + +package com.cloud.Identity.dao; + +import com.cloud.api.IdentityMapper; +import com.cloud.utils.db.GenericDao; + +public interface IdentityDao extends GenericDao { + Long getIdentityId(IdentityMapper mapper, String identityString); +} diff --git a/server/src/com/cloud/Identity/dao/IdentityDaoImpl.java b/server/src/com/cloud/Identity/dao/IdentityDaoImpl.java new file mode 100644 index 00000000000..1d49c6899d7 --- /dev/null +++ b/server/src/com/cloud/Identity/dao/IdentityDaoImpl.java @@ -0,0 +1,66 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.Identity.dao; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import javax.ejb.Local; + +import org.apache.log4j.Logger; + +import com.cloud.api.IdentityMapper; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.Transaction; + +@Local(value={IdentityDao.class}) +public class IdentityDaoImpl extends GenericDaoBase implements IdentityDao { + private static final Logger s_logger = Logger.getLogger(IdentityDaoImpl.class); + + public Long getIdentityId(IdentityMapper mapper, String identityString) { + assert(mapper.entityTableName() != null); + assert(identityString != null); + + PreparedStatement pstmt = null; + Transaction txn = Transaction.currentTxn();; + + try { + pstmt = txn.prepareAutoCloseStatement( + String.format("SELECT id FROM %s WHERE id=? OR uuid=?", mapper.entityTableName())); + + long id = 0; + try { + id = Long.parseLong(identityString); + } catch(NumberFormatException e) { + // this could happen when it is a uuid string, so catch and ignore it + } + + pstmt.setLong(1, id); + pstmt.setString(2, identityString); + + ResultSet rs = pstmt.executeQuery(); + if(rs.next()) { + return rs.getLong(1); + } + } catch (SQLException e) { + s_logger.error("Unexpected exception ", e); + } + return null; + } +} diff --git a/server/src/com/cloud/Identity/dao/IdentityVO.java b/server/src/com/cloud/Identity/dao/IdentityVO.java new file mode 100644 index 00000000000..0d6cb4175f8 --- /dev/null +++ b/server/src/com/cloud/Identity/dao/IdentityVO.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2011 Citrix Systems, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.Identity.dao; + +import javax.persistence.Entity; + +/** + * This is a dummy class to fit for CloudStack Dao framework + */ +@Entity +public class IdentityVO { +} diff --git a/server/src/com/cloud/api/ApiDispatcher.java b/server/src/com/cloud/api/ApiDispatcher.java index 0c5af3d4c46..8c4aa2bb2fe 100755 --- a/server/src/com/cloud/api/ApiDispatcher.java +++ b/server/src/com/cloud/api/ApiDispatcher.java @@ -30,6 +30,8 @@ import java.util.regex.Matcher; import org.apache.log4j.Logger; +import com.cloud.Identity.dao.IdentityDao; +import com.cloud.Identity.dao.IdentityDaoImpl; import com.cloud.api.BaseCmd.CommandType; import com.cloud.api.commands.ListEventsCmd; import com.cloud.async.AsyncCommandQueued; @@ -55,6 +57,7 @@ public class ApiDispatcher { ComponentLocator _locator; AsyncJobManager _asyncMgr; + IdentityDao _identityDao; // singleton class private static ApiDispatcher s_instance = new ApiDispatcher(); @@ -66,6 +69,7 @@ public class ApiDispatcher { private ApiDispatcher() { _locator = ComponentLocator.getLocator(ManagementServer.Name); _asyncMgr = _locator.getManager(AsyncJobManager.class); + _identityDao = _locator.getDao(IdentityDao.class); } public void dispatchCreateCmd(BaseAsyncCreateCmd cmd, Map params) { @@ -194,6 +198,8 @@ public class ApiDispatcher { if ((parameterAnnotation == null) || !parameterAnnotation.expose()) { continue; } + + IdentityMapper identityMapper = field.getAnnotation(IdentityMapper.class); Object paramObj = unpackedParams.get(parameterAnnotation.name()); if (paramObj == null) { @@ -205,7 +211,7 @@ public class ApiDispatcher { // marshall the parameter into the correct type and set the field value try { - setFieldValue(field, cmd, paramObj, parameterAnnotation); + setFieldValue(field, cmd, paramObj, parameterAnnotation, identityMapper); } catch (IllegalArgumentException argEx) { if (s_logger.isDebugEnabled()) { s_logger.debug("Unable to execute API command " + cmd.getCommandName() + " due to invalid value " + paramObj + " for parameter " + parameterAnnotation.name()); @@ -229,7 +235,7 @@ public class ApiDispatcher { } @SuppressWarnings({ "unchecked", "rawtypes" }) - private static void setFieldValue(Field field, BaseCmd cmdObj, Object paramObj, Parameter annotation) throws IllegalArgumentException, ParseException { + private static void setFieldValue(Field field, BaseCmd cmdObj, Object paramObj, Parameter annotation, IdentityMapper identityMapper) throws IllegalArgumentException, ParseException { try { field.setAccessible(true); CommandType fieldType = annotation.type(); @@ -296,7 +302,10 @@ public class ApiDispatcher { field.set(cmdObj, listParam); break; case LONG: - field.set(cmdObj, Long.valueOf(paramObj.toString())); + if(identityMapper != null) + field.set(cmdObj, s_instance._identityDao.getIdentityId(identityMapper, paramObj.toString())); + else + field.set(cmdObj, Long.valueOf(paramObj.toString())); break; case SHORT: field.set(cmdObj, Short.valueOf(paramObj.toString())); diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 30566b5b622..c8bb7d28323 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -1169,7 +1169,8 @@ public class ApiResponseHelper implements ResponseGenerator { // stats calculation String cpuUsed = null; - VmStats vmStats = ApiDBUtils.getVmStatistics(userVmResponse.getId()); + // VmStats vmStats = ApiDBUtils.getVmStatistics(userVmResponse.getId()); + VmStats vmStats = ApiDBUtils.getVmStatistics(uvd.getId()); if (vmStats != null) { float cpuUtil = (float) vmStats.getCPUUtilization(); cpuUsed = decimalFormat.format(cpuUtil) + "%"; @@ -2525,7 +2526,10 @@ public class ApiResponseHelper implements ResponseGenerator { public UserVmResponse newUserVmResponse(UserVmData userVmData, boolean caller_is_admin){ UserVmResponse userVmResponse = new UserVmResponse(); userVmResponse.setHypervisor(userVmData.getHypervisor()); - userVmResponse.setId(userVmData.getId()); + if(userVmData.getUuid() != null && !userVmData.getUuid().isEmpty()) + userVmResponse.setId(userVmData.getUuid()); + else + userVmResponse.setId(String.valueOf(userVmData.getId())); userVmResponse.setName(userVmData.getName()); userVmResponse.setDisplayName(userVmData.getDisplayName()); userVmResponse.setIpAddress(userVmData.getIpAddress()); diff --git a/server/src/com/cloud/configuration/DefaultComponentLibrary.java b/server/src/com/cloud/configuration/DefaultComponentLibrary.java index 00588f36190..eeb08f0baef 100755 --- a/server/src/com/cloud/configuration/DefaultComponentLibrary.java +++ b/server/src/com/cloud/configuration/DefaultComponentLibrary.java @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.cloud.Identity.dao.IdentityDaoImpl; import com.cloud.agent.manager.ClusteredAgentManagerImpl; import com.cloud.alert.AlertManagerImpl; import com.cloud.alert.dao.AlertDaoImpl; @@ -281,6 +282,7 @@ public class DefaultComponentLibrary extends ComponentLibraryBase implements Com addDao("ElasticLbVmMap", ElasticLbVmMapDaoImpl.class); addDao("ProjectsAccountDao", ProjectAccountDaoImpl.class); addDao("ProjectInvitationDao", ProjectInvitationDaoImpl.class); + addDao("IdentityDao", IdentityDaoImpl.class); info = addDao("HypervisorCapabilitiesDao",HypervisorCapabilitiesDaoImpl.class); info.addParameter("cache.size", "100"); info.addParameter("cache.time.to.live", "600"); diff --git a/server/src/com/cloud/vm/dao/UserVmDao.java b/server/src/com/cloud/vm/dao/UserVmDao.java index 31a72f7b066..26a6a902b92 100755 --- a/server/src/com/cloud/vm/dao/UserVmDao.java +++ b/server/src/com/cloud/vm/dao/UserVmDao.java @@ -21,8 +21,6 @@ import java.util.Date; import java.util.Hashtable; import java.util.List; -import com.cloud.api.response.UserVmResponse; -import com.cloud.uservm.UserVm; import com.cloud.utils.db.GenericDao; import com.cloud.vm.UserVmVO; import com.cloud.vm.VirtualMachine.State; diff --git a/server/src/com/cloud/vm/dao/UserVmDaoImpl.java b/server/src/com/cloud/vm/dao/UserVmDaoImpl.java index a2092898af5..77edb2a0d41 100755 --- a/server/src/com/cloud/vm/dao/UserVmDaoImpl.java +++ b/server/src/com/cloud/vm/dao/UserVmDaoImpl.java @@ -1,568 +1,596 @@ -/** - * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. - * - * This software is licensed under the GNU General Public License v3 or later. - * - * It is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or any later version. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ -package com.cloud.vm.dao; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Date; -import java.util.Hashtable; -import java.util.List; -import java.util.Map; - -import javax.ejb.Local; - -import org.apache.log4j.Logger; - -import com.cloud.user.Account; -import com.cloud.utils.component.ComponentLocator; -import com.cloud.utils.db.Attribute; -import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.GenericSearchBuilder; -import com.cloud.utils.db.JoinBuilder; -import com.cloud.utils.db.SearchBuilder; -import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.SearchCriteria.Func; -import com.cloud.utils.db.Transaction; -import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.vm.NicVO; -import com.cloud.vm.UserVmVO; -import com.cloud.vm.VirtualMachine; -import com.cloud.vm.VirtualMachine.State; -import com.cloud.vm.dao.UserVmData.NicData; -import com.cloud.vm.dao.UserVmData.SecurityGroupData; - -@Local(value={UserVmDao.class}) -public class UserVmDaoImpl extends GenericDaoBase implements UserVmDao { - public static final Logger s_logger = Logger.getLogger(UserVmDaoImpl.class); - - protected final SearchBuilder AccountPodSearch; - protected final SearchBuilder AccountDataCenterSearch; - protected final SearchBuilder AccountSearch; - protected final SearchBuilder HostSearch; - protected final SearchBuilder LastHostSearch; - protected final SearchBuilder HostUpSearch; - protected final SearchBuilder HostRunningSearch; - protected final SearchBuilder StateChangeSearch; - protected final SearchBuilder AccountHostSearch; - - protected final SearchBuilder DestroySearch; - protected SearchBuilder AccountDataCenterVirtualSearch; - protected GenericSearchBuilder CountByAccountPod; - protected GenericSearchBuilder CountByAccount; - protected GenericSearchBuilder PodsHavingVmsForAccount; - - protected SearchBuilder UserVmSearch; - protected final Attribute _updateTimeAttr; - - private static final String LIST_PODS_HAVING_VMS_FOR_ACCOUNT = "SELECT pod_id FROM cloud.vm_instance WHERE data_center_id = ? AND account_id = ? AND pod_id IS NOT NULL AND (state = 'Running' OR state = 'Stopped') " + - "GROUP BY pod_id HAVING count(id) > 0 ORDER BY count(id) DESC"; - - private static final int VM_DETAILS_BATCH_SIZE=100; - - protected final UserVmDetailsDaoImpl _detailsDao = ComponentLocator.inject(UserVmDetailsDaoImpl.class); - protected final NicDaoImpl _nicDao = ComponentLocator.inject(NicDaoImpl.class); - - protected UserVmDaoImpl() { - AccountSearch = createSearchBuilder(); - AccountSearch.and("account", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ); - AccountSearch.done(); - - HostSearch = createSearchBuilder(); - HostSearch.and("host", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ); - HostSearch.done(); - - LastHostSearch = createSearchBuilder(); - LastHostSearch.and("lastHost", LastHostSearch.entity().getLastHostId(), SearchCriteria.Op.EQ); - LastHostSearch.and("state", LastHostSearch.entity().getState(), SearchCriteria.Op.EQ); - LastHostSearch.done(); - - HostUpSearch = createSearchBuilder(); - HostUpSearch.and("host", HostUpSearch.entity().getHostId(), SearchCriteria.Op.EQ); - HostUpSearch.and("states", HostUpSearch.entity().getState(), SearchCriteria.Op.NIN); - HostUpSearch.done(); - - HostRunningSearch = createSearchBuilder(); - HostRunningSearch.and("host", HostRunningSearch.entity().getHostId(), SearchCriteria.Op.EQ); - HostRunningSearch.and("state", HostRunningSearch.entity().getState(), SearchCriteria.Op.EQ); - HostRunningSearch.done(); - - AccountPodSearch = createSearchBuilder(); - AccountPodSearch.and("account", AccountPodSearch.entity().getAccountId(), SearchCriteria.Op.EQ); - AccountPodSearch.and("pod", AccountPodSearch.entity().getPodIdToDeployIn(), SearchCriteria.Op.EQ); - AccountPodSearch.done(); - - AccountDataCenterSearch = createSearchBuilder(); - AccountDataCenterSearch.and("account", AccountDataCenterSearch.entity().getAccountId(), SearchCriteria.Op.EQ); - AccountDataCenterSearch.and("dc", AccountDataCenterSearch.entity().getDataCenterIdToDeployIn(), SearchCriteria.Op.EQ); - AccountDataCenterSearch.done(); - - StateChangeSearch = createSearchBuilder(); - StateChangeSearch.and("id", StateChangeSearch.entity().getId(), SearchCriteria.Op.EQ); - StateChangeSearch.and("states", StateChangeSearch.entity().getState(), SearchCriteria.Op.EQ); - StateChangeSearch.and("host", StateChangeSearch.entity().getHostId(), SearchCriteria.Op.EQ); - StateChangeSearch.and("update", StateChangeSearch.entity().getUpdated(), SearchCriteria.Op.EQ); - StateChangeSearch.done(); - - DestroySearch = createSearchBuilder(); - DestroySearch.and("state", DestroySearch.entity().getState(), SearchCriteria.Op.IN); - DestroySearch.and("updateTime", DestroySearch.entity().getUpdateTime(), SearchCriteria.Op.LT); - DestroySearch.done(); - - AccountHostSearch = createSearchBuilder(); - AccountHostSearch.and("accountId", AccountHostSearch.entity().getAccountId(), SearchCriteria.Op.EQ); - AccountHostSearch.and("hostId", AccountHostSearch.entity().getHostId(), SearchCriteria.Op.EQ); - AccountHostSearch.done(); - - CountByAccountPod = createSearchBuilder(Long.class); - CountByAccountPod.select(null, Func.COUNT, null); - CountByAccountPod.and("account", CountByAccountPod.entity().getAccountId(), SearchCriteria.Op.EQ); - CountByAccountPod.and("pod", CountByAccountPod.entity().getPodIdToDeployIn(), SearchCriteria.Op.EQ); - CountByAccountPod.done(); - - CountByAccount = createSearchBuilder(Long.class); - CountByAccount.select(null, Func.COUNT, null); - CountByAccount.and("account", CountByAccount.entity().getAccountId(), SearchCriteria.Op.EQ); - CountByAccount.and("type", CountByAccount.entity().getType(), SearchCriteria.Op.EQ); - CountByAccount.and("state", CountByAccount.entity().getState(), SearchCriteria.Op.NIN); - CountByAccount.done(); - - - SearchBuilder nicSearch = _nicDao.createSearchBuilder(); - nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); - nicSearch.and("ip4Address", nicSearch.entity().getIp4Address(), SearchCriteria.Op.NNULL); - - AccountDataCenterVirtualSearch = createSearchBuilder(); - AccountDataCenterVirtualSearch.and("account", AccountDataCenterVirtualSearch.entity().getAccountId(), SearchCriteria.Op.EQ); - AccountDataCenterVirtualSearch.and("dc", AccountDataCenterVirtualSearch.entity().getDataCenterIdToDeployIn(), SearchCriteria.Op.EQ); - AccountDataCenterVirtualSearch.join("nicSearch", nicSearch, AccountDataCenterVirtualSearch.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER); - AccountDataCenterVirtualSearch.done(); - - - _updateTimeAttr = _allAttributes.get("updateTime"); - assert _updateTimeAttr != null : "Couldn't get this updateTime attribute"; - } - - @Override - public List listByAccountAndPod(long accountId, long podId) { - SearchCriteria sc = AccountPodSearch.create(); - sc.setParameters("account", accountId); - sc.setParameters("pod", podId); - - return listIncludingRemovedBy(sc); - } - - @Override - public List listByAccountAndDataCenter(long accountId, long dcId) { - SearchCriteria sc = AccountDataCenterSearch.create(); - sc.setParameters("account", accountId); - sc.setParameters("dc", dcId); - - return listIncludingRemovedBy(sc); - } - - @Override - public void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData) { - UserVmVO vo = createForUpdate(); - vo.setDisplayName(displayName); - vo.setHaEnabled(enable); - vo.setGuestOSId(osTypeId); - vo.setUserData(userData); - update(id, vo); - } - - @Override - public List findDestroyedVms(Date date) { - SearchCriteria sc = DestroySearch.create(); - sc.setParameters("state", State.Destroyed, State.Expunging, State.Error); - sc.setParameters("updateTime", date); - - return listBy(sc); - } - - @Override - public List listByAccountId(long id) { - SearchCriteria sc = AccountSearch.create(); - sc.setParameters("account", id); - return listBy(sc); - } - - @Override - public List listByHostId(Long id) { - SearchCriteria sc = HostSearch.create(); - sc.setParameters("host", id); - - return listBy(sc); - } - - @Override - public List listUpByHostId(Long hostId) { - SearchCriteria sc = HostUpSearch.create(); - sc.setParameters("host", hostId); - sc.setParameters("states", new Object[] {State.Destroyed, State.Stopped, State.Expunging}); - return listBy(sc); - } - - @Override - public List listRunningByHostId(long hostId) { - SearchCriteria sc = HostRunningSearch.create(); - sc.setParameters("host", hostId); - sc.setParameters("state", State.Running); - - return listBy(sc); - } - - @Override - public List listVirtualNetworkInstancesByAcctAndZone(long accountId, long dcId, long networkId) { - - SearchCriteria sc = AccountDataCenterVirtualSearch.create(); - sc.setParameters("account", accountId); - sc.setParameters("dc", dcId); - sc.setJoinParameters("nicSearch", "networkId", networkId); - - return listBy(sc); - } - - @Override - public List listByNetworkIdAndStates(long networkId, State... states) { - if (UserVmSearch == null) { - NicDao _nicDao = ComponentLocator.getLocator("management-server").getDao(NicDao.class); - SearchBuilder nicSearch = _nicDao.createSearchBuilder(); - nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); - nicSearch.and("ip4Address", nicSearch.entity().getIp4Address(), SearchCriteria.Op.NNULL); - - UserVmSearch = createSearchBuilder(); - UserVmSearch.and("states", UserVmSearch.entity().getState(), SearchCriteria.Op.IN); - UserVmSearch.join("nicSearch", nicSearch, UserVmSearch.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER); - UserVmSearch.done(); - } - - SearchCriteria sc = UserVmSearch.create(); - if (states != null && states.length != 0) { - sc.setParameters("states", (Object[]) states); - } - sc.setJoinParameters("nicSearch", "networkId", networkId); - - return listBy(sc); - } - - @Override - public List listByLastHostId(Long hostId) { - SearchCriteria sc = LastHostSearch.create(); - sc.setParameters("lastHost", hostId); - sc.setParameters("state", State.Stopped); - return listBy(sc); - } - - @Override - public List listByAccountIdAndHostId(long accountId, long hostId) { - SearchCriteria sc = AccountHostSearch.create(); - sc.setParameters("hostId", hostId); - sc.setParameters("accountId", accountId); - return listBy(sc); - } - - @Override - public void loadDetails(UserVmVO vm) { - Map details = _detailsDao.findDetails(vm.getId()); - vm.setDetails(details); - } - - @Override - public void saveDetails(UserVmVO vm) { - Map details = vm.getDetails(); - if (details == null) { - return; - } - _detailsDao.persist(vm.getId(), details); - } - - @Override - public List listPodIdsHavingVmsforAccount(long zoneId, long accountId){ - Transaction txn = Transaction.currentTxn(); - PreparedStatement pstmt = null; - List result = new ArrayList(); - - try { - String sql = LIST_PODS_HAVING_VMS_FOR_ACCOUNT; - pstmt = txn.prepareAutoCloseStatement(sql); - pstmt.setLong(1, zoneId); - pstmt.setLong(2, accountId); - - ResultSet rs = pstmt.executeQuery(); - while (rs.next()) { - result.add(rs.getLong(1)); - } - return result; - } catch (SQLException e) { - throw new CloudRuntimeException("DB Exception on: " + LIST_PODS_HAVING_VMS_FOR_ACCOUNT, e); - } catch (Throwable e) { - throw new CloudRuntimeException("Caught: " + LIST_PODS_HAVING_VMS_FOR_ACCOUNT, e); - } - } - - @Override - public Hashtable listVmDetails(Hashtable userVmDataHash, int details){ - Transaction txn = Transaction.currentTxn(); - PreparedStatement pstmt = null; - DetailSql sql = new DetailSql(details); - - - try { - int curr_index=0; - - List userVmDataList = new ArrayList(userVmDataHash.values()); - - if (userVmDataList.size() > VM_DETAILS_BATCH_SIZE){ - pstmt = txn.prepareStatement(sql.getSql() + getQueryBatchAppender(VM_DETAILS_BATCH_SIZE)); - while ( (curr_index + VM_DETAILS_BATCH_SIZE) <= userVmDataList.size()){ - // set the vars value - for (int k=1,j=curr_index;j 0){ - userVmData.setGroupId(grp_id); - userVmData.setGroup(rs.getString("instance_group.name")); - } - - //"data_center.id, data_center.name, host.id, host.name, vm_template.id, vm_template.name, vm_template.display_text, vm_template.enable_password, - userVmData.setZoneId(rs.getLong("data_center.id")); - userVmData.setZoneName(rs.getString("data_center.name")); - - userVmData.setHostId(rs.getLong("host.id")); - userVmData.setHostName(rs.getString("host.name")); - - long template_id = rs.getLong("vm_template.id"); - if (template_id > 0){ - userVmData.setTemplateId(template_id); - userVmData.setTemplateName(rs.getString("vm_template.name")); - userVmData.setTemplateDisplayText(rs.getString("vm_template.display_text")); - userVmData.setPasswordEnabled(rs.getBoolean("vm_template.enable_password")); - } - else { - userVmData.setTemplateId(-1L); - userVmData.setTemplateName("ISO Boot"); - userVmData.setTemplateDisplayText("ISO Boot"); - userVmData.setPasswordEnabled(false); - } - - long iso_id = rs.getLong("iso.id"); - if (iso_id > 0){ - userVmData.setIsoId(iso_id); - userVmData.setIsoName(rs.getString("iso.name")); - } - - - //service_offering.id, disk_offering.name, " - //"service_offering.cpu, service_offering.speed, service_offering.ram_size, - userVmData.setServiceOfferingId(rs.getLong("service_offering.id")); - userVmData.setServiceOfferingName(rs.getString("disk_offering.name")); - userVmData.setCpuNumber(rs.getInt("service_offering.cpu")); - userVmData.setCpuSpeed(rs.getInt("service_offering.speed")); - userVmData.setMemory(rs.getInt("service_offering.ram_size")); - - // volumes.device_id, volumes.volume_type, - long vol_id = rs.getLong("volumes.id"); - if (vol_id > 0){ - userVmData.setRootDeviceId(rs.getLong("volumes.device_id")); - userVmData.setRootDeviceType(rs.getString("volumes.volume_type")); - // storage pool - long pool_id = rs.getLong("storage_pool.id"); - if (pool_id > 0){ - userVmData.setRootDeviceType(rs.getString("storage_pool.pool_type")); - } - else { - userVmData.setRootDeviceType("Not created"); - } - } - userVmData.setInitialized(); - } - - - boolean is_data_center_security_group_enabled = rs.getBoolean("data_center.is_security_group_enabled"); - //security_group.id, security_group.name, security_group.description, , data_center.is_security_group_enabled - if (is_data_center_security_group_enabled){ - SecurityGroupData resp = userVmData.newSecurityGroupData(); - resp.setId(rs.getLong("security_group.id")); - resp.setName(rs.getString("security_group.name")); - resp.setDescription(rs.getString("security_group.description")); - resp.setObjectName("securitygroup"); - userVmData.addSecurityGroup(resp); - } - - - //nics.id, nics.ip4_address, nics.gateway, nics.network_id, nics.netmask, nics. mac_address, nics.broadcast_uri, nics.isolation_uri, " + - //"networks.traffic_type, networks.guest_type, networks.is_default from vm_instance, " - long nic_id = rs.getLong("nics.id"); - if (nic_id > 0){ - NicData nicResponse = userVmData.newNicData(); - nicResponse.setId(nic_id); - nicResponse.setIpaddress(rs.getString("nics.ip4_address")); - nicResponse.setGateway(rs.getString("nics.gateway")); - nicResponse.setNetmask(rs.getString("nics.netmask")); - nicResponse.setNetworkid(rs.getLong("nics.network_id")); - nicResponse.setMacAddress(rs.getString("nics.mac_address")); - - int account_type = rs.getInt("account.type"); - if (account_type == Account.ACCOUNT_TYPE_ADMIN) { - nicResponse.setBroadcastUri(rs.getString("nics.broadcast_uri")); - nicResponse.setIsolationUri(rs.getString("nics.isolation_uri")); - } - - - nicResponse.setTrafficType(rs.getString("networks.traffic_type")); - nicResponse.setType(rs.getString("networks.guest_type")); - nicResponse.setIsDefault(rs.getBoolean("networks.is_default")); - nicResponse.setObjectName("nic"); - userVmData.addNic(nicResponse); - } - return userVmData; - } - - public String getQueryBatchAppender(int count){ - StringBuilder sb = new StringBuilder(); - for (int i=0;i sc = CountByAccount.create(); - sc.setParameters("account", accountId); - sc.setParameters("type", VirtualMachine.Type.User); - sc.setParameters("state", new Object[] {State.Destroyed, State.Error, State.Expunging}); - return customSearch(sc, null).get(0); - } - - - public static class DetailSql { - private DetailsMask _details; - - private static String VM_DETAILS = "select vm_instance.id, " + - "account.id, account.account_name, account.type, domain.name, instance_group.id, instance_group.name," + - "data_center.id, data_center.name, data_center.is_security_group_enabled, host.id, host.name, " + - "vm_template.id, vm_template.name, vm_template.display_text, iso.id, iso.name, " + - "vm_template.enable_password, service_offering.id, disk_offering.name, storage_pool.id, storage_pool.pool_type, " + - "service_offering.cpu, service_offering.speed, service_offering.ram_size, volumes.id, volumes.device_id, volumes.volume_type, security_group.id, security_group.name, " + - "security_group.description, nics.id, nics.ip4_address, nics.gateway, nics.network_id, nics.netmask, nics.mac_address, nics.broadcast_uri, nics.isolation_uri, " + - "networks.traffic_type, networks.guest_type, networks.is_default from vm_instance " + - "left join account on vm_instance.account_id=account.id " + - "left join domain on vm_instance.domain_id=domain.id " + - "left join instance_group_vm_map on vm_instance.id=instance_group_vm_map.instance_id " + - "left join instance_group on instance_group_vm_map.group_id=instance_group.id " + - "left join data_center on vm_instance.data_center_id=data_center.id " + - "left join host on vm_instance.host_id=host.id " + - "left join vm_template on vm_instance.vm_template_id=vm_template.id " + - "left join user_vm on vm_instance.id=user_vm.id " + - "left join vm_template iso on iso.id=user_vm.iso_id " + - "left join service_offering on vm_instance.service_offering_id=service_offering.id " + - "left join disk_offering on vm_instance.service_offering_id=disk_offering.id " + - "left join volumes on vm_instance.id=volumes.instance_id " + - "left join storage_pool on volumes.pool_id=storage_pool.id " + - "left join security_group_vm_map on vm_instance.id=security_group_vm_map.instance_id " + - "left join security_group on security_group_vm_map.security_group_id=security_group.id " + - "left join nics on vm_instance.id=nics.instance_id " + - "left join networks on nics.network_id=networks.id " + - "where vm_instance.id in ("; - - - - - - public DetailSql(int details){ - _details = new DetailsMask(details); - } - - public String getSql(){ - return VM_DETAILS; - } - - - - } - - -} +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.vm.dao; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Date; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; + +import javax.ejb.Local; + +import org.apache.log4j.Logger; + +import com.cloud.user.Account; +import com.cloud.utils.component.ComponentLocator; +import com.cloud.utils.db.Attribute; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.GenericSearchBuilder; +import com.cloud.utils.db.JoinBuilder; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.SearchCriteria.Func; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.vm.NicVO; +import com.cloud.vm.UserVmVO; +import com.cloud.vm.VirtualMachine; +import com.cloud.vm.VirtualMachine.State; +import com.cloud.vm.dao.UserVmData.NicData; +import com.cloud.vm.dao.UserVmData.SecurityGroupData; + +@Local(value={UserVmDao.class}) +public class UserVmDaoImpl extends GenericDaoBase implements UserVmDao { + public static final Logger s_logger = Logger.getLogger(UserVmDaoImpl.class); + + protected final SearchBuilder AccountPodSearch; + protected final SearchBuilder AccountDataCenterSearch; + protected final SearchBuilder AccountSearch; + protected final SearchBuilder HostSearch; + protected final SearchBuilder LastHostSearch; + protected final SearchBuilder HostUpSearch; + protected final SearchBuilder HostRunningSearch; + protected final SearchBuilder StateChangeSearch; + protected final SearchBuilder AccountHostSearch; + + protected final SearchBuilder DestroySearch; + protected SearchBuilder AccountDataCenterVirtualSearch; + protected GenericSearchBuilder CountByAccountPod; + protected GenericSearchBuilder CountByAccount; + protected GenericSearchBuilder PodsHavingVmsForAccount; + + protected SearchBuilder UserVmSearch; + protected final Attribute _updateTimeAttr; + + private static final String LIST_PODS_HAVING_VMS_FOR_ACCOUNT = "SELECT pod_id FROM cloud.vm_instance WHERE data_center_id = ? AND account_id = ? AND pod_id IS NOT NULL AND (state = 'Running' OR state = 'Stopped') " + + "GROUP BY pod_id HAVING count(id) > 0 ORDER BY count(id) DESC"; + + private static final int VM_DETAILS_BATCH_SIZE=100; + private static final String VM_DETAILS = "select vm_instance.id, vm_instance.uuid, " + + "account.id, account.account_name, account.type, domain.name, instance_group.id, instance_group.name," + + "data_center.id, data_center.name, data_center.is_security_group_enabled, host.id, host.name, " + + "vm_template.id, vm_template.name, vm_template.display_text, iso.id, iso.name, " + + "vm_template.enable_password, service_offering.id, disk_offering.name, storage_pool.id, storage_pool.pool_type, " + + "service_offering.cpu, service_offering.speed, service_offering.ram_size, volumes.id, volumes.device_id, volumes.volume_type, security_group.id, security_group.name, " + + "security_group.description, nics.id, nics.ip4_address, nics.gateway, nics.network_id, nics.netmask, nics.mac_address, nics.broadcast_uri, nics.isolation_uri, " + + "networks.traffic_type, networks.guest_type, networks.is_default from vm_instance " + + "left join account on vm_instance.account_id=account.id " + + "left join domain on vm_instance.domain_id=domain.id " + + "left join instance_group_vm_map on vm_instance.id=instance_group_vm_map.instance_id " + + "left join instance_group on instance_group_vm_map.group_id=instance_group.id " + + "left join data_center on vm_instance.data_center_id=data_center.id " + + "left join host on vm_instance.host_id=host.id " + + "left join vm_template on vm_instance.vm_template_id=vm_template.id " + + "left join user_vm on vm_instance.id=user_vm.id " + + "left join vm_template iso on iso.id=user_vm.iso_id " + + "left join service_offering on vm_instance.service_offering_id=service_offering.id " + + "left join disk_offering on vm_instance.service_offering_id=disk_offering.id " + + "left join volumes on vm_instance.id=volumes.instance_id " + + "left join storage_pool on volumes.pool_id=storage_pool.id " + + "left join security_group_vm_map on vm_instance.id=security_group_vm_map.instance_id " + + "left join security_group on security_group_vm_map.security_group_id=security_group.id " + + "left join nics on vm_instance.id=nics.instance_id " + + "left join networks on nics.network_id=networks.id " + + "where vm_instance.id in ("; + + protected final UserVmDetailsDaoImpl _detailsDao = ComponentLocator.inject(UserVmDetailsDaoImpl.class); + protected final NicDaoImpl _nicDao = ComponentLocator.inject(NicDaoImpl.class); + + protected UserVmDaoImpl() { + AccountSearch = createSearchBuilder(); + AccountSearch.and("account", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ); + AccountSearch.done(); + + HostSearch = createSearchBuilder(); + HostSearch.and("host", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ); + HostSearch.done(); + + LastHostSearch = createSearchBuilder(); + LastHostSearch.and("lastHost", LastHostSearch.entity().getLastHostId(), SearchCriteria.Op.EQ); + LastHostSearch.and("state", LastHostSearch.entity().getState(), SearchCriteria.Op.EQ); + LastHostSearch.done(); + + HostUpSearch = createSearchBuilder(); + HostUpSearch.and("host", HostUpSearch.entity().getHostId(), SearchCriteria.Op.EQ); + HostUpSearch.and("states", HostUpSearch.entity().getState(), SearchCriteria.Op.NIN); + HostUpSearch.done(); + + HostRunningSearch = createSearchBuilder(); + HostRunningSearch.and("host", HostRunningSearch.entity().getHostId(), SearchCriteria.Op.EQ); + HostRunningSearch.and("state", HostRunningSearch.entity().getState(), SearchCriteria.Op.EQ); + HostRunningSearch.done(); + + AccountPodSearch = createSearchBuilder(); + AccountPodSearch.and("account", AccountPodSearch.entity().getAccountId(), SearchCriteria.Op.EQ); + AccountPodSearch.and("pod", AccountPodSearch.entity().getPodIdToDeployIn(), SearchCriteria.Op.EQ); + AccountPodSearch.done(); + + AccountDataCenterSearch = createSearchBuilder(); + AccountDataCenterSearch.and("account", AccountDataCenterSearch.entity().getAccountId(), SearchCriteria.Op.EQ); + AccountDataCenterSearch.and("dc", AccountDataCenterSearch.entity().getDataCenterIdToDeployIn(), SearchCriteria.Op.EQ); + AccountDataCenterSearch.done(); + + StateChangeSearch = createSearchBuilder(); + StateChangeSearch.and("id", StateChangeSearch.entity().getId(), SearchCriteria.Op.EQ); + StateChangeSearch.and("states", StateChangeSearch.entity().getState(), SearchCriteria.Op.EQ); + StateChangeSearch.and("host", StateChangeSearch.entity().getHostId(), SearchCriteria.Op.EQ); + StateChangeSearch.and("update", StateChangeSearch.entity().getUpdated(), SearchCriteria.Op.EQ); + StateChangeSearch.done(); + + DestroySearch = createSearchBuilder(); + DestroySearch.and("state", DestroySearch.entity().getState(), SearchCriteria.Op.IN); + DestroySearch.and("updateTime", DestroySearch.entity().getUpdateTime(), SearchCriteria.Op.LT); + DestroySearch.done(); + + AccountHostSearch = createSearchBuilder(); + AccountHostSearch.and("accountId", AccountHostSearch.entity().getAccountId(), SearchCriteria.Op.EQ); + AccountHostSearch.and("hostId", AccountHostSearch.entity().getHostId(), SearchCriteria.Op.EQ); + AccountHostSearch.done(); + + CountByAccountPod = createSearchBuilder(Long.class); + CountByAccountPod.select(null, Func.COUNT, null); + CountByAccountPod.and("account", CountByAccountPod.entity().getAccountId(), SearchCriteria.Op.EQ); + CountByAccountPod.and("pod", CountByAccountPod.entity().getPodIdToDeployIn(), SearchCriteria.Op.EQ); + CountByAccountPod.done(); + + CountByAccount = createSearchBuilder(Long.class); + CountByAccount.select(null, Func.COUNT, null); + CountByAccount.and("account", CountByAccount.entity().getAccountId(), SearchCriteria.Op.EQ); + CountByAccount.and("type", CountByAccount.entity().getType(), SearchCriteria.Op.EQ); + CountByAccount.and("state", CountByAccount.entity().getState(), SearchCriteria.Op.NIN); + CountByAccount.done(); + + + SearchBuilder nicSearch = _nicDao.createSearchBuilder(); + nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); + nicSearch.and("ip4Address", nicSearch.entity().getIp4Address(), SearchCriteria.Op.NNULL); + + AccountDataCenterVirtualSearch = createSearchBuilder(); + AccountDataCenterVirtualSearch.and("account", AccountDataCenterVirtualSearch.entity().getAccountId(), SearchCriteria.Op.EQ); + AccountDataCenterVirtualSearch.and("dc", AccountDataCenterVirtualSearch.entity().getDataCenterIdToDeployIn(), SearchCriteria.Op.EQ); + AccountDataCenterVirtualSearch.join("nicSearch", nicSearch, AccountDataCenterVirtualSearch.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER); + AccountDataCenterVirtualSearch.done(); + + + _updateTimeAttr = _allAttributes.get("updateTime"); + assert _updateTimeAttr != null : "Couldn't get this updateTime attribute"; + } + + @Override + public List listByAccountAndPod(long accountId, long podId) { + SearchCriteria sc = AccountPodSearch.create(); + sc.setParameters("account", accountId); + sc.setParameters("pod", podId); + + return listIncludingRemovedBy(sc); + } + + @Override + public List listByAccountAndDataCenter(long accountId, long dcId) { + SearchCriteria sc = AccountDataCenterSearch.create(); + sc.setParameters("account", accountId); + sc.setParameters("dc", dcId); + + return listIncludingRemovedBy(sc); + } + + @Override + public void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData) { + UserVmVO vo = createForUpdate(); + vo.setDisplayName(displayName); + vo.setHaEnabled(enable); + vo.setGuestOSId(osTypeId); + vo.setUserData(userData); + update(id, vo); + } + + @Override + public List findDestroyedVms(Date date) { + SearchCriteria sc = DestroySearch.create(); + sc.setParameters("state", State.Destroyed, State.Expunging, State.Error); + sc.setParameters("updateTime", date); + + return listBy(sc); + } + + @Override + public List listByAccountId(long id) { + SearchCriteria sc = AccountSearch.create(); + sc.setParameters("account", id); + return listBy(sc); + } + + @Override + public List listByHostId(Long id) { + SearchCriteria sc = HostSearch.create(); + sc.setParameters("host", id); + + return listBy(sc); + } + + @Override + public List listUpByHostId(Long hostId) { + SearchCriteria sc = HostUpSearch.create(); + sc.setParameters("host", hostId); + sc.setParameters("states", new Object[] {State.Destroyed, State.Stopped, State.Expunging}); + return listBy(sc); + } + + @Override + public List listRunningByHostId(long hostId) { + SearchCriteria sc = HostRunningSearch.create(); + sc.setParameters("host", hostId); + sc.setParameters("state", State.Running); + + return listBy(sc); + } + + @Override + public List listVirtualNetworkInstancesByAcctAndZone(long accountId, long dcId, long networkId) { + + SearchCriteria sc = AccountDataCenterVirtualSearch.create(); + sc.setParameters("account", accountId); + sc.setParameters("dc", dcId); + sc.setJoinParameters("nicSearch", "networkId", networkId); + + return listBy(sc); + } + + @Override + public List listByNetworkIdAndStates(long networkId, State... states) { + if (UserVmSearch == null) { + NicDao _nicDao = ComponentLocator.getLocator("management-server").getDao(NicDao.class); + SearchBuilder nicSearch = _nicDao.createSearchBuilder(); + nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); + nicSearch.and("ip4Address", nicSearch.entity().getIp4Address(), SearchCriteria.Op.NNULL); + + UserVmSearch = createSearchBuilder(); + UserVmSearch.and("states", UserVmSearch.entity().getState(), SearchCriteria.Op.IN); + UserVmSearch.join("nicSearch", nicSearch, UserVmSearch.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER); + UserVmSearch.done(); + } + + SearchCriteria sc = UserVmSearch.create(); + if (states != null && states.length != 0) { + sc.setParameters("states", (Object[]) states); + } + sc.setJoinParameters("nicSearch", "networkId", networkId); + + return listBy(sc); + } + + @Override + public List listByLastHostId(Long hostId) { + SearchCriteria sc = LastHostSearch.create(); + sc.setParameters("lastHost", hostId); + sc.setParameters("state", State.Stopped); + return listBy(sc); + } + + @Override + public List listByAccountIdAndHostId(long accountId, long hostId) { + SearchCriteria sc = AccountHostSearch.create(); + sc.setParameters("hostId", hostId); + sc.setParameters("accountId", accountId); + return listBy(sc); + } + + @Override + public void loadDetails(UserVmVO vm) { + Map details = _detailsDao.findDetails(vm.getId()); + vm.setDetails(details); + } + + @Override + public void saveDetails(UserVmVO vm) { + Map details = vm.getDetails(); + if (details == null) { + return; + } + _detailsDao.persist(vm.getId(), details); + } + + @Override + public List listPodIdsHavingVmsforAccount(long zoneId, long accountId){ + Transaction txn = Transaction.currentTxn(); + PreparedStatement pstmt = null; + List result = new ArrayList(); + + try { + String sql = LIST_PODS_HAVING_VMS_FOR_ACCOUNT; + pstmt = txn.prepareAutoCloseStatement(sql); + pstmt.setLong(1, zoneId); + pstmt.setLong(2, accountId); + + ResultSet rs = pstmt.executeQuery(); + while (rs.next()) { + result.add(rs.getLong(1)); + } + return result; + } catch (SQLException e) { + throw new CloudRuntimeException("DB Exception on: " + LIST_PODS_HAVING_VMS_FOR_ACCOUNT, e); + } catch (Throwable e) { + throw new CloudRuntimeException("Caught: " + LIST_PODS_HAVING_VMS_FOR_ACCOUNT, e); + } + } + + @Override + public Hashtable listVmDetails(Hashtable userVmDataHash, int details){ + Transaction txn = Transaction.currentTxn(); + PreparedStatement pstmt = null; + DetailSql sql = new DetailSql(details); + + + try { + int curr_index=0; + + List userVmDataList = new ArrayList(userVmDataHash.values()); + + if (userVmDataList.size() > VM_DETAILS_BATCH_SIZE){ + pstmt = txn.prepareStatement(sql.getSql() + getQueryBatchAppender(VM_DETAILS_BATCH_SIZE)); + while ( (curr_index + VM_DETAILS_BATCH_SIZE) <= userVmDataList.size()){ + // set the vars value + for (int k=1,j=curr_index;j 0){ + userVmData.setGroupId(grp_id); + userVmData.setGroup(rs.getString("instance_group.name")); + } + + //"data_center.id, data_center.name, host.id, host.name, vm_template.id, vm_template.name, vm_template.display_text, vm_template.enable_password, + userVmData.setZoneId(rs.getLong("data_center.id")); + userVmData.setZoneName(rs.getString("data_center.name")); + + userVmData.setHostId(rs.getLong("host.id")); + userVmData.setHostName(rs.getString("host.name")); + + long template_id = rs.getLong("vm_template.id"); + if (template_id > 0){ + userVmData.setTemplateId(template_id); + userVmData.setTemplateName(rs.getString("vm_template.name")); + userVmData.setTemplateDisplayText(rs.getString("vm_template.display_text")); + userVmData.setPasswordEnabled(rs.getBoolean("vm_template.enable_password")); + } + else { + userVmData.setTemplateId(-1L); + userVmData.setTemplateName("ISO Boot"); + userVmData.setTemplateDisplayText("ISO Boot"); + userVmData.setPasswordEnabled(false); + } + + long iso_id = rs.getLong("iso.id"); + if (iso_id > 0){ + userVmData.setIsoId(iso_id); + userVmData.setIsoName(rs.getString("iso.name")); + } + + + //service_offering.id, disk_offering.name, " + //"service_offering.cpu, service_offering.speed, service_offering.ram_size, + userVmData.setServiceOfferingId(rs.getLong("service_offering.id")); + userVmData.setServiceOfferingName(rs.getString("disk_offering.name")); + userVmData.setCpuNumber(rs.getInt("service_offering.cpu")); + userVmData.setCpuSpeed(rs.getInt("service_offering.speed")); + userVmData.setMemory(rs.getInt("service_offering.ram_size")); + + // volumes.device_id, volumes.volume_type, + long vol_id = rs.getLong("volumes.id"); + if (vol_id > 0){ + userVmData.setRootDeviceId(rs.getLong("volumes.device_id")); + userVmData.setRootDeviceType(rs.getString("volumes.volume_type")); + // storage pool + long pool_id = rs.getLong("storage_pool.id"); + if (pool_id > 0){ + userVmData.setRootDeviceType(rs.getString("storage_pool.pool_type")); + } + else { + userVmData.setRootDeviceType("Not created"); + } + } + userVmData.setInitialized(); + } + + + boolean is_data_center_security_group_enabled = rs.getBoolean("data_center.is_security_group_enabled"); + //security_group.id, security_group.name, security_group.description, , data_center.is_security_group_enabled + if (is_data_center_security_group_enabled){ + SecurityGroupData resp = userVmData.newSecurityGroupData(); + resp.setId(rs.getLong("security_group.id")); + resp.setName(rs.getString("security_group.name")); + resp.setDescription(rs.getString("security_group.description")); + resp.setObjectName("securitygroup"); + userVmData.addSecurityGroup(resp); + } + + + //nics.id, nics.ip4_address, nics.gateway, nics.network_id, nics.netmask, nics. mac_address, nics.broadcast_uri, nics.isolation_uri, " + + //"networks.traffic_type, networks.guest_type, networks.is_default from vm_instance, " + long nic_id = rs.getLong("nics.id"); + if (nic_id > 0){ + NicData nicResponse = userVmData.newNicData(); + nicResponse.setId(nic_id); + nicResponse.setIpaddress(rs.getString("nics.ip4_address")); + nicResponse.setGateway(rs.getString("nics.gateway")); + nicResponse.setNetmask(rs.getString("nics.netmask")); + nicResponse.setNetworkid(rs.getLong("nics.network_id")); + nicResponse.setMacAddress(rs.getString("nics.mac_address")); + + int account_type = rs.getInt("account.type"); + if (account_type == Account.ACCOUNT_TYPE_ADMIN) { + nicResponse.setBroadcastUri(rs.getString("nics.broadcast_uri")); + nicResponse.setIsolationUri(rs.getString("nics.isolation_uri")); + } + + + nicResponse.setTrafficType(rs.getString("networks.traffic_type")); + nicResponse.setType(rs.getString("networks.guest_type")); + nicResponse.setIsDefault(rs.getBoolean("networks.is_default")); + nicResponse.setObjectName("nic"); + userVmData.addNic(nicResponse); + } + return userVmData; + } + + public String getQueryBatchAppender(int count){ + StringBuilder sb = new StringBuilder(); + for (int i=0;i sc = CountByAccount.create(); + sc.setParameters("account", accountId); + sc.setParameters("type", VirtualMachine.Type.User); + sc.setParameters("state", new Object[] {State.Destroyed, State.Error, State.Expunging}); + return customSearch(sc, null).get(0); + } + + + public static class DetailSql { + private DetailsMask _details; + + private static String VM_DETAILS = "select vm_instance.id, " + + "account.id, account.account_name, account.type, domain.name, instance_group.id, instance_group.name," + + "data_center.id, data_center.name, data_center.is_security_group_enabled, host.id, host.name, " + + "vm_template.id, vm_template.name, vm_template.display_text, iso.id, iso.name, " + + "vm_template.enable_password, service_offering.id, disk_offering.name, storage_pool.id, storage_pool.pool_type, " + + "service_offering.cpu, service_offering.speed, service_offering.ram_size, volumes.id, volumes.device_id, volumes.volume_type, security_group.id, security_group.name, " + + "security_group.description, nics.id, nics.ip4_address, nics.gateway, nics.network_id, nics.netmask, nics.mac_address, nics.broadcast_uri, nics.isolation_uri, " + + "networks.traffic_type, networks.guest_type, networks.is_default from vm_instance " + + "left join account on vm_instance.account_id=account.id " + + "left join domain on vm_instance.domain_id=domain.id " + + "left join instance_group_vm_map on vm_instance.id=instance_group_vm_map.instance_id " + + "left join instance_group on instance_group_vm_map.group_id=instance_group.id " + + "left join data_center on vm_instance.data_center_id=data_center.id " + + "left join host on vm_instance.host_id=host.id " + + "left join vm_template on vm_instance.vm_template_id=vm_template.id " + + "left join user_vm on vm_instance.id=user_vm.id " + + "left join vm_template iso on iso.id=user_vm.iso_id " + + "left join service_offering on vm_instance.service_offering_id=service_offering.id " + + "left join disk_offering on vm_instance.service_offering_id=disk_offering.id " + + "left join volumes on vm_instance.id=volumes.instance_id " + + "left join storage_pool on volumes.pool_id=storage_pool.id " + + "left join security_group_vm_map on vm_instance.id=security_group_vm_map.instance_id " + + "left join security_group on security_group_vm_map.security_group_id=security_group.id " + + "left join nics on vm_instance.id=nics.instance_id " + + "left join networks on nics.network_id=networks.id " + + "where vm_instance.id in ("; + + + + + + public DetailSql(int details){ + _details = new DetailsMask(details); + } + + public String getSql(){ + return VM_DETAILS; + } + + + + } + + +} diff --git a/server/src/com/cloud/vm/dao/UserVmData.java b/server/src/com/cloud/vm/dao/UserVmData.java index c32f17578f9..56b3a778f97 100644 --- a/server/src/com/cloud/vm/dao/UserVmData.java +++ b/server/src/com/cloud/vm/dao/UserVmData.java @@ -27,6 +27,7 @@ import com.cloud.api.response.IngressRuleResponse; public class UserVmData { private Long id; private String name; + private String uuid; private String displayName; private String ipAddress; private String accountName; @@ -119,6 +120,14 @@ public class UserVmData { public void setName(String name) { this.name = name; } + + public String getUuid() { + return this.uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } public String getDisplayName() { return displayName; diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index bff2a329398..128cabd5ed6 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -870,6 +870,7 @@ CREATE TABLE `cloud`.`vm_template` ( CREATE TABLE `cloud`.`vm_instance` ( `id` bigint unsigned UNIQUE NOT NULL, `name` varchar(255) NOT NULL, + `uuid` varchar(255), `instance_name` varchar(255) NOT NULL COMMENT 'name of the vm instance running on the hosts', `state` varchar(32) NOT NULL, `vm_template_id` bigint unsigned, @@ -912,7 +913,8 @@ CREATE TABLE `cloud`.`vm_instance` ( CONSTRAINT `fk_vm_instance__account_id` FOREIGN KEY `fk_vm_instance__account_id` (`account_id`) REFERENCES `account` (`id`), INDEX `i_vm_instance__account_id`(`account_id`), CONSTRAINT `fk_vm_instance__service_offering_id` FOREIGN KEY `fk_vm_instance__service_offering_id` (`service_offering_id`) REFERENCES `service_offering` (`id`), - INDEX `i_vm_instance__service_offering_id`(`service_offering_id`) + INDEX `i_vm_instance__service_offering_id`(`service_offering_id`), + CONSTRAINT `uc_vm_instance_uuid` UNIQUE (`uuid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `cloud`.`user_vm` ( diff --git a/setup/db/db/schema-2213to30.sql b/setup/db/db/schema-2213to30.sql index 3eee6e2d16e..7312089f83e 100755 --- a/setup/db/db/schema-2213to30.sql +++ b/setup/db/db/schema-2213to30.sql @@ -109,10 +109,6 @@ update configuration set name = 'zone.virtualnetwork.publicip.capacity.notificat update configuration set name = 'pod.privateip.capacity.notificationthreshold' , category = 'Alert' where name = 'private.ip.capacity.threshold' ; - - - - ALTER TABLE `cloud`.`domain_router` ADD COLUMN `template_version` varchar(100) COMMENT 'template version' AFTER role; ALTER TABLE `cloud`.`domain_router` ADD COLUMN `scripts_version` varchar(100) COMMENT 'scripts version' AFTER template_version; ALTER TABLE `cloud`.`alert` ADD `cluster_id` bigint unsigned; @@ -121,3 +117,5 @@ DELETE from `cloud`.`op_host_capacity` where capacity_type in (2,4,6); ALTER TABLE `cloud`.`user_statistics` ADD COLUMN `agg_bytes_received` bigint unsigned NOT NULL default '0'; ALTER TABLE `cloud`.`user_statistics` ADD COLUMN `agg_bytes_sent` bigint unsigned NOT NULL default '0'; +ALTER TABLE `cloud`.`vm_instance` ADD COLUMN `uuid` varchar(255); +ALTER TABLE `cloud`.`vm_instance` ADD CONSTRAINT `uc_vm_instance_uuid` UNIQUE (`uuid`);