Fix issues related to Magnesium bump
[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.util.List;
12
13 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.Link1;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.span.attributes.LinkConcatenation.FiberType;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.networks.network.link.oms.attributes.Span;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmLinkType;
17 import org.opendaylight.yang.gen.v1.http.transportpce.topology.rev200129.OtnLinkType;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class PceLink {
25
26     /* Logging. */
27     private static final Logger LOG = LoggerFactory.getLogger(PceLink.class);
28     ///////////////////////// LINKS ////////////////////
29     /*
30      * extension of Link to include constraints and Graph weight
31      */
32     double weight = 0;
33     private boolean isValid = true;
34     private boolean isOtnValid = true;
35
36     // this member is for XPONDER INPUT/OUTPUT links.
37     // it keeps name of client corresponding to NETWORK TP
38     private String client = "";
39     private final LinkId linkId;
40     private final OpenroadmLinkType linkType;
41     private final NodeId sourceId;
42     private final NodeId destId;
43     private final Object sourceTP;
44     private final Object destTP;
45     private final String sourceNetworkSupNodeId;
46     private final String destNetworkSupNodeId;
47     private final String sourceCLLI;
48     private final String destCLLI;
49     private final LinkId oppositeLink;
50     private final Long latency;
51     private final Long availableBandwidth;
52     private final Long usedBandwidth;
53     private final List<Long> srlgList;
54     private final double osnr;
55     private final Span omsAttributesSpan;
56     private static final double CELERITY = 2.99792458 * 1e5; //meter per ms
57     private static final double NOISE_MASK_A = 0.571429;
58     private static final double NOISE_MASK_B = 39.285714;
59     private static final double UPPER_BOUND_OSNR = 33;
60     private static final double LOWER_BOUND_OSNR = 0.1;
61
62     public PceLink(Link link, PceNode source, PceNode dest) {
63         LOG.info("PceLink: : PceLink start ");
64
65         this.linkId = link.getLinkId();
66
67         this.sourceId = link.getSource().getSourceNode();
68         this.destId = link.getDestination().getDestNode();
69
70         this.sourceTP = link.getSource().getSourceTp();
71         this.destTP = link.getDestination().getDestTp();
72
73         this.sourceNetworkSupNodeId = source.getSupNetworkNodeId();
74         this.destNetworkSupNodeId = dest.getSupNetworkNodeId();
75
76         this.sourceCLLI = source.getSupClliNodeId();
77         this.destCLLI = dest.getSupClliNodeId();
78
79         this.linkType = MapUtils.calcType(link);
80
81         this.oppositeLink = calcOpposite(link);
82
83         if (this.linkType == OpenroadmLinkType.ROADMTOROADM) {
84             this.omsAttributesSpan = MapUtils.getOmsAttributesSpan(link);
85             this.srlgList = MapUtils.getSRLG(link);
86             this.latency = calcLatency(link);
87             this.osnr = calcSpanOSNR();
88             this.availableBandwidth = 0L;
89             this.usedBandwidth = 0L;
90         } else if (this.linkType == OpenroadmLinkType.OTNLINK) {
91             this.availableBandwidth = MapUtils.getAvailableBandwidth(link);
92             this.usedBandwidth = MapUtils.getUsedBandwidth(link);
93             this.srlgList = MapUtils.getSRLGfromLink(link);
94             this.osnr = 0.0;
95             this.latency = 0L;
96             this.omsAttributesSpan = null;
97         } else {
98             this.omsAttributesSpan = null;
99             this.srlgList = null;
100             this.latency = 0L;
101             this.osnr = 100L; //infinite OSNR in DB
102             this.availableBandwidth = 0L;
103             this.usedBandwidth = 0L;
104         }
105         LOG.debug("PceLink: created PceLink  {}", toString());
106     }
107
108     //Retrieve the opposite link
109     private LinkId calcOpposite(Link link) {
110         LinkId tmpoppositeLink = MapUtils.extractOppositeLink(link);
111         if (tmpoppositeLink == null) {
112             LOG.error("PceLink: Error calcOpposite. Link is ignored {}", link.getLinkId().getValue());
113             isValid = false;
114         }
115         return tmpoppositeLink;
116     }
117
118     //Compute the link latency : if the latency is not defined, the latency is computed from the omsAttributesSpan
119     private Long calcLatency(Link link) {
120         Link1 link1 = null;
121         Long tmplatency;
122         link1 = link.augmentation(Link1.class);
123         if (link1.getLinkLatency() != null) {
124             tmplatency = link1.getLinkLatency().toJava();
125             return tmplatency;
126         }
127
128         try {
129             double tmp = 0;
130             for (int i = 0; i < this.omsAttributesSpan.getLinkConcatenation().size(); i++) {
131                 //Length is expressed in meter and latency is expressed in ms according to OpenROADM MSA
132                 tmp += this.omsAttributesSpan.getLinkConcatenation().get(i).getSRLGLength().toJava() / CELERITY;
133                 LOG.info("In PceLink: The latency of link {} == {}",link.getLinkId(),tmp);
134             }
135             tmplatency = (long) Math.ceil(tmp);
136         } catch (NullPointerException e) {
137             LOG.debug("In PceLink: cannot compute the latency for the link {}",link.getLinkId().getValue());
138             tmplatency = 1L;
139         }
140         return tmplatency;
141     }
142
143     //Compute the OSNR of a span
144     public double calcSpanOSNR() {
145         try {
146             double pout; //power on the output of the previous ROADM (dBm)
147             pout = retrievePower(this.omsAttributesSpan.getLinkConcatenation().get(0).getFiberType());
148             double spanLoss = this.omsAttributesSpan.getSpanlossCurrent().getValue().doubleValue(); // span loss (dB)
149             double pin = pout - spanLoss; //power on the input of the current ROADM (dBm)
150             double spanOsnrDb;
151             spanOsnrDb = NOISE_MASK_A * pin + NOISE_MASK_B;
152             if (spanOsnrDb > UPPER_BOUND_OSNR) {
153                 spanOsnrDb =  UPPER_BOUND_OSNR;
154             } else if (spanOsnrDb < LOWER_BOUND_OSNR) {
155                 spanOsnrDb = LOWER_BOUND_OSNR;
156             }
157             return spanOsnrDb;
158         } catch (NullPointerException e) {
159             LOG.error("in PceLink : Null field in the OmsAttrubtesSpan");
160             return 0L;
161         }
162     }
163
164     private double retrievePower(FiberType fiberType) {
165         double power;
166         switch (fiberType) {
167             case Smf:
168                 power = 2;
169                 break;
170             case Eleaf:
171                 power = 1;
172                 break;
173             case Truewavec:
174                 power = -1;
175                 break;
176             case Oleaf:
177             case Dsf:
178             case Truewave:
179             case NzDsf:
180             case Ull:
181             default:
182                 power = 0;
183                 break;
184         }
185         return power;
186     }
187
188     public LinkId getOppositeLink() {
189         return oppositeLink;
190     }
191
192     public Object getSourceTP() {
193         return sourceTP;
194     }
195
196     public Object getDestTP() {
197         return destTP;
198     }
199
200     public OpenroadmLinkType getlinkType() {
201         return linkType;
202     }
203
204     public LinkId getLinkId() {
205         return linkId;
206     }
207
208     public NodeId getSourceId() {
209         return sourceId;
210     }
211
212     public NodeId getDestId() {
213         return destId;
214     }
215
216     public String getClient() {
217         return client;
218     }
219
220     public void setClient(String client) {
221         this.client = client;
222     }
223
224     // Double for transformer of JUNG graph
225     public Double getLatency() {
226         return latency.doubleValue();
227     }
228
229     public Long getAvailableBandwidth() {
230         return availableBandwidth;
231     }
232
233     public Long getUsedBandwidth() {
234         return availableBandwidth;
235     }
236
237     public String getsourceNetworkSupNodeId() {
238         return sourceNetworkSupNodeId;
239     }
240
241     public String getdestNetworkSupNodeId() {
242         return destNetworkSupNodeId;
243     }
244
245     public List<Long> getsrlgList() {
246         return srlgList;
247     }
248
249     public double getosnr() {
250         return osnr;
251     }
252
253     public String getsourceCLLI() {
254         return sourceCLLI;
255     }
256
257     public String getdestCLLI() {
258         return destCLLI;
259     }
260
261     public boolean isValid() {
262         if ((this.linkId == null) || (this.linkType == null) || (this.oppositeLink == null)) {
263             isValid = false;
264             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
265         }
266         if ((this.sourceId == null) || (this.destId == null) || (this.sourceTP == null) || (this.destTP == null)) {
267             isValid = false;
268             LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", linkId);
269         }
270         if ((this.sourceNetworkSupNodeId.equals("")) || (this.destNetworkSupNodeId.equals(""))) {
271             isValid = false;
272             LOG.error("PceLink: No Link source SuppNodeID or destination SuppNodeID is available. Link is ignored {}",
273                 linkId);
274         }
275         if ((this.sourceCLLI.equals("")) || (this.destCLLI.equals(""))) {
276             isValid = false;
277             LOG.error("PceLink: No Link source CLLI or destination CLLI is available. Link is ignored {}", linkId);
278         }
279         if ((this.omsAttributesSpan == null) && (this.linkType == OpenroadmLinkType.ROADMTOROADM)) {
280             isValid = false;
281             LOG.error("PceLink: Error reading Span for OMS link. Link is ignored {}", linkId);
282         }
283         if ((this.srlgList != null) && (this.srlgList.isEmpty())) {
284             isValid = false;
285             LOG.error("PceLink: Empty srlgList for OMS link. Link is ignored {}", linkId);
286         }
287         return isValid;
288     }
289
290     public boolean isOtnValid(Link link, String serviceType) {
291
292         if (this.linkType != OpenroadmLinkType.OTNLINK) {
293             LOG.error("PceLink: Not an OTN link. Link is ignored {}", linkId);
294             return false;
295         }
296
297         OtnLinkType otnLinkType = link
298             .augmentation(org.opendaylight.yang.gen.v1.http.transportpce.topology.rev200129.Link1.class)
299             .getOtnLinkType();
300         if (this.availableBandwidth == 0L) {
301             LOG.error("PceLink: No bandwidth available for OTN Link, link {}  is ignored ", linkId);
302             return false;
303         }
304
305         long neededBW = 0L;
306         OtnLinkType neededType = null;
307         switch (serviceType) {
308
309             case "ODU4":
310                 if (this.usedBandwidth != 0L) {
311                     return false;
312                 }
313                 neededBW = 100000L;
314                 neededType = OtnLinkType.OTU4;
315                 break;
316             case "ODU2":
317             case "ODU2e":
318                 neededBW = 12500L;
319                 break;
320             case "ODU0":
321                 neededBW = 1250L;
322                 break;
323             case "ODU1":
324                 neededBW = 2500L;
325                 break;
326             case "10GE":
327                 neededBW = 10000L;
328                 neededType = OtnLinkType.ODTU4;
329                 break;
330             case "1GE":
331                 neededBW = 1000L;
332                 neededType = OtnLinkType.ODTU4;
333                 break;
334             default:
335                 LOG.error("PceLink: isOtnValid Link {} unsupported serviceType {} ", linkId, serviceType);
336                 return false;
337         }
338
339         if ((this.availableBandwidth >= neededBW)
340             && ((neededType == null) || (neededType.equals(otnLinkType)))) {
341             LOG.info("PceLink: Selected Link {} has available bandwidth and is eligible for {} creation ",
342                 linkId, serviceType);
343         }
344
345         if ((this.linkId == null) || (this.linkType == null) || (this.oppositeLink == null)) {
346             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
347             return false;
348         }
349         if ((this.sourceId == null) || (this.destId == null) || (this.sourceTP == null) || (this.destTP == null)) {
350             LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", linkId);
351             return false;
352         }
353         if ((this.sourceNetworkSupNodeId.equals("")) || (this.destNetworkSupNodeId.equals(""))) {
354             LOG.error("PceLink: No Link source SuppNodeID or destination SuppNodeID is available. Link is ignored {}",
355                 linkId);
356             return false;
357         }
358         if ((this.sourceCLLI.equals("")) || (this.destCLLI.equals(""))) {
359             LOG.error("PceLink: No Link source CLLI or destination CLLI is available. Link is ignored {}", linkId);
360             return false;
361         }
362
363         return true;
364     }
365
366     public String toString() {
367         return "PceLink type=" + linkType + " ID=" + linkId.getValue() + " latency=" + latency;
368     }
369
370 }