Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected void init() {
AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ);
AllFieldsSearch.and("related", AllFieldsSearch.entity().getRelated(), Op.EQ);
AllFieldsSearch.and("guestType", AllFieldsSearch.entity().getGuestType(), Op.EQ);
AllFieldsSearch.and("physicalNetwork", AllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
AllFieldsSearch.and("physicalNetworkId", AllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
AllFieldsSearch.and("broadcastUri", AllFieldsSearch.entity().getBroadcastUri(), Op.EQ);
AllFieldsSearch.and("vpcId", AllFieldsSearch.entity().getVpcId(), Op.EQ);
AllFieldsSearch.and("aclId", AllFieldsSearch.entity().getNetworkACLId(), Op.EQ);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6653,19 +6653,10 @@ public Pair<List<? extends NetworkOffering>, Integer> searchForNetworkOfferings(
final Boolean sourceNatSupported = cmd.getSourceNatSupported();
final List<String> pNtwkTags = new ArrayList<String>();
boolean checkForTags = false;
boolean allowNullTag = false;
if (zone != null) {
final List<PhysicalNetworkVO> pNtwks = _physicalNetworkDao.listByZoneAndTrafficType(zoneId, TrafficType.Guest);
if (pNtwks.size() > 1) {
checkForTags = true;
// go through tags
for (final PhysicalNetworkVO pNtwk : pNtwks) {
final List<String> pNtwkTag = pNtwk.getTags();
if (pNtwkTag == null || pNtwkTag.isEmpty()) {
throw new CloudRuntimeException("Tags are not defined for physical network in the zone id=" + zoneId);
}
pNtwkTags.addAll(pNtwkTag);
}
}
allowNullTag = allowNetworkOfferingWithNullTag(zoneId, pNtwkTags);
checkForTags = !pNtwkTags.isEmpty() || allowNullTag;
}

// filter by supported services
Expand Down Expand Up @@ -6695,10 +6686,8 @@ public Pair<List<? extends NetworkOffering>, Integer> searchForNetworkOfferings(
boolean addOffering = true;
List<Service> checkForProviders = new ArrayList<Service>();

if (checkForTags) {
if (!pNtwkTags.contains(offering.getTags())) {
continue;
}
if (checkForTags && ! checkNetworkOfferingTags(pNtwkTags, allowNullTag, offering.getTags())) {
continue;
}

if (listBySupportedServices) {
Expand Down Expand Up @@ -6748,6 +6737,28 @@ public Pair<List<? extends NetworkOffering>, Integer> searchForNetworkOfferings(
}
}

private boolean allowNetworkOfferingWithNullTag(Long zoneId, List<String> allPhysicalNetworkTags) {
boolean allowNullTag = false;
final List<PhysicalNetworkVO> physicalNetworks = _physicalNetworkDao.listByZoneAndTrafficType(zoneId, TrafficType.Guest);
for (final PhysicalNetworkVO physicalNetwork : physicalNetworks) {
final List<String> physicalNetworkTags = physicalNetwork.getTags();
if (CollectionUtils.isEmpty(physicalNetworkTags)) {
if (!allowNullTag) {
allowNullTag = true;
} else {
throw new CloudRuntimeException("There are more than 1 physical network with empty tag in the zone id=" + zoneId);
}
} else {
allPhysicalNetworkTags.addAll(physicalNetworkTags);
}
}
return allowNullTag;
}

private boolean checkNetworkOfferingTags(List<String> physicalNetworkTags, boolean allowNullTag, String offeringTags) {
return (offeringTags != null || allowNullTag) && (offeringTags == null || physicalNetworkTags.contains(offeringTags));
}

@Override
public boolean isOfferingForVpc(final NetworkOffering offering) {
return offering.isForVpc();
Expand Down
37 changes: 21 additions & 16 deletions server/src/main/java/com/cloud/network/NetworkModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1188,26 +1188,31 @@ public long findPhysicalNetworkId(long zoneId, String tag, TrafficType trafficTy
}

if (pNtwks.size() > 1) {
if (tag == null) {
throw new InvalidParameterValueException("More than one physical networks exist in zone id=" + zoneId +
" and no tags are specified in order to make a choice");
}
return getPhysicalNetworkId(zoneId, pNtwks, tag);
} else {
return pNtwks.get(0).getId();
}
}

Long pNtwkId = null;
for (PhysicalNetwork pNtwk : pNtwks) {
if (pNtwk.getTags().contains(tag)) {
s_logger.debug("Found physical network id=" + pNtwk.getId() + " based on requested tags " + tag);
pNtwkId = pNtwk.getId();
break;
private Long getPhysicalNetworkId(long zoneId, List<PhysicalNetworkVO> pNtwks, String tag) {
Long pNtwkId = null;
for (PhysicalNetwork pNtwk : pNtwks) {
if (tag == null && pNtwk.getTags().isEmpty()) {
s_logger.debug("Found physical network id=" + pNtwk.getId() + " with null tag");
if (pNtwkId != null) {
throw new CloudRuntimeException("There is more than 1 physical network with empty tag in the zone id=" + zoneId);
}
pNtwkId = pNtwk.getId();
} else if (tag != null && pNtwk.getTags().contains(tag)) {
s_logger.debug("Found physical network id=" + pNtwk.getId() + " based on requested tags " + tag);
pNtwkId = pNtwk.getId();
break;
}
if (pNtwkId == null) {
throw new InvalidParameterValueException("Unable to find physical network which match the tags " + tag);
}
return pNtwkId;
} else {
return pNtwks.get(0).getId();
}
if (pNtwkId == null) {
throw new InvalidParameterValueException("Unable to find physical network which match the tags " + tag);
}
return pNtwkId;
}

@Override
Expand Down
73 changes: 46 additions & 27 deletions server/src/main/java/com/cloud/network/NetworkServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3718,6 +3718,12 @@ public PhysicalNetwork updatePhysicalNetwork(Long id, String networkSpeed, List<
throw new InvalidParameterException("Unable to support more than one tag on network yet");
}

// If tags are null, then check if there are any other networks with null tags
// of the same traffic type. If so then dont update the tags
if (tags != null && tags.size() == 0) {
checkForPhysicalNetworksWithoutTag(network);
}

PhysicalNetwork.State networkState = null;
if (state != null && !state.isEmpty()) {
try {
Expand Down Expand Up @@ -3748,6 +3754,18 @@ public PhysicalNetwork updatePhysicalNetwork(Long id, String networkSpeed, List<

}

private void checkForPhysicalNetworksWithoutTag(PhysicalNetworkVO network) {
// Get all physical networks according to traffic type
Pair<List<PhysicalNetworkTrafficTypeVO>, Integer> result = _pNTrafficTypeDao
.listAndCountBy(network.getId());
if (result.second() > 0) {
for (PhysicalNetworkTrafficTypeVO physicalNetworkTrafficTypeVO : result.first()) {
TrafficType trafficType = physicalNetworkTrafficTypeVO.getTrafficType();
checkForPhysicalNetworksWithoutTag(network, trafficType);
}
}
}

@DB
public void addOrRemoveVnets(String[] listOfRanges, final PhysicalNetworkVO network) {
List<String> addVnets = null;
Expand Down Expand Up @@ -4617,36 +4635,30 @@ public PhysicalNetworkServiceProvider getCreatedPhysicalNetworkServiceProvider(L

@Override
public long findPhysicalNetworkId(long zoneId, String tag, TrafficType trafficType) {
List<PhysicalNetworkVO> pNtwks = new ArrayList<PhysicalNetworkVO>();
if (trafficType != null) {
pNtwks = _physicalNetworkDao.listByZoneAndTrafficType(zoneId, trafficType);
} else {
pNtwks = _physicalNetworkDao.listByZone(zoneId);
}

if (pNtwks.isEmpty()) {
throw new InvalidParameterValueException("Unable to find physical network in zone id=" + zoneId);
}
return _networkModel.findPhysicalNetworkId(zoneId, tag, trafficType);
}

if (pNtwks.size() > 1) {
if (tag == null) {
throw new InvalidParameterValueException("More than one physical networks exist in zone id=" + zoneId + " and no tags are specified in order to make a choice");
}
/**
* Function to check if there are any physical networks with traffic type of "trafficType"
* and check their tags. If there is more than one network with null tags then throw exception
* @param physicalNetwork
* @param trafficType
*/
private void checkForPhysicalNetworksWithoutTag(PhysicalNetworkVO physicalNetwork, TrafficType trafficType) {
int networkWithoutTagCount = 0;
List<PhysicalNetworkVO> physicalNetworkVOList = _physicalNetworkDao
.listByZoneAndTrafficType(physicalNetwork.getDataCenterId(), trafficType);

Long pNtwkId = null;
for (PhysicalNetwork pNtwk : pNtwks) {
if (pNtwk.getTags().contains(tag)) {
s_logger.debug("Found physical network id=" + pNtwk.getId() + " based on requested tags " + tag);
pNtwkId = pNtwk.getId();
break;
}
for (PhysicalNetworkVO physicalNetworkVO : physicalNetworkVOList) {
List<String> tags = physicalNetworkVO.getTags();
if (CollectionUtils.isEmpty(tags)) {
networkWithoutTagCount++;
}
if (pNtwkId == null) {
throw new InvalidParameterValueException("Unable to find physical network which match the tags " + tag);
}
return pNtwkId;
} else {
return pNtwks.get(0).getId();
}
if (networkWithoutTagCount > 0) {
s_logger.error("Number of physical networks without tags are " + networkWithoutTagCount);
throw new CloudRuntimeException("There are more than 1 physical network without tags in the zone= " +
physicalNetwork.getDataCenterId());
}
}

Expand Down Expand Up @@ -4697,6 +4709,13 @@ public PhysicalNetworkTrafficType addTrafficTypeToPhysicalNetwork(Long physicalN
}
}

// Check if there are more than 1 physical network with null tags in same traffic type.
// If so then dont allow to add traffic type.
List<String> tags = network.getTags();
if (CollectionUtils.isEmpty(tags)) {
checkForPhysicalNetworksWithoutTag(network, trafficType);
}

try {
// Create the new traffic type in the database
if (xenLabel == null) {
Expand Down
Loading