BUG 8914: Fix NPE in sfc.classifier-impl
[netvirt.git] / vpnservice / sfc / classifier / impl / src / main / java / org / opendaylight / netvirt / sfc / classifier / service / domain / impl / ConfigurationClassifierImpl.java
index 65ec59f1637d2320567b96b4ee55e8c72b2ce05a..b1114dc68eda42a5eeefeaad30b9096cf7d06c28 100644 (file)
@@ -8,8 +8,9 @@
 
 package org.opendaylight.netvirt.sfc.classifier.service.domain.impl;
 
-import java.util.AbstractMap;
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -78,9 +79,10 @@ public class ConfigurationClassifierImpl implements ClassifierState {
 
     public List<Acl> readAcls() {
         InstanceIdentifier<AccessLists> aclsIID = InstanceIdentifier.builder(AccessLists.class).build();
-        AccessLists acls = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, aclsIID).orNull();
+        com.google.common.base.Optional<AccessLists> acls =
+                MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, aclsIID);
         LOG.trace("Acls read from datastore: {}", acls);
-        return Optional.ofNullable(acls).map(AccessLists::getAcl).orElse(Collections.emptyList());
+        return acls.transform(AccessLists::getAcl).or(Collections.emptyList());
     }
 
     public Set<ClassifierRenderableEntry> getEntries(Ace ace) {
@@ -89,18 +91,18 @@ public class ConfigurationClassifierImpl implements ClassifierState {
 
         Matches matches = ace.getMatches();
 
-        if (Objects.isNull(matches)) {
+        if (matches == null) {
             LOG.trace("Ace has no matches");
             return Collections.emptySet();
         }
 
-        RenderedServicePath rsp =  Optional.ofNullable(ace.getActions())
+        RenderedServicePath rsp = Optional.ofNullable(ace.getActions())
                 .map(actions -> actions.getAugmentation(RedirectToSfc.class))
                 .map(NetvirtsfcAclActions::getRspName)
                 .flatMap(sfcProvider::getRenderedServicePath)
                 .orElse(null);
 
-        if (Objects.isNull(rsp)) {
+        if (rsp == null) {
             LOG.trace("Ace has no valid SFC redirect action");
             return Collections.emptySet();
         }
@@ -108,7 +110,7 @@ public class ConfigurationClassifierImpl implements ClassifierState {
         Long nsp = rsp.getPathId();
         Short nsi = rsp.getStartingIndex();
 
-        if (Objects.isNull(nsp) || Objects.isNull(nsi)) {
+        if (nsp == null || nsi == null) {
             LOG.trace("RSP has no valid NSI or NSP");
             return Collections.emptySet();
         }
@@ -117,23 +119,20 @@ public class ConfigurationClassifierImpl implements ClassifierState {
                 .flatMap(geniusProvider::getIpFromInterfaceName)
                 .orElse(null);
 
-        if (Objects.isNull(firstHopIp)) {
+        if (firstHopIp == null) {
             LOG.trace("Could not acquire a valid first RSP hop destination ip");
             return Collections.emptySet();
         }
 
-        Map<NodeId, List<InterfaceKey>> nodeToInterfaces = Optional.ofNullable(matches.getAugmentation(NeutronNetwork
-                .class))
-                .map(netvirtProvider::getLogicalInterfacesFromNeutronNetwork)
-                .orElse(Collections.emptyList())
-                .stream()
-                .map(iface -> new AbstractMap.SimpleEntry<>(
-                        new InterfaceKey(iface),
-                        geniusProvider.getNodeIdFromLogicalInterface(iface).orElse(null)))
-                .filter(entry -> Objects.nonNull(entry.getValue()))
-                .collect(Collectors.groupingBy(
-                        AbstractMap.Entry::getValue,
-                        Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
+        Map<NodeId, List<InterfaceKey>> nodeToInterfaces = new HashMap<>();
+        NeutronNetwork neutronNetwork = matches.getAugmentation(NeutronNetwork.class);
+        if (neutronNetwork != null) {
+            for (String iface : netvirtProvider.getLogicalInterfacesFromNeutronNetwork(neutronNetwork)) {
+                geniusProvider.getNodeIdFromLogicalInterface(iface).ifPresent(
+                    nodeId -> nodeToInterfaces.computeIfAbsent(nodeId, key -> new ArrayList<>()).add(
+                            new InterfaceKey(iface)));
+            }
+        }
 
         LOG.trace("Got classifier nodes and interfaces: {}", nodeToInterfaces);
 
@@ -177,8 +176,8 @@ public class ConfigurationClassifierImpl implements ClassifierState {
             List<String> interfaceUuidStrList = geniusProvider.getInterfacesFromNode(nodeId);
             interfaceUuidStrList.forEach(interfaceUuidStr -> {
                 InterfaceKey interfaceKey = new InterfaceKey(interfaceUuidStr);
-                String remoteIp = geniusProvider.getRemoteIpAddress(interfaceUuidStr);
-                entries.add(ClassifierEntry.buildEgressEntry(interfaceKey, remoteIp == null ? nodeIp : remoteIp));
+                Optional<String> remoteIp = geniusProvider.getRemoteIpAddress(interfaceUuidStr);
+                entries.add(ClassifierEntry.buildEgressEntry(interfaceKey, remoteIp.orElse(nodeIp)));
             });
         });