Handle missing spanloss in PCE without crashing
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / PceLink.java
1 /*
2  * Copyright © 2017 AT&T, 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.io.Serializable;
12 import java.util.Collection;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Map;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkutils.rev220630.OtnLinkType;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev211210.Link1;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev211210.span.attributes.LinkConcatenation1;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev211210.span.attributes.LinkConcatenation1.FiberType;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev211210.networks.network.link.oms.attributes.Span;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.OpenroadmLinkType;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.link.concatenation.LinkConcatenation;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.link.concatenation.LinkConcatenationKey;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.TpId;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @SuppressWarnings("serial")
34 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
35     value = "SE_NO_SERIALVERSIONID",
36     justification = "https://github.com/rzwitserloot/lombok/wiki/WHY-NOT:-serialVersionUID")
37 public class PceLink implements Serializable {
38
39     /* Logging. */
40     private static final Logger LOG = LoggerFactory.getLogger(PceLink.class);
41     ///////////////////////// LINKS ////////////////////
42     /*
43      * extension of Link to include constraints and Graph weight
44      */
45     double weight = 0;
46     private boolean isValid = true;
47
48     // this member is for XPONDER INPUT/OUTPUT links.
49     // it keeps name of client corresponding to NETWORK TP
50     private String client = "";
51     private final LinkId linkId;
52     private final OpenroadmLinkType linkType;
53     private final NodeId sourceId;
54     private final NodeId destId;
55     private final TpId sourceTP;
56     private final TpId destTP;
57     private final String sourceNetworkSupNodeId;
58     private final String destNetworkSupNodeId;
59     private final String sourceCLLI;
60     private final String destCLLI;
61     private final LinkId oppositeLink;
62     private final AdminStates adminStates;
63     private final State state;
64     private final Long latency;
65     private final Long availableBandwidth;
66     private final Long usedBandwidth;
67     private final List<Long> srlgList;
68     private final double osnr;
69     private final transient Span omsAttributesSpan;
70     //meter per ms
71     private static final double CELERITY = 2.99792458 * 1e5;
72     private static final double NOISE_MASK_A = 0.571429;
73     private static final double NOISE_MASK_B = 39.285714;
74     private static final double UPPER_BOUND_OSNR = 33;
75     private static final double LOWER_BOUND_OSNR = 0.1;
76
77     public PceLink(Link link, PceNode source, PceNode dest) {
78         LOG.debug("PceLink: : PceLink start ");
79
80         this.linkId = link.getLinkId();
81
82         this.sourceId = link.getSource().getSourceNode();
83         this.destId = link.getDestination().getDestNode();
84
85         this.sourceTP = link.getSource().getSourceTp();
86         this.destTP = link.getDestination().getDestTp();
87
88         this.sourceNetworkSupNodeId = source.getSupNetworkNodeId();
89         this.destNetworkSupNodeId = dest.getSupNetworkNodeId();
90
91         this.sourceCLLI = source.getSupClliNodeId();
92         this.destCLLI = dest.getSupClliNodeId();
93
94         this.linkType = MapUtils.calcType(link);
95
96         this.oppositeLink = calcOpposite(link);
97
98         this.adminStates = link.augmentation(Link1.class).getAdministrativeState();
99         this.state = link.augmentation(Link1.class).getOperationalState();
100
101         if (this.linkType == OpenroadmLinkType.ROADMTOROADM) {
102             this.omsAttributesSpan = MapUtils.getOmsAttributesSpan(link);
103             this.srlgList = MapUtils.getSRLG(link);
104             this.latency = calcLatency(link);
105             this.osnr = calcSpanOSNR();
106             this.availableBandwidth = 0L;
107             this.usedBandwidth = 0L;
108         } else if (this.linkType == OpenroadmLinkType.OTNLINK) {
109             this.availableBandwidth = MapUtils.getAvailableBandwidth(link);
110             this.usedBandwidth = MapUtils.getUsedBandwidth(link);
111             this.srlgList = MapUtils.getSRLGfromLink(link);
112             this.osnr = 0.0;
113             this.latency = 0L;
114             this.omsAttributesSpan = null;
115         } else {
116             this.omsAttributesSpan = null;
117             this.srlgList = null;
118             this.latency = 0L;
119             //infinite OSNR in DB
120             this.osnr = 100L;
121             this.availableBandwidth = 0L;
122             this.usedBandwidth = 0L;
123         }
124         LOG.debug("PceLink: created PceLink  {}", linkId);
125     }
126
127     //Retrieve the opposite link
128     private LinkId calcOpposite(Link link) {
129         LinkId tmpoppositeLink = MapUtils.extractOppositeLink(link);
130         if (tmpoppositeLink == null) {
131             LOG.error("PceLink: Error calcOpposite. Link is ignored {}", link.getLinkId().getValue());
132             isValid = false;
133         }
134         return tmpoppositeLink;
135     }
136
137     //Compute the link latency : if the latency is not defined, the latency is computed from the omsAttributesSpan
138     private Long calcLatency(Link link) {
139         Link1 link1 = link.augmentation(Link1.class);
140         if (link1.getLinkLatency() != null) {
141             return link1.getLinkLatency().toJava();
142         }
143         if (this.omsAttributesSpan == null) {
144             return 1L;
145         }
146         double tmp = 0;
147         Map<LinkConcatenationKey, LinkConcatenation> linkConcatenationMap = this.omsAttributesSpan
148                 .nonnullLinkConcatenation();
149         for (Map.Entry<LinkConcatenationKey, LinkConcatenation> entry : linkConcatenationMap.entrySet()) {
150             // Length is expressed in meter and latency is expressed in ms according to OpenROADM MSA
151             if (entry == null || entry.getValue() == null || entry.getValue().getSRLGLength() == null) {
152                 LOG.debug("In PceLink: cannot compute the latency for the link {}", link.getLinkId().getValue());
153                 return 1L;
154             }
155             tmp += entry.getValue().getSRLGLength().doubleValue() / CELERITY;
156             LOG.debug("In PceLink: The latency of link {} == {}", link.getLinkId(), tmp);
157         }
158         return (long) Math.ceil(tmp);
159     }
160
161     //Compute the OSNR of a span
162     private double calcSpanOSNR() {
163         if (this.omsAttributesSpan == null) {
164             return 0L;
165         }
166         Collection<LinkConcatenation> linkConcatenationList =
167             this.omsAttributesSpan.nonnullLinkConcatenation().values();
168         if (linkConcatenationList == null) {
169             LOG.error("in PceLink : Null field in the OmsAttrubtesSpan");
170             return 0L;
171         }
172         Iterator<LinkConcatenation> linkConcatenationiterator = linkConcatenationList.iterator();
173         if (!linkConcatenationiterator.hasNext()) {
174             return 0L;
175         }
176         if (this.omsAttributesSpan.getSpanlossCurrent() == null) {
177             LOG.error("in PceLink : Spanloss is null");
178             return 0L;
179         }
180         // power on the output of the previous ROADM (dBm)
181         double pout = retrievePower(linkConcatenationiterator.next().augmentation(LinkConcatenation1.class)
182             .getFiberType());
183         // span loss (dB)
184         double spanLoss = this.omsAttributesSpan.getSpanlossCurrent().getValue().doubleValue();
185         // power on the input of the current ROADM (dBm)
186         double pin = pout - spanLoss;
187         double spanOsnrDb = NOISE_MASK_A * pin + NOISE_MASK_B;
188         if (spanOsnrDb > UPPER_BOUND_OSNR) {
189             spanOsnrDb = UPPER_BOUND_OSNR;
190         } else if (spanOsnrDb < LOWER_BOUND_OSNR) {
191             spanOsnrDb = LOWER_BOUND_OSNR;
192         }
193         return spanOsnrDb;
194     }
195
196     private double retrievePower(FiberType fiberType) {
197         double power;
198         switch (fiberType) {
199             case Smf:
200                 power = 2;
201                 break;
202             case Eleaf:
203                 power = 1;
204                 break;
205             case Truewavec:
206                 power = -1;
207                 break;
208             case Oleaf:
209             case Dsf:
210             case Truewave:
211             case NzDsf:
212             case Ull:
213             default:
214                 power = 0;
215                 break;
216         }
217         return power;
218     }
219
220     public LinkId getOppositeLink() {
221         return oppositeLink;
222     }
223
224     public AdminStates getAdminStates() {
225         return adminStates;
226     }
227
228     public State getState() {
229         return state;
230     }
231
232     public TpId getSourceTP() {
233         return sourceTP;
234     }
235
236     public TpId getDestTP() {
237         return destTP;
238     }
239
240     public OpenroadmLinkType getlinkType() {
241         return linkType;
242     }
243
244     public LinkId getLinkId() {
245         return linkId;
246     }
247
248     public NodeId getSourceId() {
249         return sourceId;
250     }
251
252     public NodeId getDestId() {
253         return destId;
254     }
255
256     public String getClient() {
257         return client;
258     }
259
260     public void setClient(String client) {
261         this.client = client;
262     }
263
264     // Double for transformer of JUNG graph
265     public Double getLatency() {
266         return latency.doubleValue();
267     }
268
269     public Long getAvailableBandwidth() {
270         return availableBandwidth;
271     }
272
273     public Long getUsedBandwidth() {
274         return usedBandwidth;
275     }
276
277     public String getsourceNetworkSupNodeId() {
278         return sourceNetworkSupNodeId;
279     }
280
281     public String getdestNetworkSupNodeId() {
282         return destNetworkSupNodeId;
283     }
284
285     public List<Long> getsrlgList() {
286         return srlgList;
287     }
288
289     public double getosnr() {
290         return osnr;
291     }
292
293     public String getsourceCLLI() {
294         return sourceCLLI;
295     }
296
297     public String getdestCLLI() {
298         return destCLLI;
299     }
300
301     public boolean isValid() {
302         if ((this.linkId == null) || (this.linkType == null) || (this.oppositeLink == null)) {
303             isValid = false;
304             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
305         }
306         isValid = checkParams();
307         if (this.linkType == OpenroadmLinkType.ROADMTOROADM) {
308             if (this.omsAttributesSpan == null) {
309                 isValid = false;
310                 LOG.error("PceLink: Error reading Span for OMS link. Link is ignored {}", linkId);
311             } else if (this.omsAttributesSpan.getSpanlossCurrent() == null) {
312                 isValid = false;
313                 LOG.error("PceLink: Error reading Spanloss for OMS link. Link is ignored {}", linkId);
314             }
315         }
316         if ((this.srlgList != null) && (this.srlgList.isEmpty())) {
317             isValid = false;
318             LOG.error("PceLink: Empty srlgList for OMS link. Link is ignored {}", linkId);
319         }
320         return isValid;
321     }
322
323     public boolean isOtnValid(Link link, String serviceType) {
324
325         if (this.linkType != OpenroadmLinkType.OTNLINK) {
326             LOG.error("PceLink: Not an OTN link. Link is ignored {}", linkId);
327             return false;
328         }
329
330         OtnLinkType otnLinkType = link
331             .augmentation(org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkutils
332                     .rev220630.Link1.class)
333             .getOtnLinkType();
334         if (this.availableBandwidth == 0L) {
335             LOG.error("PceLink: No bandwidth available for OTN Link, link {}  is ignored ", linkId);
336             return false;
337         }
338
339         long neededBW;
340         OtnLinkType neededType = null;
341         switch (serviceType) {
342             case "ODUC2":
343                 if (this.usedBandwidth != 0L) {
344                     return false;
345                 }
346                 neededBW = 200000L;
347                 // Add intermediate rate otn-link-type
348                 neededType = OtnLinkType.OTUC2;
349                 break;
350             case "ODUC3":
351                 if (this.usedBandwidth != 0L) {
352                     return false;
353                 }
354                 neededBW = 300000L;
355                 // hange otn-link-type
356                 neededType = OtnLinkType.OTUC3;
357                 break;
358             case "ODUC4":
359                 if (this.usedBandwidth != 0L) {
360                     return false;
361                 }
362                 neededBW = 400000L;
363                 neededType = OtnLinkType.OTUC4;
364                 break;
365             case "ODU4":
366             case "100GEs":
367                 if (this.usedBandwidth != 0L) {
368                     return false;
369                 }
370                 neededBW = 100000L;
371                 neededType = OtnLinkType.OTU4;
372                 break;
373             case "ODU2":
374             case "ODU2e":
375                 neededBW = 12500L;
376                 break;
377             case "ODU0":
378                 neededBW = 1250L;
379                 break;
380             case "ODU1":
381                 neededBW = 2500L;
382                 break;
383             case "100GEm":
384                 neededBW = 100000L;
385                 // TODO: Here link type needs to be changed, based on the line-rate
386                 neededType = OtnLinkType.ODUC4;
387                 break;
388             case "10GE":
389                 neededBW = 10000L;
390                 neededType = OtnLinkType.ODTU4;
391                 break;
392             case "1GE":
393                 neededBW = 1000L;
394                 neededType = OtnLinkType.ODTU4;
395                 break;
396             default:
397                 LOG.error("PceLink: isOtnValid Link {} unsupported serviceType {} ", linkId, serviceType);
398                 return false;
399         }
400
401         if ((this.availableBandwidth >= neededBW)
402             && ((neededType == null) || (neededType.equals(otnLinkType)))) {
403             LOG.debug("PceLink: Selected Link {} has available bandwidth and is eligible for {} creation ",
404                 linkId, serviceType);
405         }
406
407         return checkParams();
408     }
409
410     private boolean checkParams() {
411         if ((this.linkId == null) || (this.linkType == null) || (this.oppositeLink == null)) {
412             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
413             return false;
414         }
415         if ((this.adminStates == null) || (this.state == null)) {
416             LOG.error("PceLink: Link is not available. Link is ignored {}", linkId);
417             return false;
418         }
419         if ((this.sourceId == null) || (this.destId == null) || (this.sourceTP == null) || (this.destTP == null)) {
420             LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", linkId);
421             return false;
422         }
423         if ((this.sourceNetworkSupNodeId.equals("")) || (this.destNetworkSupNodeId.equals(""))) {
424             LOG.error("PceLink: No Link source SuppNodeID or destination SuppNodeID is available. Link is ignored {}",
425                 linkId);
426             return false;
427         }
428         if ((this.sourceCLLI.equals("")) || (this.destCLLI.equals(""))) {
429             LOG.error("PceLink: No Link source CLLI or destination CLLI is available. Link is ignored {}", linkId);
430             return false;
431         }
432
433         return true;
434     }
435
436     @Override
437     public String toString() {
438         return "PceLink type=" + linkType + " ID=" + linkId.getValue() + " latency=" + latency;
439     }
440 }