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