|
27 | 27 | import java.util.LinkedList; |
28 | 28 | import java.util.List; |
29 | 29 | import java.util.Map; |
| 30 | +import java.util.Optional; |
30 | 31 | import java.util.Set; |
31 | 32 | import java.util.UUID; |
32 | 33 | import java.util.concurrent.Executors; |
@@ -707,17 +708,15 @@ public void doInTransactionWithoutResult(final TransactionStatus status) { |
707 | 708 | } |
708 | 709 |
|
709 | 710 | if (vo.getBroadcastDomainType() == BroadcastDomainType.Vlan && vo.getBroadcastUri() != null && offering.getSpecifyVlan()) { |
710 | | - s_logger.info("Recording a manually assigned vlan to table"); |
711 | 711 | String vnet = BroadcastDomainType.getValue(vo.getBroadcastUri()); |
712 | 712 | List<DataCenterVnetVO> vnetVOs = _datacenterVnetDao.findVnet(plan.getDataCenterId(), plan.getPhysicalNetworkId(), vnet); |
713 | 713 | if (vnetVOs.size() == 1) { |
| 714 | + s_logger.info("Recording a manually assigned vlan to table"); |
714 | 715 | DataCenterVnetVO vnetVO = vnetVOs.get(0); |
715 | | - if (vnetVO.getTakenAt() == null) { |
716 | | - vnetVO.setTakenAt(new Date()); |
717 | | - vnetVO.setAccountId(owner.getId()); |
718 | | - vnetVO.setReservationId(vo.getUuid()); |
719 | | - _datacenterVnetDao.update(vnetVO.getId(), vnetVO); |
720 | | - } |
| 716 | + vnetVO.setTakenAt(new Date()); |
| 717 | + vnetVO.setAccountId(owner.getId()); |
| 718 | + vnetVO.setReservationId(vo.getUuid()); |
| 719 | + _datacenterVnetDao.update(vnetVO.getId(), vnetVO); |
721 | 720 | } |
722 | 721 | } |
723 | 722 | } |
@@ -2699,8 +2698,46 @@ public void doInTransactionWithoutResult(final TransactionStatus status) { |
2699 | 2698 | } |
2700 | 2699 | } |
2701 | 2700 | if (networkFinal.getBroadcastDomainType() == BroadcastDomainType.Vlan && networkFinal.getBroadcastUri() != null && networkOffering.getSpecifyVlan()) { |
2702 | | - s_logger.info("Cleaning up our manually assigned vlan from table"); |
2703 | | - _dcDao.releaseVnet(BroadcastDomainType.getValue(networkFinal.getBroadcastUri()), networkFinal.getDataCenterId(), networkFinal.getPhysicalNetworkId(), networkFinal.getAccountId(), networkFinal.getUuid()); |
| 2701 | + String vlan = BroadcastDomainType.getValue(networkFinal.getBroadcastUri()); |
| 2702 | + List<DataCenterVnetVO> vnetVOs = _datacenterVnetDao.findVnet(networkFinal.getDataCenterId(), networkFinal.getPhysicalNetworkId(), vlan); |
| 2703 | + |
| 2704 | + if (vnetVOs.size() == 1) { |
| 2705 | + s_logger.info("Cleaning up our manually assigned vlan from table"); |
| 2706 | + List<NetworkVO> overlappingNetworks = _networksDao.listByZoneAndUriAndGuestType(networkFinal.getDataCenterId(), networkFinal.getBroadcastUri().toString(), null); |
| 2707 | + |
| 2708 | + Optional<NetworkVO> networkToAssign = overlappingNetworks.stream().filter(n -> n.getId() != networkId).sorted((a, b) -> { |
| 2709 | + // The entity layer caches these, so performance shouldn't suffer |
| 2710 | + NetworkOffering aOff = _networkOfferingDao.findById(a.getNetworkOfferingId()); |
| 2711 | + NetworkOffering bOff = _networkOfferingDao.findById(b.getNetworkOfferingId()); |
| 2712 | + // We are sorting so that networks that specify a vlan manually get sorted first, aka smaller |
| 2713 | + if (aOff.getSpecifyVlan() && !bOff.getSpecifyVlan()) { |
| 2714 | + return -1; |
| 2715 | + } else if (bOff.getSpecifyVlan() && !aOff.getSpecifyVlan()) { |
| 2716 | + return 1; |
| 2717 | + } |
| 2718 | + return 0; |
| 2719 | + }).findFirst(); |
| 2720 | + |
| 2721 | + if (networkToAssign.isPresent()) { |
| 2722 | + NetworkVO network = networkToAssign.get(); |
| 2723 | + NetworkOffering offering = _networkOfferingDao.findById(network.getNetworkOfferingId()); |
| 2724 | + s_logger.info("Re-assigning vlan " + vlan + " to network: " + network); |
| 2725 | + DataCenterVnetVO vnetVO = vnetVOs.get(0); |
| 2726 | + vnetVO.setTakenAt(network.getCreated()); |
| 2727 | + vnetVO.setAccountId(network.getAccountId()); |
| 2728 | + if (network.getReservationId() != null) { |
| 2729 | + vnetVO.setReservationId(network.getReservationId()); |
| 2730 | + } else if (offering.getSpecifyVlan()) { |
| 2731 | + // Only do this if it is one of our manually assigned networks |
| 2732 | + vnetVO.setReservationId(network.getUuid()); |
| 2733 | + } else { |
| 2734 | + vnetVO.setReservationId(null); |
| 2735 | + } |
| 2736 | + _datacenterVnetDao.update(vnetVO.getId(), vnetVO); |
| 2737 | + } else { |
| 2738 | + _dcDao.releaseVnet(BroadcastDomainType.getValue(networkFinal.getBroadcastUri()), networkFinal.getDataCenterId(), networkFinal.getPhysicalNetworkId(), networkFinal.getAccountId(), networkFinal.getUuid()); |
| 2739 | + } |
| 2740 | + } |
2704 | 2741 | } |
2705 | 2742 | } |
2706 | 2743 | }); |
|
0 commit comments