Skip to content

Commit 55c0616

Browse files
committed
CLOUDSTACK-728 Get the framework in place the support the removal of the portgroup that is created for a nic connected to an lswitch.
Add a command to tell a hypervisor guru to take some action when expunging a nic
1 parent a0fe6ec commit 55c0616

6 files changed

Lines changed: 530 additions & 360 deletions

File tree

api/src/com/cloud/hypervisor/HypervisorGuru.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,18 @@ public interface HypervisorGuru extends Adapter {
5959
* @return
6060
*/
6161
NicTO toNicTO(NicProfile profile);
62-
62+
6363
/**
6464
* Give hypervisor guru opportunity to decide if certain command needs to be done after expunge VM from DB
6565
* @param vm
6666
* @return a list of Commands
6767
*/
6868
List<Command> finalizeExpunge(VirtualMachine vm);
69+
70+
/**
71+
* Give the hypervisor guru the opportinity to decide if additional clean is
72+
* required for nics before expunging the VM
73+
*
74+
*/
75+
List<Command> finalizeExpungeNics(VirtualMachine vm, List<NicProfile> nics);
6976
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.agent.api;
18+
19+
import java.util.UUID;
20+
21+
/**
22+
* This command will tell the hypervisor to cleanup any resources dedicated for
23+
* this particular nic. Orginally implemented to cleanup dedicated portgroups
24+
* from a vmware standard switch
25+
*
26+
*/
27+
public class UnregisterNicCommand extends Command {
28+
private String vmName;
29+
private String trafficLabel;
30+
private UUID nicUuid;
31+
32+
public UnregisterNicCommand(String vmName, String trafficLabel, UUID nicUuid) {
33+
this.nicUuid = nicUuid;
34+
this.vmName = vmName;
35+
this.trafficLabel = trafficLabel;
36+
}
37+
38+
public UUID getNicUuid() {
39+
return nicUuid;
40+
}
41+
42+
public String getVmName() {
43+
return vmName;
44+
}
45+
46+
public String getTrafficLabel() {
47+
return trafficLabel;
48+
}
49+
50+
@Override
51+
public boolean executeInSequence() {
52+
return false;
53+
}
54+
55+
}

plugins/hypervisors/vmware/src/com/cloud/hypervisor/guru/VMwareGuru.java

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// Unless required by applicable law or agreed to in writing,
1313
// software distributed under the License is distributed on an
1414
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15-
// KIND, either express or implied. See the License for the
15+
// KIND, either express or implied. See the License for the
1616
// specific language governing permissions and limitations
1717
// under the License.
1818
package com.cloud.hypervisor.guru;
@@ -23,20 +23,19 @@
2323
import java.util.HashMap;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.UUID;
2627

2728
import javax.ejb.Local;
2829
import javax.inject.Inject;
2930

30-
import org.apache.cloudstack.api.ApiConstants.VMDetails;
31-
import org.apache.cloudstack.storage.command.CopyCommand;
3231
import org.apache.log4j.Logger;
33-
import org.springframework.stereotype.Component;
3432

3533
import com.cloud.agent.api.BackupSnapshotCommand;
3634
import com.cloud.agent.api.Command;
3735
import com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand;
3836
import com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand;
3937
import com.cloud.agent.api.CreateVolumeFromSnapshotCommand;
38+
import com.cloud.agent.api.UnregisterNicCommand;
4039
import com.cloud.agent.api.UnregisterVMCommand;
4140
import com.cloud.agent.api.storage.CopyVolumeCommand;
4241
import com.cloud.agent.api.storage.CreateVolumeOVACommand;
@@ -60,11 +59,15 @@
6059
import com.cloud.hypervisor.vmware.manager.VmwareManager;
6160
import com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType;
6261
import com.cloud.network.Network.Provider;
63-
import com.cloud.network.NetworkModel;
6462
import com.cloud.network.Network.Service;
63+
import com.cloud.network.NetworkModel;
64+
import com.cloud.network.Networks.BroadcastDomainType;
6565
import com.cloud.network.Networks.TrafficType;
6666
import com.cloud.network.dao.NetworkDao;
6767
import com.cloud.network.dao.NetworkVO;
68+
import com.cloud.network.dao.PhysicalNetworkDao;
69+
import com.cloud.network.dao.PhysicalNetworkTrafficTypeDao;
70+
import com.cloud.network.dao.PhysicalNetworkTrafficTypeVO;
6871
import com.cloud.secstorage.CommandExecLogDao;
6972
import com.cloud.secstorage.CommandExecLogVO;
7073
import com.cloud.storage.DataStoreRole;
@@ -79,10 +82,14 @@
7982
import com.cloud.vm.ConsoleProxyVO;
8083
import com.cloud.vm.DomainRouterVO;
8184
import com.cloud.vm.NicProfile;
85+
import com.cloud.vm.NicVO;
8286
import com.cloud.vm.SecondaryStorageVmVO;
8387
import com.cloud.vm.VirtualMachine;
8488
import com.cloud.vm.VirtualMachineProfile;
8589
import com.cloud.vm.VmDetailConstants;
90+
import com.cloud.vm.dao.NicDao;
91+
92+
import org.apache.cloudstack.storage.command.CopyCommand;
8693

8794
@Local(value=HypervisorGuru.class)
8895
public class VMwareGuru extends HypervisorGuruBase implements HypervisorGuru {
@@ -98,6 +105,12 @@ public class VMwareGuru extends HypervisorGuruBase implements HypervisorGuru {
98105
@Inject SecondaryStorageVmManager _secStorageMgr;
99106
@Inject NetworkModel _networkMgr;
100107
@Inject ConfigurationDao _configDao;
108+
@Inject
109+
NicDao _nicDao;
110+
@Inject
111+
PhysicalNetworkDao _physicalNetworkDao;
112+
@Inject
113+
PhysicalNetworkTrafficTypeDao _physicalNetworkTrafficTypeDao;
101114

102115
protected VMwareGuru() {
103116
super();
@@ -118,7 +131,7 @@ public <T extends VirtualMachine> VirtualMachineTO implement(VirtualMachineProfi
118131
details = new HashMap<String, String>();
119132

120133
String nicDeviceType = details.get(VmDetailConstants.NIC_ADAPTER);
121-
if(vm.getVirtualMachine() instanceof DomainRouterVO || vm.getVirtualMachine() instanceof ConsoleProxyVO
134+
if(vm.getVirtualMachine() instanceof DomainRouterVO || vm.getVirtualMachine() instanceof ConsoleProxyVO
122135
|| vm.getVirtualMachine() instanceof SecondaryStorageVmVO) {
123136

124137
if(nicDeviceType == null) {
@@ -144,13 +157,13 @@ public <T extends VirtualMachine> VirtualMachineTO implement(VirtualMachineProfi
144157
}
145158
}
146159
}
147-
160+
148161
String diskDeviceType = details.get(VmDetailConstants.ROOK_DISK_CONTROLLER);
149-
if (!(vm.getVirtualMachine() instanceof DomainRouterVO || vm.getVirtualMachine() instanceof ConsoleProxyVO
150-
|| vm.getVirtualMachine() instanceof SecondaryStorageVmVO)){
162+
if (!(vm.getVirtualMachine() instanceof DomainRouterVO || vm.getVirtualMachine() instanceof ConsoleProxyVO
163+
|| vm.getVirtualMachine() instanceof SecondaryStorageVmVO)){
151164
// user vm
152165
if (diskDeviceType == null){
153-
details.put(VmDetailConstants.ROOK_DISK_CONTROLLER, _vmwareMgr.getRootDiskController());
166+
details.put(VmDetailConstants.ROOK_DISK_CONTROLLER, _vmwareMgr.getRootDiskController());
154167
}
155168
}
156169

@@ -236,19 +249,19 @@ public <T extends VirtualMachine> VirtualMachineTO implement(VirtualMachineProfi
236249
sbMacSequence.deleteCharAt(sbMacSequence.length() - 1);
237250
String bootArgs = to.getBootArgs();
238251
to.setBootArgs(bootArgs + " nic_macs=" + sbMacSequence.toString());
239-
252+
240253
}
241-
254+
242255
// Don't do this if the virtual machine is one of the special types
243256
// Should only be done on user machines
244-
if(!(vm.getVirtualMachine() instanceof DomainRouterVO || vm.getVirtualMachine() instanceof ConsoleProxyVO
257+
if(!(vm.getVirtualMachine() instanceof DomainRouterVO || vm.getVirtualMachine() instanceof ConsoleProxyVO
245258
|| vm.getVirtualMachine() instanceof SecondaryStorageVmVO)) {
246259
String nestedVirt = _configDao.getValue(Config.VmwareEnableNestedVirtualization.key());
247260
if (nestedVirt != null) {
248261
s_logger.debug("Nested virtualization requested, adding flag to vm configuration");
249262
details.put(VmDetailConstants.NESTED_VIRTUALIZATION_FLAG, nestedVirt);
250263
to.setDetails(details);
251-
264+
252265
}
253266
}
254267
// Determine the VM's OS description
@@ -284,7 +297,7 @@ public int compare(NicTO arg0, NicTO arg1) {
284297
public long getCommandHostDelegation(long hostId, Command cmd) {
285298
boolean needDelegation = false;
286299

287-
if(cmd instanceof PrimaryStorageDownloadCommand ||
300+
if(cmd instanceof PrimaryStorageDownloadCommand ||
288301
cmd instanceof BackupSnapshotCommand ||
289302
cmd instanceof CreatePrivateTemplateFromVolumeCommand ||
290303
cmd instanceof CreatePrivateTemplateFromSnapshotCommand ||
@@ -299,7 +312,7 @@ public long getCommandHostDelegation(long hostId, Command cmd) {
299312
DataStoreTO srcStoreTO = srcData.getDataStore();
300313
DataTO destData = cpyCommand.getDestTO();
301314
DataStoreTO destStoreTO = destData.getDataStore();
302-
315+
303316
if (destData.getObjectType() == DataObjectType.VOLUME && destStoreTO.getRole() == DataStoreRole.Primary &&
304317
srcData.getObjectType() == DataObjectType.TEMPLATE && srcStoreTO.getRole() == DataStoreRole.Primary) {
305318
needDelegation = false;
@@ -309,14 +322,14 @@ public long getCommandHostDelegation(long hostId, Command cmd) {
309322
} else {
310323
needDelegation = true;
311324
}
312-
325+
313326
}
314327
/* Fang: remove this before checking in */
315328
// needDelegation = false;
316329

317330
if (cmd instanceof PrepareOVAPackingCommand ||
318331
cmd instanceof CreateVolumeOVACommand ) {
319-
cmd.setContextParam("hypervisor", HypervisorType.VMware.toString());
332+
cmd.setContextParam("hypervisor", HypervisorType.VMware.toString());
320333
}
321334
if(needDelegation) {
322335
HostVO host = _hostDao.findById(hostId);
@@ -339,8 +352,8 @@ public long getCommandHostDelegation(long hostId, Command cmd) {
339352
_cmdExecLogDao.persist(execLog);
340353
cmd.setContextParam("execid", String.valueOf(execLog.getId()));
341354

342-
if(cmd instanceof BackupSnapshotCommand ||
343-
cmd instanceof CreatePrivateTemplateFromVolumeCommand ||
355+
if(cmd instanceof BackupSnapshotCommand ||
356+
cmd instanceof CreatePrivateTemplateFromVolumeCommand ||
344357
cmd instanceof CreatePrivateTemplateFromSnapshotCommand ||
345358
cmd instanceof CopyVolumeCommand ||
346359
cmd instanceof CreateVolumeOVACommand ||
@@ -349,14 +362,14 @@ public long getCommandHostDelegation(long hostId, Command cmd) {
349362

350363
String workerName = _vmwareMgr.composeWorkerName();
351364
long checkPointId = 1;
352-
// FIXME: Fix long checkPointId = _checkPointMgr.pushCheckPoint(new VmwareCleanupMaid(hostDetails.get("guid"), workerName));
365+
// FIXME: Fix long checkPointId = _checkPointMgr.pushCheckPoint(new VmwareCleanupMaid(hostDetails.get("guid"), workerName));
353366
cmd.setContextParam("worker", workerName);
354367
cmd.setContextParam("checkpoint", String.valueOf(checkPointId));
355368

356369
// some commands use 2 workers
357370
String workerName2 = _vmwareMgr.composeWorkerName();
358371
long checkPointId2 = 1;
359-
// FIXME: Fix long checkPointId2 = _checkPointMgr.pushCheckPoint(new VmwareCleanupMaid(hostDetails.get("guid"), workerName2));
372+
// FIXME: Fix long checkPointId2 = _checkPointMgr.pushCheckPoint(new VmwareCleanupMaid(hostDetails.get("guid"), workerName2));
360373
cmd.setContextParam("worker2", workerName2);
361374
cmd.setContextParam("checkpoint2", String.valueOf(checkPointId2));
362375
}
@@ -388,12 +401,33 @@ private static String resolveNameInGuid(String guid) {
388401

389402
return tokens[0] + "@" + vCenterIp;
390403
}
391-
404+
392405
@Override
393406
public List<Command> finalizeExpunge(VirtualMachine vm) {
394407
UnregisterVMCommand unregisterVMCommand = new UnregisterVMCommand(vm.getInstanceName());
395408
List<Command> commands = new ArrayList<Command>();
396409
commands.add(unregisterVMCommand);
397410
return commands;
398411
}
412+
413+
@Override
414+
public List<Command> finalizeExpungeNics(VirtualMachine vm, List<NicProfile> nics) {
415+
List<Command> commands = new ArrayList<Command>();
416+
List<NicVO> nicVOs = _nicDao.listByVmId(vm.getId());
417+
for (NicVO nic : nicVOs) {
418+
NetworkVO network = _networkDao.findById(nic.getNetworkId());
419+
if (network.getBroadcastDomainType() == BroadcastDomainType.Lswitch) {
420+
s_logger.debug("Nic " + nic.toString() + " is connected to an lswitch, cleanup required");
421+
NetworkVO networkVO = _networkDao.findById(nic.getNetworkId());
422+
// We need the traffic label to figure out which vSwitch has the
423+
// portgroup
424+
PhysicalNetworkTrafficTypeVO trafficTypeVO = _physicalNetworkTrafficTypeDao.findBy(
425+
networkVO.getPhysicalNetworkId(), networkVO.getTrafficType());
426+
UnregisterNicCommand unregisterNicCommand = new UnregisterNicCommand(vm.getInstanceName(),
427+
trafficTypeVO.getVmwareNetworkLabel(), UUID.fromString(nic.getUuid()));
428+
commands.add(unregisterNicCommand);
429+
}
430+
}
431+
return commands;
432+
}
399433
}

0 commit comments

Comments
 (0)