Handle nullable lists in aclservice 93/76993/3
authorStephen Kitt <skitt@redhat.com>
Mon, 15 Oct 2018 13:26:32 +0000 (15:26 +0200)
committerSam Hague <shague@redhat.com>
Tue, 16 Oct 2018 19:44:02 +0000 (19:44 +0000)
Following YANGTOOLS-585, lists can be null (which is correctly
indicated with an @Nullable annotation). This patch deals with the
fallout.

Change-Id: Ie38fb51625d299a825deb883440f9e6658e96b90
Signed-off-by: Stephen Kitt <skitt@redhat.com>
13 files changed:
aclservice/api/src/main/java/org/opendaylight/netvirt/aclservice/api/AclServiceManager.java
aclservice/api/src/main/java/org/opendaylight/netvirt/aclservice/api/utils/AclInterface.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/AbstractAclServiceImpl.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/AclInterfaceCacheImpl.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/listeners/AclEventListener.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/listeners/AclInterfaceListener.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/listeners/AclNodeListener.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/recovery/AclInstanceRecoveryHandler.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/stats/AclLiveStatisticsHelper.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/utils/AclDataUtil.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/utils/AclNodeDefaultFlowsTxBuilder.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/utils/AclServiceOFFlowBuilder.java
aclservice/impl/src/main/java/org/opendaylight/netvirt/aclservice/utils/AclServiceUtils.java

index 79cbae343c44063d1325aab99f794be6beea445c..e4b064537f62d4e76e9fbd1cf36164318775e4d4 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.netvirt.aclservice.api;
 
 import java.util.Collection;
+import javax.annotation.Nullable;
 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
@@ -27,7 +28,7 @@ public interface AclServiceManager {
         MATCH_DESTINATION
     }
 
-    void notify(AclInterface port, AclInterface oldPort, Action action);
+    void notify(AclInterface port, @Nullable AclInterface oldPort, Action action);
 
     void notifyAce(AclInterface port, Action action, String aclName, Ace ace);
 
index 2aff16b2d599be0bb471a0b365aad2df263d1ab4..9a451304d136150d0070b91b42ccca4d6851ec03 100644 (file)
@@ -12,6 +12,7 @@ import com.google.common.collect.ImmutableSortedSet;
 import java.math.BigInteger;
 import java.util.List;
 import java.util.SortedSet;
+import javax.annotation.Nullable;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.port.subnets.port.subnet.SubnetInfo;
@@ -273,11 +274,11 @@ public final class AclInterface {
         private BigInteger dpId;
         private Long elanId;
         private boolean portSecurityEnabled;
-        private List<Uuid> securityGroups;
-        private List<AllowedAddressPairs> allowedAddressPairs;
-        private List<SubnetInfo> subnetInfo;
-        private SortedSet<Integer> ingressRemoteAclTags;
-        private SortedSet<Integer> egressRemoteAclTags;
+        private @Nullable List<Uuid> securityGroups;
+        private @Nullable List<AllowedAddressPairs> allowedAddressPairs;
+        private @Nullable List<SubnetInfo> subnetInfo;
+        private @Nullable SortedSet<Integer> ingressRemoteAclTags;
+        private @Nullable SortedSet<Integer> egressRemoteAclTags;
         private boolean isMarkedForDelete;
 
         private Builder() {
@@ -322,27 +323,27 @@ public final class AclInterface {
             return this;
         }
 
-        public Builder securityGroups(List<Uuid> list) {
+        public Builder securityGroups(@Nullable List<Uuid> list) {
             this.securityGroups = list == null ? null : ImmutableList.copyOf(list);
             return this;
         }
 
-        public Builder allowedAddressPairs(List<AllowedAddressPairs> list) {
+        public Builder allowedAddressPairs(@Nullable List<AllowedAddressPairs> list) {
             this.allowedAddressPairs = list == null ? null : ImmutableList.copyOf(list);
             return this;
         }
 
-        public Builder subnetInfo(List<SubnetInfo> list) {
+        public Builder subnetInfo(@Nullable List<SubnetInfo> list) {
             this.subnetInfo = list == null ? null : ImmutableList.copyOf(list);
             return this;
         }
 
-        public Builder ingressRemoteAclTags(SortedSet<Integer> list) {
+        public Builder ingressRemoteAclTags(@Nullable SortedSet<Integer> list) {
             this.ingressRemoteAclTags = list == null ? null : ImmutableSortedSet.copyOf(list);
             return this;
         }
 
-        public Builder egressRemoteAclTags(SortedSet<Integer> list) {
+        public Builder egressRemoteAclTags(@Nullable SortedSet<Integer> list) {
             this.egressRemoteAclTags = list == null ? null : ImmutableSortedSet.copyOf(list);
             return this;
         }
index d33c4653722c71a81140b150d8dcfc56d715e969..93edcbfe847692d2a0cac4ec269e9100a67b2352 100644 (file)
@@ -20,6 +20,7 @@ import java.util.Map.Entry;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.stream.Collectors;
+import javax.annotation.Nullable;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.genius.infra.Datastore;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
@@ -340,9 +341,10 @@ public abstract class AbstractAclServiceImpl implements AclServiceListener {
                 continue;
             }
             AccessListEntries accessListEntries = acl.getAccessListEntries();
-            List<Ace> aceList = accessListEntries.getAce();
-            for (Ace ace: aceList) {
-                programAceRule(port, aclUuid.getValue(), ace, addOrRemove);
+            if (accessListEntries != null && accessListEntries.getAce() != null) {
+                for (Ace ace: accessListEntries.getAce()) {
+                    programAceRule(port, aclUuid.getValue(), ace, addOrRemove);
+                }
             }
         }
         return true;
@@ -357,7 +359,7 @@ public abstract class AbstractAclServiceImpl implements AclServiceListener {
      * @param addOrRemove whether to delete or add flow
      */
     protected void programAceRule(AclInterface port, String aclName, Ace ace, int addOrRemove) {
-        SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
+        SecurityRuleAttr aceAttr = AclServiceUtils.getAccessListAttributes(ace);
         if (!isValidDirection(aceAttr.getDirection())) {
             LOG.trace("Ignoring {} direction while processing for {} ACE Rule {}", aceAttr.getDirection(),
                     this.directionString, ace.getRuleName());
@@ -368,9 +370,8 @@ public abstract class AbstractAclServiceImpl implements AclServiceListener {
                 port.getInterfaceId());
 
         Matches matches = ace.getMatches();
-        Map<String, List<MatchInfoBase>> flowMap = null;
-        if (matches.getAceType() instanceof AceIp) {
-            flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
+        if (matches != null && matches.getAceType() instanceof AceIp) {
+            Map<String, List<MatchInfoBase>> flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
             if (!AclServiceUtils.doesAceHaveRemoteGroupId(aceAttr)) {
                 // programming for ACE which doesn't have any remote group Id
                 programForAceNotHavingRemoteAclId(port, aclName, ace, flowMap, addOrRemove);
@@ -383,7 +384,7 @@ public abstract class AbstractAclServiceImpl implements AclServiceListener {
     }
 
     protected void programForAceNotHavingRemoteAclId(AclInterface port, String aclName, Ace ace,
-            Map<String, List<MatchInfoBase>> flowMap, int addOrRemove) {
+            @Nullable Map<String, List<MatchInfoBase>> flowMap, int addOrRemove) {
         if (null == flowMap) {
             return;
         }
@@ -409,7 +410,7 @@ public abstract class AbstractAclServiceImpl implements AclServiceListener {
     }
 
     protected void programAceSpecificFlows(AclInterface port, String aclName, Ace ace,
-            Map<String, List<MatchInfoBase>> flowMap, Uuid remoteAclId, int addOrRemove) {
+            @Nullable Map<String, List<MatchInfoBase>> flowMap, Uuid remoteAclId, int addOrRemove) {
         if (null == flowMap) {
             return;
         }
@@ -602,7 +603,7 @@ public abstract class AbstractAclServiceImpl implements AclServiceListener {
     protected void handleRemoteAclUpdate(Acl aclBefore, Acl aclAfter, Collection<AclInterface> portsBefore) {
         String aclName = aclAfter.getAclName();
         Collection<AclInterface> interfaceList = aclDataUtil.getInterfaceList(new Uuid(aclName));
-        if (interfaceList == null || interfaceList.isEmpty()) {
+        if (interfaceList.isEmpty()) {
             LOG.trace("handleRemoteAclUpdate: No interfaces found with ACL={}", aclName);
             return;
         }
@@ -634,7 +635,7 @@ public abstract class AbstractAclServiceImpl implements AclServiceListener {
     private void programRemoteAclTable(String aclName, Set<Uuid> remoteAclIds, Set<BigInteger> dpns, int addOrRemove) {
         for (Uuid remoteAclId : remoteAclIds) {
             Collection<AclInterface> remoteAclInterfaces = aclDataUtil.getInterfaceList(remoteAclId);
-            if (remoteAclInterfaces == null || remoteAclInterfaces.isEmpty()) {
+            if (remoteAclInterfaces.isEmpty()) {
                 continue;
             }
             Set<AllowedAddressPairs> aaps =
@@ -729,7 +730,7 @@ public abstract class AbstractAclServiceImpl implements AclServiceListener {
     private void syncRemoteAclTableFromOtherDpns(AclInterface port, Uuid remoteAclId, int addOrRemove) {
         Collection<AclInterface> aclInterfaces = aclDataUtil.getInterfaceList(remoteAclId);
 
-        if (aclInterfaces != null && !aclInterfaces.isEmpty() && isFirstPortInDpnWithRemoteAclId(port, remoteAclId)) {
+        if (!aclInterfaces.isEmpty() && isFirstPortInDpnWithRemoteAclId(port, remoteAclId)) {
             Integer aclTag = aclServiceUtils.getAclTag(remoteAclId);
             for (AclInterface aclInterface : aclInterfaces) {
                 if (port.getInterfaceId().equals(aclInterface.getInterfaceId())) {
@@ -767,7 +768,7 @@ public abstract class AbstractAclServiceImpl implements AclServiceListener {
     protected abstract void programRemoteAclTableFlow(BigInteger dpId, Integer aclTag, AllowedAddressPairs aap,
             int addOrRemove);
 
-    protected Set<BigInteger> collectDpns(Map<String, Set<AclInterface>> mapAclWithPortSet) {
+    protected Set<BigInteger> collectDpns(@Nullable Map<String, Set<AclInterface>> mapAclWithPortSet) {
         Set<BigInteger> dpns = new HashSet<>();
         if (mapAclWithPortSet == null) {
             return dpns;
@@ -969,6 +970,7 @@ public abstract class AbstractAclServiceImpl implements AclServiceListener {
                 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
     }
 
+    @Nullable
     protected Long getElanIdFromAclInterface(String elanInterfaceName) {
         AclInterface aclInterface = aclInterfaceCache.get(elanInterfaceName);
         if (null != aclInterface) {
index dc8fd8ca04f2742e9ad8505aa99481d4fa09c166..4be2c34401cec45a749b8a59e55bef87731a20b5 100644 (file)
@@ -16,6 +16,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.BiConsumer;
 import java.util.function.BiFunction;
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import javax.inject.Singleton;
 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
@@ -79,6 +80,7 @@ public class AclInterfaceCacheImpl implements AclInterfaceCache {
     }
 
     @Override
+    @Nullable
     public AclInterface remove(String interfaceId) {
         AclInterface aclInterface = cache.get(interfaceId);
         if (aclInterface == null) {
index 9f87b617527e5839c7be5de8b3538c266a58d575..32326ac859c7e7447c3d68d044d38cfcd0fc2e7c 100644 (file)
@@ -10,11 +10,15 @@ package org.opendaylight.netvirt.aclservice.listeners;
 import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 import java.util.SortedSet;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.inject.Singleton;
@@ -121,13 +125,13 @@ public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEv
         // Find and update deleted ace rules in acl
         List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
 
-        if (interfacesBefore != null && aclClusterUtil.isEntityOwner()) {
+        if (aclClusterUtil.isEntityOwner()) {
             LOG.debug("On update event, remove Ace rules: {} for ACL: {}", deletedAceRules, aclName);
             updateAceRules(interfacesBefore, aclName, deletedAceRules, AclServiceManager.Action.REMOVE);
         }
         updateAclCaches(aclBefore, aclAfter, interfacesBefore);
 
-        if (interfacesBefore != null && aclClusterUtil.isEntityOwner()) {
+        if (aclClusterUtil.isEntityOwner()) {
             LOG.debug("On update event, add Ace rules: {} for ACL: {}", addedAceRules, aclName);
             updateAceRules(interfacesBefore, aclName, addedAceRules, AclServiceManager.Action.ADD);
 
@@ -173,7 +177,7 @@ public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEv
      * @param aclName the acl name
      * @param action the action
      */
-    private void updateRemoteAclCache(List<Ace> aceList, String aclName, AclServiceManager.Action action) {
+    private void updateRemoteAclCache(@Nullable List<Ace> aceList, String aclName, AclServiceManager.Action action) {
         if (null == aceList) {
             return;
         }
@@ -251,21 +255,26 @@ public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEv
         return this;
     }
 
+    @Nonnull
     private List<Ace> getChangedAceList(Acl updatedAcl, Acl currentAcl) {
         if (updatedAcl == null) {
-            return null;
+            return Collections.emptyList();
         }
-        List<Ace> updatedAceList = updatedAcl.getAccessListEntries() == null ? new ArrayList<>()
+        List<Ace> updatedAceList =
+            updatedAcl.getAccessListEntries() == null || updatedAcl.getAccessListEntries().getAce() == null
+                ? new ArrayList<>()
                 : new ArrayList<>(updatedAcl.getAccessListEntries().getAce());
         if (currentAcl == null) {
             return updatedAceList;
         }
-        List<Ace> currentAceList = currentAcl.getAccessListEntries() == null ? new ArrayList<>()
+        List<Ace> currentAceList =
+            currentAcl.getAccessListEntries() == null || currentAcl.getAccessListEntries().getAce() == null
+                ? new ArrayList<>()
                 : new ArrayList<>(currentAcl.getAccessListEntries().getAce());
         for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext();) {
             Ace ace1 = iterator.next();
             for (Ace ace2 : currentAceList) {
-                if (ace1.getRuleName().equals(ace2.getRuleName())) {
+                if (Objects.equals(ace1.getRuleName(), ace2.getRuleName())) {
                     iterator.remove();
                 }
             }
index 43eb661cbc4c8df0c3d26af01265732c9a178f3a..f36b7a5a1b73e96657a11b79e8d5ea5b1f7fd7a8 100644 (file)
@@ -8,8 +8,8 @@
 package org.opendaylight.netvirt.aclservice.listeners;
 
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.List;
+import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.inject.Singleton;
@@ -98,7 +98,7 @@ public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interf
     }
 
     @Override
-    public void update(InstanceIdentifier<Interface> key, Interface portBefore, Interface portAfter) {
+    public void update(@Nullable InstanceIdentifier<Interface> key, Interface portBefore, Interface portAfter) {
         if (portBefore.augmentation(ParentRefs.class) == null
                 && portAfter.augmentation(ParentRefs.class) != null) {
             LOG.trace("Ignoring event for update in ParentRefs for {} ", portAfter.getName());
@@ -138,9 +138,8 @@ public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interf
                             isPortSecurityEnable);
                     aclServiceManager.notify(aclInterfaceAfter, null, Action.UNBIND);
                 }
-                if (interfaceState != null && interfaceState.getOperStatus().equals(
-                        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
-                            .state.Interface.OperStatus.Up)) {
+                if (interfaceState != null && org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces
+                        .rev140508.interfaces.state.Interface.OperStatus.Up.equals(interfaceState.getOperStatus())) {
                     // if port security enable is changed and is enabled, bind ACL service
                     if (isPortSecurityEnableBefore != isPortSecurityEnable && isPortSecurityEnable) {
                         LOG.debug("Notify bind ACL service for interface={}, isPortSecurityEnable={}", interfaceId,
@@ -179,17 +178,14 @@ public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interf
                 && aclInPortAfter.isPortSecurityEnabled();
     }
 
-    private boolean isSecurityGroupsChanged(List<Uuid> sgsBefore, List<Uuid> sgsAfter) {
+    private boolean isSecurityGroupsChanged(@Nullable List<Uuid> sgsBefore, @Nullable List<Uuid> sgsAfter) {
         if (sgsBefore == null && sgsAfter == null) {
             return false;
         }
-        if ((sgsBefore == null && sgsAfter != null) || (sgsBefore != null && sgsAfter == null)) {
+        if (sgsBefore == null || sgsAfter == null) {
             return true;
         }
-        if (sgsBefore != null && sgsAfter != null) {
-            return !(new HashSet<>(sgsBefore)).equals(new HashSet<>(sgsAfter));
-        }
-        return true;
+        return !(sgsBefore.containsAll(sgsAfter) && sgsAfter.containsAll(sgsBefore));
     }
 
     private AclInterface addOrUpdateAclInterfaceCache(String interfaceId, InterfaceAcl aclInPort) {
@@ -197,8 +193,8 @@ public class AclInterfaceListener extends AsyncDataTreeChangeListenerBase<Interf
     }
 
     private AclInterface addOrUpdateAclInterfaceCache(String interfaceId, InterfaceAcl aclInPort, boolean isSgChanged,
-            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
-                    .Interface interfaceState) {
+            @Nullable org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
+                .state.Interface interfaceState) {
         AclInterface aclInterface = aclInterfaceCache.addOrUpdate(interfaceId, (prevAclInterface, builder) -> {
             List<Uuid> sgs = new ArrayList<>();
             if (aclInPort != null) {
index 02efa438a67ae2241aef8f55b0529443ac8c44fc..64c425299d6d6fc31122da9536c3c2494887f202 100644 (file)
@@ -12,6 +12,7 @@ import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
 
 import java.math.BigInteger;
 import java.util.Collections;
+import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.inject.Singleton;
@@ -55,6 +56,7 @@ public class AclNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapable
     private final AclServiceUtils aclServiceUtils;
     private final JobCoordinator jobCoordinator;
 
+    @Nullable
     private SecurityGroupMode securityGroupMode = null;
 
     @Inject
index cef2eff39e46a464eeb5ddc0ea3b6b480555b9e9..79869246ef320b0459b3f1eb2838ce37a3505961 100644 (file)
@@ -64,7 +64,8 @@ public class AclInstanceRecoveryHandler implements ServiceRecoveryInterface {
                 Interface interfaceBefore = interfaceOptional.get();
                 LOG.debug("Starting Recovery of acl Instance {} for interface {}", entityId, interfaceBefore.getName());
                 InterfaceAcl interfaceAclBefore = interfaceBefore.augmentation(InterfaceAcl.class);
-                List<Uuid> sgList = new ArrayList<>(interfaceAclBefore.getSecurityGroups());
+                List<Uuid> sgList = interfaceAclBefore.getSecurityGroups() != null ? new ArrayList<>(
+                    interfaceAclBefore.getSecurityGroups()) : new ArrayList<>();
                 sgList.remove(aclId);
                 InterfaceAcl interfaceAclAfter = new InterfaceAclBuilder(interfaceAclBefore).setSecurityGroups(sgList)
                         .build();
index 53591a6d09f04806196e461d01d13def917193c8..941a8cb620c9c75574fc360b43bd576d6c2f2f21 100644 (file)
@@ -10,13 +10,16 @@ package org.opendaylight.netvirt.aclservice.stats;
 
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+import javax.annotation.Nullable;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
 import org.opendaylight.genius.mdsalutil.NwConstants;
 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
+import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInputBuilder;
@@ -42,7 +45,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.acl.drop.stats.BytesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.acl.drop.stats.PacketsBuilder;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -70,14 +72,14 @@ public final class AclLiveStatisticsHelper {
      * @param dataBroker the data broker
      * @return the acl port stats
      */
-    public static List<AclPortStats> getAclPortStats(Direction direction, List<String> interfaceNames,
+    public static List<AclPortStats> getAclPortStats(Direction direction, @Nullable List<String> interfaceNames,
             OpendaylightDirectStatisticsService odlDirectStatsService, DataBroker dataBroker) {
         LOG.trace("Get ACL port stats for direction {} and interfaces {}", direction, interfaceNames);
         List<AclPortStats> lstAclPortStats = new ArrayList<>();
 
         FlowCookie aclDropFlowCookieMask = new FlowCookie(COOKIE_ACL_DROP_FLOW_MASK);
 
-        for (String interfaceName : interfaceNames) {
+        for (String interfaceName : AclDataUtil.requireNonNullElse(interfaceNames, Collections.<String>emptyList())) {
             AclPortStatsBuilder aclStatsBuilder = new AclPortStatsBuilder().setInterfaceName(interfaceName);
 
             Interface interfaceState = AclServiceUtils.getInterfaceStateFromOperDS(dataBroker, interfaceName);
@@ -131,14 +133,11 @@ public final class AclLiveStatisticsHelper {
      * @param rpcResult the rpc result
      */
     private static void handleRpcErrors(List<AclPortStats> lstAclPortStats, AclPortStatsBuilder aclStatsBuilder,
-            RpcResult<GetFlowStatisticsOutput> rpcResult) {
+            @Nullable RpcResult<GetFlowStatisticsOutput> rpcResult) {
         LOG.error("Unable to retrieve drop counts due to error: {}", rpcResult);
         String errMsg = "Unable to retrieve drop counts due to error: ";
-        if (rpcResult != null && rpcResult.getErrors() != null) {
-            for (RpcError error : rpcResult.getErrors()) {
-                errMsg += error.getMessage();
-                break;
-            }
+        if (rpcResult != null && rpcResult.getErrors() != null && !rpcResult.getErrors().isEmpty()) {
+            errMsg += rpcResult.getErrors().iterator().next().getMessage();
         } else {
             errMsg += "Internal RPC call failed.";
         }
@@ -170,11 +169,11 @@ public final class AclLiveStatisticsHelper {
         for (FlowAndStatisticsMapList flowStats : flowAndStatisticsMapList) {
             switch (flowStats.getTableId()) {
                 case NwConstants.INGRESS_ACL_FILTER_CUM_DISPATCHER_TABLE:
-                    if (flowStats.getPriority().equals(AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY)) {
+                    if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority())) {
                         portEgressBytesBuilder.setInvalidDropCount(flowStats.getByteCount().getValue());
                         portEgressPacketsBuilder.setInvalidDropCount(flowStats.getPacketCount().getValue());
-                    } else if (flowStats.getPriority().equals(AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY)
-                            || flowStats.getPriority().equals(AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY)) {
+                    } else if (AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY.equals(flowStats.getPriority())
+                            || AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY.equals(flowStats.getPriority())) {
                         BigInteger portEgressBytesBuilderDropCount = BigInteger.valueOf(0);
                         BigInteger portEgressPacketsBuilderDropCount = BigInteger.valueOf(0);
                         if (portEgressBytesBuilder.getDropCount() != null) {
@@ -193,11 +192,11 @@ public final class AclLiveStatisticsHelper {
                     break;
 
                 case NwConstants.EGRESS_ACL_FILTER_CUM_DISPATCHER_TABLE:
-                    if (flowStats.getPriority().equals(AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY)) {
+                    if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority())) {
                         portIngressBytesBuilder.setInvalidDropCount(flowStats.getByteCount().getValue());
                         portIngressPacketsBuilder.setInvalidDropCount(flowStats.getPacketCount().getValue());
-                    } else if (flowStats.getPriority().equals(AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY)
-                            || flowStats.getPriority().equals(AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY)) {
+                    } else if (AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY.equals(flowStats.getPriority())
+                            || AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY.equals(flowStats.getPriority())) {
                         BigInteger portIngressBytesBuilderDropCount = BigInteger.valueOf(0);
                         BigInteger portIngressPacketsBuilderDropCount = BigInteger.valueOf(0);
                         if (portIngressBytesBuilder.getDropCount() != null) {
@@ -215,13 +214,13 @@ public final class AclLiveStatisticsHelper {
                     // TODO: Update stats for other drops
                     break;
                 case NwConstants.INGRESS_ACL_COMMITTER_TABLE:
-                    if (flowStats.getPriority().equals(AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY)) {
+                    if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority())) {
                         portEgressBytesBuilder.setAntiSpoofDropCount(flowStats.getByteCount().getValue());
                         portEgressPacketsBuilder.setAntiSpoofDropCount(flowStats.getPacketCount().getValue());
                     }
                     break;
                 case NwConstants.EGRESS_ACL_COMMITTER_TABLE:
-                    if (flowStats.getPriority().equals(AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY)) {
+                    if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority())) {
                         portIngressBytesBuilder.setAntiSpoofDropCount(flowStats.getByteCount().getValue());
                         portIngressPacketsBuilder.setAntiSpoofDropCount(flowStats.getPacketCount().getValue());
                     }
index 9e46d99484da62a1f38218c067205bab4e9040ab..0ca87944899dfeb035539992898f325ee72975f6 100644 (file)
@@ -8,6 +8,8 @@
 
 package org.opendaylight.netvirt.aclservice.utils;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
 import java.math.BigInteger;
@@ -21,6 +23,8 @@ import java.util.Map.Entry;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import javax.inject.Singleton;
 import org.opendaylight.netvirt.aclservice.api.utils.AclDataCache;
 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
@@ -95,6 +99,7 @@ public class AclDataUtil implements AclDataCache {
     }
 
     @Override
+    @Nonnull
     public Collection<AclInterface> getInterfaceList(Uuid acl) {
         final ConcurrentMap<String, AclInterface> interfaceMap = aclInterfaceMap.get(acl);
         return interfaceMap != null ? interfaceMap.values() : Collections.emptySet();
@@ -109,6 +114,7 @@ public class AclDataUtil implements AclDataCache {
      * @return the set of ACL interfaces per ACL (in a map) which has specified
      *         remote ACL ID.
      */
+    @Nullable
     public Map<String, Set<AclInterface>> getRemoteAclInterfaces(Uuid remoteAclId,
             Class<? extends DirectionBase> direction) {
         Collection<Uuid> remoteAclList = getRemoteAcl(remoteAclId, direction);
@@ -118,10 +124,9 @@ public class AclDataUtil implements AclDataCache {
 
         Map<String, Set<AclInterface>> mapOfAclWithInterfaces = new HashMap<>();
         for (Uuid acl : remoteAclList) {
-            Set<AclInterface> interfaceSet = new HashSet<>();
             Collection<AclInterface> interfaces = getInterfaceList(acl);
-            if (interfaces != null && !interfaces.isEmpty()) {
-                interfaceSet.addAll(interfaces);
+            if (!interfaces.isEmpty()) {
+                Set<AclInterface> interfaceSet = new HashSet<>(interfaces);
                 mapOfAclWithInterfaces.put(acl.getValue(), interfaceSet);
             }
         }
@@ -238,4 +243,10 @@ public class AclDataUtil implements AclDataCache {
     public Map<String, Acl> getAclMap() {
         return ImmutableMap.copyOf(aclMap);
     }
+
+    // Use Objects.requireNonNullElse instead with JDK9+
+    @Nonnull
+    public static <T> T requireNonNullElse(@Nullable T obj, @Nonnull T defaultObj) {
+        return obj != null ? obj : requireNonNull(defaultObj);
+    }
 }
index 2ff6c3fd86282229d33a8694c120a33306461bb7..02d8df61433717b39c404975beeb66a5732c0633 100644 (file)
@@ -13,6 +13,7 @@ import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import javax.annotation.Nullable;
 import org.opendaylight.genius.infra.Datastore.Configuration;
 import org.opendaylight.genius.infra.TypedWriteTransaction;
 import org.opendaylight.genius.mdsalutil.ActionInfo;
@@ -254,7 +255,8 @@ public class AclNodeDefaultFlowsTxBuilder {
         addGotoOrResubmitTableMissFlow(tableId, nextTableId, null);
     }
 
-    private void addGotoOrResubmitTableMissFlow(short tableId, short nextTableId, List<InstructionInfo> instructions) {
+    private void addGotoOrResubmitTableMissFlow(short tableId, short nextTableId,
+            @Nullable List<InstructionInfo> instructions) {
         List<MatchInfoBase> matches = Collections.emptyList();
         List<InstructionInfo> ins = getGotoOrResubmitInstructions(tableId, nextTableId);
         if (instructions != null && !instructions.isEmpty()) {
index abb839bdc66e656570deeb71b1f8fb7b616f3e38..cd48ac37d0df3e6902ea40fc09e2051fb531d5e4 100644 (file)
@@ -14,6 +14,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.function.BiFunction;
+import javax.annotation.Nullable;
 import org.opendaylight.genius.mdsalutil.ActionInfo;
 import org.opendaylight.genius.mdsalutil.InstructionInfo;
 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
@@ -60,6 +61,7 @@ public final class AclServiceOFFlowBuilder {
      *            the matches
      * @return the map containing the flows and the respective flow id
      */
+    @Nullable
     public static Map<String, List<MatchInfoBase>> programIpFlow(Matches matches) {
         if (matches != null) {
             AceIp acl = (AceIp) matches.getAceType();
index ba90703de57498aadf446152dad21bdd845b9fc6..451ca998b9413075ed23ff0170273e18d491c755 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
@@ -214,6 +215,7 @@ public final class AclServiceUtils {
      * @param interfaceName the interface name.
      * @return the interface state.
      */
+    @Nullable
     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
         .Interface getInterfaceStateFromOperDS(DataBroker dataBroker, String interfaceName) {
         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
@@ -241,7 +243,8 @@ public final class AclServiceUtils {
      * @param ace the access list entry
      * @return the security rule attributes
      */
-    public static SecurityRuleAttr  getAccesssListAttributes(Ace ace) {
+    @Nullable
+    public static SecurityRuleAttr getAccessListAttributes(Ace ace) {
         if (ace == null) {
             LOG.error("Ace is Null");
             return null;
@@ -383,9 +386,10 @@ public final class AclServiceUtils {
         return newAclList;
     }
 
+    @Nullable
     public static List<AllowedAddressPairs> getUpdatedAllowedAddressPairs(
-            List<AllowedAddressPairs> updatedAllowedAddressPairs,
-            List<AllowedAddressPairs> currentAllowedAddressPairs) {
+            @Nullable List<AllowedAddressPairs> updatedAllowedAddressPairs,
+            @Nullable List<AllowedAddressPairs> currentAllowedAddressPairs) {
         if (updatedAllowedAddressPairs == null) {
             return null;
         }
@@ -406,6 +410,7 @@ public final class AclServiceUtils {
         return newAllowedAddressPairs;
     }
 
+    @Nullable
     public static BigInteger getDpIdFromIterfaceState(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
             .interfaces.rev140508.interfaces.state.Interface interfaceState) {
         BigInteger dpId = null;
@@ -695,7 +700,8 @@ public final class AclServiceUtils {
         return isNotIpv4AllNetwork(aap) && isNotIpv6AllNetwork(aap);
     }
 
-    public static Long getElanIdFromInterface(String elanInterfaceName,DataBroker broker) {
+    @Nullable
+    public static Long getElanIdFromInterface(String elanInterfaceName, DataBroker broker) {
         ElanInterface elanInterface = getElanInterfaceByElanInterfaceName(elanInterfaceName, broker);
         if (null != elanInterface) {
             ElanInstance elanInfo = getElanInstanceByName(elanInterface.getElanInstanceName(), broker);
@@ -704,6 +710,7 @@ public final class AclServiceUtils {
         return null;
     }
 
+    @Nullable
     public static ElanInterface getElanInterfaceByElanInterfaceName(String elanInterfaceName,DataBroker broker) {
         InstanceIdentifier<ElanInterface> elanInterfaceId = getElanInterfaceConfigurationDataPathId(elanInterfaceName);
         return read(broker, LogicalDatastoreType.CONFIGURATION, elanInterfaceId).orNull();
@@ -715,6 +722,7 @@ public final class AclServiceUtils {
     }
 
     // elan-instances config container
+    @Nullable
     public static ElanInstance getElanInstanceByName(String elanInstanceName, DataBroker broker) {
         InstanceIdentifier<ElanInstance> elanIdentifierId = getElanInstanceConfigurationDataPath(elanInstanceName);
         return read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId).orNull();
@@ -725,6 +733,7 @@ public final class AclServiceUtils {
                 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
     }
 
+    @Nullable
     public List<SubnetInfo> getSubnetInfo(String portId) {
         InstanceIdentifier<PortSubnet> id = InstanceIdentifier.builder(PortSubnets.class)
                 .child(PortSubnet.class, new PortSubnetKey(portId)).build();
@@ -784,6 +793,7 @@ public final class AclServiceUtils {
      * @param aclId the acl id
      * @return the acl tag
      */
+    @Nullable
     public Integer getAclTag(final Uuid aclId) {
         String aclName = aclId.getValue();
         Integer aclTag = this.aclDataUtil.getAclTag(aclName);
@@ -979,7 +989,7 @@ public final class AclServiceUtils {
         return aceAttr != null && aceAttr.getRemoteGroupId() != null;
     }
 
-    public SortedSet<Integer> getRemoteAclTags(List<Uuid> aclIds, Class<? extends DirectionBase> direction) {
+    public SortedSet<Integer> getRemoteAclTags(@Nullable List<Uuid> aclIds, Class<? extends DirectionBase> direction) {
         SortedSet<Integer> remoteAclTags = new TreeSet<>();
         Set<Uuid> remoteAclIds = getRemoteAclIdsByDirection(aclIds, direction);
         for (Uuid remoteAclId : remoteAclIds) {
@@ -991,7 +1001,7 @@ public final class AclServiceUtils {
         return remoteAclTags;
     }
 
-    public Set<Uuid> getRemoteAclIdsByDirection(List<Uuid> aclIds, Class<? extends DirectionBase> direction) {
+    public Set<Uuid> getRemoteAclIdsByDirection(@Nullable List<Uuid> aclIds, Class<? extends DirectionBase> direction) {
         Set<Uuid> remoteAclIds = new HashSet<>();
         if (aclIds == null || aclIds.isEmpty()) {
             return remoteAclIds;
@@ -1013,8 +1023,8 @@ public final class AclServiceUtils {
         AccessListEntries accessListEntries = acl.getAccessListEntries();
         if (accessListEntries != null && accessListEntries.getAce() != null) {
             for (Ace ace : accessListEntries.getAce()) {
-                SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
-                if (aceAttr.getDirection().equals(direction) && doesAceHaveRemoteGroupId(aceAttr)) {
+                SecurityRuleAttr aceAttr = AclServiceUtils.getAccessListAttributes(ace);
+                if (Objects.equals(aceAttr.getDirection(), direction) && doesAceHaveRemoteGroupId(aceAttr)) {
                     remoteAclIds.add(aceAttr.getRemoteGroupId());
                 }
             }
@@ -1283,12 +1293,14 @@ public final class AclServiceUtils {
         return instructions;
     }
 
-    public static List<AllowedAddressPairs> excludeMulticastAAPs(List<AllowedAddressPairs> allowedAddresses) {
+    public static List<AllowedAddressPairs> excludeMulticastAAPs(@Nullable List<AllowedAddressPairs> allowedAddresses) {
         List<AllowedAddressPairs> filteredAAPs = new ArrayList<>();
-        for (AllowedAddressPairs allowedAddress : allowedAddresses) {
-            InetAddress inetAddr = getInetAddress(allowedAddress.getIpAddress());
-            if (inetAddr != null && !inetAddr.isMulticastAddress()) {
-                filteredAAPs.add(allowedAddress);
+        if (allowedAddresses != null) {
+            for (AllowedAddressPairs allowedAddress : allowedAddresses) {
+                InetAddress inetAddr = getInetAddress(allowedAddress.getIpAddress());
+                if (inetAddr != null && !inetAddr.isMulticastAddress()) {
+                    filteredAAPs.add(allowedAddress);
+                }
             }
         }
         return filteredAAPs;
@@ -1298,9 +1310,9 @@ public final class AclServiceUtils {
         return NetvirtAcl.class.toString();
     }
 
+    @Nullable
     private static InetAddress getInetAddress(IpPrefixOrAddress ipPrefixOrAddress) {
-        InetAddress inetAddress = null;
-        String addr = null;
+        String addr;
 
         IpPrefix ipPrefix = ipPrefixOrAddress.getIpPrefix();
         if (ipPrefix != null) {
@@ -1315,12 +1327,11 @@ public final class AclServiceUtils {
             }
         }
         try {
-            inetAddress = InetAddress.getByName(addr);
+            return InetAddress.getByName(addr);
         } catch (UnknownHostException e) {
             LOG.error("Invalid address : {}", addr, e);
             return null;
         }
-        return inetAddress;
     }
 
     public static Boolean isIpv6Subnet(List<SubnetInfo> subnetInfoList) {