Some more refactoring

This commit is contained in:
abhishek 2010-08-17 10:07:57 -07:00
parent b0cfdf1f37
commit 3870d8f1dc
2 changed files with 134 additions and 133 deletions

View File

@ -1,133 +1,134 @@
/** //TODO -- This will be removed
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved. ///**
* // * Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
* This software is licensed under the GNU General Public License v3 or later. // *
* // * 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 // * It is free software: you can redistribute it and/or modify
* the Free Software Foundation, either version 3 of the License, or any later version. // * it under the terms of the GNU General Public License as published by
* This program is distributed in the hope that it will be useful, // * the Free Software Foundation, either version 3 of the License, or any later version.
* but WITHOUT ANY WARRANTY; without even the implied warranty of // * This program is distributed in the hope that it will be useful,
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // * but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details. // * 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 <http://www.gnu.org/licenses/>. // * You should have received a copy of the GNU General Public License
* // * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ // *
// */
package com.cloud.async.executor; //
//package com.cloud.async.executor;
import org.apache.log4j.Logger; //
//import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd; //
import com.cloud.async.AsyncJobManager; //import com.cloud.api.BaseCmd;
import com.cloud.async.AsyncJobResult; //import com.cloud.async.AsyncJobManager;
import com.cloud.async.AsyncJobVO; //import com.cloud.async.AsyncJobResult;
import com.cloud.async.BaseAsyncJobExecutor; //import com.cloud.async.AsyncJobVO;
import com.cloud.serializer.GsonHelper; //import com.cloud.async.BaseAsyncJobExecutor;
import com.cloud.server.ManagementServer; //import com.cloud.serializer.GsonHelper;
import com.cloud.service.ServiceOfferingVO; //import com.cloud.server.ManagementServer;
import com.cloud.storage.VMTemplateVO; //import com.cloud.service.ServiceOfferingVO;
import com.cloud.user.Account; //import com.cloud.storage.VMTemplateVO;
import com.cloud.uservm.UserVm; //import com.cloud.user.Account;
import com.cloud.vm.UserVmVO; //import com.cloud.uservm.UserVm;
import com.cloud.vm.VMInstanceVO; //import com.cloud.vm.UserVmVO;
import com.cloud.vm.VmStats; //import com.cloud.vm.VMInstanceVO;
import com.google.gson.Gson; //import com.cloud.vm.VmStats;
//import com.google.gson.Gson;
public class UpgradeVMExecutor extends BaseAsyncJobExecutor { //
public static final Logger s_logger = Logger.getLogger(UpgradeVMExecutor.class.getName()); //public class UpgradeVMExecutor extends BaseAsyncJobExecutor {
// public static final Logger s_logger = Logger.getLogger(UpgradeVMExecutor.class.getName());
public boolean execute() { //
AsyncJobManager asyncMgr = getAsyncJobMgr(); // public boolean execute() {
AsyncJobVO job = getJob(); // AsyncJobManager asyncMgr = getAsyncJobMgr();
Gson gson = GsonHelper.getBuilder().create(); // AsyncJobVO job = getJob();
// Gson gson = GsonHelper.getBuilder().create();
if(getSyncSource() == null) { //
VMOperationParam param = gson.fromJson(job.getCmdInfo(), VMOperationParam.class); // if(getSyncSource() == null) {
asyncMgr.syncAsyncJobExecution(job.getId(), "UserVM", param.getVmId()); // VMOperationParam param = gson.fromJson(job.getCmdInfo(), VMOperationParam.class);
// asyncMgr.syncAsyncJobExecution(job.getId(), "UserVM", param.getVmId());
// always true if it does not have sync-source //
return true; // // always true if it does not have sync-source
} else { // return true;
ManagementServer managementServer = asyncMgr.getExecutorContext().getManagementServer(); // } else {
UpgradeVMParam param = gson.fromJson(job.getCmdInfo(), UpgradeVMParam.class); // ManagementServer managementServer = asyncMgr.getExecutorContext().getManagementServer();
// UpgradeVMParam param = gson.fromJson(job.getCmdInfo(), UpgradeVMParam.class);
try { //
asyncMgr.updateAsyncJobAttachment(job.getId(), "vm_instance", param.getVmId()); // try {
boolean success = managementServer.upgradeVirtualMachine(param.getUserId(), // asyncMgr.updateAsyncJobAttachment(job.getId(), "vm_instance", param.getVmId());
param.getVmId(), param.getServiceOfferingId(), param.getEventId()); // boolean success = managementServer.upgradeVirtualMachine(param.getUserId(),
// param.getVmId(), param.getServiceOfferingId(), param.getEventId());
if (success) { //
//get the upgraded vm to compose the result object // if (success) {
UserVmVO userVm = managementServer.findUserVMInstanceById(param.getVmId()); // //get the upgraded vm to compose the result object
asyncMgr.completeAsyncJob(getJob().getId(), AsyncJobResult.STATUS_SUCCEEDED, 0, // UserVmVO userVm = managementServer.findUserVMInstanceById(param.getVmId());
composeResultObject(userVm, managementServer)); // asyncMgr.completeAsyncJob(getJob().getId(), AsyncJobResult.STATUS_SUCCEEDED, 0,
// composeResultObject(userVm, managementServer));
} else { //
asyncMgr.completeAsyncJob(getJob().getId(), AsyncJobResult.STATUS_FAILED, // } else {
BaseCmd.VM_CHANGE_SERVICE_ERROR, // asyncMgr.completeAsyncJob(getJob().getId(), AsyncJobResult.STATUS_FAILED,
composeResultObject(null, managementServer)); // BaseCmd.VM_CHANGE_SERVICE_ERROR,
} // composeResultObject(null, managementServer));
} catch(Exception e) { // }
s_logger.warn("Unable to upgrade VM " + param.getVmId() + ":" + e.getMessage(), e); // } catch(Exception e) {
asyncMgr.completeAsyncJob(getJob().getId(), AsyncJobResult.STATUS_FAILED, // s_logger.warn("Unable to upgrade VM " + param.getVmId() + ":" + e.getMessage(), e);
BaseCmd.INTERNAL_ERROR, // asyncMgr.completeAsyncJob(getJob().getId(), AsyncJobResult.STATUS_FAILED,
e.getMessage()); // BaseCmd.INTERNAL_ERROR,
} // e.getMessage());
return true; // }
} // return true;
} // }
// }
private VmResultObject composeResultObject(UserVmVO vm, ManagementServer ms) //
{ // private VmResultObject composeResultObject(UserVmVO vm, ManagementServer ms)
if(vm == null) // {
return null; // if(vm == null)
// return null;
VmResultObject resultObj = new VmResultObject(); //
Account acct = ms.findAccountById(Long.valueOf(vm.getAccountId())); // VmResultObject resultObj = new VmResultObject();
resultObj.setAccount(acct.getAccountName()); // Account acct = ms.findAccountById(Long.valueOf(vm.getAccountId()));
// resultObj.setAccount(acct.getAccountName());
ServiceOfferingVO offering = ms.findServiceOfferingById(vm.getServiceOfferingId()); //
resultObj.setCpuSpeed(offering.getSpeed()); // ServiceOfferingVO offering = ms.findServiceOfferingById(vm.getServiceOfferingId());
resultObj.setMemory(offering.getRamSize()); // resultObj.setCpuSpeed(offering.getSpeed());
if(offering.getDisplayText()!=null) // resultObj.setMemory(offering.getRamSize());
resultObj.setServiceOfferingName(offering.getDisplayText()); // if(offering.getDisplayText()!=null)
else // resultObj.setServiceOfferingName(offering.getDisplayText());
resultObj.setServiceOfferingName(offering.getName()); // else
resultObj.setServiceOfferingId(vm.getServiceOfferingId()); // resultObj.setServiceOfferingName(offering.getName());
// resultObj.setServiceOfferingId(vm.getServiceOfferingId());
VmStats vmStats = ms.getVmStatistics(vm.getId()); //
if(vmStats != null) // VmStats vmStats = ms.getVmStatistics(vm.getId());
{ // if(vmStats != null)
resultObj.setCpuUsed((long) vmStats.getCPUUtilization()); // {
resultObj.setNetworkKbsRead((long) vmStats.getNetworkReadKBs()); // resultObj.setCpuUsed((long) vmStats.getCPUUtilization());
resultObj.setNetworkKbsWrite((long) vmStats.getNetworkWriteKBs()); // resultObj.setNetworkKbsRead((long) vmStats.getNetworkReadKBs());
} // resultObj.setNetworkKbsWrite((long) vmStats.getNetworkWriteKBs());
// }
resultObj.setCreated(vm.getCreated()); //
resultObj.setDisplayName(vm.getDisplayName()); // resultObj.setCreated(vm.getCreated());
resultObj.setDomain(ms.findDomainIdById(acct.getDomainId()).getName()); // resultObj.setDisplayName(vm.getDisplayName());
resultObj.setDomainId(acct.getDomainId()); // resultObj.setDomain(ms.findDomainIdById(acct.getDomainId()).getName());
resultObj.setHaEnable(vm.isHaEnabled()); // resultObj.setDomainId(acct.getDomainId());
if(vm.getHostId() != null) // resultObj.setHaEnable(vm.isHaEnabled());
{ // if(vm.getHostId() != null)
resultObj.setHostId(vm.getHostId()); // {
resultObj.setHostName(ms.getHostBy(vm.getHostId()).getName()); // resultObj.setHostId(vm.getHostId());
} // resultObj.setHostName(ms.getHostBy(vm.getHostId()).getName());
resultObj.setIpAddress(vm.getPrivateIpAddress()); // }
resultObj.setName(vm.getName()); // resultObj.setIpAddress(vm.getPrivateIpAddress());
resultObj.setState(vm.getState().toString()); // resultObj.setName(vm.getName());
resultObj.setZoneId(vm.getDataCenterId()); // resultObj.setState(vm.getState().toString());
resultObj.setZoneName(ms.findDataCenterById(vm.getDataCenterId()).getName()); // resultObj.setZoneId(vm.getDataCenterId());
// resultObj.setZoneName(ms.findDataCenterById(vm.getDataCenterId()).getName());
VMTemplateVO template = ms.findTemplateById(vm.getTemplateId()); //
resultObj.setPasswordEnabled(template.getEnablePassword()); // VMTemplateVO template = ms.findTemplateById(vm.getTemplateId());
resultObj.setTemplateDisplayText(template.getDisplayText()); // resultObj.setPasswordEnabled(template.getEnablePassword());
resultObj.setTemplateId(template.getId()); // resultObj.setTemplateDisplayText(template.getDisplayText());
resultObj.setTemplateName(template.getName()); // resultObj.setTemplateId(template.getId());
// resultObj.setTemplateName(template.getName());
return resultObj; //
} // return resultObj;
} // }
//}

0
server/src/com/cloud/server/ManagementServerImpl.java Normal file → Executable file
View File