Don't use NotificationListener (NodeRegistration)
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / listeners / AlarmNotificationListener221.java
index 3564623300af18758bb1c663f2e92283070f1c51..d7336eb559d6cbdff72f373920e29569bde88d81 100644 (file)
@@ -10,9 +10,10 @@ package org.opendaylight.transportpce.networkmodel.listeners;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Optional;
+import java.util.Set;
 import java.util.concurrent.ExecutionException;
-
 import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
 import org.opendaylight.mdsal.binding.api.ReadTransaction;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.ServiceNodelist;
@@ -20,7 +21,6 @@ import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsupp
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.nodelist.Nodes;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.nodelist.NodesBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.AlarmNotification;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.OrgOpenroadmAlarmListener;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.alarm.ProbableCause;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.ResourceType;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.Resource;
@@ -38,7 +38,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class AlarmNotificationListener221 implements OrgOpenroadmAlarmListener {
+public class AlarmNotificationListener221 {
 
     private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListener221.class);
     private static final String PIPE = "|";
@@ -48,14 +48,17 @@ public class AlarmNotificationListener221 implements OrgOpenroadmAlarmListener {
         this.dataBroker = dataBroker;
     }
 
+    public CompositeListener getCompositeListener() {
+        return new CompositeListener(Set.of(
+            new CompositeListener.Component<>(AlarmNotification.class, this::onAlarmNotification)));
+    }
 
     /**
      * Callback for alarm-notification.
      *
      * @param notification AlarmNotification object
      */
-    @Override
-    public void onAlarmNotification(AlarmNotification notification) {
+    private void onAlarmNotification(AlarmNotification notification) {
         List<Nodes> allNodeList = new ArrayList<>();
         InstanceIdentifier<ServiceNodelist> serviceNodeListIID = InstanceIdentifier.create(ServiceNodelist.class);
         try {
@@ -63,24 +66,19 @@ public class AlarmNotificationListener221 implements OrgOpenroadmAlarmListener {
             Optional<ServiceNodelist> serviceListObject =
                     rtx.read(LogicalDatastoreType.OPERATIONAL, serviceNodeListIID).get();
             if (serviceListObject.isPresent()) {
-                for (Nodelist nodelist : serviceListObject.get().getNodelist()) {
-                    allNodeList.addAll(nodelist.getNodes());
+                for (Nodelist nodelist : serviceListObject.orElseThrow().nonnullNodelist().values()) {
+                    allNodeList.addAll(nodelist.nonnullNodes().values());
                 }
             }
         } catch (InterruptedException | ExecutionException ex) {
             LOG.warn("Exception thrown while reading Logical Connection Point value", ex);
         }
-        StringBuilder sb = new StringBuilder(notification.getResource().getDevice().getNodeId().getValue())
-            .append(PIPE);
-        sb.append(buildCause(notification.getProbableCause()));
-        sb.append(notification.getId() != null ? notification.getId() : "").append(PIPE)
-                .append(notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "").append(PIPE)
-                .append(notification.getSeverity() != null ? notification.getSeverity().getName() : "").append(PIPE)
-                .append(notification.getCircuitId() != null ? notification.getCircuitId() : "").append(PIPE);
-
-        sb.append(buildType(notification));
+        String message = String.join(PIPE,notification.getResource().getDevice().getNodeId().getValue(),
+                buildCause(notification.getProbableCause()),notification.getId() != null ? notification.getId() : "",
+                notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "",
+                notification.getSeverity() != null ? notification.getSeverity().getName() : "",
+                notification.getCircuitId() != null ? notification.getCircuitId() : "", buildType(notification));
 
-        String message = sb.toString();
         Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId().getValue())
             .build();
         if (allNodeList.contains(build)) {
@@ -91,16 +89,14 @@ public class AlarmNotificationListener221 implements OrgOpenroadmAlarmListener {
     }
 
     private String buildCause(ProbableCause probableCause) {
-        StringBuilder sb = new StringBuilder();
         if (probableCause == null) {
             return "||||";
         }
-        sb.append((probableCause.getCause() != null) ? probableCause.getCause().getName() : "").append(PIPE)
-                .append((probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "")
-                .append(PIPE).append((probableCause.getExtension() != null) ? probableCause.getExtension() : "")
-                .append(PIPE).append((probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "")
-                .append(PIPE);
-        return sb.toString();
+        return String.join(PIPE,
+                (probableCause.getCause() != null) ? probableCause.getCause().getName() : "",
+                (probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "",
+                (probableCause.getExtension() != null) ? probableCause.getExtension() : "",
+                (probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "");
     }
 
     @SuppressWarnings("unchecked")
@@ -108,13 +104,14 @@ public class AlarmNotificationListener221 implements OrgOpenroadmAlarmListener {
             Resource resource) {
         if (resource == null) {
             LOG.error("Resource is null.");
-        } else if (!resourceClass.isInstance(resource)) {
+            return Optional.empty();
+        }
+        if (!resourceClass.isInstance(resource)) {
             LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
                     resourceClass.getSimpleName(), resource.getClass().getSimpleName());
-        } else {
-            return Optional.of((T) resource);
+            return Optional.empty();
         }
-        return Optional.empty();
+        return Optional.of((T) resource);
     }
 
     private static String buildType(AlarmNotification notification) {
@@ -137,82 +134,78 @@ public class AlarmNotificationListener221 implements OrgOpenroadmAlarmListener {
             case CircuitPack:
                 Optional<CircuitPack> circuitPackOptional = tryCastToParticularResource(CircuitPack.class, resource);
                 if (circuitPackOptional.isPresent()) {
-                    circuitPack = circuitPackOptional.get().getCircuitPackName();
+                    circuitPack = circuitPackOptional.orElseThrow().getCircuitPackName();
                 }
                 break;
 
             case Connection:
                 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
                 if (connectionOptional.isPresent()) {
-                    connection = connectionOptional.get().getConnectionName();
+                    connection = connectionOptional.orElseThrow().getConnectionName();
                 }
                 break;
 
             case Degree:
                 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
                 if (degreeOptional.isPresent()) {
-                    degree = degreeOptional.get().getDegreeNumber().toString();
+                    degree = degreeOptional.orElseThrow().getDegreeNumber().toString();
                 }
                 break;
 
             case Interface:
                 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
                 if (interfaceOptional.isPresent()) {
-                    iface = interfaceOptional.get().getInterfaceName();
+                    iface = interfaceOptional.orElseThrow().getInterfaceName();
                 }
                 break;
 
             case InternalLink:
                 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
                 if (internalLinkOptional.isPresent()) {
-                    internalLink = internalLinkOptional.get().getInternalLinkName();
+                    internalLink = internalLinkOptional.orElseThrow().getInternalLinkName();
                 }
                 break;
 
             case PhysicalLink:
                 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
                 if (physicalLinkOptional.isPresent()) {
-                    physicalLink = physicalLinkOptional.get().getPhysicalLinkName();
+                    physicalLink = physicalLinkOptional.orElseThrow().getPhysicalLinkName();
                 }
                 break;
 
             case Service:
                 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
                 if (serviceOptional.isPresent()) {
-                    service = serviceOptional.get().getServiceName();
+                    service = serviceOptional.orElseThrow().getServiceName();
                 }
                 break;
 
             case Shelf:
                 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
                 if (shelfOptional.isPresent()) {
-                    shelf = shelfOptional.get().getShelfName();
+                    shelf = shelfOptional.orElseThrow().getShelfName();
                 }
                 break;
 
             case SharedRiskGroup:
                 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
                 if (sharedRiskGroupOptional.isPresent()) {
-                    sharedRiskGroup = sharedRiskGroupOptional.get().getSrgNumber().toString();
+                    sharedRiskGroup = sharedRiskGroupOptional.orElseThrow().getSrgNumber().toString();
                 }
                 break;
 
             case Port:
                 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
                 if (portContainerOptional.isPresent()) {
-                    port = portContainerOptional.get().getPort().getPortName();
-                    portCircuitPack = portContainerOptional.get().getPort().getCircuitPackName();
+                    port = portContainerOptional.orElseThrow().getPort().getPortName();
+                    portCircuitPack = portContainerOptional.orElseThrow().getPort().getCircuitPackName();
                 }
                 break;
 
             default:
                 LOG.warn("Unknown resource type {}", wantedResourceType);
         }
-        StringBuilder sb = new StringBuilder(circuitPack);
-        sb.append(PIPE).append(connection).append(PIPE).append(degree).append(PIPE).append(iface);
-        sb.append(PIPE).append(internalLink).append(PIPE).append(physicalLink).append(PIPE).append(service);
-        sb.append(PIPE).append(shelf).append(PIPE).append(sharedRiskGroup).append(PIPE).append(port);
-        sb.append(PIPE).append(portCircuitPack);
-        return sb.toString();
+        return String.join(PIPE, circuitPack, connection, degree, iface, internalLink, physicalLink,
+                service, shelf, sharedRiskGroup, port, portCircuitPack);
     }
 }