From 25e4ed6997173a75fb543efb41e820e2d2708ffa Mon Sep 17 00:00:00 2001 From: Alex Huang Date: Thu, 21 Oct 2010 16:53:14 -0700 Subject: [PATCH] more changes in the api --- .../InvalidParameterValueException.java | 5 +- .../exception/PermissionDeniedException.java | 4 +- server/src/com/cloud/api/ApiDispatcher.java | 6 +- .../api/commands/CancelMaintenanceCmd.java | 2 +- .../com/cloud/api/commands/DeployVmCmd.java | 2 +- .../commands/PrepareForMaintenanceCmd.java | 2 +- .../cloud/api/commands/ReconnectHostCmd.java | 2 +- .../cloud/network/DomainRouterService.java | 2 +- server/src/com/cloud/vm/UserVmManager.java | 96 +------------ .../src/com/cloud/vm/UserVmManagerImpl.java | 4 +- server/src/com/cloud/vm/UserVmService.java | 128 ++++++++++++++++++ 11 files changed, 147 insertions(+), 106 deletions(-) create mode 100644 server/src/com/cloud/vm/UserVmService.java diff --git a/api/src/com/cloud/exception/InvalidParameterValueException.java b/api/src/com/cloud/exception/InvalidParameterValueException.java index e6b2dcd2a4a..1449fc1fc9a 100644 --- a/api/src/com/cloud/exception/InvalidParameterValueException.java +++ b/api/src/com/cloud/exception/InvalidParameterValueException.java @@ -18,17 +18,18 @@ package com.cloud.exception; +import com.cloud.utils.exception.CloudRuntimeException; + /** * @author chiradeep * */ -public class InvalidParameterValueException extends ManagementServerException { +public class InvalidParameterValueException extends CloudRuntimeException { private static final long serialVersionUID = -2232066904895010203L; public InvalidParameterValueException(String message) { super(message); - } } diff --git a/api/src/com/cloud/exception/PermissionDeniedException.java b/api/src/com/cloud/exception/PermissionDeniedException.java index 25f02f57fc0..1141fa6d260 100644 --- a/api/src/com/cloud/exception/PermissionDeniedException.java +++ b/api/src/com/cloud/exception/PermissionDeniedException.java @@ -17,11 +17,13 @@ */ package com.cloud.exception; +import com.cloud.utils.exception.CloudRuntimeException; + /** * @author chiradeep * */ -public class PermissionDeniedException extends ManagementServerException { +public class PermissionDeniedException extends CloudRuntimeException { private static final long serialVersionUID = -4631412831814398074L; diff --git a/server/src/com/cloud/api/ApiDispatcher.java b/server/src/com/cloud/api/ApiDispatcher.java index 803c261a6a1..326191b9b3a 100644 --- a/server/src/com/cloud/api/ApiDispatcher.java +++ b/server/src/com/cloud/api/ApiDispatcher.java @@ -47,7 +47,7 @@ import com.cloud.user.AccountManager; import com.cloud.utils.DateUtil; import com.cloud.utils.component.ComponentLocator; import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.vm.UserVmManager; +import com.cloud.vm.UserVmService; /** * A class that dispatches API commands to the appropriate manager for execution. @@ -65,7 +65,7 @@ public class ApiDispatcher { private SnapshotManager _snapshotMgr; private StorageManager _storageMgr; private TemplateManager _templateMgr; - private UserVmManager _userVmMgr; + private UserVmService _userVmMgr; private DomainRouterService _domainRouterService; // singleton class @@ -87,7 +87,7 @@ public class ApiDispatcher { _snapshotMgr = locator.getManager(SnapshotManager.class); _storageMgr = locator.getManager(StorageManager.class); _templateMgr = locator.getManager(TemplateManager.class); - _userVmMgr = locator.getManager(UserVmManager.class); + _userVmMgr = locator.getManager(UserVmService.class); _domainRouterService = locator.getManager(DomainRouterService.class); } diff --git a/server/src/com/cloud/api/commands/CancelMaintenanceCmd.java b/server/src/com/cloud/api/commands/CancelMaintenanceCmd.java index 51ed4787141..a52af58c555 100644 --- a/server/src/com/cloud/api/commands/CancelMaintenanceCmd.java +++ b/server/src/com/cloud/api/commands/CancelMaintenanceCmd.java @@ -82,7 +82,7 @@ public class CancelMaintenanceCmd extends BaseAsyncCmd { @Override public long getAccountId() { - Account account = (Account)UserContext.current().getAccount(); + Account account = UserContext.current().getAccount(); if (account != null) { return account.getId(); } diff --git a/server/src/com/cloud/api/commands/DeployVmCmd.java b/server/src/com/cloud/api/commands/DeployVmCmd.java index 1dce88d55e0..b65d8ae6d5f 100644 --- a/server/src/com/cloud/api/commands/DeployVmCmd.java +++ b/server/src/com/cloud/api/commands/DeployVmCmd.java @@ -164,7 +164,7 @@ public class DeployVmCmd extends BaseAsyncCmd { @Override public long getAccountId() { - Account account = (Account)UserContext.current().getAccount(); + Account account = UserContext.current().getAccount(); if ((account == null) || isAdmin(account.getType())) { if ((domainId != null) && (accountName != null)) { Account userAccount = ApiDBUtils.findAccountByNameDomain(accountName, domainId); diff --git a/server/src/com/cloud/api/commands/PrepareForMaintenanceCmd.java b/server/src/com/cloud/api/commands/PrepareForMaintenanceCmd.java index 3e41bccd214..cf77ca6dc75 100644 --- a/server/src/com/cloud/api/commands/PrepareForMaintenanceCmd.java +++ b/server/src/com/cloud/api/commands/PrepareForMaintenanceCmd.java @@ -64,7 +64,7 @@ public class PrepareForMaintenanceCmd extends BaseAsyncCmd { @Override public long getAccountId() { - Account account = (Account)UserContext.current().getAccount(); + Account account = UserContext.current().getAccount(); if (account != null) { return account.getId(); } diff --git a/server/src/com/cloud/api/commands/ReconnectHostCmd.java b/server/src/com/cloud/api/commands/ReconnectHostCmd.java index 43ed7354db8..85c29fa1175 100644 --- a/server/src/com/cloud/api/commands/ReconnectHostCmd.java +++ b/server/src/com/cloud/api/commands/ReconnectHostCmd.java @@ -80,7 +80,7 @@ public class ReconnectHostCmd extends BaseAsyncCmd { @Override public long getAccountId() { - Account account = (Account)UserContext.current().getAccount(); + Account account = UserContext.current().getAccount(); if (account != null) { return account.getId(); } diff --git a/server/src/com/cloud/network/DomainRouterService.java b/server/src/com/cloud/network/DomainRouterService.java index ce93e7a51d2..5e76cd5ca99 100644 --- a/server/src/com/cloud/network/DomainRouterService.java +++ b/server/src/com/cloud/network/DomainRouterService.java @@ -39,5 +39,5 @@ public interface DomainRouterService extends Manager { * @return router if successful, null otherwise * @throws InvalidParameterValueException, PermissionDeniedException */ - DomainRouterVO stopRouter(StopRouterCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; + DomainRouterVO stopRouter(StopRouterCmd cmd); } diff --git a/server/src/com/cloud/vm/UserVmManager.java b/server/src/com/cloud/vm/UserVmManager.java index 708820962a7..822aa390398 100644 --- a/server/src/com/cloud/vm/UserVmManager.java +++ b/server/src/com/cloud/vm/UserVmManager.java @@ -21,23 +21,7 @@ import java.util.HashMap; import java.util.List; import com.cloud.agent.api.VmStatsEntry; -import com.cloud.api.ServerApiException; -import com.cloud.api.commands.AttachVolumeCmd; -import com.cloud.api.commands.CreateTemplateCmd; -import com.cloud.api.commands.CreateVMGroupCmd; -import com.cloud.api.commands.DeleteVMGroupCmd; -import com.cloud.api.commands.DeployVmCmd; -import com.cloud.api.commands.DestroyVMCmd; -import com.cloud.api.commands.DetachVolumeCmd; -import com.cloud.api.commands.RebootVMCmd; -import com.cloud.api.commands.RecoverVMCmd; -import com.cloud.api.commands.ResetVMPasswordCmd; -import com.cloud.api.commands.StartVMCmd; -import com.cloud.api.commands.StopVMCmd; -import com.cloud.api.commands.UpdateVMCmd; -import com.cloud.api.commands.UpgradeVMCmd; import com.cloud.async.executor.OperationResponse; -import com.cloud.async.executor.RebootVMExecutor; import com.cloud.async.executor.StartVMExecutor; import com.cloud.async.executor.StopVMExecutor; import com.cloud.async.executor.VMOperationParam; @@ -46,10 +30,7 @@ import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.InsufficientStorageCapacityException; import com.cloud.exception.InternalErrorException; -import com.cloud.exception.InvalidParameterValueException; -import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.ResourceAllocationException; -import com.cloud.exception.ResourceUnavailableException; import com.cloud.exception.StorageUnavailableException; import com.cloud.network.security.NetworkGroupVO; import com.cloud.offerings.NetworkOfferingVO; @@ -101,38 +82,10 @@ public interface UserVmManager extends Manager, VirtualMachineManager UserVmVO createDirectlyAttachedVMExternal(Long vmId, long userId, AccountVO account, DataCenterVO dc, ServiceOfferingVO offering, VMTemplateVO template, DiskOfferingVO diskOffering, String displayName, String userData, List a, List networkGroupVO, long startEventId, long size) throws InternalErrorException, ResourceAllocationException; - /** - * Destroys one virtual machine - * @param userId the id of the user performing the action - * @param vmId the id of the virtual machine. - */ - boolean destroyVm(DestroyVMCmd cmd); boolean destroyVirtualMachine(long userId, long vmId); // OperationResponse executeDestroyVM(DestroyVMExecutor executor, VMOperationParam param); - /** - * Resets the password of a virtual machine. - * @param cmd - the command specifying vmId, password - * @return the VM if reset worked successfully, null otherwise - */ - UserVm resetVMPassword(ResetVMPasswordCmd cmd); - - /** - * Attaches the specified volume to the specified VM - * @param cmd - the command specifying volumeId and vmId - * @throws InternalErrorException, InvalidParameterValueException, PermissionDeniedException - */ - void attachVolumeToVM(AttachVolumeCmd cmd) throws InternalErrorException, InvalidParameterValueException, PermissionDeniedException; - - /** - * Detaches the specified volume from the VM it is currently attached to. - * @param cmd - the command specifying volumeId - * @throws InternalErrorException - * @throws InvalidParameterValueException - */ - void detachVolumeFromVM(DetachVolumeCmd cmmd) throws InternalErrorException, InvalidParameterValueException; - /** * Attaches an ISO to the virtual CDROM device of the specified VM. Will eject any existing virtual CDROM if isoPath is null. * @param vmId @@ -165,7 +118,6 @@ public interface UserVmManager extends Manager, VirtualMachineManager * @throws ConcurrentOperationException */ UserVmVO startVirtualMachine(long userId, long vmId, String password, String isoPath, long startEventId) throws ExecutionException, StorageUnavailableException, ConcurrentOperationException; - UserVmVO startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, ExecutionException, ConcurrentOperationException; /** * Stops the virtual machine @@ -175,19 +127,9 @@ public interface UserVmManager extends Manager, VirtualMachineManager * @return true if stopped; false if problems. */ boolean stopVirtualMachine(long userId, long vmId, long eventId); - UserVmVO stopVirtualMachine(StopVMCmd cmd) throws ServerApiException; - OperationResponse executeStopVM(StopVMExecutor executor, VMOperationParam param); void completeStopCommand(long userId, UserVmVO vm, Event e, long startEventId); - /** - * upgrade the service offering of the virtual machine - * @param cmd - the command specifying vmId and new serviceOfferingId - * @return the vm - * @throws InvalidParameterValueException - */ - UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ServerApiException, InvalidParameterValueException; - /** * Obtains statistics for a list of host or VMs; CPU and network utilization * @param host ID @@ -198,28 +140,6 @@ public interface UserVmManager extends Manager, VirtualMachineManager */ HashMap getVirtualMachineStatistics(long hostId, String hostName, List vmIds) throws InternalErrorException; - boolean rebootVirtualMachine(RebootVMCmd cmd); - OperationResponse executeRebootVM(RebootVMExecutor executor, VMOperationParam param); - - boolean recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException, InternalErrorException; - - /** - * Create a template database record in preparation for creating a private template. - * @param cmd the command object that defines the name, display text, snapshot/volume, bits, public/private, etc. - * for the private template - * @return the vm template object if successful, null otherwise - * @throws InvalidParameterValueException, PermissionDeniedException - */ - VMTemplateVO createPrivateTemplateRecord(CreateTemplateCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; - - /** - * Creates a private template from a snapshot of a VM - * @param cmd - the command specifying snapshotId, name, description - * @return a template if successfully created, null otherwise - * @throws InvalidParameterValueException - */ - VMTemplateVO createPrivateTemplate(CreateTemplateCmd cmd) throws InternalErrorException; - boolean destroyTemplateSnapshot(Long userId, long snapshotId); /** @@ -235,14 +155,6 @@ public interface UserVmManager extends Manager, VirtualMachineManager */ void releaseGuestIpAddress(UserVmVO userVm); - /** - * Creates a vm group. - * @param name - name of the group - * @param accountId - accountId - */ - InstanceGroupVO createVmGroup(CreateVMGroupCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; - - boolean deleteVmGroup(DeleteVMGroupCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; boolean deleteVmGroup(long groupId); boolean addInstanceToGroup(long userVmId, String group); @@ -250,10 +162,8 @@ public interface UserVmManager extends Manager, VirtualMachineManager InstanceGroupVO getGroupForVm(long vmId); void removeInstanceFromGroup(long vmId); + + @Deprecated + OperationResponse executeStopVM(StopVMExecutor executor, VMOperationParam param); - void updateVirtualMachine(UpdateVMCmd cmd); - - UserVm createVirtualMachine(DeployVmCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException; - - UserVm startVirtualMachine(DeployVmCmd cmd) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException; } diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index ef185a54af9..9aed0643f7a 100644 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -202,8 +202,8 @@ import com.cloud.vm.dao.InstanceGroupDao; import com.cloud.vm.dao.InstanceGroupVMMapDao; import com.cloud.vm.dao.UserVmDao; -@Local(value={UserVmManager.class}) -public class UserVmManagerImpl implements UserVmManager { +@Local(value={UserVmManager.class, UserVmService.class}) +public class UserVmManagerImpl implements UserVmManager, UserVmService { private static final Logger s_logger = Logger.getLogger(UserVmManagerImpl.class); private static final int ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION = 3; // 3 seconds diff --git a/server/src/com/cloud/vm/UserVmService.java b/server/src/com/cloud/vm/UserVmService.java new file mode 100644 index 00000000000..37564f167df --- /dev/null +++ b/server/src/com/cloud/vm/UserVmService.java @@ -0,0 +1,128 @@ +/** + * 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; + +import com.cloud.api.ServerApiException; +import com.cloud.api.commands.AttachVolumeCmd; +import com.cloud.api.commands.CreateTemplateCmd; +import com.cloud.api.commands.CreateVMGroupCmd; +import com.cloud.api.commands.DeleteVMGroupCmd; +import com.cloud.api.commands.DeployVmCmd; +import com.cloud.api.commands.DestroyVMCmd; +import com.cloud.api.commands.DetachVolumeCmd; +import com.cloud.api.commands.RebootVMCmd; +import com.cloud.api.commands.RecoverVMCmd; +import com.cloud.api.commands.ResetVMPasswordCmd; +import com.cloud.api.commands.StartVMCmd; +import com.cloud.api.commands.StopVMCmd; +import com.cloud.api.commands.UpdateVMCmd; +import com.cloud.api.commands.UpgradeVMCmd; +import com.cloud.async.executor.OperationResponse; +import com.cloud.async.executor.RebootVMExecutor; +import com.cloud.async.executor.VMOperationParam; +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.InternalErrorException; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.exception.PermissionDeniedException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.exception.StorageUnavailableException; +import com.cloud.storage.VMTemplateVO; +import com.cloud.uservm.UserVm; +import com.cloud.utils.component.Manager; +import com.cloud.utils.exception.ExecutionException; + +public interface UserVmService extends Manager { + /** + * Destroys one virtual machine + * @param userId the id of the user performing the action + * @param vmId the id of the virtual machine. + */ + boolean destroyVm(DestroyVMCmd cmd); + + /** + * Resets the password of a virtual machine. + * @param cmd - the command specifying vmId, password + * @return the VM if reset worked successfully, null otherwise + */ + UserVm resetVMPassword(ResetVMPasswordCmd cmd); + + /** + * Attaches the specified volume to the specified VM + * @param cmd - the command specifying volumeId and vmId + * @throws InternalErrorException, InvalidParameterValueException, PermissionDeniedException + */ + void attachVolumeToVM(AttachVolumeCmd cmd) throws InternalErrorException, InvalidParameterValueException, PermissionDeniedException; + + /** + * Detaches the specified volume from the VM it is currently attached to. + * @param cmd - the command specifying volumeId + * @throws InternalErrorException + * @throws InvalidParameterValueException + */ + void detachVolumeFromVM(DetachVolumeCmd cmmd) throws InternalErrorException, InvalidParameterValueException; + + UserVmVO startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, ExecutionException, ConcurrentOperationException; + UserVmVO stopVirtualMachine(StopVMCmd cmd) throws ServerApiException; + boolean rebootVirtualMachine(RebootVMCmd cmd); + + @Deprecated + OperationResponse executeRebootVM(RebootVMExecutor executor, VMOperationParam param); + + boolean recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException, InternalErrorException; + + /** + * Create a template database record in preparation for creating a private template. + * @param cmd the command object that defines the name, display text, snapshot/volume, bits, public/private, etc. + * for the private template + * @return the vm template object if successful, null otherwise + * @throws InvalidParameterValueException, PermissionDeniedException + */ + VMTemplateVO createPrivateTemplateRecord(CreateTemplateCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; + + /** + * Creates a private template from a snapshot of a VM + * @param cmd - the command specifying snapshotId, name, description + * @return a template if successfully created, null otherwise + * @throws InvalidParameterValueException + */ + VMTemplateVO createPrivateTemplate(CreateTemplateCmd cmd) throws InternalErrorException; + + void updateVirtualMachine(UpdateVMCmd cmd); + + UserVm createVirtualMachine(DeployVmCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException; + + UserVm startVirtualMachine(DeployVmCmd cmd) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException; + /** + * Creates a vm group. + * @param name - name of the group + * @param accountId - accountId + */ + InstanceGroupVO createVmGroup(CreateVMGroupCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; + + boolean deleteVmGroup(DeleteVMGroupCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; + + /** + * upgrade the service offering of the virtual machine + * @param cmd - the command specifying vmId and new serviceOfferingId + * @return the vm + * @throws InvalidParameterValueException + */ + UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ServerApiException, InvalidParameterValueException; +}