Technical debt - Fix PCE sonar issues
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / PceLink.java
index 0a2159484e271882bfef084c2be86b14381ff4bb..a38682dec36aa124ea4c6a6ea1dbf28374f15b75 100644 (file)
@@ -8,10 +8,18 @@
 
 package org.opendaylight.transportpce.pce.networkanalyzer;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
-
+import java.util.Map;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.Link1;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.span.attributes.LinkConcatenation;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.span.attributes.LinkConcatenation.FiberType;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.span.attributes.LinkConcatenationKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.networks.network.link.oms.attributes.Span;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmLinkType;
 import org.opendaylight.yang.gen.v1.http.transportpce.topology.rev200129.OtnLinkType;
@@ -21,7 +29,11 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.top
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class PceLink {
+@SuppressWarnings("serial")
+@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
+    value = "SE_NO_SERIALVERSIONID",
+    justification = "https://github.com/rzwitserloot/lombok/wiki/WHY-NOT:-serialVersionUID")
+public class PceLink implements Serializable {
 
     /* Logging. */
     private static final Logger LOG = LoggerFactory.getLogger(PceLink.class);
@@ -31,7 +43,6 @@ public class PceLink {
      */
     double weight = 0;
     private boolean isValid = true;
-    private boolean isOtnValid = true;
 
     // this member is for XPONDER INPUT/OUTPUT links.
     // it keeps name of client corresponding to NETWORK TP
@@ -40,8 +51,8 @@ public class PceLink {
     private final OpenroadmLinkType linkType;
     private final NodeId sourceId;
     private final NodeId destId;
-    private final Object sourceTP;
-    private final Object destTP;
+    private transient Object sourceTP;
+    private transient Object destTP;
     private final String sourceNetworkSupNodeId;
     private final String destNetworkSupNodeId;
     private final String sourceCLLI;
@@ -52,8 +63,9 @@ public class PceLink {
     private final Long usedBandwidth;
     private final List<Long> srlgList;
     private final double osnr;
-    private final Span omsAttributesSpan;
-    private static final double CELERITY = 2.99792458 * 1e5; //meter per ms
+    private final transient Span omsAttributesSpan;
+    //meter per ms
+    private static final double CELERITY = 2.99792458 * 1e5;
     private static final double NOISE_MASK_A = 0.571429;
     private static final double NOISE_MASK_B = 39.285714;
     private static final double UPPER_BOUND_OSNR = 33;
@@ -98,11 +110,12 @@ public class PceLink {
             this.omsAttributesSpan = null;
             this.srlgList = null;
             this.latency = 0L;
-            this.osnr = 100L; //infinite OSNR in DB
+            //infinite OSNR in DB
+            this.osnr = 100L;
             this.availableBandwidth = 0L;
             this.usedBandwidth = 0L;
         }
-        LOG.debug("PceLink: created PceLink  {}", toString());
+        LOG.debug("PceLink: created PceLink  {}", linkId);
     }
 
     //Retrieve the opposite link
@@ -115,49 +128,58 @@ public class PceLink {
         return tmpoppositeLink;
     }
 
-    //Compute the link latency : if the latency is not defined, the latency it is computed from the omsAttributesSpan
+    //Compute the link latency : if the latency is not defined, the latency is computed from the omsAttributesSpan
     private Long calcLatency(Link link) {
-        Link1 link1 = null;
-        link1 = link.augmentation(Link1.class);
-        Long tmplatency = link1.getLinkLatency();
-        if (tmplatency != null) {
-            return tmplatency;
+        Link1 link1 = link.augmentation(Link1.class);
+        if (link1.getLinkLatency() != null) {
+            return link1.getLinkLatency().toJava();
         }
-
-        try {
-            double tmp = 0;
-            for (int i = 0; i < this.omsAttributesSpan.getLinkConcatenation().size(); i++) {
-                //Length is expressed in meter and latency is expressed in ms according to OpenROADM MSA
-                tmp += this.omsAttributesSpan.getLinkConcatenation().get(i).getSRLGLength() / CELERITY;
-                LOG.info("In PceLink: The latency of link {} == {}",link.getLinkId(),tmplatency);
+        if (this.omsAttributesSpan == null) {
+            return 1L;
+        }
+        double tmp = 0;
+        Map<LinkConcatenationKey, LinkConcatenation> linkConcatenationMap = this.omsAttributesSpan
+                .nonnullLinkConcatenation();
+        for (Map.Entry<LinkConcatenationKey, LinkConcatenation> entry : linkConcatenationMap.entrySet()) {
+            // Length is expressed in meter and latency is expressed in ms according to OpenROADM MSA
+            if (entry == null || entry.getValue() == null || entry.getValue().getSRLGLength() == null) {
+                LOG.debug("In PceLink: cannot compute the latency for the link {}", link.getLinkId().getValue());
+                return 1L;
             }
-            tmplatency = (long) Math.ceil(tmp);
-        } catch (NullPointerException e) {
-            LOG.debug("In PceLink: cannot compute the latency for the link {}",link.getLinkId().getValue());
-            tmplatency = 1L;
+            tmp += entry.getValue().getSRLGLength().toJava() / CELERITY;
+            LOG.info("In PceLink: The latency of link {} == {}", link.getLinkId(), tmp);
         }
-        return tmplatency;
+        return (long) Math.ceil(tmp);
     }
 
     //Compute the OSNR of a span
     public double calcSpanOSNR() {
-        try {
-            double pout; //power on the output of the previous ROADM (dBm)
-            pout = retrievePower(this.omsAttributesSpan.getLinkConcatenation().get(0).getFiberType());
-            double spanLoss = this.omsAttributesSpan.getSpanlossCurrent().getValue().doubleValue(); // span loss (dB)
-            double pin = pout - spanLoss; //power on the input of the current ROADM (dBm)
-            double spanOsnrDb;
-            spanOsnrDb = NOISE_MASK_A * pin + NOISE_MASK_B;
-            if (spanOsnrDb > UPPER_BOUND_OSNR) {
-                spanOsnrDb =  UPPER_BOUND_OSNR;
-            } else if (spanOsnrDb < LOWER_BOUND_OSNR) {
-                spanOsnrDb = LOWER_BOUND_OSNR;
-            }
-            return spanOsnrDb;
-        } catch (NullPointerException e) {
+        if (this.omsAttributesSpan == null) {
+            return 0L;
+        }
+        Collection<LinkConcatenation> linkConcatenationList =
+            this.omsAttributesSpan.nonnullLinkConcatenation().values();
+        if (linkConcatenationList == null) {
             LOG.error("in PceLink : Null field in the OmsAttrubtesSpan");
             return 0L;
         }
+        Iterator<LinkConcatenation> linkConcatenationiterator = linkConcatenationList.iterator();
+        if (!linkConcatenationiterator.hasNext()) {
+            return 0L;
+        }
+        // power on the output of the previous ROADM (dBm)
+        double pout = retrievePower(linkConcatenationiterator.next().getFiberType());
+        // span loss (dB)
+        double spanLoss = this.omsAttributesSpan.getSpanlossCurrent().getValue().doubleValue();
+        // power on the input of the current ROADM (dBm)
+        double pin = pout - spanLoss;
+        double spanOsnrDb = NOISE_MASK_A * pin + NOISE_MASK_B;
+        if (spanOsnrDb > UPPER_BOUND_OSNR) {
+            spanOsnrDb = UPPER_BOUND_OSNR;
+        } else if (spanOsnrDb < LOWER_BOUND_OSNR) {
+            spanOsnrDb = LOWER_BOUND_OSNR;
+        }
+        return spanOsnrDb;
     }
 
     private double retrievePower(FiberType fiberType) {
@@ -230,7 +252,7 @@ public class PceLink {
     }
 
     public Long getUsedBandwidth() {
-        return availableBandwidth;
+        return usedBandwidth;
     }
 
     public String getsourceNetworkSupNodeId() {
@@ -262,19 +284,7 @@ public class PceLink {
             isValid = false;
             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
         }
-        if ((this.sourceId == null) || (this.destId == null) || (this.sourceTP == null) || (this.destTP == null)) {
-            isValid = false;
-            LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", linkId);
-        }
-        if ((this.sourceNetworkSupNodeId.equals("")) || (this.destNetworkSupNodeId.equals(""))) {
-            isValid = false;
-            LOG.error("PceLink: No Link source SuppNodeID or destination SuppNodeID is available. Link is ignored {}",
-                linkId);
-        }
-        if ((this.sourceCLLI.equals("")) || (this.destCLLI.equals(""))) {
-            isValid = false;
-            LOG.error("PceLink: No Link source CLLI or destination CLLI is available. Link is ignored {}", linkId);
-        }
+        isValid = checkParams();
         if ((this.omsAttributesSpan == null) && (this.linkType == OpenroadmLinkType.ROADMTOROADM)) {
             isValid = false;
             LOG.error("PceLink: Error reading Span for OMS link. Link is ignored {}", linkId);
@@ -301,7 +311,7 @@ public class PceLink {
             return false;
         }
 
-        long neededBW = 0L;
+        long neededBW;
         OtnLinkType neededType = null;
         switch (serviceType) {
 
@@ -341,6 +351,10 @@ public class PceLink {
                 linkId, serviceType);
         }
 
+        return checkParams();
+    }
+
+    private boolean checkParams() {
         if ((this.linkId == null) || (this.linkType == null) || (this.oppositeLink == null)) {
             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
             return false;
@@ -362,8 +376,20 @@ public class PceLink {
         return true;
     }
 
+    @Override
     public String toString() {
         return "PceLink type=" + linkType + " ID=" + linkId.getValue() + " latency=" + latency;
     }
 
+    private void writeObject(ObjectOutputStream out) throws IOException {
+        out.defaultWriteObject();
+        out.writeObject(this.sourceTP);
+        out.writeObject(this.destTP);
+    }
+
+    private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
+        in.defaultReadObject();
+        this.sourceTP = in.readObject();
+        this.destTP = in.readObject();
+    }
 }