Merge "Technical debt - Fix PCE sonar issues"
[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.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Optional;
16 import java.util.TreeMap;
17 import org.opendaylight.transportpce.pce.SortPortsByName;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.TerminationPoint1;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.Node1;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.networks.network.node.termination.point.pp.attributes.UsedWavelength;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmNodeType;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmTpType;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.format.rev190531.ServiceFormat;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
26 import org.opendaylight.yangtools.yang.common.Uint16;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class PceOpticalNode implements PceNode {
31     private static final Logger LOG = LoggerFactory.getLogger(PceOpticalNode.class);
32
33     private boolean valid = true;
34
35     private final Node node;
36     private final NodeId nodeId;
37     private final OpenroadmNodeType nodeType;
38     private final ServiceFormat serviceFormat;
39     private final String pceNodeType;
40
41     // wavelength calculation per node type
42     private List<Long> availableWLindex = new ArrayList<>();
43     private Map<String, OpenroadmTpType> availableSrgPp = new TreeMap<>();
44     private Map<String, OpenroadmTpType> availableSrgCp = new TreeMap<>();
45     private List<String> usedXpndrNWTps = new ArrayList<>();
46     private List<PceLink> outgoingLinks = new ArrayList<>();
47     private Map<String, String> clientPerNwTp = new HashMap<>();
48
49     public PceOpticalNode(Node node, OpenroadmNodeType nodeType, NodeId nodeId, ServiceFormat serviceFormat,
50         String pceNodeType) {
51         this.node = node;
52         this.nodeId = nodeId;
53         this.nodeType = nodeType;
54         this.serviceFormat = serviceFormat;
55         this.pceNodeType = pceNodeType;
56
57         if ((node == null) || (nodeId == null) || (nodeType == null)) {
58             LOG.error("PceNode: one of parameters is not populated : nodeId, node type");
59             this.valid = false;
60         }
61     }
62
63     public void initSrgTps() {
64         this.availableSrgPp.clear();
65         this.availableSrgCp.clear();
66         if (!isValid()) {
67             return;
68         }
69         LOG.info("initSrgTpList: getting SRG tps from ROADM node {}", this.nodeId);
70         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
71                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
72                     .ietf.network.topology.rev180226.Node1.class);
73         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
74             .node.TerminationPoint> allTps = new ArrayList<>(nodeTp.nonnullTerminationPoint().values());
75         if (allTps.isEmpty()) {
76             LOG.error("initSrgTpList: ROADM TerminationPoint list is empty for node {}", this);
77             this.valid = false;
78             return;
79         }
80         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
81             .node.TerminationPoint tp : allTps) {
82             TerminationPoint1 cntp1 = tp.augmentation(TerminationPoint1.class);
83             org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.TerminationPoint1 nttp1 = tp
84                 .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130
85                 .TerminationPoint1.class);
86             OpenroadmTpType type = cntp1.getTpType();
87             LOG.info("type = {} for tp {}", type.getName(), tp);
88
89             switch (type) {
90                 case SRGTXRXCP:
91                 case SRGRXCP:
92                 case SRGTXCP:
93                     LOG.info("initSrgTpList: adding SRG-CP tp = {} ", tp.getTpId().getValue());
94                     this.availableSrgCp.put(tp.getTpId().getValue(), cntp1.getTpType());
95                     break;
96                 case SRGRXPP:
97                 case SRGTXPP:
98                 case SRGTXRXPP:
99                     boolean used = true;
100                     LOG.info("initSrgTpList: SRG-PP tp = {} found", tp.getTpId().getValue());
101                     try {
102                         List<UsedWavelength> usedWavelengths =
103                             new ArrayList<>(nttp1.getPpAttributes().getUsedWavelength().values());
104                         if (usedWavelengths.isEmpty()) {
105                             used = false;
106                         }
107                     } catch (NullPointerException e) {
108                         LOG.warn("initSrgTpList: 'usedWavelengths' for tp={} is null !", tp.getTpId().getValue());
109                         used = false;
110                     }
111                     if (!used) {
112                         LOG.info("initSrgTpList: adding SRG-PP tp '{}'", tp.getTpId().getValue());
113                         this.availableSrgPp.put(tp.getTpId().getValue(), cntp1.getTpType());
114                     } else {
115                         LOG.warn("initSrgTpList: SRG-PP tp = {} found is busy !!", tp.getTpId().getValue());
116                     }
117                     break;
118                 default:
119                     break;
120             }
121         }
122         if (this.availableSrgPp.isEmpty() || this.availableSrgCp.isEmpty()) {
123             LOG.error("initSrgTpList: ROADM SRG TerminationPoint list is empty for node {}", this);
124             this.valid = false;
125             return;
126         }
127         LOG.info("initSrgTpList: availableSrgPp size = {} && availableSrgCp size = {} in {}",
128             this.availableSrgPp.size(), this.availableSrgCp.size(), this);
129     }
130
131     public void initWLlist() {
132         this.availableWLindex.clear();
133         if (!isValid()) {
134             return;
135         }
136         Node1 node1 = this.node.augmentation(Node1.class);
137         switch (this.nodeType) {
138             case SRG :
139                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev181130.srg.node.attributes
140                     .AvailableWavelengths> srgAvailableWL =
141                         new ArrayList<>(node1.getSrgAttributes().nonnullAvailableWavelengths().values());
142                 if (srgAvailableWL.isEmpty()) {
143                     this.valid = false;
144                     LOG.error("initWLlist: SRG AvailableWavelengths is empty for node  {}", this);
145                     return;
146                 }
147                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev181130.srg.node.attributes
148                         .AvailableWavelengths awl : srgAvailableWL) {
149                     this.availableWLindex.add(awl.getIndex().toJava());
150                     LOG.debug("initWLlist: SRG next = {} in {}", awl.getIndex(), this);
151                 }
152                 break;
153             case DEGREE :
154                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev181130.degree.node.attributes
155                         .AvailableWavelengths> degAvailableWL =
156                     new ArrayList<>(node1.getDegreeAttributes().nonnullAvailableWavelengths().values());
157                 if (degAvailableWL.isEmpty()) {
158                     this.valid = false;
159                     LOG.error("initWLlist: DEG AvailableWavelengths is empty for node  {}", this);
160                     return;
161                 }
162                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev181130.degree.node.attributes
163                             .AvailableWavelengths awl : degAvailableWL) {
164                     this.availableWLindex.add(awl.getIndex().toJava());
165                     LOG.debug("initWLlist: DEGREE next = {} in {}", awl.getIndex(), this);
166                 }
167                 break;
168             case XPONDER :
169                 // HARD CODED 96
170                 for (long i = 1; i <= 96; i++) {
171                     this.availableWLindex.add(i);
172                 }
173                 break;
174             default:
175                 LOG.error("initWLlist: unsupported node type {} in node {}", this.nodeType, this);
176                 break;
177         }
178         if (this.availableWLindex.isEmpty()) {
179             LOG.debug("initWLlist: There are no available wavelengths in node {}", this);
180             this.valid = false;
181         }
182         LOG.debug("initWLlist: availableWLindex size = {} in {}", this.availableWLindex.size(), this);
183     }
184
185     public void initXndrTps() {
186         LOG.info("PceNod: initXndrTps for node : {}", this.nodeId);
187         if (!isValid()) {
188             return;
189         }
190         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
191                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
192                     .ietf.network.topology.rev180226.Node1.class);
193         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
194             .node.TerminationPoint> allTps = new ArrayList<>(nodeTp.nonnullTerminationPoint().values());
195         if (allTps.isEmpty()) {
196             this.valid = false;
197             LOG.error("initXndrTps: XPONDER TerminationPoint list is empty for node {}", this);
198             return;
199         }
200         this.valid = false;
201         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
202             .node.TerminationPoint tp : allTps) {
203             TerminationPoint1 cntp1 = tp.augmentation(TerminationPoint1.class);
204             org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.TerminationPoint1 nttp1 = tp
205                 .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130
206                 .TerminationPoint1.class);
207             if (cntp1.getTpType() == OpenroadmTpType.XPONDERNETWORK) {
208                 if (nttp1 != null && nttp1.getXpdrNetworkAttributes().getWavelength() != null) {
209                     this.usedXpndrNWTps.add(tp.getTpId().getValue());
210                     LOG.info("initXndrTps: XPONDER tp = {} is used", tp.getTpId().getValue());
211                 } else {
212                     this.valid = true;
213                 }
214                 // find Client of this network TP
215                 String client;
216                 org.opendaylight.yang.gen.v1.http.transportpce.topology.rev200129.TerminationPoint1 tpceTp1 =
217                     tp.augmentation(org.opendaylight.yang.gen.v1.http.transportpce.topology.rev200129
218                         .TerminationPoint1.class);
219                 if (tpceTp1 != null) {
220                     client = tpceTp1.getAssociatedConnectionMapPort();
221                     if (client != null) {
222                         this.clientPerNwTp.put(tp.getTpId().getValue(), client);
223                         this.valid = true;
224                     } else {
225                         LOG.error("initXndrTps: XPONDER {} NW TP doesn't have defined Client {}",
226                             this, tp.getTpId().getValue());
227                     }
228                 } else if (ServiceFormat.OTU.equals(this.serviceFormat)) {
229                     LOG.info("Infrastructure OTU4 connection");
230                     this.valid = true;
231                 } else {
232                     LOG.error("Service Format {} not managed yet", this.serviceFormat.getName());
233                 }
234             }
235         }
236         if (!isValid()) {
237             LOG.error("initXndrTps: XPONDER doesn't have available wavelengths for node  {}", this);
238         }
239     }
240
241     @Override
242     public String getRdmSrgClient(String tp) {
243         LOG.info("getRdmSrgClient: Getting PP client for tp '{}' on node : {}", tp, this.nodeId);
244         OpenroadmTpType srgType = null;
245         OpenroadmTpType cpType = this.availableSrgCp.get(tp);
246         if (cpType == null) {
247             LOG.error("getRdmSrgClient: tp {} not existed in SRG CPterminationPoint list", tp);
248             return null;
249         }
250         switch (cpType) {
251             case SRGTXRXCP:
252                 LOG.info("getRdmSrgClient: Getting BI Directional PP port ...");
253                 srgType = OpenroadmTpType.SRGTXRXPP;
254                 break;
255             case SRGTXCP:
256                 LOG.info("getRdmSrgClient: Getting UNI Rx PP port ...");
257                 srgType = OpenroadmTpType.SRGRXPP;
258                 break;
259             case SRGRXCP:
260                 LOG.info("getRdmSrgClient: Getting UNI Tx PP port ...");
261                 srgType = OpenroadmTpType.SRGTXPP;
262                 break;
263             default:
264                 break;
265         }
266         LOG.info("getRdmSrgClient:  Getting client PP for CP '{}'", tp);
267         if (!this.availableSrgPp.isEmpty()) {
268             Optional<String> client = null;
269             final OpenroadmTpType openType = srgType;
270             client = this.availableSrgPp.entrySet()
271                     .stream().filter(pp -> pp.getValue().getName().equals(openType.getName()))
272                     .map(Map.Entry::getKey)
273                     .sorted(new SortPortsByName())
274                     .findFirst();
275             if (!client.isPresent()) {
276                 LOG.error("getRdmSrgClient: ROADM {} doesn't have PP Client for CP {}", this, tp);
277                 return null;
278             }
279             LOG.info("getRdmSrgClient: client PP {} for CP {} found !", client, tp);
280             return client.get();
281         } else {
282             LOG.error("getRdmSrgClient: SRG TerminationPoint PP list is not available for node {}", this);
283             return null;
284         }
285     }
286
287
288     public void validateAZxponder(String anodeId, String znodeId) {
289         if (!isValid()) {
290             return;
291         }
292         if (this.nodeType != OpenroadmNodeType.XPONDER) {
293             return;
294         }
295         // Detect A and Z
296         if (this.getSupNetworkNodeId().equals(anodeId) || (this.getSupNetworkNodeId().equals(znodeId))) {
297             LOG.info("validateAZxponder: A or Z node detected == {}", nodeId.getValue());
298             initXndrTps();
299             return;
300         }
301         LOG.debug("validateAZxponder: XPONDER is ignored == {}", nodeId.getValue());
302         valid = false;
303     }
304
305     @Override
306     public boolean checkTP(String tp) {
307         return !this.usedXpndrNWTps.contains(tp);
308     }
309
310     @Override
311     public boolean checkWL(long index) {
312         return (this.availableWLindex.contains(index));
313     }
314
315     public boolean isValid() {
316         if (node == null || nodeId == null || nodeType == null || this.getSupNetworkNodeId() == null
317             || this.getSupClliNodeId() == null) {
318             LOG.error("PceNode: one of parameters is not populated : nodeId, node type, supporting nodeId");
319             valid = false;
320         }
321         return valid;
322     }
323
324     @Override
325     public List<PceLink> getOutgoingLinks() {
326         return outgoingLinks;
327     }
328
329     @Override
330     public NodeId getNodeId() {
331         return nodeId;
332     }
333
334     @Override
335     public String toString() {
336         return "PceNode type=" + nodeType + " ID=" + nodeId.getValue() + " CLLI=" + this.getSupClliNodeId();
337     }
338
339     @Override
340     public String getPceNodeType() {
341         return this.pceNodeType;
342     }
343
344     @Override
345     public String getSupNetworkNodeId() {
346         return MapUtils.getSupNetworkNode(this.node);
347     }
348
349     @Override
350     public String getSupClliNodeId() {
351         return MapUtils.getSupClliNode(this.node);
352     }
353
354     @Override
355     public void addOutgoingLink(PceLink outLink) {
356         this.outgoingLinks.add(outLink);
357     }
358
359     @Override
360     public String getXpdrClient(String tp) {
361         return this.clientPerNwTp.get(tp);
362     }
363
364     @Override
365     public Map<String, List<Uint16>> getAvailableTribPorts() {
366         return null;
367     }
368
369     @Override
370     public Map<String, List<Uint16>> getAvailableTribSlots() {
371         return null;
372     }
373 }