bda495cfa1727d9b69c7e3735422e73e23d76104
[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     public void validateXponder(String anodeId, String znodeId) {
275         if (!isValid()) {
276             return;
277         }
278         if (OpenroadmNodeType.SWITCH.equals(this.nodeType)) {
279             initXndrTps("intermediate");
280         }
281         if (this.nodeId.getValue().equals(anodeId) || (this.nodeId.getValue().equals(znodeId))) {
282             initXndrTps("AZ");
283         } else {
284             LOG.info("validateAZxponder: XPONDER is ignored == {}", nodeId.getValue());
285             valid = false;
286         }
287     }
288
289     public boolean validateSwitchingPoolBandwidth(
290             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
291                 .ietf.network.topology.rev180226.networks.network.node.TerminationPoint tp1,
292             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
293                 .ietf.network.topology.rev180226.networks.network.node.TerminationPoint tp2,
294             Long neededBW) {
295         if (this.nodeType != OpenroadmNodeType.TPDR) {
296             return true;
297         }
298         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.Node1 node1 =
299             node.augmentation(
300                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.Node1.class);
301         SwitchingPools sp = node1.getSwitchingPools();
302         List<OduSwitchingPools> osp = sp.getOduSwitchingPools();
303         for (OduSwitchingPools ospx : osp) {
304             List<NonBlockingList> nbl = ospx.getNonBlockingList();
305             for (NonBlockingList nbll : nbl) {
306                 if (nbll.getAvailableInterconnectBandwidth().toJava() >= neededBW && nbll.getTpList() != null
307                         && nbll.getTpList().contains(tp1.getTpId()) && nbll.getTpList().contains(tp2.getTpId())) {
308                     LOG.debug("validateSwitchingPoolBandwidth: couple  of tp {} x {} valid for crossconnection",
309                         tp1.getTpId(), tp2.getTpId());
310                     return true;
311                 }
312             }
313         }
314
315         LOG.debug("validateSwitchingPoolBandwidth: No valid Switching pool for crossconnecting tp {} and {}",
316             tp1.getTpId(), tp2.getTpId());
317         return false;
318
319     }
320
321     public void validateIntermediateSwitch() {
322         if (!isValid()) {
323             return;
324         }
325         if (this.nodeType != OpenroadmNodeType.SWITCH) {
326             return;
327         }
328         // Validate switch for use as an intermediate XPONDER on the path
329         initXndrTps("intermediate");
330         if (!this.valid) {
331             LOG.debug("validateIntermediateSwitch: Switch unusable for transit == {}", nodeId.getValue());
332         } else {
333             LOG.info("validateIntermediateSwitch: Switch usable for transit == {}", nodeId.getValue());
334         }
335     }
336
337     public void checkAvailableTribPort() {
338         List<TerminationPoint> networkTpList = node.augmentation(
339             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1.class)
340             .getTerminationPoint().stream()
341             .filter(type -> type
342                 .augmentation(
343                     org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.TerminationPoint1.class)
344                 .getTpType().equals(OpenroadmTpType.XPONDERNETWORK))
345             .collect(Collectors.toList());
346
347         for (TerminationPoint tp : networkTpList) {
348             if (tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getOdtuTpnPool() != null
349                 && tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getOdtuTpnPool().get(0)
350                     .getOdtuType().equals(ODTU4TsAllocated.class)) {
351                 @Nullable
352                 List<Uint16> tpnPool = tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes()
353                     .getOdtuTpnPool().get(0).getTpnPool();
354                 if (tpnPool != null) {
355                     tpAvailableTribPort.put(tp.getTpId().getValue(), tpnPool);
356                 }
357             }
358         }
359     }
360
361     public void checkAvailableTribSlot() {
362         List<TerminationPoint> networkTpList = node.augmentation(
363             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1.class)
364             .getTerminationPoint().stream()
365             .filter(type -> type
366                 .augmentation(
367                     org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.TerminationPoint1.class)
368                 .getTpType().equals(OpenroadmTpType.XPONDERNETWORK))
369             .collect(Collectors.toList());
370
371         for (TerminationPoint tp : networkTpList) {
372             if (tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getTsPool() != null) {
373                 @Nullable
374                 List<Uint16> tsPool = tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes()
375                     .getTsPool();
376                 tpAvailableTribSlot.put(tp.getTpId().getValue(), tsPool);
377             }
378         }
379     }
380
381     public boolean isValid() {
382         if ((node == null) || (nodeId == null) || (nodeType == null) || (this.getSupNetworkNodeId() == null)
383             || (this.getSupClliNodeId() == null)) {
384             LOG.error("PceNode: one of parameters is not populated : nodeId, node type, supporting nodeId");
385             valid = false;
386         }
387         return valid;
388     }
389
390     @Override
391     public void addOutgoingLink(PceLink outLink) {
392         this.outgoingLinks.add(outLink);
393     }
394
395     @Override
396     public List<PceLink> getOutgoingLinks() {
397         return outgoingLinks;
398     }
399
400     @Override
401     public String getXpdrClient(String tp) {
402         return this.clientPerNwTp.get(tp);
403     }
404
405     @Override
406     public String toString() {
407         return "PceNode type=" + nodeType + " ID=" + nodeId.getValue() + " CLLI=" + this.getSupClliNodeId();
408     }
409
410     public void printLinksOfNode() {
411         LOG.info(" outgoing links of node {} : {} ", nodeId.getValue(), this.getOutgoingLinks());
412     }
413
414     @Override
415     public Map<String, List<Uint16>> getAvailableTribPorts() {
416         return tpAvailableTribPort;
417     }
418
419     @Override
420     public Map<String, List<Uint16>> getAvailableTribSlots() {
421         return tpAvailableTribSlot;
422     }
423
424     public List<TpId> getUsableXpdrNWTps() {
425         return usableXpdrNWTps;
426     }
427
428     public List<TpId> getUsableXpdrClientTps() {
429         return usableXpdrClientTps;
430     }
431
432     @Override
433     public String getPceNodeType() {
434         return this.pceNodeType;
435     }
436
437     @Override
438     public String getSupNetworkNodeId() {
439         return MapUtils.getSupNetworkNode(this.node);
440     }
441
442     @Override
443     public String getSupClliNodeId() {
444         return MapUtils.getSupClliNode(this.node);
445     }
446
447     @Override
448     public String getRdmSrgClient(String tp) {
449         return null;
450     }
451
452     @Override
453     public NodeId getNodeId() {
454         return nodeId;
455     }
456
457     @Override
458     public boolean checkTP(String tp) {
459         return false;
460     }
461
462     @Override
463     public boolean checkWL(long index) {
464         return false;
465     }
466 }