Refactoring StopSystemVmCmd

This commit is contained in:
abhishek 2010-08-18 14:55:19 -07:00
parent 716271fca1
commit c58749b917
4 changed files with 49 additions and 36 deletions

View File

@ -25,20 +25,19 @@ import java.util.Map;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd; import com.cloud.api.BaseCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter; import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException; import com.cloud.api.ServerApiException;
import com.cloud.api.BaseCmd.Manager;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VMInstanceVO;
@Implementation(method="stopSystemVM", manager=Manager.ManagementServer)
public class StopSystemVmCmd extends BaseCmd { public class StopSystemVmCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(StopSystemVmCmd.class.getName()); public static final Logger s_logger = Logger.getLogger(StopSystemVmCmd.class.getName());
private static final String s_name = "stopsystemvmresponse"; private static final String s_name = "stopsystemvmresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>(); private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.TRUE));
}
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
//////////////// API parameters ///////////////////// //////////////// API parameters /////////////////////
@ -63,29 +62,31 @@ public class StopSystemVmCmd extends BaseCmd {
return s_name; return s_name;
} }
public List<Pair<Enum, Boolean>> getProperties() { // public List<Pair<String, Object>> execute(Map<String, Object> params) {
return s_properties; // Long sysVmId = (Long)params.get(BaseCmd.Properties.ID.getName());
} //
// // verify parameters
public List<Pair<String, Object>> execute(Map<String, Object> params) { // VMInstanceVO systemVM = getManagementServer().findSystemVMById(sysVmId);
Long sysVmId = (Long)params.get(BaseCmd.Properties.ID.getName()); // if (systemVM == null) {
// throw new ServerApiException (BaseCmd.PARAM_ERROR, "unable to find a system vm with id " + sysVmId);
// verify parameters // }
VMInstanceVO systemVM = getManagementServer().findSystemVMById(sysVmId); //
if (systemVM == null) { // long jobId = getManagementServer().stopSystemVmAsync(sysVmId.longValue());
throw new ServerApiException (BaseCmd.PARAM_ERROR, "unable to find a system vm with id " + sysVmId); // if(jobId == 0) {
} // s_logger.warn("Unable to schedule async-job for StopSystemVM comamnd");
// } else {
long jobId = getManagementServer().stopSystemVmAsync(sysVmId.longValue()); // if(s_logger.isDebugEnabled())
if(jobId == 0) { // s_logger.debug("StopSystemVM command has been accepted, job id: " + jobId);
s_logger.warn("Unable to schedule async-job for StopSystemVM comamnd"); // }
} else { //
if(s_logger.isDebugEnabled()) // List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
s_logger.debug("StopSystemVM command has been accepted, job id: " + jobId); // returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
} // return returnValues;
// }
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); @Override
return returnValues; public String getResponse() {
} // TODO Construct the response object (refer executor)
return null;
}
} }

View File

@ -1965,7 +1965,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager,
return stop(proxy, startEventId); return stop(proxy, startEventId);
} catch (AgentUnavailableException e) { } catch (AgentUnavailableException e) {
if (s_logger.isDebugEnabled()) 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; return false;
} }
} }

View File

@ -29,6 +29,7 @@ import com.cloud.api.commands.CreateDomainCmd;
import com.cloud.api.commands.EnableAccountCmd; import com.cloud.api.commands.EnableAccountCmd;
import com.cloud.api.commands.EnableUserCmd; import com.cloud.api.commands.EnableUserCmd;
import com.cloud.api.commands.GetCloudIdentifierCmd; import com.cloud.api.commands.GetCloudIdentifierCmd;
import com.cloud.api.commands.StopSystemVmCmd;
import com.cloud.api.commands.UpdateAccountCmd; import com.cloud.api.commands.UpdateAccountCmd;
import com.cloud.api.commands.UpdateDomainCmd; import com.cloud.api.commands.UpdateDomainCmd;
import com.cloud.api.commands.UpdateTemplateCmd; import com.cloud.api.commands.UpdateTemplateCmd;
@ -1459,7 +1460,7 @@ public interface ManagementServer {
String getConsoleAccessUrlRoot(long vmId); String getConsoleAccessUrlRoot(long vmId);
ConsoleProxyVO findConsoleProxyById(long instanceId); ConsoleProxyVO findConsoleProxyById(long instanceId);
VMInstanceVO findSystemVMById(long instanceId); VMInstanceVO findSystemVMById(long instanceId);
boolean stopSystemVM(long instanceId, long startEventId); boolean stopSystemVM(StopSystemVmCmd cmd);
VMInstanceVO startSystemVM(long instanceId, long startEventId) throws InternalErrorException; VMInstanceVO startSystemVM(long instanceId, long startEventId) throws InternalErrorException;
long startSystemVmAsync(long instanceId); long startSystemVmAsync(long instanceId);
long stopSystemVmAsync(long instanceId); long stopSystemVmAsync(long instanceId);

View File

@ -81,6 +81,7 @@ import com.cloud.api.commands.ReconnectHostCmd;
import com.cloud.api.commands.StartRouterCmd; import com.cloud.api.commands.StartRouterCmd;
import com.cloud.api.commands.StartSystemVMCmd; import com.cloud.api.commands.StartSystemVMCmd;
import com.cloud.api.commands.StartVMCmd; import com.cloud.api.commands.StartVMCmd;
import com.cloud.api.commands.StopSystemVmCmd;
import com.cloud.api.commands.UpdateAccountCmd; import com.cloud.api.commands.UpdateAccountCmd;
import com.cloud.api.commands.UpdateDomainCmd; import com.cloud.api.commands.UpdateDomainCmd;
import com.cloud.api.commands.UpdateIsoPermissionsCmd; import com.cloud.api.commands.UpdateIsoPermissionsCmd;
@ -7939,12 +7940,22 @@ public class ManagementServerImpl implements ManagementServer {
} }
@Override @Override
public boolean stopSystemVM(long instanceId, long startEventId) { public boolean stopSystemVM(StopSystemVmCmd cmd) {
VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(instanceId, VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm); 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)){ 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 { } 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);
} }
} }