Merge changes from topic "97346"
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / listeners / AlarmNotificationListener.java
index ee71be736193da9a90f5ca336a52d79c6f5a2e78..c3d6ac1eeeb410e4f895347e3da18af839a06634 100644 (file)
@@ -57,48 +57,40 @@ public class AlarmNotificationListener implements OrgOpenroadmAlarmListener {
     public void onAlarmNotification(AlarmNotification notification) {
         List<Nodes> allNodeList = new ArrayList<>();
         InstanceIdentifier<ServiceNodelist> serviceNodeListIID = InstanceIdentifier.create(ServiceNodelist.class);
-        try {
-            ReadTransaction rtx = dataBroker.newReadOnlyTransaction();
+        try (ReadTransaction rtx = dataBroker.newReadOnlyTransaction()) {
             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.get().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()).append(PIPE);
-        sb.append(buildCause(notification.getProbableCause()));
-        sb.append(notification.getId() != null ? notification.getId() : "").append(PIPE)
-                .append(notification.getType() != null ? notification.getType().toString() : "").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);
+        String message = String.join(PIPE,notification.getResource().getDevice().getNodeId(),
+                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));
 
-        sb.append(buildType(notification));
-
-        String message = sb.toString();
         Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId()).build();
         if (allNodeList.contains(build)) {
-            LOG.info(message);
+            LOG.info("onAlarmNotification: {}", message);
         } else {
-            LOG.warn(message);
+            LOG.warn("onAlarmNotification: {}", message);
         }
     }
 
     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")
@@ -106,13 +98,14 @@ public class AlarmNotificationListener 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) {
@@ -206,11 +199,7 @@ public class AlarmNotificationListener implements OrgOpenroadmAlarmListener {
             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);
     }
 }