ROADM To ROADM Path Calculation on unidir ports
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / PceNode.java
index 6ef46633ffeefb0176842f987abaad688da480ab..27d8d1c30eed24a4a3cb96502968f7d56cf19cd4 100644 (file)
@@ -9,11 +9,10 @@ package org.opendaylight.transportpce.pce;
 
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
+import java.util.Optional;
+import java.util.TreeMap;
 
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev170929.Node1;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev170929.TerminationPoint1;
@@ -37,9 +36,11 @@ public class PceNode {
     private final OpenroadmNodeType nodeType;
     // wavelength calculation per node type
     private List<Long> availableWLindex = new ArrayList<Long>();
-    private Set<String> availableSrgPp = new TreeSet<String>();
+    // private Set<String> availableSrgPp = new TreeSet<String>();
+    private Map<String, OpenroadmTpType> availableSrgPp = new TreeMap<String, OpenroadmTpType>();
+    private Map<String, OpenroadmTpType> availableSrgCp = new TreeMap<String, OpenroadmTpType>();
     private List<String> usedXpndrNWTps = new ArrayList<String>();
-    private List<String> usedRdmCpTps = new ArrayList<String>();
+    private List<String> usedSrgPP = new ArrayList<String>();
     private List<PceLink> outgoingLinks = new ArrayList<PceLink>();
     private Map<String, String> clientPerNwTp = new HashMap<String, String>();
     private Map<String, String> clientPerPpTp = new HashMap<String, String>();
@@ -54,35 +55,55 @@ public class PceNode {
         }
     }
 
-    public void initSrgPpList() {
+    public void initSrgTpList() {
         this.availableSrgPp.clear();
+        this.availableSrgCp.clear();
         if (!isValid()) {
             return;
         }
-        LOG.info("getting SRG-PP tps from ROADM node {}", this.nodeId);
+        LOG.info("initSrgTpList: getting SRG tps from ROADM node {}", this.nodeId);
         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.Node1 nodeTp =
                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology
                                 .rev150608.Node1.class);
         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.node
-            .TerminationPoint> allTps = nodeTp.getTerminationPoint();
+            .TerminationPoint> allTps =
+                nodeTp.getTerminationPoint();
         if (allTps == null) {
-            LOG.error("initXndrTps: ROADM TerminationPoint list is empty for node {}", this.toString());
+            LOG.error("initSrgTpList: ROADM TerminationPoint list is empty for node {}", this.toString());
             this.valid = false;
             return;
         }
+        boolean used;
         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.node
                 .TerminationPoint tp : allTps) {
+            used = true;
             TerminationPoint1 tp1 = tp.augmentation(TerminationPoint1.class);
-            if (tp1.getTpType() == OpenroadmTpType.SRGTXRXPP) {
-                this.availableSrgPp.add(tp.getTpId().getValue());
+            try {
+                List<UsedWavelengths> usedWavelengths = tp1.getCpAttributes().getUsedWavelengths();
+                if (usedWavelengths.isEmpty()) {
+                    LOG.info("initSrgTpList: SRG-CP tp = {} found", tp.getTpId().getValue());
+                    used = false;
+                }
+            } catch (NullPointerException e) {
+                LOG.warn("initSrgTpList: 'usedWavelengths' for tp={} is null !", tp.getTpId().getValue());
+                used = false;
+            }
+            if (!used) {
+                if (tp1.getTpType().getName().contains("-PP")) {
+                    LOG.info("initSrgTpList: adding tp '{}'", tp1.getTpType());
+                    this.availableSrgPp.put(tp.getTpId().getValue(), tp1.getTpType());
+                } else if (tp1.getTpType().getName().contains("-CP")) {
+                    this.availableSrgCp.put(tp.getTpId().getValue(), tp1.getTpType());
+                }
             }
         }
-        if (this.availableSrgPp.isEmpty()) {
-            LOG.error("initSrgPpList: ROADM SRG PP TerminationPoint list is empty for node {}", this.toString());
+        if (this.availableSrgPp.isEmpty() && this.availableSrgCp.isEmpty()) {
+            LOG.error("initSrgTpList: ROADM SRG TerminationPoint list is empty for node {}", this.toString());
             this.valid = false;
             return;
         }
-        LOG.info("initSrgPpList: availableSrgPp size = {} in {}", this.availableSrgPp.size(), this.toString());
+        LOG.info("initSrgTpList: availableSrgPp size = {} && availableSrgCp size = {} in {}", this.availableSrgPp
+                .size(), this.availableSrgCp.size(), this.toString());
         return;
     }
 
@@ -95,7 +116,8 @@ public class PceNode {
         switch (this.nodeType) {
             case SRG :
                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev170929.srg.node.attributes
-                    .AvailableWavelengths> srgAvailableWL = node1.getSrgAttributes().getAvailableWavelengths();
+                    .AvailableWavelengths> srgAvailableWL =
+                        node1.getSrgAttributes().getAvailableWavelengths();
                 if (srgAvailableWL == null) {
                     this.valid = false;
                     LOG.error("initWLlist: SRG AvailableWavelengths is empty for node  {}", this.toString());
@@ -109,15 +131,14 @@ public class PceNode {
                 break;
             case DEGREE :
                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev170929.degree.node.attributes
-                    .AvailableWavelengths> degAvailableWL =
-                        node1.getDegreeAttributes().getAvailableWavelengths();
+                    .AvailableWavelengths> degAvailableWL = node1.getDegreeAttributes().getAvailableWavelengths();
                 if (degAvailableWL == null) {
                     this.valid = false;
                     LOG.error("initWLlist: DEG AvailableWavelengths is empty for node  {}", this.toString());
                     return;
                 }
                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev170929.degree.node.attributes
-                        .AvailableWavelengths awl : degAvailableWL) {
+                            .AvailableWavelengths awl : degAvailableWL) {
                     this.availableWLindex.add(awl.getIndex());
                     LOG.debug("initWLlist: DEGREE next = {} in {}", awl.getIndex(), this.toString());
                 }
@@ -157,7 +178,7 @@ public class PceNode {
         }
         this.valid = false;
         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.node
-                .TerminationPoint tp : allTps) {
+                    .TerminationPoint tp : allTps) {
             TerminationPoint1 tp1 = tp.augmentation(TerminationPoint1.class);
             if (tp1.getTpType() == OpenroadmTpType.XPONDERNETWORK) {
                 if (tp1.getXpdrNetworkAttributes().getWavelength() != null) {
@@ -182,91 +203,82 @@ public class PceNode {
         }
     }
 
-    public void initRdmSrgTps() {
+    public void initRdmSrgTps(Boolean aend) {
         LOG.info("initRdmSrgTps for node : {}", this.nodeId);
-        initSrgPpList();
+        initSrgTpList();
         if (!isValid()) {
             return;
         }
-        LOG.info("initRdmSrgTps: getting terminationPoint list for node {}", this.toString());
-        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.Node1 nodeTp =
-                this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology
-                        .rev150608.Node1.class);
-        List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.node
-            .TerminationPoint> allTps = nodeTp.getTerminationPoint();
-        if (allTps == null) {
-            this.valid = false;
-            LOG.error("initRdmSrgTps: SRG TerminationPoint list is empty for node {}", this.toString());
-            return;
-        }
         this.valid = false;
-        for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.network.node
-                .TerminationPoint tp : allTps) {
-            TerminationPoint1 tp1 = tp.augmentation(TerminationPoint1.class);
-            if (tp1.getTpType() == OpenroadmTpType.SRGTXRXCP) {
-                try {
-                    List<UsedWavelengths> usedWavelengths = tp1.getCpAttributes().getUsedWavelengths();
-                    if (!usedWavelengths.isEmpty()) {
-                        this.usedRdmCpTps.add(tp.getTpId().getValue());
-                        LOG.warn("initRdmSrgTps: SRG-CP tp = {} is used", tp.getTpId().getValue());
-                    } else {
-                        LOG.info("initRdmSrgTps: SRG-CP tp = {} found", tp.getTpId().getValue());
-                        this.valid = true;
-                    }
-                } catch (NullPointerException e) {
-                    LOG.warn("'usedWavelengths' for tp={} is null !", tp.getTpId().getValue());
-                    this.valid = true;
-                }
+        Optional<String> optTp = null;
+        OpenroadmTpType srgType = OpenroadmTpType.SRGTXRXPP;
+        OpenroadmTpType oppositeSrgType = null;
+        Optional<String> oppositeTp = null;
+        boolean unidir = false;
+        optTp = this.availableSrgCp.entrySet().stream().filter(cp -> cp.getValue() == OpenroadmTpType.SRGTXRXCP)
+                .map(Map.Entry::getKey).findFirst();
+        if (!optTp.isPresent()) {
+            srgType = null;
+            unidir = true;
+            LOG.info("UNI Directional ports ...");
+            if (aend) {
+                LOG.info("Tx port ...");
+                optTp = this.availableSrgCp.entrySet().stream().filter(cp -> cp.getValue() == OpenroadmTpType.SRGTXCP)
+                        .map(Map.Entry::getKey).findFirst();
+                srgType = OpenroadmTpType.SRGRXPP;
+                oppositeSrgType = OpenroadmTpType.SRGTXPP;
+                oppositeTp = this.availableSrgCp.entrySet().stream()
+                        .filter(cp -> cp.getValue() == OpenroadmTpType.SRGRXCP).map(Map.Entry::getKey).findFirst();
+            } else {
+                LOG.info("Rx port ...");
+                optTp = this.availableSrgCp.entrySet().stream().filter(cp -> cp.getValue() == OpenroadmTpType.SRGRXCP)
+                        .map(Map.Entry::getKey).findFirst();
+                srgType = OpenroadmTpType.SRGTXPP;
+                oppositeSrgType = OpenroadmTpType.SRGRXPP;
+                oppositeTp = this.availableSrgCp.entrySet().stream()
+                        .filter(cp -> cp.getValue() == OpenroadmTpType.SRGTXCP).map(Map.Entry::getKey).findFirst();
             }
-            if (tp1.getTpType() == OpenroadmTpType.SRGTXRXCP) {
-                // Find an available PP of this CP
-                if (!this.availableSrgPp.isEmpty()) {
-                    LOG.info("finding PP for CP {}", tp.getTpId().getValue());
-                    Iterator<String> iterator = this.availableSrgPp.iterator();
-                    while (iterator.hasNext()) {
-                        String client = iterator.next();
-                        if ((client.equals("")) || (client == null)) {
-                            LOG.error("initRdmSrgTps: ROADM {} doesn't have defined Client {}", this.toString(), tp
-                                    .getTpId().getValue());
-                            this.valid = false;
-                        } else {
-                            this.valid = true;
-                            this.clientPerPpTp.put(tp.getTpId().getValue(), client);
-                            LOG.info("initRdmSrgTps: client PP {} for CP {} found !", client, tp.getTpId().getValue());
-                            iterator.remove();
-                            break;
-                        }
-                    }
-                } else {
-                    LOG.error("initRdmSrgTps: ROADM {} doesn't have available PP", this.nodeId.getValue());
+        } else {
+            LOG.info("BI Directional ports ...");
+        }
+        if (optTp.isPresent() && (srgType != null)) {
+            String tp = optTp.get();
+            if (!this.availableSrgPp.isEmpty()) {
+                LOG.info("finding PP for CP {}", optTp.get());
+                Optional<String> client = null;
+                final OpenroadmTpType openType = srgType;
+                client = this.availableSrgPp.entrySet().stream().filter(pp -> pp.getValue() == openType)
+                        .map(Map.Entry::getKey).findFirst();
+                if (!client.isPresent()) {
+                    LOG.error("initRdmSrgTps: ROADM {} doesn't have defined Client {}", this.toString(), tp);
                     this.valid = false;
                     return;
                 }
+                if (unidir) {
+                    final OpenroadmTpType oppositeOpType = oppositeSrgType;
+                    String opTp = oppositeTp.get();
+                    Optional<String> oppositeClient = this.availableSrgPp.entrySet().stream()
+                            .filter(pp -> pp.getValue() == oppositeOpType)
+                            .map(Map.Entry::getKey).findFirst();
+                    if (!oppositeClient.isPresent()) {
+                        LOG.error("initRdmSrgTps: ROADM {} doesn't have defined opposite Client {}",
+                                this.toString(), tp);
+                        this.valid = false;
+                        return;
+                    }
+                    this.clientPerPpTp.put(opTp, oppositeClient.get());
+                    LOG.info("initRdmSrgTps: client PP {} for oposite CP {} found !", client, tp);
+                }
+                this.valid = true;
+                this.clientPerPpTp.put(tp, client.get());
+                LOG.info("initRdmSrgTps: client PP {} for CP {} found !", client, tp);
             }
         }
         if (!isValid()) {
-            LOG.error("initRdmSrgTps: SRG doesn't have available wavelengths for node  {}", this.toString());
-            return;
-        }
-    }
-
-    public void initNodeTps() {
-        if (!isValid()) {
+            this.valid = false;
+            LOG.error("initRdmSrgTps: SRG TerminationPoint list is empty for node {}", this.toString());
             return;
         }
-        switch (this.nodeType) {
-            case SRG :
-                initSrgPpList();
-                initRdmSrgTps();
-                break;
-            case XPONDER :
-                initXndrTps();
-                break;
-            default:
-                this.valid = true;
-                LOG.warn("initNodeTps: Node TerminationPoints list is not SRG or XPONDER or DEGREE!");
-                break;
-        }
     }
 
     public boolean checkTP(String tp) {
@@ -304,7 +316,7 @@ public class PceNode {
     }
 
     public String getRdmSrgClient(String tp) {
-        LOG.info("Getting ROADM Client PP for CP {}", tp, this.clientPerPpTp.get(tp));
+        LOG.info("Getting ROADM Client PP for CP {} : {}", tp, this.clientPerPpTp.get(tp));
         return this.clientPerPpTp.get(tp);
     }