Make PCE optical node validation fail when no PPs are available
[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 = nodeTp.getTerminationPoint();
75         if (allTps == null) {
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 = nttp1.getPpAttributes().getUsedWavelength();
103                         if (usedWavelengths.isEmpty()) {
104                             used = false;
105                         }
106                     } catch (NullPointerException e) {
107                         LOG.warn("initSrgTpList: 'usedWavelengths' for tp={} is null !", tp.getTpId().getValue());
108                         used = false;
109                     }
110                     if (!used) {
111                         LOG.info("initSrgTpList: adding SRG-PP tp '{}'", tp.getTpId().getValue());
112                         this.availableSrgPp.put(tp.getTpId().getValue(), cntp1.getTpType());
113                     } else {
114                         LOG.warn("initSrgTpList: SRG-PP tp = {} found is busy !!", tp.getTpId().getValue());
115                     }
116                     break;
117                 default:
118                     break;
119             }
120         }
121         if (this.availableSrgPp.isEmpty() || this.availableSrgCp.isEmpty()) {
122             LOG.error("initSrgTpList: ROADM SRG TerminationPoint list is empty for node {}", this);
123             this.valid = false;
124             return;
125         }
126         LOG.info("initSrgTpList: availableSrgPp size = {} && availableSrgCp size = {} in {}",
127             this.availableSrgPp.size(), this.availableSrgCp.size(), this);
128     }
129
130     public void initWLlist() {
131         this.availableWLindex.clear();
132         if (!isValid()) {
133             return;
134         }
135         Node1 node1 = this.node.augmentation(Node1.class);
136         switch (this.nodeType) {
137             case SRG :
138                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev181130.srg.node.attributes
139                     .AvailableWavelengths> srgAvailableWL =
140                         node1.getSrgAttributes().getAvailableWavelengths();
141                 if (srgAvailableWL == null) {
142                     this.valid = false;
143                     LOG.error("initWLlist: SRG AvailableWavelengths is empty for node  {}", this);
144                     return;
145                 }
146                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.srg.rev181130.srg.node.attributes
147                         .AvailableWavelengths awl : srgAvailableWL) {
148                     this.availableWLindex.add(awl.getIndex().toJava());
149                     LOG.debug("initWLlist: SRG next = {} in {}", awl.getIndex(), this);
150                 }
151                 break;
152             case DEGREE :
153                 List<org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev181130.degree.node.attributes
154                     .AvailableWavelengths> degAvailableWL = node1.getDegreeAttributes().getAvailableWavelengths();
155                 if (degAvailableWL == null) {
156                     this.valid = false;
157                     LOG.error("initWLlist: DEG AvailableWavelengths is empty for node  {}", this);
158                     return;
159                 }
160                 for (org.opendaylight.yang.gen.v1.http.org.openroadm.degree.rev181130.degree.node.attributes
161                             .AvailableWavelengths awl : degAvailableWL) {
162                     this.availableWLindex.add(awl.getIndex().toJava());
163                     LOG.debug("initWLlist: DEGREE next = {} in {}", awl.getIndex(), this);
164                 }
165                 break;
166             case XPONDER :
167                 // HARD CODED 96
168                 for (long i = 1; i <= 96; i++) {
169                     this.availableWLindex.add(i);
170                 }
171                 break;
172             default:
173                 LOG.error("initWLlist: unsupported node type {} in node {}", this.nodeType, this);
174                 break;
175         }
176         if (this.availableWLindex.isEmpty()) {
177             LOG.debug("initWLlist: There are no available wavelengths in node {}", this);
178             this.valid = false;
179         }
180         LOG.debug("initWLlist: availableWLindex size = {} in {}", this.availableWLindex.size(), this);
181     }
182
183     public void initXndrTps() {
184         LOG.info("PceNod: initXndrTps for node : {}", this.nodeId);
185         if (!isValid()) {
186             return;
187         }
188         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp =
189                 this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
190                     .ietf.network.topology.rev180226.Node1.class);
191         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
192             .node.TerminationPoint> allTps = nodeTp.getTerminationPoint();
193         if (allTps == null) {
194             this.valid = false;
195             LOG.error("initXndrTps: XPONDER TerminationPoint list is empty for node {}", this);
196             return;
197         }
198         this.valid = false;
199         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
200             .node.TerminationPoint tp : allTps) {
201             TerminationPoint1 cntp1 = tp.augmentation(TerminationPoint1.class);
202             org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.TerminationPoint1 nttp1 = tp
203                 .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130
204                 .TerminationPoint1.class);
205             if (cntp1.getTpType() == OpenroadmTpType.XPONDERNETWORK) {
206                 if (nttp1 != null && nttp1.getXpdrNetworkAttributes().getWavelength() != null) {
207                     this.usedXpndrNWTps.add(tp.getTpId().getValue());
208                     LOG.info("initXndrTps: XPONDER tp = {} is used", tp.getTpId().getValue());
209                 } else {
210                     this.valid = true;
211                 }
212                 // find Client of this network TP
213                 String client;
214                 org.opendaylight.yang.gen.v1.http.transportpce.topology.rev200129.TerminationPoint1 tpceTp1 =
215                     tp.augmentation(org.opendaylight.yang.gen.v1.http.transportpce.topology.rev200129
216                         .TerminationPoint1.class);
217                 if (tpceTp1 != null) {
218                     client = tpceTp1.getAssociatedConnectionMapPort();
219                     if (client != null) {
220                         this.clientPerNwTp.put(tp.getTpId().getValue(), client);
221                         this.valid = true;
222                     } else {
223                         LOG.error("initXndrTps: XPONDER {} NW TP doesn't have defined Client {}",
224                             this, tp.getTpId().getValue());
225                     }
226                 } else if (ServiceFormat.OTU.equals(this.serviceFormat)) {
227                     LOG.info("Infrastructure OTU4 connection");
228                     this.valid = true;
229                 } else {
230                     LOG.error("Service Format {} not managed yet", this.serviceFormat.getName());
231                 }
232             }
233         }
234         if (!isValid()) {
235             LOG.error("initXndrTps: XPONDER doesn't have available wavelengths for node  {}", this);
236         }
237     }
238
239     @Override
240     public String getRdmSrgClient(String tp) {
241         LOG.info("getRdmSrgClient: Getting PP client for tp '{}' on node : {}", tp, this.nodeId);
242         OpenroadmTpType srgType = null;
243         OpenroadmTpType cpType = this.availableSrgCp.get(tp);
244         if (cpType == null) {
245             LOG.error("getRdmSrgClient: tp {} not existed in SRG CPterminationPoint list", tp);
246             return null;
247         }
248         switch (cpType) {
249             case SRGTXRXCP:
250                 LOG.info("getRdmSrgClient: Getting BI Directional PP port ...");
251                 srgType = OpenroadmTpType.SRGTXRXPP;
252                 break;
253             case SRGTXCP:
254                 LOG.info("getRdmSrgClient: Getting UNI Rx PP port ...");
255                 srgType = OpenroadmTpType.SRGRXPP;
256                 break;
257             case SRGRXCP:
258                 LOG.info("getRdmSrgClient: Getting UNI Tx PP port ...");
259                 srgType = OpenroadmTpType.SRGTXPP;
260                 break;
261             default:
262                 break;
263         }
264         LOG.info("getRdmSrgClient:  Getting client PP for CP '{}'", tp);
265         if (!this.availableSrgPp.isEmpty()) {
266             Optional<String> client = null;
267             final OpenroadmTpType openType = srgType;
268             client = this.availableSrgPp.entrySet()
269                     .stream().filter(pp -> pp.getValue().getName().equals(openType.getName()))
270                     .map(Map.Entry::getKey)
271                     .sorted(new SortPortsByName())
272                     .findFirst();
273             if (!client.isPresent()) {
274                 LOG.error("getRdmSrgClient: ROADM {} doesn't have PP Client for CP {}", this, tp);
275                 return null;
276             }
277             LOG.info("getRdmSrgClient: client PP {} for CP {} found !", client, tp);
278             return client.get();
279         } else {
280             LOG.error("getRdmSrgClient: SRG TerminationPoint PP list is not available for node {}", this);
281             return null;
282         }
283     }
284
285
286     public void validateAZxponder(String anodeId, String znodeId) {
287         if (!isValid()) {
288             return;
289         }
290         if (this.nodeType != OpenroadmNodeType.XPONDER) {
291             return;
292         }
293         // Detect A and Z
294         if (this.getSupNetworkNodeId().equals(anodeId) || (this.getSupNetworkNodeId().equals(znodeId))) {
295             LOG.info("validateAZxponder: A or Z node detected == {}", nodeId.getValue());
296             initXndrTps();
297             return;
298         }
299         LOG.debug("validateAZxponder: XPONDER is ignored == {}", nodeId.getValue());
300         valid = false;
301     }
302
303     @Override
304     public boolean checkTP(String tp) {
305         return !this.usedXpndrNWTps.contains(tp);
306     }
307
308     @Override
309     public boolean checkWL(long index) {
310         return (this.availableWLindex.contains(index));
311     }
312
313     public boolean isValid() {
314         if (node == null || nodeId == null || nodeType == null || this.getSupNetworkNodeId() == null
315             || this.getSupClliNodeId() == null) {
316             LOG.error("PceNode: one of parameters is not populated : nodeId, node type, supporting nodeId");
317             valid = false;
318         }
319         return valid;
320     }
321
322     @Override
323     public List<PceLink> getOutgoingLinks() {
324         return outgoingLinks;
325     }
326
327     @Override
328     public NodeId getNodeId() {
329         return nodeId;
330     }
331
332     @Override
333     public String toString() {
334         return "PceNode type=" + nodeType + " ID=" + nodeId.getValue() + " CLLI=" + this.getSupClliNodeId();
335     }
336
337     @Override
338     public String getPceNodeType() {
339         return this.pceNodeType;
340     }
341
342     @Override
343     public String getSupNetworkNodeId() {
344         return MapUtils.getSupNetworkNode(this.node);
345     }
346
347     @Override
348     public String getSupClliNodeId() {
349         return MapUtils.getSupClliNode(this.node);
350     }
351
352     @Override
353     public void addOutgoingLink(PceLink outLink) {
354         this.outgoingLinks.add(outLink);
355     }
356
357     @Override
358     public String getXpdrClient(String tp) {
359         return this.clientPerNwTp.get(tp);
360     }
361
362     @Override
363     public Map<String, List<Uint16>> getAvailableTribPorts() {
364         return null;
365     }
366
367     @Override
368     public Map<String, List<Uint16>> getAvailableTribSlots() {
369         return null;
370     }
371 }