Fix Acl.getAccessListEntries() NPE
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / utils / AclServiceUtils.java
index a26b8238d8721ee95419b0eeece3ea53049e6633..c1e0f7b2c8b55a0b5c6df792d166aa954a8626c8 100644 (file)
@@ -33,6 +33,7 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import javax.inject.Inject;
 import javax.inject.Singleton;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
@@ -374,23 +375,15 @@ public final class AclServiceUtils {
             return newAclList;
         }
         List<Uuid> origAclList = new ArrayList<>(currentAclList);
-        for (Iterator<Uuid> iterator = newAclList.iterator(); iterator.hasNext();) {
-            Uuid updatedAclUuid = iterator.next();
-            for (Uuid currentAclUuid :origAclList) {
-                if (updatedAclUuid.getValue().equals(currentAclUuid.getValue())) {
-                    iterator.remove();
-                }
-            }
-        }
+        newAclList.removeAll(origAclList);
         return newAclList;
     }
 
-    @Nullable
     public static List<AllowedAddressPairs> getUpdatedAllowedAddressPairs(
             @Nullable List<AllowedAddressPairs> updatedAllowedAddressPairs,
             @Nullable List<AllowedAddressPairs> currentAllowedAddressPairs) {
         if (updatedAllowedAddressPairs == null) {
-            return null;
+            return Collections.emptyList();
         }
         List<AllowedAddressPairs> newAllowedAddressPairs = new ArrayList<>(updatedAllowedAddressPairs);
         if (currentAllowedAddressPairs == null) {
@@ -969,13 +962,15 @@ public final class AclServiceUtils {
         return flowMatches;
     }
 
-    public static List<Ace> getAceListFromAcl(Acl acl) {
-        if (acl.getAccessListEntries() != null) {
-            List<Ace> aceList = acl.getAccessListEntries().getAce();
-            if (aceList != null && !aceList.isEmpty()
-                    && aceList.get(0).augmentation(SecurityRuleAttr.class) != null) {
-                return aceList;
-            }
+    public static @NonNull List<Ace> aceList(@NonNull Acl acl) {
+        final AccessListEntries ale = acl.getAccessListEntries();
+        return ale == null ? Collections.emptyList() : ale.nonnullAce();
+    }
+
+    public static @NonNull List<Ace> getAceListFromAcl(Acl acl) {
+        List<Ace> aceList = aceList(acl);
+        if (!aceList.isEmpty() && aceList.get(0).augmentation(SecurityRuleAttr.class) != null) {
+            return aceList;
         }
         return Collections.emptyList();
     }
@@ -1032,14 +1027,11 @@ public final class AclServiceUtils {
 
     public static Set<Uuid> getRemoteAclIdsByDirection(Acl acl, Class<? extends DirectionBase> direction) {
         Set<Uuid> remoteAclIds = new HashSet<>();
-        AccessListEntries accessListEntries = acl.getAccessListEntries();
-        if (accessListEntries != null && accessListEntries.getAce() != null) {
-            for (Ace ace : accessListEntries.getAce()) {
-                SecurityRuleAttr aceAttr = AclServiceUtils.getAccessListAttributes(ace);
-                if (aceAttr != null && Objects.equals(aceAttr.getDirection(), direction)
-                        && doesAceHaveRemoteGroupId(aceAttr)) {
-                    remoteAclIds.add(aceAttr.getRemoteGroupId());
-                }
+        for (Ace ace : aceList(acl)) {
+            SecurityRuleAttr aceAttr = AclServiceUtils.getAccessListAttributes(ace);
+            if (aceAttr != null && Objects.equals(aceAttr.getDirection(), direction)
+                    && doesAceHaveRemoteGroupId(aceAttr)) {
+                remoteAclIds.add(aceAttr.getRemoteGroupId());
             }
         }
         return remoteAclIds;
@@ -1103,12 +1095,12 @@ public final class AclServiceUtils {
         LOG.debug("Processing interface additions for port {}", portAfter.getInterfaceId());
         List<AllowedAddressPairs> addedAllowedAddressPairs = getUpdatedAllowedAddressPairs(
                 portAfter.getAllowedAddressPairs(), portBefore.getAllowedAddressPairs());
-        if (addedAllowedAddressPairs != null && !addedAllowedAddressPairs.isEmpty()) {
+        if (!addedAllowedAddressPairs.isEmpty()) {
             addAclPortsLookup(portAfter, portAfter.getSecurityGroups(), addedAllowedAddressPairs);
         }
 
         List<Uuid> addedAcls = getUpdatedAclList(portAfter.getSecurityGroups(), portBefore.getSecurityGroups());
-        if (addedAcls != null && !addedAcls.isEmpty()) {
+        if (!addedAcls.isEmpty()) {
             addAclPortsLookup(portAfter, addedAcls, portAfter.getAllowedAddressPairs());
         }
     }
@@ -1117,12 +1109,12 @@ public final class AclServiceUtils {
         LOG.debug("Processing interface removals for port {}", portAfter.getInterfaceId());
         List<AllowedAddressPairs> deletedAllowedAddressPairs = getUpdatedAllowedAddressPairs(
                 portBefore.getAllowedAddressPairs(), portAfter.getAllowedAddressPairs());
-        if (deletedAllowedAddressPairs != null && !deletedAllowedAddressPairs.isEmpty()) {
+        if (!deletedAllowedAddressPairs.isEmpty()) {
             deleteAclPortsLookup(portAfter, portAfter.getSecurityGroups(), deletedAllowedAddressPairs);
         }
 
         List<Uuid> deletedAcls = getUpdatedAclList(portBefore.getSecurityGroups(), portAfter.getSecurityGroups());
-        if (deletedAcls != null && !deletedAcls.isEmpty()) {
+        if (!deletedAcls.isEmpty()) {
             deleteAclPortsLookup(portAfter, deletedAcls, portAfter.getAllowedAddressPairs());
         }
     }