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