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.
1818package com .cloud .hypervisor .guru ;
2323import java .util .HashMap ;
2424import java .util .List ;
2525import java .util .Map ;
26+ import java .util .UUID ;
2627
2728import javax .ejb .Local ;
2829import javax .inject .Inject ;
2930
30- import org .apache .cloudstack .api .ApiConstants .VMDetails ;
31- import org .apache .cloudstack .storage .command .CopyCommand ;
3231import org .apache .log4j .Logger ;
33- import org .springframework .stereotype .Component ;
3432
3533import com .cloud .agent .api .BackupSnapshotCommand ;
3634import com .cloud .agent .api .Command ;
3735import com .cloud .agent .api .CreatePrivateTemplateFromSnapshotCommand ;
3836import com .cloud .agent .api .CreatePrivateTemplateFromVolumeCommand ;
3937import com .cloud .agent .api .CreateVolumeFromSnapshotCommand ;
38+ import com .cloud .agent .api .UnregisterNicCommand ;
4039import com .cloud .agent .api .UnregisterVMCommand ;
4140import com .cloud .agent .api .storage .CopyVolumeCommand ;
4241import com .cloud .agent .api .storage .CreateVolumeOVACommand ;
6059import com .cloud .hypervisor .vmware .manager .VmwareManager ;
6160import com .cloud .hypervisor .vmware .mo .VirtualEthernetCardType ;
6261import com .cloud .network .Network .Provider ;
63- import com .cloud .network .NetworkModel ;
6462import com .cloud .network .Network .Service ;
63+ import com .cloud .network .NetworkModel ;
64+ import com .cloud .network .Networks .BroadcastDomainType ;
6565import com .cloud .network .Networks .TrafficType ;
6666import com .cloud .network .dao .NetworkDao ;
6767import 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 ;
6871import com .cloud .secstorage .CommandExecLogDao ;
6972import com .cloud .secstorage .CommandExecLogVO ;
7073import com .cloud .storage .DataStoreRole ;
7982import com .cloud .vm .ConsoleProxyVO ;
8083import com .cloud .vm .DomainRouterVO ;
8184import com .cloud .vm .NicProfile ;
85+ import com .cloud .vm .NicVO ;
8286import com .cloud .vm .SecondaryStorageVmVO ;
8387import com .cloud .vm .VirtualMachine ;
8488import com .cloud .vm .VirtualMachineProfile ;
8589import 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 )
8895public 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