|
16 | 16 | // under the License. |
17 | 17 | package org.apache.cloudstack.engine.cloud.entity.api; |
18 | 18 |
|
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.UUID; |
| 23 | + |
| 24 | +import javax.inject.Inject; |
| 25 | + |
19 | 26 | import org.apache.cloudstack.engine.cloud.entity.api.db.VMEntityVO; |
| 27 | +import org.apache.cloudstack.engine.cloud.entity.api.db.VMReservationVO; |
| 28 | +import org.apache.cloudstack.engine.cloud.entity.api.db.dao.VMEntityDao; |
| 29 | +import org.apache.cloudstack.engine.cloud.entity.api.db.dao.VMReservationDao; |
20 | 30 | import org.springframework.stereotype.Component; |
21 | 31 |
|
| 32 | +import com.cloud.dc.DataCenter; |
| 33 | +import com.cloud.deploy.DataCenterDeployment; |
| 34 | +import com.cloud.deploy.DeployDestination; |
| 35 | +import com.cloud.deploy.DeploymentPlan; |
| 36 | +import com.cloud.deploy.DeploymentPlanner; |
| 37 | +import com.cloud.deploy.DeploymentPlanner.ExcludeList; |
| 38 | +import com.cloud.exception.AgentUnavailableException; |
| 39 | +import com.cloud.exception.ConcurrentOperationException; |
| 40 | +import com.cloud.exception.InsufficientCapacityException; |
| 41 | +import com.cloud.exception.InsufficientServerCapacityException; |
| 42 | +import com.cloud.exception.OperationTimedoutException; |
| 43 | +import com.cloud.exception.ResourceUnavailableException; |
| 44 | +import com.cloud.network.dao.NetworkDao; |
| 45 | +import com.cloud.org.Cluster; |
| 46 | +import com.cloud.service.dao.ServiceOfferingDao; |
| 47 | +import com.cloud.storage.StoragePoolVO; |
| 48 | +import com.cloud.storage.Volume; |
| 49 | +import com.cloud.storage.VolumeVO; |
| 50 | +import com.cloud.storage.dao.DiskOfferingDao; |
| 51 | +import com.cloud.storage.dao.StoragePoolDao; |
| 52 | +import com.cloud.storage.dao.VMTemplateDao; |
| 53 | +import com.cloud.storage.dao.VolumeDao; |
| 54 | +import com.cloud.user.Account; |
| 55 | +import com.cloud.user.User; |
| 56 | +import com.cloud.user.dao.AccountDao; |
| 57 | +import com.cloud.user.dao.UserDao; |
| 58 | +import com.cloud.utils.component.ComponentContext; |
| 59 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 60 | +import com.cloud.vm.VMInstanceVO; |
| 61 | +import com.cloud.vm.VirtualMachineManager; |
| 62 | +import com.cloud.vm.VirtualMachineProfile; |
| 63 | +import com.cloud.vm.VirtualMachineProfileImpl; |
| 64 | +import com.cloud.vm.dao.VMInstanceDao; |
| 65 | + |
22 | 66 | @Component |
23 | 67 | public class VMEntityManagerImpl implements VMEntityManager { |
24 | 68 |
|
| 69 | + @Inject |
| 70 | + protected VMInstanceDao _vmDao; |
| 71 | + @Inject |
| 72 | + protected VMTemplateDao _templateDao = null; |
| 73 | + |
| 74 | + @Inject |
| 75 | + protected ServiceOfferingDao _serviceOfferingDao; |
| 76 | + |
| 77 | + @Inject |
| 78 | + protected DiskOfferingDao _diskOfferingDao = null; |
| 79 | + |
| 80 | + @Inject |
| 81 | + protected NetworkDao _networkDao; |
| 82 | + |
| 83 | + @Inject |
| 84 | + protected AccountDao _accountDao = null; |
| 85 | + |
| 86 | + @Inject |
| 87 | + protected UserDao _userDao = null; |
| 88 | + |
| 89 | + @Inject |
| 90 | + protected VMEntityDao _vmEntityDao; |
| 91 | + |
| 92 | + @Inject |
| 93 | + protected VMReservationDao _reservationDao; |
| 94 | + |
| 95 | + @Inject |
| 96 | + protected VirtualMachineManager _itMgr; |
| 97 | + |
| 98 | + @Inject |
| 99 | + protected List<DeploymentPlanner> _planners; |
| 100 | + |
| 101 | + @Inject |
| 102 | + protected VolumeDao _volsDao; |
| 103 | + |
| 104 | + @Inject |
| 105 | + protected StoragePoolDao _storagePoolDao; |
| 106 | + |
25 | 107 | @Override |
26 | 108 | public VMEntityVO loadVirtualMachine(String vmId) { |
27 | 109 | // TODO Auto-generated method stub |
28 | 110 | return null; |
29 | 111 | } |
30 | 112 |
|
31 | 113 | @Override |
32 | | - public void saveVirtualMachine(VMEntityVO vmInstanceVO) { |
33 | | - // TODO Auto-generated method stub |
| 114 | + public void saveVirtualMachine(VMEntityVO entity) { |
| 115 | + _vmEntityDao.persist(entity); |
34 | 116 |
|
35 | 117 | } |
36 | 118 |
|
| 119 | + @Override |
| 120 | + public String reserveVirtualMachine(VMEntityVO vmEntityVO, String plannerToUse, DeploymentPlan planToDeploy, ExcludeList exclude) |
| 121 | + throws InsufficientCapacityException, ResourceUnavailableException { |
| 122 | + |
| 123 | + //call planner and get the deployDestination. |
| 124 | + //load vm instance and offerings and call virtualMachineManagerImpl |
| 125 | + //FIXME: profile should work on VirtualMachineEntity |
| 126 | + VMInstanceVO vm = _vmDao.findByUuid(vmEntityVO.getUuid()); |
| 127 | + VirtualMachineProfileImpl<VMInstanceVO> vmProfile = new VirtualMachineProfileImpl<VMInstanceVO>(vm); |
| 128 | + DeploymentPlan plan = planToDeploy; |
| 129 | + |
| 130 | + List<VolumeVO> vols = _volsDao.findReadyRootVolumesByInstance(vm.getId()); |
| 131 | + if(!vols.isEmpty()){ |
| 132 | + VolumeVO vol = vols.get(0); |
| 133 | + StoragePoolVO pool = _storagePoolDao.findById(vol.getPoolId()); |
| 134 | + if (!pool.isInMaintenance()) { |
| 135 | + long rootVolDcId = pool.getDataCenterId(); |
| 136 | + Long rootVolPodId = pool.getPodId(); |
| 137 | + Long rootVolClusterId = pool.getClusterId(); |
| 138 | + if (planToDeploy != null && planToDeploy.getDataCenterId() != 0) { |
| 139 | + Long clusterIdSpecified = planToDeploy.getClusterId(); |
| 140 | + if (clusterIdSpecified != null && rootVolClusterId != null) { |
| 141 | + if (rootVolClusterId.longValue() != clusterIdSpecified.longValue()) { |
| 142 | + // cannot satisfy the plan passed in to the |
| 143 | + // planner |
| 144 | + throw new ResourceUnavailableException("Root volume is ready in different cluster, Deployment plan provided cannot be satisfied, unable to create a deployment for " |
| 145 | + + vm, Cluster.class, clusterIdSpecified); |
| 146 | + } |
| 147 | + } |
| 148 | + plan = new DataCenterDeployment(planToDeploy.getDataCenterId(), planToDeploy.getPodId(), planToDeploy.getClusterId(), planToDeploy.getHostId(), vol.getPoolId(), null, null); |
| 149 | + }else{ |
| 150 | + plan = new DataCenterDeployment(rootVolDcId, rootVolPodId, rootVolClusterId, null, vol.getPoolId(), null, null); |
| 151 | + |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + DeploymentPlanner planner = ComponentContext.getCompanent(plannerToUse); |
| 158 | + DeployDestination dest = null; |
| 159 | + |
| 160 | + if (planner.canHandle(vmProfile, plan, exclude)) { |
| 161 | + dest = planner.plan(vmProfile, plan, exclude); |
| 162 | + } |
| 163 | + |
| 164 | + if (dest != null) { |
| 165 | + //save destination with VMEntityVO |
| 166 | + VMReservationVO vmReservation = new VMReservationVO(vm.getId(), dest.getDataCenter().getId(), dest.getPod().getId(), dest.getCluster().getId(), dest.getHost().getId()); |
| 167 | + Map<Long,Long> volumeReservationMap = new HashMap<Long,Long>(); |
| 168 | + for(Volume vo : dest.getStorageForDisks().keySet()){ |
| 169 | + volumeReservationMap.put(vo.getId(), dest.getStorageForDisks().get(vo).getId()); |
| 170 | + } |
| 171 | + vmReservation.setVolumeReservation(volumeReservationMap); |
| 172 | + |
| 173 | + vmEntityVO.setVmReservation(vmReservation); |
| 174 | + _vmEntityDao.persist(vmEntityVO); |
| 175 | + |
| 176 | + return vmReservation.getUuid(); |
| 177 | + }else{ |
| 178 | + throw new InsufficientServerCapacityException("Unable to create a deployment for " + vmProfile, DataCenter.class, plan.getDataCenterId()); |
| 179 | + } |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void deployVirtualMachine(String reservationId, String caller) throws InsufficientCapacityException, ResourceUnavailableException{ |
| 185 | + //grab the VM Id and destination using the reservationId. |
| 186 | + |
| 187 | + VMReservationVO vmReservation = _reservationDao.findByReservationId(reservationId); |
| 188 | + long vmId = vmReservation.getVmId(); |
| 189 | + |
| 190 | + VMInstanceVO vm = _vmDao.findById(vmId); |
| 191 | + //Pass it down |
| 192 | + Long poolId = null; |
| 193 | + Map<Long,Long> storage = vmReservation.getVolumeReservation(); |
| 194 | + if(storage != null){ |
| 195 | + Long[] array = new Long[storage.keySet().size()]; |
| 196 | + storage.keySet().toArray(array); |
| 197 | + poolId = array[0]; |
| 198 | + } |
| 199 | + |
| 200 | + DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterIdToDeployIn(), vmReservation.getPodId(), vmReservation.getClusterId(), |
| 201 | + vmReservation.getHostId(), poolId , null); |
| 202 | + |
| 203 | + VMInstanceVO vmDeployed = _itMgr.start(vm, null, _userDao.findById(new Long(caller)), _accountDao.findById(vm.getAccountId()), plan); |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public boolean stopvirtualmachine(VMEntityVO vmEntityVO, String caller) throws ResourceUnavailableException { |
| 209 | + |
| 210 | + VMInstanceVO vm = _vmDao.findByUuid(vmEntityVO.getUuid()); |
| 211 | + return _itMgr.stop(vm, _userDao.findById(new Long(caller)), _accountDao.findById(vm.getAccountId())); |
| 212 | + |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public boolean destroyVirtualMachine(VMEntityVO vmEntityVO, String caller) throws AgentUnavailableException, OperationTimedoutException, ConcurrentOperationException{ |
| 217 | + |
| 218 | + VMInstanceVO vm = _vmDao.findByUuid(vmEntityVO.getUuid()); |
| 219 | + return _itMgr.destroy(vm, _userDao.findById(new Long(caller)), _accountDao.findById(vm.getAccountId())); |
| 220 | + |
| 221 | + |
| 222 | + } |
| 223 | + |
37 | 224 | } |
0 commit comments