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