Skip to content

Commit 4ba359c

Browse files
author
Alex Huang
committed
Moved VirtualMachineManager into engine
1 parent 9064acb commit 4ba359c

26 files changed

Lines changed: 250 additions & 202 deletions

File tree

api/src/com/cloud/agent/api/Command.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
*/
2828
public abstract class Command {
2929

30+
public static enum OnError {
31+
Continue, Stop
32+
}
33+
3034
public static final String HYPERVISOR_TYPE = "hypervisorType";
3135

3236
// allow command to carry over hypervisor or other environment related context info

core/src/com/cloud/exception/OperationTimedoutException.java renamed to api/src/com/cloud/exception/OperationTimedoutException.java

File renamed without changes.

server/src/com/cloud/vm/VirtualMachineGuru.java renamed to engine/api/src/com/cloud/vm/VirtualMachineGuru.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// under the License.
1717
package com.cloud.vm;
1818

19-
import com.cloud.agent.api.StopAnswer;
19+
import com.cloud.agent.api.Answer;
2020
import com.cloud.agent.manager.Commands;
2121
import com.cloud.deploy.DeployDestination;
2222
import com.cloud.exception.ResourceUnavailableException;
@@ -49,7 +49,7 @@ public interface VirtualMachineGuru {
4949

5050
boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile profile);
5151

52-
void finalizeStop(VirtualMachineProfile profile, StopAnswer answer);
52+
void finalizeStop(VirtualMachineProfile profile, Answer answer);
5353

5454
void finalizeExpunge(VirtualMachine vm);
5555

server/src/com/cloud/vm/VirtualMachineManager.java renamed to engine/api/src/com/cloud/vm/VirtualMachineManager.java

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.cloud.vm;
1818

1919
import java.net.URI;
20-
import java.util.List;
20+
import java.util.LinkedHashMap;
2121
import java.util.Map;
2222

2323
import com.cloud.agent.api.to.NicTO;
@@ -33,13 +33,11 @@
3333
import com.cloud.exception.ResourceUnavailableException;
3434
import com.cloud.hypervisor.Hypervisor.HypervisorType;
3535
import com.cloud.network.Network;
36-
import com.cloud.network.dao.NetworkVO;
36+
import com.cloud.offering.DiskOffering;
3737
import com.cloud.offering.ServiceOffering;
38-
import com.cloud.service.ServiceOfferingVO;
39-
import com.cloud.storage.DiskOfferingVO;
4038
import com.cloud.storage.StoragePool;
41-
import com.cloud.storage.VMTemplateVO;
4239
import com.cloud.storage.Volume;
40+
import com.cloud.template.VirtualMachineTemplate;
4341
import com.cloud.utils.Pair;
4442
import com.cloud.utils.component.Manager;
4543
import com.cloud.utils.fsm.NoTransitionException;
@@ -49,22 +47,39 @@
4947
*/
5048
public interface VirtualMachineManager extends Manager {
5149

50+
/**
51+
* Allocates a new virtual machine instance in the CloudStack DB. This
52+
* orchestrates the creation of all virtual resources needed in CloudStack
53+
* DB to bring up a VM.
54+
*
55+
* @param vmInstanceName Instance name of the VM. This name uniquely
56+
* a VM in CloudStack's deploy environment. The caller gets to
57+
* define this VM but it must be unqiue for all of CloudStack.
58+
* @param template The template this VM is based on.
59+
* @param serviceOffering The service offering that specifies the offering this VM should provide.
60+
* @param defaultNetwork The default network for the VM.
61+
* @param rootDiskOffering For created VMs not based on templates, root disk offering specifies the root disk.
62+
* @param dataDiskOfferings Data disks to attach to the VM.
63+
* @param auxiliaryNetworks additional networks to attach the VMs to.
64+
* @param plan How to deploy the VM.
65+
* @param hyperType Hypervisor type
66+
* @throws InsufficientCapacityException If there are insufficient capacity to deploy this vm.
67+
*/
5268
void allocate(String vmInstanceName,
53-
VMTemplateVO template,
54-
ServiceOfferingVO serviceOffering,
55-
Pair<? extends DiskOfferingVO, Long> rootDiskOffering,
56-
List<Pair<DiskOfferingVO, Long>> dataDiskOfferings,
57-
List<Pair<NetworkVO, NicProfile>> networks,
58-
Map<VirtualMachineProfile.Param, Object> params,
59-
DeploymentPlan plan,
60-
HypervisorType hyperType) throws InsufficientCapacityException;
69+
VirtualMachineTemplate template,
70+
ServiceOffering serviceOffering,
71+
Pair<? extends DiskOffering, Long> rootDiskOffering,
72+
LinkedHashMap<? extends DiskOffering, Long> dataDiskOfferings,
73+
LinkedHashMap<? extends Network, ? extends NicProfile> auxiliaryNetworks,
74+
DeploymentPlan plan,
75+
HypervisorType hyperType) throws InsufficientCapacityException;
6176

6277
void allocate(String vmInstanceName,
63-
VMTemplateVO template,
64-
ServiceOfferingVO serviceOffering,
65-
List<Pair<NetworkVO, NicProfile>> networkProfiles,
66-
DeploymentPlan plan,
67-
HypervisorType hyperType) throws InsufficientCapacityException;
78+
VirtualMachineTemplate template,
79+
ServiceOffering serviceOffering,
80+
LinkedHashMap<? extends Network, ? extends NicProfile> networkProfiles,
81+
DeploymentPlan plan,
82+
HypervisorType hyperType) throws InsufficientCapacityException;
6883

6984
void start(String vmUuid, Map<VirtualMachineProfile.Param, Object> params);
7085

@@ -76,7 +91,7 @@ void allocate(String vmInstanceName,
7691

7792
void registerGuru(VirtualMachine.Type type, VirtualMachineGuru guru);
7893

79-
boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long hostId) throws NoTransitionException;
94+
boolean stateTransitTo(VirtualMachine vm, VirtualMachine.Event e, Long hostId) throws NoTransitionException;
8095

8196
void advanceStart(String vmUuid, Map<VirtualMachineProfile.Param, Object> params) throws InsufficientCapacityException, ResourceUnavailableException,
8297
ConcurrentOperationException, OperationTimedoutException;
@@ -146,7 +161,7 @@ NicProfile addVmToNetwork(VirtualMachine vm, Network network, NicProfile request
146161
* @throws ResourceUnavailableException
147162
* @throws ConcurrentOperationException
148163
*/
149-
boolean removeNicFromVm(VirtualMachine vm, NicVO nic) throws ConcurrentOperationException, ResourceUnavailableException;
164+
boolean removeNicFromVm(VirtualMachine vm, Nic nic) throws ConcurrentOperationException, ResourceUnavailableException;
150165

151166
/**
152167
* @param vm
@@ -173,7 +188,7 @@ NicProfile addVmToNetwork(VirtualMachine vm, Network network, NicProfile request
173188
VirtualMachineTO toVmTO(VirtualMachineProfile profile);
174189

175190

176-
VMInstanceVO reConfigureVm(VMInstanceVO vm, ServiceOffering newServiceOffering, boolean sameHost) throws ResourceUnavailableException, ConcurrentOperationException;
191+
VirtualMachine reConfigureVm(String vmUuid, ServiceOffering newServiceOffering, boolean sameHost) throws ResourceUnavailableException, ConcurrentOperationException;
177192

178193
void findHostAndMigrate(String vmUuid, Long newSvcOfferingId, DeploymentPlanner.ExcludeList excludeHostList) throws InsufficientCapacityException,
179194
ConcurrentOperationException, ResourceUnavailableException;

engine/orchestration/src/org/apache/cloudstack/engine/orchestration/CloudOrchestrator.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.net.URL;
2222
import java.util.ArrayList;
23+
import java.util.LinkedHashMap;
2324
import java.util.List;
2425
import java.util.Map;
2526

@@ -41,8 +42,10 @@
4142
import com.cloud.exception.InvalidParameterValueException;
4243
import com.cloud.hypervisor.Hypervisor;
4344
import com.cloud.hypervisor.Hypervisor.HypervisorType;
45+
import com.cloud.network.Network;
4446
import com.cloud.network.dao.NetworkDao;
4547
import com.cloud.network.dao.NetworkVO;
48+
import com.cloud.offering.DiskOffering;
4649
import com.cloud.service.ServiceOfferingVO;
4750
import com.cloud.service.dao.ServiceOfferingDao;
4851
import com.cloud.storage.DiskOfferingVO;
@@ -165,11 +168,11 @@ public VirtualMachineEntity createVirtualMachine(
165168

166169
// VirtualMachineEntityImpl vmEntity = new VirtualMachineEntityImpl(id, owner, hostName, displayName, cpu, speed, memory, computeTags, rootDiskTags, networks, vmEntityManager);
167170

168-
List<Pair<NetworkVO, NicProfile>> networkIpMap = new ArrayList<Pair<NetworkVO, NicProfile>>();
171+
LinkedHashMap<NetworkVO, NicProfile> networkIpMap = new LinkedHashMap<NetworkVO, NicProfile>();
169172
for (String uuid : networkNicMap.keySet()) {
170173
NetworkVO network = _networkDao.findByUuid(uuid);
171174
if(network != null){
172-
networkIpMap.add(new Pair<NetworkVO, NicProfile>(network, networkNicMap.get(uuid)));
175+
networkIpMap.put(network, networkNicMap.get(uuid));
173176
}
174177
}
175178

@@ -186,7 +189,7 @@ public VirtualMachineEntity createVirtualMachine(
186189
// Else, a disk offering is optional, and if present will be used to create the data disk
187190

188191
Pair<DiskOfferingVO, Long> rootDiskOffering = new Pair<DiskOfferingVO, Long>(null, null);
189-
List<Pair<DiskOfferingVO, Long>> dataDiskOfferings = new ArrayList<Pair<DiskOfferingVO, Long>>();
192+
LinkedHashMap<DiskOfferingVO, Long> dataDiskOfferings = new LinkedHashMap<DiskOfferingVO, Long>();
190193

191194
ServiceOfferingVO offering = _serviceOfferingDao.findById(vm.getServiceOfferingId());
192195
rootDiskOffering.first(offering);
@@ -206,12 +209,19 @@ public VirtualMachineEntity createVirtualMachine(
206209
}
207210
_volumeMgr.validateVolumeSizeRange(size * 1024 * 1024 * 1024);
208211
}
209-
dataDiskOfferings.add(new Pair<DiskOfferingVO, Long>(diskOffering, size));
212+
dataDiskOfferings.put(diskOffering, size);
210213
}
211214

212215

213216

214-
_itMgr.allocate(vm.getInstanceName(), _templateDao.findById(new Long(templateId)), offering, rootDiskOffering, dataDiskOfferings, networkIpMap, null, plan, hypervisorType);
217+
_itMgr.allocate(vm.getInstanceName(),
218+
_templateDao.findById(new Long(templateId)),
219+
offering,
220+
rootDiskOffering,
221+
dataDiskOfferings,
222+
networkIpMap,
223+
plan,
224+
hypervisorType);
215225

216226
return vmEntity;
217227
}
@@ -228,11 +238,11 @@ public VirtualMachineEntity createVirtualMachineFromScratch(String id, String ow
228238
VMInstanceVO vm = _vmDao.findByUuid(id);
229239

230240

231-
Pair<DiskOfferingVO, Long> rootDiskOffering = new Pair<DiskOfferingVO, Long>(null, null);
241+
Pair<DiskOffering, Long> rootDiskOffering = new Pair<DiskOffering, Long>(null, null);
232242
ServiceOfferingVO offering = _serviceOfferingDao.findById(vm.getServiceOfferingId());
233243
rootDiskOffering.first(offering);
234244

235-
List<Pair<DiskOfferingVO, Long>> dataDiskOfferings = new ArrayList<Pair<DiskOfferingVO, Long>>();
245+
LinkedHashMap<DiskOffering, Long> dataDiskOfferings = new LinkedHashMap<DiskOffering, Long>();
236246
Long diskOfferingId = vm.getDiskOfferingId();
237247
if (diskOfferingId == null) {
238248
throw new InvalidParameterValueException(
@@ -254,17 +264,17 @@ public VirtualMachineEntity createVirtualMachineFromScratch(String id, String ow
254264
rootDiskOffering.first(diskOffering);
255265
rootDiskOffering.second(size);
256266

257-
List<Pair<NetworkVO, NicProfile>> networkIpMap = new ArrayList<Pair<NetworkVO, NicProfile>>();
267+
LinkedHashMap<Network, NicProfile> networkIpMap = new LinkedHashMap<Network, NicProfile>();
258268
for (String uuid : networkNicMap.keySet()) {
259269
NetworkVO network = _networkDao.findByUuid(uuid);
260270
if(network != null){
261-
networkIpMap.add(new Pair<NetworkVO, NicProfile>(network, networkNicMap.get(uuid)));
271+
networkIpMap.put(network, networkNicMap.get(uuid));
262272
}
263273
}
264274

265275
HypervisorType hypervisorType = HypervisorType.valueOf(hypervisor);
266276

267-
_itMgr.allocate(vm.getInstanceName(), _templateDao.findById(new Long(isoId)), offering, rootDiskOffering, dataDiskOfferings, networkIpMap, null, plan, hypervisorType);
277+
_itMgr.allocate(vm.getInstanceName(), _templateDao.findById(new Long(isoId)), offering, rootDiskOffering, dataDiskOfferings, networkIpMap, plan, hypervisorType);
268278

269279
return vmEntity;
270280
}

framework/ipc/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@
3333
<artifactId>cloud-utils</artifactId>
3434
<version>${project.version}</version>
3535
</dependency>
36+
<dependency>
37+
<groupId>org.apache.cloudstack</groupId>
38+
<artifactId>cloud-api</artifactId>
39+
<version>${project.version}</version>
40+
</dependency>
3641
</dependencies>
3742
</project>

server/src/com/cloud/agent/manager/Commands.java renamed to framework/ipc/src/com/cloud/agent/manager/Commands.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
import java.util.Iterator;
2121
import java.util.List;
2222

23-
import com.cloud.agent.AgentManager.OnError;
2423
import com.cloud.agent.api.Answer;
2524
import com.cloud.agent.api.Command;
25+
import com.cloud.agent.api.Command.OnError;
2626
import com.cloud.utils.exception.CloudRuntimeException;
2727

2828
public class Commands implements Iterable<Command> {
2929
OnError _handler;
30-
private ArrayList<String> _ids = new ArrayList<String>();
31-
private ArrayList<Command> _cmds = new ArrayList<Command>();
30+
private final ArrayList<String> _ids = new ArrayList<String>();
31+
private final ArrayList<Command> _cmds = new ArrayList<Command>();
3232
private Answer[] _answers;
3333

3434
public Commands(OnError handler) {
@@ -126,7 +126,7 @@ public <T extends Command> T getCommand(Class<T> clazz) {
126126
}
127127

128128
/**
129-
* @return For Commands with handler OnError.Continue, one command succeeding is successful. If not, all commands must succeed to be successful.
129+
* @return For Commands with handler OnError.Continue, one command succeeding is successful. If not, all commands must succeed to be successful.
130130
*/
131131
public boolean isSuccessful() {
132132
if (_answers == null) {

plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.HashMap;
2222
import java.util.HashSet;
23+
import java.util.LinkedHashMap;
2324
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Random;
@@ -41,9 +42,8 @@
4142
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
4243

4344
import com.cloud.agent.AgentManager;
44-
import com.cloud.agent.AgentManager.OnError;
4545
import com.cloud.agent.api.Answer;
46-
import com.cloud.agent.api.StopAnswer;
46+
import com.cloud.agent.api.Command;
4747
import com.cloud.agent.api.check.CheckSshAnswer;
4848
import com.cloud.agent.api.check.CheckSshCommand;
4949
import com.cloud.agent.api.routing.LoadBalancerConfigCommand;
@@ -115,7 +115,6 @@
115115
import com.cloud.user.User;
116116
import com.cloud.user.dao.AccountDao;
117117
import com.cloud.utils.NumbersUtil;
118-
import com.cloud.utils.Pair;
119118
import com.cloud.utils.component.ManagerBase;
120119
import com.cloud.utils.concurrency.NamedThreadFactory;
121120
import com.cloud.utils.db.DB;
@@ -327,7 +326,7 @@ private void createApplyLoadBalancingRulesCommands(
327326

328327
protected boolean applyLBRules(DomainRouterVO elbVm,
329328
List<LoadBalancingRule> rules, long guestNetworkId) throws ResourceUnavailableException {
330-
Commands cmds = new Commands(OnError.Continue);
329+
Commands cmds = new Commands(Command.OnError.Continue);
331330
createApplyLoadBalancingRulesCommands(rules, elbVm, cmds, guestNetworkId);
332331
// Send commands to elbVm
333332
return sendCommandsToRouter(elbVm, cmds);
@@ -491,11 +490,11 @@ public DomainRouterVO deployELBVm(Network guestNetwork, DeployDestination dest,
491490
NetworkOffering controlOffering = offerings.get(0);
492491
NetworkVO controlConfig = _networkMgr.setupNetwork(_systemAcct, controlOffering, plan, null, null, false).get(0);
493492

494-
List<Pair<NetworkVO, NicProfile>> networks = new ArrayList<Pair<NetworkVO, NicProfile>>(2);
493+
LinkedHashMap<NetworkVO, NicProfile> networks = new LinkedHashMap<NetworkVO, NicProfile>(2);
495494
NicProfile guestNic = new NicProfile();
496495
guestNic.setDefaultNic(true);
497-
networks.add(new Pair<NetworkVO, NicProfile>(controlConfig, null));
498-
networks.add(new Pair<NetworkVO, NicProfile>((NetworkVO) guestNetwork, guestNic));
496+
networks.put(controlConfig, null);
497+
networks.put((NetworkVO)guestNetwork, guestNic);
499498

500499
VMTemplateVO template = _templateDao.findSystemVMTemplate(dcId);
501500

@@ -939,7 +938,7 @@ public boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile prof
939938
}
940939

941940
@Override
942-
public void finalizeStop(VirtualMachineProfile profile, StopAnswer answer) {
941+
public void finalizeStop(VirtualMachineProfile profile, Answer answer) {
943942
if (answer != null) {
944943
DomainRouterVO elbVm = _routerDao.findById(profile.getVirtualMachine().getId());
945944
processStopOrRebootAnswer(elbVm, answer);

0 commit comments

Comments
 (0)