Adapt TransportPCE code to Chlorine
[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.rev220316.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.rev201211.IfOCH;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev201211.IfOCHOTU4ODU4;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev201211.IfOtsiOtsigroup;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev201211.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.debug("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.debug("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.debug("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.debug("initSrgTpList: SRG-PP tp = {} found", tp.getTpId().getValue());
137                     if (isTerminationPointAvailable(nttp1)) {
138                         LOG.debug("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.debug("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.debug("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 (SupportedIfCapability ifCap : mapping.getSupportedInterfaceCapability()) {
183                     if (ifCap.equals(IfOtsiOtsigroup.VALUE)) {
184                         return true;
185                     }
186                 }
187                 return false;
188             case "100GE":
189                 return mapping.getSupportedInterfaceCapability().contains(IfOCH.VALUE)
190                         || mapping.getSupportedInterfaceCapability().contains(IfOCHOTU4ODU4.VALUE);
191             default:
192                 return true;
193         }
194     }
195
196     public void initFrequenciesBitSet() {
197         if (!isValid()) {
198             return;
199         }
200         Node1 node1 = this.node.augmentation(Node1.class);
201         org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev211210.Node1 node11 =
202                 this.node.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev211210.Node1
203                         .class);
204         switch (this.nodeType) {
205             case SRG :
206                 if (!State.InService.equals(node11.getOperationalState())) {
207                     this.valid = false;
208                     LOG.error("initWLlist: SRG node {} is OOS/degraded", this);
209                     return;
210                 }
211                 if (!node1.getSrgAttributes().nonnullAvailFreqMaps().containsKey(freqMapKey)) {
212                     LOG.error("initFrequenciesBitSet: SRG no cband available freq maps for node  {}", this);
213                     this.valid = false;
214                     return;
215                 }
216                 this.frequenciesBitSet = BitSet.valueOf(node1.getSrgAttributes()
217                         .nonnullAvailFreqMaps().get(freqMapKey).getFreqMap());
218                 break;
219             case DEGREE :
220                 if (!State.InService.equals(node11.getOperationalState())) {
221                     this.valid = false;
222                     LOG.error("initWLlist: Degree node {} is OOS/degraded", this);
223                     return;
224                 }
225                 if (!node1.getDegreeAttributes().nonnullAvailFreqMaps().containsKey(freqMapKey)) {
226                     LOG.error("initFrequenciesBitSet: DEG no cband available freq maps for node  {}", this);
227                     this.valid = false;
228                     return;
229                 }
230                 this.frequenciesBitSet = BitSet.valueOf(node1.getDegreeAttributes()
231                         .nonnullAvailFreqMaps().get(freqMapKey).getFreqMap());
232                 break;
233             case XPONDER :
234                 // at init all bits are set to false (unavailable)
235                 this.frequenciesBitSet = new BitSet(GridConstant.EFFECTIVE_BITS);
236                 //set all bits to true (available)
237                 this.frequenciesBitSet.set(0, GridConstant.EFFECTIVE_BITS);
238                 if (!State.InService.equals(node11.getOperationalState())) {
239                     this.valid = false;
240                     LOG.error("initWLlist: XPDR node {} is OOS/degraded", this);
241                 }
242                 break;
243             default:
244                 LOG.error("initFrequenciesBitSet: unsupported node type {} in node {}", this.nodeType, this);
245                 break;
246         }
247     }
248
249     public void initXndrTps(ServiceFormat serviceFormat) {
250         LOG.debug("PceNod: initXndrTps for node : {}", this.nodeId);
251         if (!isValid()) {
252             return;
253         }
254         this.valid = false;
255         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
256                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
257                     .ietf.network.topology.rev180226.Node1.class);
258         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
259             .node.TerminationPoint> allTps = new ArrayList<>(nodeTp.nonnullTerminationPoint().values());
260         if (allTps.isEmpty()) {
261             LOG.error("initXndrTps: XPONDER TerminationPoint list is empty for node {}", this);
262             return;
263         }
264         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
265             .node.TerminationPoint tp : allTps) {
266             TerminationPoint1 cntp1 = tp.augmentation(TerminationPoint1.class);
267             if (cntp1.getTpType() != OpenroadmTpType.XPONDERNETWORK) {
268                 LOG.debug("initXndrTps: {} is not an Xponder network port", cntp1.getTpType().getName());
269                 continue;
270             }
271             if (!isTpWithGoodCapabilities(tp)) {
272                 LOG.warn("initXndrTps: {} network port has not correct if-capabilities", tp.getTpId().getValue());
273                 continue;
274             }
275             if (!State.InService.equals(cntp1.getOperationalState())) {
276                 LOG.warn("initXndrTps: XPONDER tp = {} is OOS/degraded", tp.getTpId().getValue());
277                 continue;
278             }
279             org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev211210.TerminationPoint1 nttp1 = tp
280                 .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev211210
281                 .TerminationPoint1.class);
282             if (nttp1 != null && nttp1.getXpdrNetworkAttributes().getWavelength() != null) {
283                 this.usedXpndrNWTps.add(tp.getTpId().getValue());
284                 LOG.debug("initXndrTps: XPONDER tp = {} is used", tp.getTpId().getValue());
285                 continue;
286             }
287             // find Client of this network TP
288             if (cntp1.getAssociatedConnectionMapTp() != null) {
289                 String client = cntp1.getAssociatedConnectionMapTp().iterator().next().getValue();
290                 if (client != null) {
291                     this.clientPerNwTp.put(tp.getTpId().getValue(), client);
292                     this.valid = true;
293                 } else {
294                     LOG.error("Service Format {} not managed yet", serviceFormat.getName());
295                 }
296             } else {
297                 this.valid = true;
298             }
299         }
300         if (!isValid()) {
301             LOG.error("initXndrTps: XPONDER doesn't have available wavelengths for node  {}", this);
302         }
303     }
304
305     @Override
306     public String getRdmSrgClient(String tp, String direction) {
307         LOG.debug("getRdmSrgClient: Getting PP client for tp '{}' on node : {}", tp, this.nodeId);
308         OpenroadmTpType srgType = null;
309         OpenroadmTpType cpType = this.availableSrgCp.get(tp);
310         if (cpType == null) {
311             LOG.error("getRdmSrgClient: tp {} not existed in SRG CPterminationPoint list", tp);
312             return null;
313         }
314         switch (cpType) {
315             case SRGTXRXCP:
316                 LOG.debug("getRdmSrgClient: Getting BI Directional PP port ...");
317                 // Take the first-element in the available PP key set
318                 if (availableSrgPp.entrySet().iterator().next().getKey()
319                         // and check if the port is bidirectional
320                         .contains("TXRX")) {
321                     srgType = OpenroadmTpType.SRGTXRXPP;
322                 } else if (direction.equalsIgnoreCase("aToz")) {
323                     srgType = OpenroadmTpType.SRGRXPP;
324                 } else {
325                     srgType = OpenroadmTpType.SRGTXPP;
326                 }
327                 break;
328             case SRGTXCP:
329                 LOG.debug("getRdmSrgClient: Getting UNI Rx PP port ...");
330                 srgType = OpenroadmTpType.SRGRXPP;
331                 break;
332             case SRGRXCP:
333                 LOG.debug("getRdmSrgClient: Getting UNI Tx PP port ...");
334                 srgType = OpenroadmTpType.SRGTXPP;
335                 break;
336             default:
337                 break;
338         }
339         LOG.debug("getRdmSrgClient:  Getting client PP for CP '{}'", tp);
340         if (this.availableSrgPp.isEmpty()) {
341             LOG.error("getRdmSrgClient: SRG TerminationPoint PP list is not available for node {}", this);
342             return null;
343         }
344         final OpenroadmTpType openType = srgType;
345         Optional<String> client = this.availableSrgPp.entrySet()
346                 .stream().filter(pp -> pp.getValue().getName().equals(openType.getName()))
347                 .map(Map.Entry::getKey).min(new SortPortsByName());
348         if (client.isEmpty()) {
349             LOG.error("getRdmSrgClient: ROADM {} doesn't have PP Client for CP {}", this, tp);
350             return null;
351         }
352         LOG.debug("getRdmSrgClient: client PP {} for CP {} found !", client, tp);
353         return client.get();
354     }
355
356
357     public void validateAZxponder(String anodeId, String znodeId, ServiceFormat serviceFormat) {
358         if (!isValid() || this.nodeType != OpenroadmNodeType.XPONDER) {
359             return;
360         }
361         // Detect A and Z
362         if (anodeId.contains(this.getSupNetworkNodeId()) || (znodeId.contains(this.getSupNetworkNodeId()))) {
363             LOG.info("validateAZxponder: A or Z node detected == {}", nodeId.getValue());
364             initXndrTps(serviceFormat);
365             return;
366         }
367         LOG.debug("validateAZxponder: XPONDER is ignored == {}", nodeId.getValue());
368         valid = false;
369     }
370
371     @Override
372     public boolean checkTP(String tp) {
373         return !this.usedXpndrNWTps.contains(tp);
374     }
375
376     public boolean isValid() {
377         if (node == null || nodeId == null || nodeType == null || this.getSupNetworkNodeId() == null
378             || this.getSupClliNodeId() == null || adminStates == null || state == null) {
379             LOG.error("PceNode: one of parameters is not populated : nodeId, node type, supporting nodeId, "
380                     + "admin state, operational state");
381             valid = false;
382         }
383         return valid;
384     }
385
386     @Override
387     public List<PceLink> getOutgoingLinks() {
388         return outgoingLinks;
389     }
390
391     @Override
392     public AdminStates getAdminStates() {
393         return adminStates;
394     }
395
396     @Override
397     public State getState() {
398         return state;
399     }
400
401     @Override
402     public NodeId getNodeId() {
403         return nodeId;
404     }
405
406     @Override
407     public String toString() {
408         return "PceNode type=" + nodeType + " ID=" + nodeId.getValue() + " CLLI=" + this.getSupClliNodeId();
409     }
410
411     @Override
412     public String getPceNodeType() {
413         return "optical";
414     }
415
416     @Override
417     public String getSupNetworkNodeId() {
418         return MapUtils.getSupNetworkNode(this.node);
419     }
420
421     @Override
422     public String getSupClliNodeId() {
423         return MapUtils.getSupClliNode(this.node);
424     }
425
426     @Override
427     public void addOutgoingLink(PceLink outLink) {
428         this.outgoingLinks.add(outLink);
429     }
430
431     @Override
432     public String getXpdrClient(String tp) {
433         return this.clientPerNwTp.get(tp);
434     }
435
436     @Override
437     public Map<String, List<Uint16>> getAvailableTribPorts() {
438         return null;
439     }
440
441     @Override
442     public Map<String, List<Uint16>> getAvailableTribSlots() {
443         return null;
444     }
445
446     /*
447     * (non-Javadoc)
448     *
449     * @see org.opendaylight.transportpce.pce.networkanalyzer.PceNode#getBitSetData()
450     */
451     @Override
452     public BitSet getBitSetData() {
453         return this.frequenciesBitSet;
454     }
455
456     /*
457     * (non-Javadoc)
458     *
459     * @see org.opendaylight.transportpce.pce.networkanalyzer.PceNode#getVersion()
460     */
461     @Override
462     public String getVersion() {
463         return this.version;
464     }
465
466     /*
467     * (non-Javadoc)
468     *
469     * @see org.opendaylight.transportpce.pce.networkanalyzer.PceNode#getSlotWidthGranularity()
470     */
471     @Override
472     public BigDecimal getSlotWidthGranularity() {
473         return slotWidthGranularity;
474     }
475
476     /*
477      * (non-Javadoc)
478      *
479      * @see org.opendaylight.transportpce.pce.networkanalyzer.PceNode#getCentralFreqGranularity()
480      */
481     @Override
482     public BigDecimal getCentralFreqGranularity() {
483         return centralFreqGranularity;
484     }
485
486 }