Technical debt - Fix PCE sonar issues
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PcePathDescription.java
index b067745918879a7e84949cc05540db2cae96416e..351f4400b523856e2c51589df1cea6838c8d3fa5 100644 (file)
@@ -39,7 +39,6 @@ public class PcePathDescription {
     private static final Logger LOG = LoggerFactory.getLogger(PcePathDescription.class);
 
     private List<PceLink> pathAtoZ = null;
-    private List<PceLink> pathZtoA = null;
     private PceResult rc;
     private Map<LinkId, PceLink> allPceLinks = null;
 
@@ -52,22 +51,45 @@ public class PcePathDescription {
 
     public PceResult buildDescriptions() {
         LOG.info("In buildDescriptions: AtoZ =  {}", pathAtoZ);
-        Map<AToZKey,AToZ> atozList = new HashMap<>();
+        Map<AToZKey,AToZ> atozMap = new HashMap<>();
         if (pathAtoZ == null) {
             rc.setRC(ResponseCodes.RESPONSE_FAILED);
             LOG.error("In buildDescriptions: there is empty AtoZ path");
             return rc;
         }
 
-        buildAtoZ(atozList, pathAtoZ);
+        buildAtoZ(atozMap, pathAtoZ);
+        rc.setAtoZDirection(buildAtoZDirection(atozMap).build());
+        List<PceLink> pathZtoA = ImmutableList.copyOf(pathAtoZ).reverse();
+        LOG.info("In buildDescriptions: ZtoA {}", pathZtoA);
+
+        Map<ZToAKey,ZToA> ztoaMap = new HashMap<>();
+        if (pathZtoA == null) {
+            rc.setRC(ResponseCodes.RESPONSE_FAILED);
+            LOG.error("In buildDescriptions: there is empty ZtoA path");
+            return rc;
+        }
+        buildZtoA(ztoaMap, pathZtoA);
+        rc.setZtoADirection(buildZtoADirection(ztoaMap).build());
+
+        return rc;
+    }
+
+    /**
+     * Create a builder for AtoZDirection object.
+     * @param atozMap Map of AToZ object
+     * @return a builder for AtoZDirection object
+     */
+    private AToZDirectionBuilder buildAtoZDirection(Map<AToZKey, AToZ> atozMap) {
         AToZDirectionBuilder atoZDirectionBldr = new AToZDirectionBuilder()
             .setRate(Uint32.valueOf(rc.getRate()))
-            .setAToZ(atozList);
+            .setAToZ(atozMap);
         if ("100GE".equals(rc.getServiceType()) || "OTU4".equals(rc.getServiceType())) {
             atoZDirectionBldr.setAToZWavelengthNumber(Uint32.valueOf(rc.getResultWavelength()));
         } else if ("10GE".equals(rc.getServiceType()) || "1GE".equals(rc.getServiceType())
             || "ODU4".equals(rc.getServiceType())) {
             if (rc.getResultTribSlot() != null && rc.getResultTribPort() != null) {
+                @SuppressWarnings("unchecked")
                 List<Uint16> tribSlotList = (List<Uint16>) rc.getResultTribSlot().values().toArray()[0];
                 atoZDirectionBldr.setAToZWavelengthNumber(Uint32.valueOf(0))
                     .setTribPortNumber(Uint16.valueOf(rc.getResultTribPort().values().toArray()[0].toString()))
@@ -77,25 +99,24 @@ public class PcePathDescription {
                 atoZDirectionBldr.setTribSlotNumber(Uint16.valueOf(0)).setTribPortNumber(Uint16.valueOf(0));
             }
         }
-        rc.setAtoZDirection(atoZDirectionBldr.build());
-        pathZtoA = ImmutableList.copyOf(pathAtoZ).reverse();
-        LOG.info("In buildDescriptions: ZtoA {}", pathZtoA);
+        return atoZDirectionBldr;
+    }
 
-        Map<ZToAKey,ZToA> ztoaList = new HashMap<>();
-        if (pathZtoA == null) {
-            rc.setRC(ResponseCodes.RESPONSE_FAILED);
-            LOG.error("In buildDescriptions: there is empty ZtoA path");
-            return rc;
-        }
-        buildZtoA(ztoaList, pathZtoA);
+    /**
+     * Create a builder for ZtoADirection object.
+     * @param ztoaMap Map of ZToA object
+     * @return a builder for ZtoADirection object
+     */
+    private ZToADirectionBuilder buildZtoADirection(Map<ZToAKey, ZToA> ztoaMap) {
         ZToADirectionBuilder ztoADirectionBldr = new ZToADirectionBuilder()
             .setRate(Uint32.valueOf(rc.getRate()))
-            .setZToA(ztoaList);
+            .setZToA(ztoaMap);
         if ("100GE".equals(rc.getServiceType()) || "OTU4".equals(rc.getServiceType())) {
             ztoADirectionBldr.setZToAWavelengthNumber(Uint32.valueOf(rc.getResultWavelength()));
         } else if ("10GE".equals(rc.getServiceType()) || "1GE".equals(rc.getServiceType())
             || "ODU4".equals(rc.getServiceType())) {
             if (rc.getResultTribSlot() != null && rc.getResultTribPort() != null) {
+                @SuppressWarnings("unchecked")
                 List<Uint16> tribSlotList = (List<Uint16>) rc.getResultTribSlot().values().toArray()[0];
                 ztoADirectionBldr.setZToAWavelengthNumber(Uint32.valueOf(0))
                     .setTribPortNumber(Uint16.valueOf(rc.getResultTribPort().values().toArray()[0].toString()))
@@ -105,12 +126,13 @@ public class PcePathDescription {
                 ztoADirectionBldr.setTribSlotNumber(Uint16.valueOf(0)).setTribPortNumber(Uint16.valueOf(0));
             }
         }
-        rc.setZtoADirection(ztoADirectionBldr.build());
-
-        return rc;
+        return ztoADirectionBldr;
     }
 
-    private void buildAtoZ(Map<AToZKey, AToZ> atozList, List<PceLink> path) {
+    @SuppressWarnings("java:S138")
+    //sonar issue This method has 77 lines, which is greater than the 75 lines authorized. Split it into smaller
+    //ignore as it's not relevant to split it from functional point
+    private void buildAtoZ(Map<AToZKey, AToZ> atozMap, List<PceLink> path) {
         Integer index = 0;
         PceLink lastLink = null;
         AToZ lastResource = null;
@@ -125,7 +147,7 @@ public class PcePathDescription {
         AToZKey clientKey = new AToZKey(index.toString());
         Resource clientResource = new ResourceBuilder().setResource(stp).build();
         AToZ firstResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
-        atozList.put(firstResource.key(),firstResource);
+        atozMap.put(firstResource.key(),firstResource);
         index += 1;
         for (PceLink pcelink : path) {
             String srcName = pcelink.getSourceId().getValue();
@@ -140,7 +162,7 @@ public class PcePathDescription {
             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
             AToZ srcResource = new AToZBuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
             index += 1;
-            atozList.put(srcResource.key(),srcResource);
+            atozMap.put(srcResource.key(),srcResource);
 
             // source TP
             tpName = pcelink.getSourceTP().toString();
@@ -153,7 +175,7 @@ public class PcePathDescription {
             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
             AToZ stpResource = new AToZBuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
             index += 1;
-            atozList.put(stpResource.key(),stpResource);
+            atozMap.put(stpResource.key(),stpResource);
 
             String linkName = pcelink.getLinkId().getValue();
             // Link
@@ -167,7 +189,7 @@ public class PcePathDescription {
             Resource nodeResource2 = new ResourceBuilder().setResource(atozLink).build();
             AToZ linkResource = new AToZBuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
             index += 1;
-            atozList.put(linkResource.key(),linkResource);
+            atozMap.put(linkResource.key(),linkResource);
 
             String destName = pcelink.getDestId().getValue();
             // target TP
@@ -181,7 +203,7 @@ public class PcePathDescription {
             Resource tpResource2 = new ResourceBuilder().setResource(dtp).build();
             AToZ ttpResource = new AToZBuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
             index += 1;
-            atozList.put(ttpResource.key(),ttpResource);
+            atozMap.put(ttpResource.key(),ttpResource);
 
             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
                 .resource.resource.resource.Node targetNode = new NodeBuilder()
@@ -197,7 +219,7 @@ public class PcePathDescription {
         }
 
         if (lastResource != null) {
-            atozList.put(lastResource.key(),lastResource);
+            atozMap.put(lastResource.key(),lastResource);
         }
 
         // build Z side Client TP
@@ -211,7 +233,7 @@ public class PcePathDescription {
         clientKey = new AToZKey(index.toString());
         clientResource = new ResourceBuilder().setResource(stp).build();
         lastResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
-        atozList.put(lastResource.key(),lastResource);
+        atozMap.put(lastResource.key(),lastResource);
 
     }