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