Handle missing LLDP nbr-list 22/103722/1
authorJonas Mårtensson <jonas.martensson@smartoptics.com>
Tue, 6 Sep 2022 07:04:59 +0000 (07:04 +0000)
committerGilles Thouenon <gilles.thouenon@orange.com>
Tue, 20 Dec 2022 14:29:53 +0000 (15:29 +0100)
In ROADM-to-ROADM link discovery, if LLDP subtree is present but
nbr-list is empty/missing, the code crashes from NullPointerException.

Check and return early if nbr-list is null.

JIRA: TRNSPRTPCE-709
Signed-off-by: Jonas Mårtensson <jonas.martensson@smartoptics.com>
Change-Id: I6dcb54e24445ac4c0fd0ff27e7335eb596307418

networkmodel/src/main/java/org/opendaylight/transportpce/networkmodel/R2RLinkDiscovery.java
tapi/src/main/java/org/opendaylight/transportpce/tapi/R2RTapiLinkDiscovery.java

index 5ff7d39cf47204ba5f5097d27192deae5825b75d..4061c088b1ffb9d2719708b5d656b34be9477c68 100644 (file)
@@ -16,7 +16,6 @@ import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
-import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.MountPoint;
 import org.opendaylight.mdsal.binding.api.ReadTransaction;
@@ -69,9 +68,8 @@ public class R2RLinkDiscovery {
                 Optional<Protocols> protocol121Object = this.deviceTransactionManager.getDataFromDevice(
                     nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocols121IID, Timeouts.DEVICE_READ_TIMEOUT,
                     Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                if (!protocol121Object.isPresent()
-                        || (protocol121Object.get().augmentation(Protocols1.class) == null)) {
-                    LOG.warn("LLDP subtree is missing : isolated openroadm device");
+                if (hasNoNeighbor121(protocol121Object)) {
+                    LOG.warn("LLDP subtree is missing or incomplete: isolated openroadm device");
                     return false;
                 }
                 // get neighbor list
@@ -90,18 +88,16 @@ public class R2RLinkDiscovery {
                         org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container
                             .org.openroadm.device.Protocols.class)
                     .build();
-                Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
-                    .container.org.openroadm.device.Protocols> protocol221Object = this.deviceTransactionManager
+                var protocol221Object = this.deviceTransactionManager
                     .getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocols221IID,
                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                if (!protocol221Object.isPresent() || (protocol221Object.get().augmentation(
-                        org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class) == null)) {
-                    LOG.warn("LLDP subtree is missing : isolated openroadm device");
+                if (hasNoNeighbor221(protocol221Object)) {
+                    LOG.warn("LLDP subtree is missing or incomplete: isolated openroadm device");
                     return false;
                 }
-                org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.@Nullable NbrList
-                    nbr221List = protocol221Object.get().augmentation(org.opendaylight.yang.gen.v1.http
-                        .org.openroadm.lldp.rev181019.Protocols1.class).getLldp().getNbrList();
+                var nbr221List = protocol221Object.get().augmentation(
+                        org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class)
+                    .getLldp().getNbrList();
                 LOG.info("LLDP subtree is present. Device has {} neighbours", nbr221List.getIfName().size());
                 return rdm2rdmLinkCreatedv221(nodeId, nbr221List);
             case OPENROADM_DEVICE_VERSION_7_1:
@@ -113,6 +109,27 @@ public class R2RLinkDiscovery {
         }
     }
 
+    private boolean hasNoNeighbor121(Optional<Protocols> protocol121Object) {
+        return protocol121Object.isEmpty()
+                || protocol121Object.get().augmentation(Protocols1.class) == null
+                || protocol121Object.get().augmentation(Protocols1.class).getLldp() == null
+                || protocol121Object.get().augmentation(Protocols1.class).getLldp().getNbrList() == null;
+    }
+
+    private boolean hasNoNeighbor221(Optional<
+            org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org
+                    .openroadm.device.Protocols> protocol221Object) {
+        return protocol221Object.isEmpty()
+                || protocol221Object.get().augmentation(
+                        org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class) == null
+                || protocol221Object.get().augmentation(
+                        org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class)
+                    .getLldp() == null
+                || protocol221Object.get().augmentation(
+                        org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class)
+                    .getLldp().getNbrList() == null;
+    }
+
     private boolean rdm2rdmLinkCreatedv221(NodeId nodeId,
             org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.NbrList nbrList) {
         boolean success = true;
index e6e15ceac056c4ffb5515847d232288af724be14..a7800616ce2d5e0f81c86c184c4ace297afbe694 100644 (file)
@@ -15,7 +15,6 @@ import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
-import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.api.MountPoint;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.Timeouts;
@@ -72,9 +71,8 @@ public class R2RTapiLinkDiscovery {
                 Optional<Protocols> protocol121Object = this.deviceTransactionManager.getDataFromDevice(
                     nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocols121IID, Timeouts.DEVICE_READ_TIMEOUT,
                     Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                if (!protocol121Object.isPresent()
-                        || (protocol121Object.get().augmentation(Protocols1.class) == null)) {
-                    LOG.warn("LLDP subtree is missing : isolated openroadm device");
+                if (hasNoNeighbor121(protocol121Object)) {
+                    LOG.warn("LLDP subtree is missing or incomplete: isolated openroadm device");
                     return new HashMap<>();
                 }
                 // get neighbor list
@@ -93,18 +91,16 @@ public class R2RTapiLinkDiscovery {
                     .child(org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
                         .container.org.openroadm.device.Protocols.class)
                     .build();
-                Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device
-                    .container.org.openroadm.device.Protocols> protocol221Object = this.deviceTransactionManager
+                var protocol221Object = this.deviceTransactionManager
                     .getDataFromDevice(nodeId.getValue(), LogicalDatastoreType.OPERATIONAL, protocols221IID,
                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                if (!protocol221Object.isPresent() || (protocol221Object.get().augmentation(
-                        org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class) == null)) {
-                    LOG.warn("LLDP subtree is missing : isolated openroadm device");
+                if (hasNoNeighbor221(protocol221Object)) {
+                    LOG.warn("LLDP subtree is missing or incomplete: isolated openroadm device");
                     return new HashMap<>();
                 }
-                org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.@Nullable NbrList
-                    nbr221List = protocol221Object.get().augmentation(org.opendaylight.yang.gen.v1.http
-                        .org.openroadm.lldp.rev181019.Protocols1.class).getLldp().getNbrList();
+                var nbr221List = protocol221Object.get().augmentation(
+                        org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class)
+                    .getLldp().getNbrList();
                 LOG.info("LLDP subtree is present. Device has {} neighbours", nbr221List.getIfName().size());
                 return rdm2rdmLinkCreatev221(nodeId, tapiTopoUuid, nbr221List);
             case 3:
@@ -117,6 +113,27 @@ public class R2RTapiLinkDiscovery {
         }
     }
 
+    private boolean hasNoNeighbor121(Optional<Protocols> protocol121Object) {
+        return protocol121Object.isEmpty()
+                || protocol121Object.get().augmentation(Protocols1.class) == null
+                || protocol121Object.get().augmentation(Protocols1.class).getLldp() == null
+                || protocol121Object.get().augmentation(Protocols1.class).getLldp().getNbrList() == null;
+    }
+
+    private boolean hasNoNeighbor221(Optional<
+            org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org
+                    .openroadm.device.Protocols> protocol221Object) {
+        return protocol221Object.isEmpty()
+                || protocol221Object.get().augmentation(
+                        org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class) == null
+                || protocol221Object.get().augmentation(
+                        org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class)
+                    .getLldp() == null
+                || protocol221Object.get().augmentation(
+                        org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.Protocols1.class)
+                    .getLldp().getNbrList() == null;
+    }
+
     private Map<LinkKey, Link> rdm2rdmLinkCreatev221(NodeId nodeId, Uuid tapiTopoUuid,
             org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev181019.lldp.container.lldp.NbrList nbrList) {
         Map<LinkKey, Link> linkMap = new HashMap<>();