Light refactor on PceOpticalNode
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PcePathDescription.java
index ff46c71f21cce425a6d36d69c46d6f526715e51d..351f4400b523856e2c51589df1cea6838c8d3fa5 100644 (file)
@@ -8,7 +8,7 @@
 package org.opendaylight.transportpce.pce;
 
 import com.google.common.collect.ImmutableList;
-import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import org.opendaylight.transportpce.common.ResponseCodes;
@@ -29,6 +29,8 @@ import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdes
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce.resource.resource.resource.TerminationPoint;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce.resource.resource.resource.TerminationPointBuilder;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
+import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -37,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;
 
@@ -50,49 +51,88 @@ public class PcePathDescription {
 
     public PceResult buildDescriptions() {
         LOG.info("In buildDescriptions: AtoZ =  {}", pathAtoZ);
-        List<AToZ> atozList = new ArrayList<>();
+        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);
-        AToZDirectionBuilder atoZDirectionBldr = new AToZDirectionBuilder()
-            .setRate(rc.getRate())
-            .setAToZ(atozList);
-        if ("100GE".equals(rc.getServiceType()) || "OTU4".equals(rc.getServiceType())) {
-            atoZDirectionBldr.setAToZWavelengthNumber(rc.getResultWavelength());
-        } else if ("10GE".equals(rc.getServiceType()) || "1GE".equals(rc.getServiceType())
-            || "ODU4".equals(rc.getServiceType())) {
-            atoZDirectionBldr.setAToZWavelengthNumber(Long.valueOf(0));
-        }
-        rc.setAtoZDirection(atoZDirectionBldr.build());
-        pathZtoA = ImmutableList.copyOf(pathAtoZ).reverse();
+        buildAtoZ(atozMap, pathAtoZ);
+        rc.setAtoZDirection(buildAtoZDirection(atozMap).build());
+        List<PceLink> pathZtoA = ImmutableList.copyOf(pathAtoZ).reverse();
         LOG.info("In buildDescriptions: ZtoA {}", pathZtoA);
 
-        List<ZToA> ztoaList = new ArrayList<>();
+        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(ztoaList, pathZtoA);
-        ZToADirectionBuilder ztoADirectionBldr = new ZToADirectionBuilder()
-            .setRate(rc.getRate())
-            .setZToA(ztoaList);
+        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(atozMap);
         if ("100GE".equals(rc.getServiceType()) || "OTU4".equals(rc.getServiceType())) {
-            ztoADirectionBldr.setZToAWavelengthNumber(rc.getResultWavelength());
+            atoZDirectionBldr.setAToZWavelengthNumber(Uint32.valueOf(rc.getResultWavelength()));
         } else if ("10GE".equals(rc.getServiceType()) || "1GE".equals(rc.getServiceType())
             || "ODU4".equals(rc.getServiceType())) {
-            ztoADirectionBldr.setZToAWavelengthNumber(Long.valueOf(0));
+            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()))
+                    .setTribSlotNumber(tribSlotList.get(0));
+            } else {
+                LOG.error("Trib port and trib slot number should be present");
+                atoZDirectionBldr.setTribSlotNumber(Uint16.valueOf(0)).setTribPortNumber(Uint16.valueOf(0));
+            }
         }
-        rc.setZtoADirection(ztoADirectionBldr.build());
+        return atoZDirectionBldr;
+    }
 
-        return rc;
+    /**
+     * 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(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()))
+                    .setTribSlotNumber(tribSlotList.get(0));
+            } else {
+                LOG.error("Trib port and trib slot number should be present");
+                ztoADirectionBldr.setTribSlotNumber(Uint16.valueOf(0)).setTribPortNumber(Uint16.valueOf(0));
+            }
+        }
+        return ztoADirectionBldr;
     }
 
-    private void buildAtoZ(List<AToZ> etoeList, 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;
@@ -107,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();
-        etoeList.add(firstResource);
+        atozMap.put(firstResource.key(),firstResource);
         index += 1;
         for (PceLink pcelink : path) {
             String srcName = pcelink.getSourceId().getValue();
@@ -122,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;
-            etoeList.add(srcResource);
+            atozMap.put(srcResource.key(),srcResource);
 
             // source TP
             tpName = pcelink.getSourceTP().toString();
@@ -135,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;
-            etoeList.add(stpResource);
+            atozMap.put(stpResource.key(),stpResource);
 
             String linkName = pcelink.getLinkId().getValue();
             // Link
@@ -149,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;
-            etoeList.add(linkResource);
+            atozMap.put(linkResource.key(),linkResource);
 
             String destName = pcelink.getDestId().getValue();
             // target TP
@@ -163,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;
-            etoeList.add(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()
@@ -178,7 +218,9 @@ public class PcePathDescription {
             lastLink = pcelink;
         }
 
-        etoeList.add(lastResource);
+        if (lastResource != null) {
+            atozMap.put(lastResource.key(),lastResource);
+        }
 
         // build Z side Client TP
         tpName = lastLink.getClient();
@@ -191,11 +233,11 @@ public class PcePathDescription {
         clientKey = new AToZKey(index.toString());
         clientResource = new ResourceBuilder().setResource(stp).build();
         lastResource = new AToZBuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
-        etoeList.add(lastResource);
+        atozMap.put(lastResource.key(),lastResource);
 
     }
 
-    private void buildZtoA(List<ZToA> etoelist, List<PceLink> path) {
+    private void buildZtoA(Map<ZToAKey, ZToA> ztoaList, List<PceLink> path) {
         Integer index = 0;
         PceLink lastLink = null;
         ZToA lastResource = null;
@@ -211,7 +253,7 @@ public class PcePathDescription {
         ZToAKey clientKey = new ZToAKey(index.toString());
         Resource clientResource = new ResourceBuilder().setResource(stp).build();
         ZToA firstResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
-        etoelist.add(firstResource);
+        ztoaList.put(firstResource.key(),firstResource);
         index += 1;
 
         for (PceLink pcelinkAtoZ : path) {
@@ -233,7 +275,7 @@ public class PcePathDescription {
             Resource nodeResource1 = new ResourceBuilder().setResource(sourceNode).build();
             ZToA srcResource = new ZToABuilder().setId(srcName).withKey(sourceKey).setResource(nodeResource1).build();
             index += 1;
-            etoelist.add(srcResource);
+            ztoaList.put(srcResource.key(),srcResource);
 
             // source TP
             tpName = pcelink.getSourceTP().toString();
@@ -246,7 +288,7 @@ public class PcePathDescription {
             Resource tpResource1 = new ResourceBuilder().setResource(stp).build();
             ZToA stpResource = new ZToABuilder().setId(tpName).withKey(srcTPKey).setResource(tpResource1).build();
             index += 1;
-            etoelist.add(stpResource);
+            ztoaList.put(stpResource.key(),stpResource);
 
             String linkName = pcelink.getLinkId().getValue();
             // Link
@@ -259,7 +301,7 @@ public class PcePathDescription {
             Resource nodeResource2 = new ResourceBuilder().setResource(ztoaLink).build();
             ZToA linkResource = new ZToABuilder().setId(linkName).withKey(linkKey).setResource(nodeResource2).build();
             index += 1;
-            etoelist.add(linkResource);
+            ztoaList.put(linkResource.key(),linkResource);
 
             String destName = pcelink.getDestId().getValue();
             // target TP
@@ -272,7 +314,7 @@ public class PcePathDescription {
             Resource tpResource2 = new ResourceBuilder().setResource(ttp).build();
             ZToA ttpResource = new ZToABuilder().setId(tpName).withKey(destTPKey).setResource(tpResource2).build();
             index += 1;
-            etoelist.add(ttpResource);
+            ztoaList.put(ttpResource.key(),ttpResource);
 
 
             org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.pce
@@ -285,7 +327,9 @@ public class PcePathDescription {
 
             lastLink = pcelink;
         }
-        etoelist.add(lastResource);
+        if (lastResource != null) {
+            ztoaList.put(lastResource.key(),lastResource);
+        }
 
         // build Z side Client TP
         tpName = lastLink.getClient();
@@ -297,7 +341,7 @@ public class PcePathDescription {
         clientKey = new ZToAKey(index.toString());
         clientResource = new ResourceBuilder().setResource(stp).build();
         lastResource = new ZToABuilder().setId(tpName).withKey(clientKey).setResource(clientResource).build();
-        etoelist.add(lastResource);
+        ztoaList.put(lastResource.key(),lastResource);
     }
 
     public PceResult getReturnStructure() {