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