Skip to content

Commit bcc8ff2

Browse files
nvazquezPearl1594
authored andcommitted
NSX: Fix number of physical networks for Guest traffic checks and leftover rules on CKS cluster deletion (#45)
* Fix pf rules removal on CKS cluster deletion * Fix check for number of physical networks for guest traffic * Fix unit test
1 parent 968235a commit bcc8ff2

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxElement.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
import java.util.Objects;
128128
import java.util.Set;
129129
import java.util.function.LongFunction;
130+
import java.util.stream.Collectors;
130131

131132
@Component
132133
public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsServiceProvider, VpcProvider,
@@ -403,10 +404,18 @@ private Pair<Boolean, Account> validateVpcConfigurationAndGetAccount(DataCenterV
403404
Account account = null;
404405
boolean forNsx = false;
405406
List<PhysicalNetworkVO> physicalNetworks = physicalNetworkDao.listByZoneAndTrafficType(zone.getId(), Networks.TrafficType.Guest);
406-
if (CollectionUtils.isNullOrEmpty(physicalNetworks) || physicalNetworks.size() > 1 ) {
407-
throw new InvalidConfigurationException(String.format("Desired number of physical networks is not present in the zone %s for traffic type %s. ", zone.getName(), Networks.TrafficType.Guest.name()));
408-
}
409-
if (physicalNetworks.get(0).getIsolationMethods().contains("NSX")) {
407+
if (CollectionUtils.isNullOrEmpty(physicalNetworks)) {
408+
String err = String.format("Desired physical network is not present in the zone %s for traffic type %s. ", zone.getName(), Networks.TrafficType.Guest.name());
409+
LOGGER.error(err);
410+
throw new InvalidConfigurationException(err);
411+
}
412+
List<PhysicalNetworkVO> filteredPhysicalNetworks = physicalNetworks.stream().filter(x -> x.getIsolationMethods().contains("NSX")).collect(Collectors.toList());
413+
if (CollectionUtils.isNullOrEmpty(filteredPhysicalNetworks)) {
414+
String err = String.format("No physical network with NSX isolation type for traffic type %s is present in the zone %s.", Networks.TrafficType.Guest.name(), zone.getName());
415+
LOGGER.error(err);
416+
throw new InvalidConfigurationException(err);
417+
}
418+
if (filteredPhysicalNetworks.get(0).getIsolationMethods().contains("NSX")) {
410419
account = accountMgr.getAccount(vpc.getAccountId());
411420
forNsx = true;
412421
}
@@ -585,9 +594,9 @@ protected synchronized boolean applyPFRulesInternal(Network network, List<PortFo
585594
result &= pfRuleResult;
586595
}
587596
} else if (rule.getState() == FirewallRule.State.Revoke) {
588-
if (ruleDetail != null && ruleDetail.getValue().equalsIgnoreCase("true")) {
597+
if (ruleDetail == null || (ruleDetail != null && ruleDetail.getValue().equalsIgnoreCase("true"))) {
589598
boolean pfRuleResult = nsxService.deletePortForwardRule(networkRule);
590-
if (pfRuleResult) {
599+
if (pfRuleResult && ruleDetail != null) {
591600
LOGGER.debug(String.format("Updating firewall rule detail %s for rule %s, set to false", ruleDetail.getId(), rule.getId()));
592601
ruleDetail.setValue("false");
593602
firewallRuleDetailsDao.update(ruleDetail.getId(), ruleDetail);

plugins/network-elements/nsx/src/test/java/org/apache/cloudstack/service/NsxElementTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ public void testApplyPFRules_delete() throws ResourceUnavailableException {
283283
IPAddressVO ipAddress = new IPAddressVO(new Ip("10.1.13.10"), 1L, 1L, 1L,false);
284284
when(ApiDBUtils.findIpAddressById(anyLong())).thenReturn(ipAddress);
285285
when(nsxElement.canHandle(networkVO, service)).thenReturn(true);
286+
when(nsxService.deletePortForwardRule(any(NsxNetworkRule.class))).thenReturn(true);
286287
assertTrue(nsxElement.applyPFRules(networkVO, List.of(rule)));
287288
}
288289

0 commit comments

Comments
 (0)