Distinguish available SRG for uni/bi-direction
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / PceOpticalNode.java
1 /*
2  * Copyright © 2020 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.math.BigDecimal;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.BitSet;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Optional;
19 import java.util.TreeMap;
20 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
21 import org.opendaylight.transportpce.common.mapping.PortMapping;
22 import org.opendaylight.transportpce.pce.SortPortsByName;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220114.mapping.Mapping;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.TerminationPoint1;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529.Node1;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmNodeType;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmTpType;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.available.freq.map.AvailFreqMapsKey;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.IfOCH;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.IfOtsiOtsigroup;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.format.rev190531.ServiceFormat;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
36 import org.opendaylight.yangtools.yang.common.Uint16;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class PceOpticalNode implements PceNode {
41     private static final Logger LOG = LoggerFactory.getLogger(PceOpticalNode.class);
42
43     private boolean valid = true;
44
45     private Node node;
46     private NodeId nodeId;
47     private String deviceNodeId;
48     private OpenroadmNodeType nodeType;
49     private AdminStates adminStates;
50     private State state;
51     private String serviceType;
52     private PortMapping portMapping;
53
54     private Map<String, OpenroadmTpType> availableSrgPp = new TreeMap<>();
55     private Map<String, OpenroadmTpType> availableSrgCp = new TreeMap<>();
56     private List<String> usedXpndrNWTps = new ArrayList<>();
57     private List<PceLink> outgoingLinks = new ArrayList<>();
58     private Map<String, String> clientPerNwTp = new HashMap<>();
59     private final AvailFreqMapsKey freqMapKey = new AvailFreqMapsKey(GridConstant.C_BAND);
60     private BitSet frequenciesBitSet;
61     private String version;
62     private BigDecimal slotWidthGranularity;
63     private BigDecimal centralFreqGranularity;
64
65     public PceOpticalNode(String deviceNodeId, String serviceType, PortMapping portMapping, Node node,
66         OpenroadmNodeType nodeType, String version, BigDecimal slotWidthGranularity,
67         BigDecimal centralFreqGranularity) {
68
69         if (deviceNodeId != null
70                 && serviceType != null
71                 && portMapping != null
72                 && node != null
73                 && node.getNodeId() != null
74                 && nodeType != null
75                 && version != null
76                 && slotWidthGranularity != null) {
77             this.deviceNodeId = deviceNodeId;
78             this.serviceType = serviceType;
79             this.portMapping = portMapping;
80             this.node = node;
81             this.nodeId = node.getNodeId();
82             this.nodeType = nodeType;
83             this.version = version;
84             this.slotWidthGranularity = slotWidthGranularity;
85             this.centralFreqGranularity = centralFreqGranularity;
86             this.adminStates = node.augmentation(org.opendaylight.yang.gen.v1.http
87                     .org.openroadm.common.network.rev200529.Node1.class).getAdministrativeState();
88             this.state = node.augmentation(org.opendaylight.yang.gen.v1.http
89                     .org.openroadm.common.network.rev200529.Node1.class).getOperationalState();
90         } else {
91             LOG.error("PceNode: one of parameters is not populated : nodeId, node type, slot width granularity");
92             this.valid = false;
93         }
94     }
95
96     public void initSrgTps() {
97         this.availableSrgPp.clear();
98         this.availableSrgCp.clear();
99         if (!isValid()) {
100             return;
101         }
102         LOG.info("initSrgTpList: getting SRG tps from ROADM node {}", this.nodeId);
103         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
104                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
105                     .ietf.network.topology.rev180226.Node1.class);
106         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
107             .node.TerminationPoint> allTps = new ArrayList<>(nodeTp.nonnullTerminationPoint().values());
108         if (allTps.isEmpty()) {
109             LOG.error("initSrgTpList: ROADM TerminationPoint list is empty for node {}", this);
110             this.valid = false;
111             return;
112         }
113         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
114             .node.TerminationPoint tp : allTps) {
115             TerminationPoint1 cntp1 = tp.augmentation(TerminationPoint1.class);
116             org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529.TerminationPoint1 nttp1 = tp
117                 .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529
118                         .TerminationPoint1.class);
119             OpenroadmTpType type = cntp1.getTpType();
120             LOG.info("type = {} for tp {}", type.getName(), tp);
121
122             switch (type) {
123                 case SRGTXRXCP:
124                 case SRGRXCP:
125                 case SRGTXCP:
126                     if (State.InService.equals(cntp1.getOperationalState())) {
127                         LOG.info("initSrgTpList: adding SRG-CP tp = {} ", tp.getTpId().getValue());
128                         this.availableSrgCp.put(tp.getTpId().getValue(), cntp1.getTpType());
129                     }
130                     break;
131                 case SRGRXPP:
132                 case SRGTXPP:
133                 case SRGTXRXPP:
134                     LOG.info("initSrgTpList: SRG-PP tp = {} found", tp.getTpId().getValue());
135                     if (isTerminationPointAvailable(nttp1)) {
136                         LOG.info("initSrgTpList: adding SRG-PP tp '{}'", tp.getTpId().getValue());
137                         this.availableSrgPp.put(tp.getTpId().getValue(), cntp1.getTpType());
138                         if (State.InService.equals(cntp1.getOperationalState())) {
139                             LOG.info("initSrgTpList: adding SRG-PP tp '{}'", tp.getTpId().getValue());
140                             this.availableSrgPp.put(tp.getTpId().getValue(), cntp1.getTpType());
141                         }
142                     } else {
143                         LOG.warn("initSrgTpList: SRG-PP tp = {} found is busy !!", tp.getTpId().getValue());
144                     }
145                     break;
146                 default:
147                     break;
148             }
149         }
150         if (this.availableSrgPp.isEmpty() || this.availableSrgCp.isEmpty()) {
151             LOG.error("initSrgTpList: ROADM SRG TerminationPoint list is empty for node {}", this);
152             this.valid = false;
153             return;
154         }
155         LOG.info("initSrgTpList: availableSrgPp size = {} && availableSrgCp size = {} in {}",
156             this.availableSrgPp.size(), this.availableSrgCp.size(), this);
157     }
158
159     private boolean isTerminationPointAvailable(
160             org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529.TerminationPoint1 nttp1) {
161         byte[] availableByteArray = new byte[GridConstant.NB_OCTECTS];
162         Arrays.fill(availableByteArray, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
163         return nttp1 == null || nttp1.getPpAttributes() == null
164                 || nttp1.getPpAttributes().getAvailFreqMaps() == null
165                 || !nttp1.getPpAttributes().getAvailFreqMaps().containsKey(freqMapKey)
166                 || nttp1.getPpAttributes().getAvailFreqMaps().get(freqMapKey).getFreqMap() == null
167                 || Arrays.equals(nttp1.getPpAttributes().getAvailFreqMaps().get(freqMapKey).getFreqMap(),
168                         availableByteArray);
169     }
170
171     private boolean isTpWithGoodCapabilities(
172         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.node
173         .TerminationPoint tp) {
174         Mapping mapping = this.portMapping.getMapping(deviceNodeId, tp.getTpId().getValue());
175         if (mapping == null || mapping.getSupportedInterfaceCapability() == null) {
176             return true;
177         }
178         switch (this.serviceType) {
179             case "400GE":
180                 if (mapping.getSupportedInterfaceCapability().contains(IfOtsiOtsigroup.class)) {
181                     return true;
182                 } else {
183                     return false;
184                 }
185             case "100GE":
186                 if (mapping.getSupportedInterfaceCapability().contains(
187                         org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev181019.IfOCH.class)
188                     || mapping.getSupportedInterfaceCapability().contains(IfOCH.class)) {
189                     return true;
190                 } else {
191                     return false;
192                 }
193             default:
194                 return true;
195         }
196     }
197
198     public void initFrequenciesBitSet() {
199         if (!isValid()) {
200             return;
201         }
202         Node1 node1 = this.node.augmentation(Node1.class);
203         org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.Node1 node11 =
204                 this.node.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.Node1
205                         .class);
206         switch (this.nodeType) {
207             case SRG :
208                 if (!State.InService.equals(node11.getOperationalState())) {
209                     this.valid = false;
210                     LOG.error("initWLlist: SRG node {} is OOS/degraded", this);
211                     return;
212                 }
213                 if (!node1.getSrgAttributes().nonnullAvailFreqMaps().containsKey(freqMapKey)) {
214                     LOG.error("initFrequenciesBitSet: SRG no cband available freq maps for node  {}", this);
215                     this.valid = false;
216                     return;
217                 }
218                 this.frequenciesBitSet = BitSet.valueOf(node1.getSrgAttributes()
219                         .nonnullAvailFreqMaps().get(freqMapKey).getFreqMap());
220                 break;
221             case DEGREE :
222                 if (!State.InService.equals(node11.getOperationalState())) {
223                     this.valid = false;
224                     LOG.error("initWLlist: Degree node {} is OOS/degraded", this);
225                     return;
226                 }
227                 if (!node1.getDegreeAttributes().nonnullAvailFreqMaps().containsKey(freqMapKey)) {
228                     LOG.error("initFrequenciesBitSet: DEG no cband available freq maps for node  {}", this);
229                     this.valid = false;
230                     return;
231                 }
232                 this.frequenciesBitSet = BitSet.valueOf(node1.getDegreeAttributes()
233                         .nonnullAvailFreqMaps().get(freqMapKey).getFreqMap());
234                 break;
235             case XPONDER :
236                 // at init all bits are set to false (unavailable)
237                 this.frequenciesBitSet = new BitSet(GridConstant.EFFECTIVE_BITS);
238                 //set all bits to true (available)
239                 this.frequenciesBitSet.set(0, GridConstant.EFFECTIVE_BITS);
240                 if (!State.InService.equals(node11.getOperationalState())) {
241                     this.valid = false;
242                     LOG.error("initWLlist: XPDR node {} is OOS/degraded", this);
243                     return;
244                 }
245                 break;
246             default:
247                 LOG.error("initFrequenciesBitSet: unsupported node type {} in node {}", this.nodeType, this);
248                 break;
249         }
250     }
251
252     public void initXndrTps(ServiceFormat serviceFormat) {
253         LOG.info("PceNod: initXndrTps for node : {}", this.nodeId);
254         if (!isValid()) {
255             return;
256         }
257         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
258                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
259                     .ietf.network.topology.rev180226.Node1.class);
260         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
261             .node.TerminationPoint> allTps = new ArrayList<>(nodeTp.nonnullTerminationPoint().values());
262         if (allTps.isEmpty()) {
263             this.valid = false;
264             LOG.error("initXndrTps: XPONDER TerminationPoint list is empty for node {}", this);
265             return;
266         }
267         this.valid = false;
268         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
269             .node.TerminationPoint tp : allTps) {
270             TerminationPoint1 cntp1 = tp.augmentation(TerminationPoint1.class);
271             if (cntp1.getTpType() != OpenroadmTpType.XPONDERNETWORK) {
272                 LOG.warn("initXndrTps: {} is not an Xponder network port", cntp1.getTpType().getName());
273                 continue;
274             }
275             if (!isTpWithGoodCapabilities(tp)) {
276                 LOG.warn("initXndrTps: {} network port has not correct if-capabilities", tp.getTpId().getValue());
277                 continue;
278             }
279             if (!State.InService.equals(cntp1.getOperationalState())) {
280                 LOG.warn("initXndrTps: XPONDER tp = {} is OOS/degraded", tp.getTpId().getValue());
281                 this.valid = false;
282                 continue;
283             }
284             org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529.TerminationPoint1 nttp1 = tp
285                 .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529
286                 .TerminationPoint1.class);
287             if (nttp1 != null && nttp1.getXpdrNetworkAttributes().getWavelength() != null) {
288                 this.usedXpndrNWTps.add(tp.getTpId().getValue());
289                 LOG.info("initXndrTps: XPONDER tp = {} is used", tp.getTpId().getValue());
290             } else {
291                 this.valid = true;
292             }
293             // find Client of this network TP
294             String client;
295             org.opendaylight.yang.gen.v1.http.transportpce.topology.rev220123.TerminationPoint1 tpceTp1 =
296                     tp.augmentation(org.opendaylight.yang.gen.v1.http.transportpce.topology.rev220123
297                             .TerminationPoint1.class);
298             if (tpceTp1 != null) {
299                 client = tpceTp1.getAssociatedConnectionMapPort();
300                 if (client != null) {
301                     this.clientPerNwTp.put(tp.getTpId().getValue(), client);
302                     this.valid = true;
303                 } else {
304                     LOG.error("Service Format {} not managed yet", serviceFormat.getName());
305                 }
306             }
307         }
308         if (!isValid()) {
309             LOG.error("initXndrTps: XPONDER doesn't have available wavelengths for node  {}", this);
310         }
311     }
312
313     @Override
314     public String getRdmSrgClient(String tp, String direction) {
315         LOG.info("getRdmSrgClient: Getting PP client for tp '{}' on node : {}", tp, this.nodeId);
316         OpenroadmTpType srgType = null;
317         OpenroadmTpType cpType = this.availableSrgCp.get(tp);
318         if (cpType == null) {
319             LOG.error("getRdmSrgClient: tp {} not existed in SRG CPterminationPoint list", tp);
320             return null;
321         }
322         switch (cpType) {
323             case SRGTXRXCP:
324                 LOG.info("getRdmSrgClient: Getting BI Directional PP port ...");
325                 // Take the first-element in the available PP key set
326                 if (availableSrgPp.entrySet().iterator().next().getKey()
327                         // and check if the port is bidirectional
328                         .contains("TXRX")) {
329                     srgType = OpenroadmTpType.SRGTXRXPP;
330                 } else if (direction.equalsIgnoreCase("aToz")) {
331                     srgType = OpenroadmTpType.SRGRXPP;
332                 } else {
333                     srgType = OpenroadmTpType.SRGTXPP;
334                 }
335                 break;
336             case SRGTXCP:
337                 LOG.info("getRdmSrgClient: Getting UNI Rx PP port ...");
338                 srgType = OpenroadmTpType.SRGRXPP;
339                 break;
340             case SRGRXCP:
341                 LOG.info("getRdmSrgClient: Getting UNI Tx PP port ...");
342                 srgType = OpenroadmTpType.SRGTXPP;
343                 break;
344             default:
345                 break;
346         }
347         LOG.info("getRdmSrgClient:  Getting client PP for CP '{}'", tp);
348         if (!this.availableSrgPp.isEmpty()) {
349             Optional<String> client = null;
350             final OpenroadmTpType openType = srgType;
351             client = this.availableSrgPp.entrySet()
352                     .stream().filter(pp -> pp.getValue().getName().equals(openType.getName()))
353                     .map(Map.Entry::getKey)
354                     .sorted(new SortPortsByName())
355                     .findFirst();
356             if (!client.isPresent()) {
357                 LOG.error("getRdmSrgClient: ROADM {} doesn't have PP Client for CP {}", this, tp);
358                 return null;
359             }
360             LOG.info("getRdmSrgClient: client PP {} for CP {} found !", client, tp);
361             return client.get();
362         } else {
363             LOG.error("getRdmSrgClient: SRG TerminationPoint PP list is not available for node {}", this);
364             return null;
365         }
366     }
367
368
369     public void validateAZxponder(String anodeId, String znodeId, ServiceFormat serviceFormat) {
370         if (!isValid()) {
371             return;
372         }
373         if (this.nodeType != OpenroadmNodeType.XPONDER) {
374             return;
375         }
376         // Detect A and Z
377         if (this.getSupNetworkNodeId().equals(anodeId) || (this.getSupNetworkNodeId().equals(znodeId))) {
378             LOG.info("validateAZxponder: A or Z node detected == {}", nodeId.getValue());
379             initXndrTps(serviceFormat);
380             return;
381         }
382         LOG.debug("validateAZxponder: XPONDER is ignored == {}", nodeId.getValue());
383         valid = false;
384     }
385
386     @Override
387     public boolean checkTP(String tp) {
388         return !this.usedXpndrNWTps.contains(tp);
389     }
390
391     public boolean isValid() {
392         if (node == null || nodeId == null || nodeType == null || this.getSupNetworkNodeId() == null
393             || this.getSupClliNodeId() == null || adminStates == null || state == null) {
394             LOG.error("PceNode: one of parameters is not populated : nodeId, node type, supporting nodeId, "
395                     + "admin state, operational state");
396             valid = false;
397         }
398         return valid;
399     }
400
401     @Override
402     public List<PceLink> getOutgoingLinks() {
403         return outgoingLinks;
404     }
405
406     @Override
407     public AdminStates getAdminStates() {
408         return adminStates;
409     }
410
411     @Override
412     public State getState() {
413         return state;
414     }
415
416     @Override
417     public NodeId getNodeId() {
418         return nodeId;
419     }
420
421     @Override
422     public String toString() {
423         return "PceNode type=" + nodeType + " ID=" + nodeId.getValue() + " CLLI=" + this.getSupClliNodeId();
424     }
425
426     @Override
427     public String getPceNodeType() {
428         return "optical";
429     }
430
431     @Override
432     public String getSupNetworkNodeId() {
433         return MapUtils.getSupNetworkNode(this.node);
434     }
435
436     @Override
437     public String getSupClliNodeId() {
438         return MapUtils.getSupClliNode(this.node);
439     }
440
441     @Override
442     public void addOutgoingLink(PceLink outLink) {
443         this.outgoingLinks.add(outLink);
444     }
445
446     @Override
447     public String getXpdrClient(String tp) {
448         return this.clientPerNwTp.get(tp);
449     }
450
451     @Override
452     public Map<String, List<Uint16>> getAvailableTribPorts() {
453         return null;
454     }
455
456     @Override
457     public Map<String, List<Uint16>> getAvailableTribSlots() {
458         return null;
459     }
460
461     /*
462     * (non-Javadoc)
463     *
464     * @see org.opendaylight.transportpce.pce.networkanalyzer.PceNode#getBitSetData()
465     */
466     @Override
467     public BitSet getBitSetData() {
468         return this.frequenciesBitSet;
469     }
470
471     /*
472     * (non-Javadoc)
473     *
474     * @see org.opendaylight.transportpce.pce.networkanalyzer.PceNode#getVersion()
475     */
476     @Override
477     public String getVersion() {
478         return this.version;
479     }
480
481     /*
482     * (non-Javadoc)
483     *
484     * @see org.opendaylight.transportpce.pce.networkanalyzer.PceNode#getSlotWidthGranularity()
485     */
486     @Override
487     public BigDecimal getSlotWidthGranularity() {
488         return slotWidthGranularity;
489     }
490
491     /*
492      * (non-Javadoc)
493      *
494      * @see org.opendaylight.transportpce.pce.networkanalyzer.PceNode#getCentralFreqGranularity()
495      */
496     @Override
497     public BigDecimal getCentralFreqGranularity() {
498         return centralFreqGranularity;
499     }
500
501 }