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