Merge "Bump project to 2.0.0 and fix feature deployment"
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / PceOtnNode.java
1 /*
2  * Copyright © 2019 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.transportpce.pce.networkanalyzer;
10
11 import java.util.ArrayList;
12 import java.util.Comparator;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.TreeMap;
17 import java.util.stream.Collectors;
18
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.types.rev181130.xpdr.odu.switching.pools.OduSwitchingPools;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.types.rev181130.xpdr.odu.switching.pools.odu.switching.pools.NonBlockingList;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmNodeType;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmTpType;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.xpdr.tp.supported.interfaces.SupportedInterfaceCapability;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev181130.ODTU4TsAllocated;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.Node1;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.TerminationPoint1;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.networks.network.node.SwitchingPools;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev181130.If100GEODU4;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev181130.If10GEODU2e;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev181130.If1GEODU0;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev181130.IfOCHOTU4ODU4;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.TpId;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.node.TerminationPoint;
37 import org.opendaylight.yangtools.yang.common.Uint16;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class PceOtnNode implements PceNode {
42     /* Logging. */
43     private static final Logger LOG = LoggerFactory.getLogger(PceOtnNode.class);
44     ////////////////////////// OTN NODES ///////////////////////////
45     /*
46      * For This Class the node passed shall be at the otn-openroadm Layer
47      */
48
49     private boolean valid = true;
50
51     private final Node node;
52     private final NodeId nodeId;
53     private final OpenroadmNodeType nodeType;
54     private final String pceNodeType;
55     private final String otnServiceType;
56
57     private Map<String, List<Uint16>> tpAvailableTribPort = new TreeMap<>();
58     private Map<String, List<Uint16>> tpAvailableTribSlot = new TreeMap<>();
59     private Map<String, OpenroadmTpType> availableXponderTp = new TreeMap<>();
60     private List<String> usedXpdrNWTps = new ArrayList<>();
61     private List<TpId> availableXpdrNWTps;
62     private List<TpId> usableXpdrNWTps;
63     private List<String> usedXpdrClientTps = new ArrayList<>();
64     private List<TpId> availableXpdrClientTps;
65     private List<TpId> usableXpdrClientTps;
66
67     private List<PceLink> outgoingLinks = new ArrayList<>();
68     private Map<String, String> clientPerNwTp = new HashMap<>();
69
70     public PceOtnNode(Node node, OpenroadmNodeType nodeType, NodeId nodeId, String pceNodeType, String serviceType) {
71         this.node = node;
72         this.nodeId = nodeId;
73         this.nodeType = nodeType;
74         this.pceNodeType = pceNodeType;
75         this.otnServiceType = serviceType;
76         this.tpAvailableTribSlot.clear();
77         this.usedXpdrNWTps.clear();
78         this.availableXpdrNWTps = new ArrayList<>();
79         this.usableXpdrNWTps = new ArrayList<>();
80         this.usedXpdrClientTps.clear();
81         this.availableXpdrClientTps = new ArrayList<>();
82         this.usableXpdrClientTps = new ArrayList<>();
83         this.tpAvailableTribPort.clear();
84         checkAvailableTribPort();
85         this.tpAvailableTribSlot.clear();
86         checkAvailableTribSlot();
87         if ((node == null) || (nodeId == null) || (nodeType != OpenroadmNodeType.MUXPDR)
88             && (nodeType != OpenroadmNodeType.SWITCH) && (nodeType != OpenroadmNodeType.TPDR)) {
89             LOG.error("PceOtnNode: one of parameters is not populated : nodeId, node type");
90             this.valid = false;
91         }
92     }
93
94     public void initXndrTps(String mode) {
95         LOG.info("PceOtnNode: initXndrTps for node {}", this.nodeId.getValue());
96         this.availableXponderTp.clear();
97
98         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp
99             = this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
100                 .ietf.network.topology.rev180226.Node1.class);
101         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
102             .node.TerminationPoint> allTps = nodeTp.getTerminationPoint();
103         this.valid = false;
104         if (allTps == null) {
105             LOG.error("PceOtnNode: initXndrTps: XPONDER TerminationPoint list is empty for node {}", this);
106             return;
107         }
108
109         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
110             .node.TerminationPoint tp : allTps) {
111             org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.@Nullable TerminationPoint1 ocnTp1
112                 = tp.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130
113                 .TerminationPoint1.class);
114             //TODO many nested if-structures below, this needs to be reworked
115             if (OpenroadmTpType.XPONDERNETWORK.equals(ocnTp1.getTpType()) && this.otnServiceType.equals("ODU4")) {
116                 TerminationPoint1 ontTp1;
117                 if (tp.augmentation(TerminationPoint1.class) != null) {
118                     ontTp1 = tp.augmentation(TerminationPoint1.class);
119                 } else {
120                     continue;
121                 }
122                 if (checkTpForOdtuTermination(ontTp1)) {
123                     LOG.info("TP {} of XPONDER {} is validated", tp.getTpId(), node.getNodeId().getValue());
124                     this.availableXpdrNWTps.add(tp.getTpId());
125                 } else {
126                     LOG.error("TP {} of {} does not allow ODU4 termination creation", tp.getTpId().getValue(),
127                         node.getNodeId().getValue());
128                 }
129             } else if (OpenroadmTpType.XPONDERNETWORK.equals(ocnTp1.getTpType())
130                 && (this.otnServiceType.equals("10GE") || this.otnServiceType.equals("1GE"))) {
131                 TerminationPoint1 ontTp1;
132                 if (tp.augmentation(TerminationPoint1.class) != null) {
133                     ontTp1 = tp.augmentation(TerminationPoint1.class);
134                 } else {
135                     continue;
136                 }
137                 if ("10GE".equals(otnServiceType) && checkOdtuTTPforLoOduCreation(ontTp1, 10)
138                     || "1GE".equals(otnServiceType) && checkOdtuTTPforLoOduCreation(ontTp1, 1)) {
139                     LOG.info("TP {} of XPONDER {} is validated", tp.getTpId(), node.getNodeId().getValue());
140                     this.availableXpdrNWTps.add(tp.getTpId());
141                 } else {
142                     if ("10GE".equals(otnServiceType)) {
143                         LOG.error("TP {} of {} does not allow OD2e termination creation", tp.getTpId().getValue(),
144                             node.getNodeId().getValue());
145                     } else if ("1GE".equals(otnServiceType)) {
146                         LOG.error("TP {} of {} does not allow ODU0 termination creation", tp.getTpId().getValue(),
147                             node.getNodeId().getValue());
148                     } else {
149                         LOG.error("TP {} of {} does not allow any termination creation", tp.getTpId().getValue(),
150                             node.getNodeId().getValue());
151                     }
152                 }
153             } else if (OpenroadmTpType.XPONDERCLIENT.equals(ocnTp1.getTpType())
154                 && (this.otnServiceType.equals("10GE") || this.otnServiceType.equals("1GE"))) {
155                 TerminationPoint1 ontTp1;
156                 if (tp.augmentation(TerminationPoint1.class) != null) {
157                     ontTp1 = tp.augmentation(TerminationPoint1.class);
158                 } else {
159                     continue;
160                 }
161                 if (checkClientTp(ontTp1)) {
162                     LOG.info("TP {} of XPONDER {} is validated", tp.getTpId(), node.getNodeId().getValue());
163                     this.availableXpdrClientTps.add(tp.getTpId());
164                 } else {
165                     LOG.error("TP {} of {} does not allow lo-ODU (ODU2e or ODU0) termination creation",
166                         tp.getTpId().getValue(), node.getNodeId().getValue());
167                 }
168             }
169         }
170
171         if ((this.otnServiceType.equals("ODU4") && mode.equals("AZ"))
172             || ((this.otnServiceType.equals("10GE") || this.otnServiceType.equals("1GE"))
173                 && mode.equals("AZ") && checkSwPool(availableXpdrClientTps, availableXpdrNWTps, 1, 1))
174             || ((this.otnServiceType.equals("10GE") || this.otnServiceType.equals("1GE"))
175                 && mode.equals("intermediate") && checkSwPool(null, availableXpdrNWTps, 0, 2))) {
176             this.valid = true;
177         } else {
178             this.valid = false;
179         }
180     }
181
182     private boolean checkSwPool(List<TpId> clientTps, List<TpId> netwTps, int nbClient, int nbNetw) {
183         if (clientTps != null && netwTps != null && nbClient == 1 && nbNetw == 1) {
184             clientTps.sort(Comparator.comparing(TpId::getValue));
185             netwTps.sort(Comparator.comparing(TpId::getValue));
186             for (TpId nwTp : netwTps) {
187                 for (TpId clTp : clientTps) {
188                     @Nullable
189                     List<NonBlockingList> nblList = node.augmentation(Node1.class).getSwitchingPools()
190                         .getOduSwitchingPools().get(0).getNonBlockingList();
191                     for (NonBlockingList nbl : nblList) {
192                         if (nbl.getTpList().contains(clTp) && nbl.getTpList().contains(nwTp)) {
193                             usableXpdrClientTps.add(clTp);
194                             usableXpdrNWTps.add(nwTp);
195                         }
196                         if (usableXpdrClientTps.size() >= nbClient && usableXpdrNWTps.size() >= nbNetw) {
197                             clientPerNwTp.put(nwTp.getValue(), clTp.getValue());
198                             return true;
199                         }
200                     }
201                 }
202             }
203
204         }
205         if (clientTps == null && netwTps != null && nbClient == 0 && nbNetw == 2) {
206             netwTps.sort(Comparator.comparing(TpId::getValue));
207             @Nullable
208             List<NonBlockingList> nblList = node.augmentation(Node1.class).getSwitchingPools().getOduSwitchingPools()
209                 .get(0).getNonBlockingList();
210             for (NonBlockingList nbl : nblList) {
211                 for (TpId nwTp : netwTps) {
212                     if (nbl.getTpList().contains(nwTp)) {
213                         usableXpdrNWTps.add(nwTp);
214                     }
215                     if (usableXpdrNWTps.size() >= nbNetw) {
216                         return true;
217                     }
218                 }
219             }
220         }
221         return false;
222     }
223
224     private boolean checkTpForOdtuTermination(TerminationPoint1 ontTp1) {
225         for (SupportedInterfaceCapability sic : ontTp1.getTpSupportedInterfaces().getSupportedInterfaceCapability()) {
226             LOG.debug("in checkTpForOduTermination - sic = {}", sic.getIfCapType());
227             if (sic.getIfCapType().equals(IfOCHOTU4ODU4.class)
228                 && ontTp1.getXpdrTpPortConnectionAttributes().getTsPool() == null) {
229                 return true;
230             }
231         }
232         return false;
233     }
234
235     private boolean checkOdtuTTPforLoOduCreation(TerminationPoint1 ontTp1, int tsNb) {
236         if (ontTp1.getXpdrTpPortConnectionAttributes() != null
237             && ontTp1.getXpdrTpPortConnectionAttributes().getTsPool() != null
238             && ontTp1.getXpdrTpPortConnectionAttributes().getOdtuTpnPool() != null
239             && ontTp1.getXpdrTpPortConnectionAttributes().getOdtuTpnPool().get(0).getOdtuType()
240                 .equals(ODTU4TsAllocated.class)
241             && ontTp1.getXpdrTpPortConnectionAttributes().getOdtuTpnPool().get(0).getTpnPool().isEmpty()
242             && (ontTp1.getXpdrTpPortConnectionAttributes().getTsPool().size() >= tsNb)) {
243             return true;
244         }
245         return false;
246     }
247
248     private boolean checkClientTp(TerminationPoint1 ontTp1) {
249         for (SupportedInterfaceCapability sic : ontTp1.getTpSupportedInterfaces().getSupportedInterfaceCapability()) {
250             LOG.debug("in checkTpForOduTermination - sic = {}", sic.getIfCapType());
251             switch (otnServiceType) {
252                 case "1GE":
253                 // we could also check the administrative status of the tp
254                     if (sic.getIfCapType().equals(If1GEODU0.class)) {
255                         return true;
256                     }
257                     break;
258                 case "10GE":
259                     if (sic.getIfCapType().equals(If10GEODU2e.class)) {
260                         return true;
261                     }
262                     break;
263                 case "100GE":
264                     if (sic.getIfCapType().equals(If100GEODU4.class)) {
265                         return true;
266                     }
267                     break;
268                 default:
269                     break;
270             }
271         }
272         return false;
273     }
274
275     private Boolean findClientCompliantInterface(List<SupportedInterfaceCapability> sic) {
276         boolean compliant = false;
277         for (SupportedInterfaceCapability sit : sic) {
278             String interfacetype = sit.getIfCapType().getName();
279             switch (interfacetype) {
280                 case "If1GEODU0":
281                 case "If1GE":
282                     if ("1GE".equals(this.otnServiceType)) {
283                         compliant = true;
284                     }
285                     break;
286                 case "If10GEODU2e":
287                 case "If10GE":
288                     if ("10GE".equals(this.otnServiceType)) {
289                         compliant = true;
290                     }
291                     break;
292                 case "If100GEODU4":
293                 case "If100GE":
294                     if ("100GE".equals(this.otnServiceType)) {
295                         compliant = true;
296                     }
297                     break;
298                 case "IfOTU4ODU4":
299                 case "IfOCHOTU4ODU4":
300                     if ("OTU4".equals(this.otnServiceType) || "ODU4".equals(this.otnServiceType)) {
301                         compliant = true;
302                     }
303                     break;
304                 default:
305                     compliant = false;
306                     break;
307             }
308         }
309         return compliant;
310     }
311
312     private Boolean findNetworkCompliantInterface(List<SupportedInterfaceCapability> sic) {
313         boolean compliant = false;
314         for (SupportedInterfaceCapability sit : sic) {
315             String interfacetype = sit.getIfCapType().toString();
316             switch (interfacetype) {
317                 case "IfOTU4ODU4":
318                 case "IfOCHOTU4ODU4":
319                     compliant = true;
320                     break;
321                 case "IfOTU2ODU2":
322                 case "IfOCHOTU2ODU2":
323                     if (("1GE".equals(this.otnServiceType)) || ("10GE".equals(this.otnServiceType))) {
324                         compliant = true;
325                     }
326                     break;
327                 // add all use case with higher rate interfaces when it shows up
328                 default:
329                     compliant = false;
330                     break;
331             }
332         }
333         return compliant;
334     }
335
336     public void validateXponder(String anodeId, String znodeId) {
337         if (!isValid()) {
338             return;
339         }
340         if (OpenroadmNodeType.SWITCH.equals(this.nodeType)) {
341             initXndrTps("intermediate");
342         }
343         if (this.nodeId.getValue().equals(anodeId) || (this.nodeId.getValue().equals(znodeId))) {
344             initXndrTps("AZ");
345         } else {
346             LOG.info("validateAZxponder: XPONDER is ignored == {}", nodeId.getValue());
347             valid = false;
348         }
349     }
350
351     public boolean validateSwitchingPoolBandwidth(
352             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
353                 .ietf.network.topology.rev180226.networks.network.node.TerminationPoint tp1,
354             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
355                 .ietf.network.topology.rev180226.networks.network.node.TerminationPoint tp2,
356             Long neededBW) {
357         if (this.nodeType != OpenroadmNodeType.TPDR) {
358             return true;
359         }
360         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.Node1 node1 =
361             node.augmentation(
362                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.Node1.class);
363         SwitchingPools sp = node1.getSwitchingPools();
364         List<OduSwitchingPools> osp = sp.getOduSwitchingPools();
365         for (OduSwitchingPools ospx : osp) {
366             List<NonBlockingList> nbl = ospx.getNonBlockingList();
367             for (NonBlockingList nbll : nbl) {
368                 if (nbll.getAvailableInterconnectBandwidth().toJava() >= neededBW && nbll.getTpList() != null
369                         && nbll.getTpList().contains(tp1.getTpId()) && nbll.getTpList().contains(tp2.getTpId())) {
370                     LOG.debug("validateSwitchingPoolBandwidth: couple  of tp {} x {} valid for crossconnection",
371                         tp1.getTpId(), tp2.getTpId());
372                     return true;
373                 }
374             }
375         }
376
377         LOG.debug("validateSwitchingPoolBandwidth: No valid Switching pool for crossconnecting tp {} and {}",
378             tp1.getTpId(), tp2.getTpId());
379         return false;
380
381     }
382
383     public void validateIntermediateSwitch() {
384         if (!isValid()) {
385             return;
386         }
387         if (this.nodeType != OpenroadmNodeType.SWITCH) {
388             return;
389         }
390         // Validate switch for use as an intermediate XPONDER on the path
391         initXndrTps("intermediate");
392         if (!this.valid) {
393             LOG.debug("validateIntermediateSwitch: Switch unusable for transit == {}", nodeId.getValue());
394         } else {
395             LOG.info("validateIntermediateSwitch: Switch usable for transit == {}", nodeId.getValue());
396         }
397     }
398
399     public void checkAvailableTribPort() {
400         List<TerminationPoint> networkTpList = node.augmentation(
401             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1.class)
402             .getTerminationPoint().stream()
403             .filter(type -> type
404                 .augmentation(
405                     org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.TerminationPoint1.class)
406                 .getTpType().equals(OpenroadmTpType.XPONDERNETWORK))
407             .collect(Collectors.toList());
408
409         for (TerminationPoint tp : networkTpList) {
410             if (tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getOdtuTpnPool() != null
411                 && tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getOdtuTpnPool().get(0)
412                     .getOdtuType().equals(ODTU4TsAllocated.class)) {
413                 @Nullable
414                 List<Uint16> tpnPool = tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes()
415                     .getOdtuTpnPool().get(0).getTpnPool();
416                 if (tpnPool != null) {
417                     tpAvailableTribPort.put(tp.getTpId().getValue(), tpnPool);
418                 }
419             }
420         }
421     }
422
423     public void checkAvailableTribSlot() {
424         List<TerminationPoint> networkTpList = node.augmentation(
425             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1.class)
426             .getTerminationPoint().stream()
427             .filter(type -> type
428                 .augmentation(
429                     org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.TerminationPoint1.class)
430                 .getTpType().equals(OpenroadmTpType.XPONDERNETWORK))
431             .collect(Collectors.toList());
432
433         for (TerminationPoint tp : networkTpList) {
434             if (tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getTsPool() != null) {
435                 @Nullable
436                 List<Uint16> tsPool = tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes()
437                     .getTsPool();
438                 tpAvailableTribSlot.put(tp.getTpId().getValue(), tsPool);
439             }
440         }
441     }
442
443     public boolean isValid() {
444         if ((node == null) || (nodeId == null) || (nodeType == null) || (this.getSupNetworkNodeId() == null)
445             || (this.getSupClliNodeId() == null)) {
446             LOG.error("PceNode: one of parameters is not populated : nodeId, node type, supporting nodeId");
447             valid = false;
448         }
449         return valid;
450     }
451
452     @Override
453     public void addOutgoingLink(PceLink outLink) {
454         this.outgoingLinks.add(outLink);
455     }
456
457     @Override
458     public List<PceLink> getOutgoingLinks() {
459         return outgoingLinks;
460     }
461
462     @Override
463     public String getXpdrClient(String tp) {
464         return this.clientPerNwTp.get(tp);
465     }
466
467     @Override
468     public String toString() {
469         return "PceNode type=" + nodeType + " ID=" + nodeId.getValue() + " CLLI=" + this.getSupClliNodeId();
470     }
471
472     public void printLinksOfNode() {
473         LOG.info(" outgoing links of node {} : {} ", nodeId.getValue(), this.getOutgoingLinks());
474     }
475
476     @Override
477     public Map<String, List<Uint16>> getAvailableTribPorts() {
478         return tpAvailableTribPort;
479     }
480
481     @Override
482     public Map<String, List<Uint16>> getAvailableTribSlots() {
483         return tpAvailableTribSlot;
484     }
485
486     public List<TpId> getUsableXpdrNWTps() {
487         return usableXpdrNWTps;
488     }
489
490     public List<TpId> getUsableXpdrClientTps() {
491         return usableXpdrClientTps;
492     }
493
494     @Override
495     public String getPceNodeType() {
496         return this.pceNodeType;
497     }
498
499     @Override
500     public String getSupNetworkNodeId() {
501         return MapUtils.getSupNetworkNode(this.node);
502     }
503
504     @Override
505     public String getSupClliNodeId() {
506         return MapUtils.getSupClliNode(this.node);
507     }
508
509     @Override
510     public String getRdmSrgClient(String tp) {
511         return null;
512     }
513
514     @Override
515     public NodeId getNodeId() {
516         return nodeId;
517     }
518
519     @Override
520     public boolean checkTP(String tp) {
521         return false;
522     }
523
524     @Override
525     public boolean checkWL(long index) {
526         return false;
527     }
528 }