Source:
<code>protected void executeCommand() { VM vm = getVm(); if (vm.getVdsGroupId().equals(getParameters().getClusterId())) { setSucceeded(true); return; } List<Network> networks = getNetworkDao().getAllForCluster(getParameters().getClusterId()); List<VmNic> interfaces = getVmNicDao().getAllForVm(getParameters().getVmId()); for (final VmNic iface : interfaces) { if (iface.getVnicProfileId() != null) { Network network = NetworkHelper.getNetworkByVnicProfileId(iface.getVnicProfileId()); Optional<Network> net = networks.stream() .filter(x -> ObjectUtils.equals(x.getId(), network.getId())) <START> .findFirst(); <END> if (!net.isPresent()) { iface.setVnicProfileId(null); getVmNicDao().update(iface); } } } if (vm.getDedicatedVmForVdsList().size() > 0) { vm.setDedicatedVmForVdsList(Collections.<Guid>emptyList()); dedicatedHostWasCleared = true; } vm.setCpuProfileId(null); vm.setVdsGroupId(getParameters().getClusterId()); DbFacade.getInstance().getVmStaticDao().update(vm.getStaticData()); List<AffinityGroup> allAffinityGroupsByVmId = DbFacade.getInstance().getAffinityGroupDao().getAllAffinityGroupsByVmId(vm.getId()); if (!allAffinityGroupsByVmId.isEmpty()) { StringBuilder sb = new StringBuilder(); for (AffinityGroup affinityGroup : allAffinityGroupsByVmId) { sb.append(affinityGroup.getName() + " "); } log.info("Due to cluster change, removing VM from associated affinity group(s): {}", sb); DbFacade.getInstance().getAffinityGroupDao().removeVmFromAffinityGroups(vm.getId()); } setSucceeded(true); }</code><technical_language>care actual Network, presence (or lack thereof). short circuit logic do this: boolean networkFound = networks.stream().anyMatch(x -> ObjectUtils.equals(x.getId(), network.getId());</technical_language>

Target:
protected void executeCommand() { VM vm = getVm(); if (vm.getVdsGroupId().equals(getParameters().getClusterId())) { setSucceeded(true); return; } List<Network> networks = getNetworkDao().getAllForCluster(getParameters().getClusterId()); List<VmNic> interfaces = getVmNicDao().getAllForVm(getParameters().getVmId()); for (final VmNic iface : interfaces) { if (iface.getVnicProfileId() != null) { Network network = NetworkHelper.getNetworkByVnicProfileId(iface.getVnicProfileId()); boolean networkExistsInCluster = networks.stream() .anyMatch(x -> Objects.equals(x.getId(), network.getId())); if (!networkExistsInCluster) { iface.setVnicProfileId(null); getVmNicDao().update(iface); } } } if (vm.getDedicatedVmForVdsList().size() > 0) { vm.setDedicatedVmForVdsList(Collections.<Guid>emptyList()); dedicatedHostWasCleared = true; } vm.setCpuProfileId(null); vm.setVdsGroupId(getParameters().getClusterId()); DbFacade.getInstance().getVmStaticDao().update(vm.getStaticData()); List<AffinityGroup> allAffinityGroupsByVmId = DbFacade.getInstance().getAffinityGroupDao().getAllAffinityGroupsByVmId(vm.getId()); if (!allAffinityGroupsByVmId.isEmpty()) { StringBuilder sb = new StringBuilder(); for (AffinityGroup affinityGroup : allAffinityGroupsByVmId) { sb.append(affinityGroup.getName() + " "); } log.info("Due to cluster change, removing VM from associated affinity group(s): {}", sb); DbFacade.getInstance().getAffinityGroupDao().removeVmFromAffinityGroups(vm.getId()); } setSucceeded(true); }