diff --git a/server/src/com/cloud/api/commands/StartVMCmd.java b/server/src/com/cloud/api/commands/StartVMCmd.java index ddf27f899eb..c7848dc1087 100644 --- a/server/src/com/cloud/api/commands/StartVMCmd.java +++ b/server/src/com/cloud/api/commands/StartVMCmd.java @@ -26,24 +26,19 @@ 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.BaseCmd.Manager; import com.cloud.user.Account; import com.cloud.utils.Pair; import com.cloud.vm.UserVmVO; - + +@Implementation(method="startVirtualMachine", manager=Manager.UserVmManager) public class StartVMCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(StartVMCmd.class.getName()); private static final String s_name = "startvirtualmachineresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -71,50 +66,52 @@ public class StartVMCmd extends BaseCmd { public static String getResultObjectName() { return "virtualmachine"; } - - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map params) { - Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName()); - Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - - //if account is removed, return error - if(account!=null && account.getRemoved() != null) - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); - - //Verify input parameters - UserVmVO vmInstanceCheck = getManagementServer().findUserVMInstanceById(vmId.longValue()); - if (vmInstanceCheck == null) { - throw new ServerApiException (BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); - } - - if (account != null) { - if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstanceCheck.getAccountId())) { - throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId + " for this account"); - } else if (!getManagementServer().isChildDomain(account.getDomainId(), vmInstanceCheck.getDomainId())) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to start virtual machine."); - } - } - - if (userId == null) { - userId = Long.valueOf(1); - } - - long jobId = getManagementServer().startVirtualMachineAsync(userId.longValue(), vmId.longValue(), null); - if(jobId == 0) { - s_logger.warn("Unable to schedule async-job for StartVM comamnd"); - } else { - if(s_logger.isDebugEnabled()) - s_logger.debug("StartVM command has been accepted, job id: " + jobId); - } - - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); - - return returnValues; - } +// @Override +// public List> execute(Map params) { +// Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName()); +// Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); +// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); +// +// //if account is removed, return error +// if(account!=null && account.getRemoved() != null) +// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); +// +// //Verify input parameters +// UserVmVO vmInstanceCheck = getManagementServer().findUserVMInstanceById(vmId.longValue()); +// if (vmInstanceCheck == null) { +// throw new ServerApiException (BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); +// } +// +// if (account != null) { +// if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstanceCheck.getAccountId())) { +// throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId + " for this account"); +// } else if (!getManagementServer().isChildDomain(account.getDomainId(), vmInstanceCheck.getDomainId())) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to start virtual machine."); +// } +// } +// +// if (userId == null) { +// userId = Long.valueOf(1); +// } +// +// long jobId = getManagementServer().startVirtualMachineAsync(userId.longValue(), vmId.longValue(), null); +// if(jobId == 0) { +// s_logger.warn("Unable to schedule async-job for StartVM comamnd"); +// } else { +// if(s_logger.isDebugEnabled()) +// s_logger.debug("StartVM command has been accepted, job id: " + jobId); +// } +// +// List> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); +// +// return returnValues; +// } + + @Override + public String getResponse() { + // TODO Add the response object as per executor + return null; + } } diff --git a/server/src/com/cloud/api/commands/StopSystemVmCmd.java b/server/src/com/cloud/api/commands/StopSystemVmCmd.java index 6788be63f51..75ceacba0a3 100644 --- a/server/src/com/cloud/api/commands/StopSystemVmCmd.java +++ b/server/src/com/cloud/api/commands/StopSystemVmCmd.java @@ -25,20 +25,19 @@ import java.util.Map; import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; +import com.cloud.api.BaseCmd.Manager; import com.cloud.utils.Pair; import com.cloud.vm.VMInstanceVO; - + +@Implementation(method="stopSystemVM", manager=Manager.ManagementServer) public class StopSystemVmCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(StopSystemVmCmd.class.getName()); private static final String s_name = "stopsystemvmresponse"; private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -63,29 +62,31 @@ public class StopSystemVmCmd extends BaseCmd { return s_name; } - public List> getProperties() { - return s_properties; - } - - public List> execute(Map params) { - Long sysVmId = (Long)params.get(BaseCmd.Properties.ID.getName()); - - // verify parameters - VMInstanceVO systemVM = getManagementServer().findSystemVMById(sysVmId); - if (systemVM == null) { - throw new ServerApiException (BaseCmd.PARAM_ERROR, "unable to find a system vm with id " + sysVmId); - } - - long jobId = getManagementServer().stopSystemVmAsync(sysVmId.longValue()); - if(jobId == 0) { - s_logger.warn("Unable to schedule async-job for StopSystemVM comamnd"); - } else { - if(s_logger.isDebugEnabled()) - s_logger.debug("StopSystemVM command has been accepted, job id: " + jobId); - } - - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); - return returnValues; - } +// public List> execute(Map params) { +// Long sysVmId = (Long)params.get(BaseCmd.Properties.ID.getName()); +// +// // verify parameters +// VMInstanceVO systemVM = getManagementServer().findSystemVMById(sysVmId); +// if (systemVM == null) { +// throw new ServerApiException (BaseCmd.PARAM_ERROR, "unable to find a system vm with id " + sysVmId); +// } +// +// long jobId = getManagementServer().stopSystemVmAsync(sysVmId.longValue()); +// if(jobId == 0) { +// s_logger.warn("Unable to schedule async-job for StopSystemVM comamnd"); +// } else { +// if(s_logger.isDebugEnabled()) +// s_logger.debug("StopSystemVM command has been accepted, job id: " + jobId); +// } +// +// List> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); +// return returnValues; +// } + + @Override + public String getResponse() { + // TODO Construct the response object (refer executor) + return null; + } } diff --git a/server/src/com/cloud/api/commands/StopVMCmd.java b/server/src/com/cloud/api/commands/StopVMCmd.java index 5760d8da9ad..145384f05ab 100644 --- a/server/src/com/cloud/api/commands/StopVMCmd.java +++ b/server/src/com/cloud/api/commands/StopVMCmd.java @@ -18,30 +18,18 @@ package com.cloud.api.commands; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - import org.apache.log4j.Logger; -import com.cloud.api.BaseCmd; +import com.cloud.api.BaseAsyncCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; -import com.cloud.user.Account; -import com.cloud.utils.Pair; -import com.cloud.vm.UserVmVO; - -public class StopVMCmd extends BaseCmd { +import com.cloud.api.BaseCmd.Manager; + +@Implementation(method="stopVirtualMachine", manager=Manager.UserVmManager) +public class StopVMCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(StopVMCmd.class.getName()); private static final String s_name = "stopvirtualmachineresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -66,46 +54,48 @@ public class StopVMCmd extends BaseCmd { return s_name; } - public List> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map params) { - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName()); - Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); - - // Verify input parameters - UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue()); - if (vmInstance == null) { - throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); - } - - if (account != null) { - if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstance.getAccountId())) { - throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId + " for this account"); - } else if (!getManagementServer().isChildDomain(account.getDomainId(), vmInstance.getDomainId())) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to sop virtual machine."); - } - } - - // If command is executed via 8096 port, set userId to the id of System account (1) - if (userId == null) { - userId = Long.valueOf(1); - } - - long jobId = getManagementServer().stopVirtualMachineAsync(userId.longValue(), vmId.longValue()); - if (jobId == 0) { - s_logger.warn("Unable to schedule async-job for StopVM comamnd"); - } else { - if(s_logger.isDebugEnabled()) - s_logger.debug("StopVM command has been accepted, job id: " + jobId); - } - - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); - - return returnValues; - } +// @Override +// public List> execute(Map params) { +// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); +// Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName()); +// Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); +// +// // Verify input parameters +// UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue()); +// if (vmInstance == null) { +// throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); +// } +// +// if (account != null) { +// if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstance.getAccountId())) { +// throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId + " for this account"); +// } else if (!getManagementServer().isChildDomain(account.getDomainId(), vmInstance.getDomainId())) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to sop virtual machine."); +// } +// } +// +// // If command is executed via 8096 port, set userId to the id of System account (1) +// if (userId == null) { +// userId = Long.valueOf(1); +// } +// +// long jobId = getManagementServer().stopVirtualMachineAsync(userId.longValue(), vmId.longValue()); +// if (jobId == 0) { +// s_logger.warn("Unable to schedule async-job for StopVM comamnd"); +// } else { +// if(s_logger.isDebugEnabled()) +// s_logger.debug("StopVM command has been accepted, job id: " + jobId); +// } +// +// List> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); +// +// return returnValues; +// } + + @Override + public String getResponse() { + // TODO Auto-generated method stub + return null; + } } diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java index 787a295aef1..858928a2d4e 100644 --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java @@ -1965,7 +1965,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, return stop(proxy, startEventId); } catch (AgentUnavailableException e) { if (s_logger.isDebugEnabled()) - s_logger.debug("Stopping console proxy " + proxy.getName() + " faled : exception " + e.toString()); + s_logger.debug("Stopping console proxy " + proxy.getName() + " failed : exception " + e.toString()); return false; } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index ec669fd2844..56c1f29ae05 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -29,6 +29,7 @@ import com.cloud.api.commands.CreateDomainCmd; import com.cloud.api.commands.EnableAccountCmd; import com.cloud.api.commands.EnableUserCmd; import com.cloud.api.commands.GetCloudIdentifierCmd; +import com.cloud.api.commands.StopSystemVmCmd; import com.cloud.api.commands.UpdateAccountCmd; import com.cloud.api.commands.UpdateDomainCmd; import com.cloud.api.commands.UpdateTemplateCmd; @@ -1460,7 +1461,7 @@ public interface ManagementServer { String getConsoleAccessUrlRoot(long vmId); ConsoleProxyVO findConsoleProxyById(long instanceId); VMInstanceVO findSystemVMById(long instanceId); - boolean stopSystemVM(long instanceId, long startEventId); + boolean stopSystemVM(StopSystemVmCmd cmd); VMInstanceVO startSystemVM(long instanceId, long startEventId) throws InternalErrorException; long startSystemVmAsync(long instanceId); long stopSystemVmAsync(long instanceId); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 189769fb60e..5c1d8226f44 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -81,6 +81,7 @@ import com.cloud.api.commands.ReconnectHostCmd; import com.cloud.api.commands.StartRouterCmd; import com.cloud.api.commands.StartSystemVMCmd; import com.cloud.api.commands.StartVMCmd; +import com.cloud.api.commands.StopSystemVmCmd; import com.cloud.api.commands.UpdateAccountCmd; import com.cloud.api.commands.UpdateDomainCmd; import com.cloud.api.commands.UpdateIsoPermissionsCmd; @@ -7934,12 +7935,22 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public boolean stopSystemVM(long instanceId, long startEventId) { - VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(instanceId, VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm); + public boolean stopSystemVM(StopSystemVmCmd cmd) { + Long id = cmd.getId(); + + // verify parameters + VMInstanceVO systemVM = findSystemVMById(id); + if (systemVM == null) { + throw new ServerApiException (BaseCmd.PARAM_ERROR, "unable to find a system vm with id " + id); + } + + VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(id, VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm); if (systemVm.getType().equals(VirtualMachine.Type.ConsoleProxy)){ - return stopConsoleProxy(instanceId, startEventId); + long eventId = EventUtils.saveScheduledEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, EventTypes.EVENT_PROXY_STOP, "stopping console proxy with Id: "+id); + return stopConsoleProxy(id, eventId); } else { - return stopSecondaryStorageVm(instanceId, startEventId); + long eventId = EventUtils.saveScheduledEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, EventTypes.EVENT_SSVM_STOP, "stopping secondary storage Vm Id: "+id); + return stopSecondaryStorageVm(id, eventId); } } diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index e5397942baf..65ad99e69c6 100644 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -61,6 +61,7 @@ import com.cloud.alert.AlertManager; import com.cloud.api.BaseCmd; import com.cloud.api.ServerApiException; import com.cloud.api.commands.DeletePoolCmd; +import com.cloud.api.commands.StopVMCmd; import com.cloud.api.commands.UpdateStoragePoolCmd; import com.cloud.async.AsyncInstanceCreateStatus; import com.cloud.async.AsyncJobExecutor; @@ -1949,8 +1950,10 @@ public class StorageManagerImpl implements StorageManager { //if the instance is of type uservm, call the user vm manager if(vmInstance.getType().equals(VirtualMachine.Type.User)) { + //create a dummy event + long eventId = saveScheduledEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, EventTypes.EVENT_VM_STOP, "stopping user vm with Id: "+vmInstance.getId()); - if(!_userVmMgr.stopVirtualMachine(userId, vmInstance.getId())) + if(!_userVmMgr.stopVirtualMachine(userId, vmInstance.getId(),eventId)) { s_logger.warn("There was an error stopping the user vm id: "+vmInstance.getId()+" ,cannot enable storage maintenance"); primaryStorage.setStatus(Status.ErrorInMaintenance); diff --git a/server/src/com/cloud/vm/UserVmManager.java b/server/src/com/cloud/vm/UserVmManager.java index 25de24fbb42..7bb5c8d2cdd 100644 --- a/server/src/com/cloud/vm/UserVmManager.java +++ b/server/src/com/cloud/vm/UserVmManager.java @@ -22,6 +22,8 @@ import java.util.List; import com.cloud.agent.api.VmStatsEntry; import com.cloud.api.ServerApiException; +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.DestroyVMExecutor; @@ -154,14 +156,17 @@ 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 * @param userId the id of the user performing the action * @param vmId + * @param eventId -- id of the scheduled event for stopping vm * @return true if stopped; false if problems. */ - boolean stopVirtualMachine(long userId, long vmId); + boolean stopVirtualMachine(long userId, long vmId, long eventId); + boolean stopVirtualMachine(StopVMCmd cmd) throws ServerApiException; OperationResponse executeStopVM(StopVMExecutor executor, VMOperationParam param); void completeStopCommand(long userId, UserVmVO vm, Event e, long startEventId); diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index ce3acdd57d2..ddccd15f0a3 100644 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -66,6 +66,8 @@ import com.cloud.agent.manager.AgentManager; import com.cloud.alert.AlertManager; import com.cloud.api.BaseCmd; import com.cloud.api.ServerApiException; +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.AsyncJobExecutor; @@ -914,8 +916,9 @@ public class UserVmManagerImpl implements UserVmManager { } @Override - public boolean stopVirtualMachine(long userId, long vmId) { - if (s_logger.isDebugEnabled()) { + public boolean stopVirtualMachine(long userId, long vmId, long eventId) { + boolean status = false; + if (s_logger.isDebugEnabled()) { s_logger.debug("Stopping vm=" + vmId); } @@ -926,8 +929,21 @@ public class UserVmManagerImpl implements UserVmManager { } return true; } + + EventUtils.saveStartedEvent(userId, vm.getAccountId(), EventTypes.EVENT_VM_STOP, "stopping Vm with Id: "+vmId, eventId); - return stop(userId, vm, 0); + status = stop(userId, vm, 0); + + if(status) + { + EventUtils.saveEvent(userId, vm.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_STOP, "Successfully stopped VM instance : " + vmId); + return status; + } + else + { + EventUtils.saveEvent(userId, vm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_STOP, "Error stopping VM instance : " + vmId); + return status; + } } @Override @@ -3121,4 +3137,54 @@ public class UserVmManagerImpl implements UserVmManager { EventUtils.saveEvent(userId, accountId, EventVO.LEVEL_INFO, type, description, null); } } + + @Override + public boolean stopVirtualMachine(StopVMCmd cmd) throws ServerApiException{ + + //Input validation + Account account = (Account)UserContext.current().getAccountObject(); + Long userId = UserContext.current().getUserId(); + Long id = cmd.getId(); + + //if account is removed, return error + if(account!=null && account.getRemoved() != null) + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); + + UserVmVO vmInstance = _userVmDao.findById(id.longValue()); + if (vmInstance == null) { + throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + id); + } + + long eventId = EventUtils.saveScheduledEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_VM_STOP, "stopping Vm with Id: "+id); + + userId = accountAndUserValidation(id, account, userId, vmInstance); + + return stopVirtualMachine(userId, id, eventId); + } + + @Override + public UserVmVO startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, ExecutionException, ConcurrentOperationException { + //Input validation + Account account = (Account)UserContext.current().getAccountObject(); + Long userId = UserContext.current().getUserId(); + Long id = cmd.getId(); + + //if account is removed, return error + if(account!=null && account.getRemoved() != null) + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); + + UserVmVO vmInstance = _userVmDao.findById(id.longValue()); + if (vmInstance == null) { + throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + id); + } + + long eventId = EventUtils.saveScheduledEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_VM_START, "Starting Vm with Id: "+id); + + userId = accountAndUserValidation(id, account, userId, vmInstance); + + return startVirtualMachine(userId, id, null, null, eventId); + + } + + }