fix some sonar issues
[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.transportpce.pce.constraints.PceConstraints;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.Link1;
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.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class PceLink {
24
25     /* Logging. */
26     private static final Logger LOG = LoggerFactory.getLogger(PceLink.class);
27
28     ///////////////////////// LINKS ////////////////////
29     /*
30      * extension of Link to include constraints and Graph weight
31      */
32
33     double weight = 0;
34
35     private boolean isValid = true;
36
37     // this member is for XPONDER INPUT/OUTPUT links.
38     // it keeps name of client correcponding to NETWORK TP
39     private String client = "";
40
41     private final LinkId linkId;
42     private final OpenroadmLinkType linkType;
43     private final NodeId sourceId;
44     private final NodeId destId;
45     private final Object sourceTP;
46     private final Object destTP;
47     private final String sourceSupNodeId;
48     private final String destSupNodeId;
49     private final String sourceCLLI;
50     private final String destCLLI;
51     private final LinkId oppositeLink;
52     private final Long latency;
53     private final List<Long> srlgList;
54     private final double osnr;
55     private final Span omsAttributesSpan;
56
57     public PceLink(Link link, PceNode source, PceNode dest) {
58         LOG.debug("PceLink: : PceLink start ");
59
60         this.linkId = link.getLinkId();
61
62         this.sourceId = link.getSource().getSourceNode();
63         this.destId = link.getDestination().getDestNode();
64
65         this.sourceTP = link.getSource().getSourceTp();
66         this.destTP = link.getDestination().getDestTp();
67
68         this.sourceSupNodeId = source.getSupNodeIdPceNode();
69         this.destSupNodeId = dest.getSupNodeIdPceNode();
70
71         this.sourceCLLI = source.getCLLI();
72         this.destCLLI = dest.getCLLI();
73
74         this.linkType = MapUtils.calcType(link);
75
76         this.oppositeLink = calcOpposite(link);
77         this.latency = calcLatency(link);
78
79         if (this.linkType == OpenroadmLinkType.ROADMTOROADM) {
80             this.omsAttributesSpan = MapUtils.getOmsAttributesSpan(link);
81             this.srlgList = MapUtils.getSRLG(link);
82             this.osnr = retrieveOSNR();
83         } else {
84             this.omsAttributesSpan = null;
85             this.srlgList = null;
86             this.osnr = 0.0;
87         }
88
89
90         LOG.debug("PceLink: created PceLink  {}", toString());
91     }
92
93     /*private OpenroadmLinkType calcType(Link link) {
94         org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.@Nullable Link1 link1 = null;
95         OpenroadmLinkType tmplinkType = null;
96
97         // ID and type
98         link1 = link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130
99             .Link1.class);
100         if (link1 == null) {
101             this.isValid = false;
102             LOG.error("PceLink: No Link augmentation available. Link is ignored {}", this.linkId);
103             return null;
104         }
105
106         tmplinkType = link1.getLinkType();
107         if (tmplinkType == null) {
108             this.isValid = false;
109             LOG.error("PceLink: No Link type available. Link is ignored {}", this.linkId);
110             return null;
111         }
112         return tmplinkType;
113     }*/
114
115     private LinkId calcOpposite(Link link) {
116         // opposite link
117
118         LinkId tmpoppositeLink = MapUtils.extractOppositeLink(link);
119         if (tmpoppositeLink == null) {
120             LOG.error("PceLink: Error calcOpposite. Link is ignored {}", link.getLinkId().getValue());
121             isValid = false;
122         }
123         return tmpoppositeLink;
124     }
125
126     private Long calcLatency(Link link) {
127         Long tmplatency = 1L;
128         Link1 link1 = null;
129         // latency
130         link1 = link.augmentation(Link1.class);
131         try {
132             tmplatency = link1.getLinkLatency();
133         } catch (NullPointerException e) {
134             LOG.debug("the latency does not exist for this link");
135         }
136         return tmplatency;
137     }
138
139     @SuppressWarnings("checkstyle:VariableDeclarationUsageDistance")
140     public double retrieveOSNR() {
141         // sum of 1 over the span OSNRs (linear units)
142         double sum = 0;
143         // link OSNR, in dB
144         //double linkOsnrDb;
145         // link OSNR, in dB
146         double linkOsnrLu;
147         // span OSNR, in dB
148         double spanOsnrDb;
149         // span OSNR, in linear units
150         double spanOsnrLu;
151         // default amplifier noise value, in dB
152         //double ampNoise = 5.5;
153         // fiber span measured loss, in dB
154         double loss;
155         // launch power, in dB
156         double power;
157         double constantA = 38.97293;
158         double constantB = 0.72782;
159         double constantC = -0.532331;
160         double constactD = -0.019549;
161         double upperBoundosnr = 33;
162         double lowerBoundosnr = 0.1;
163
164         if (omsAttributesSpan ==  null) {
165             // indicates no data or N/A
166             return 0L;
167         }
168         loss = omsAttributesSpan.getSpanlossCurrent().getValue().doubleValue();
169         switch (omsAttributesSpan.getLinkConcatenation().get(0).getFiberType()) {
170             case Smf:
171                 power = 2;
172                 break;
173
174             case Eleaf:
175                 power = 1;
176                 break;
177
178             case Oleaf:
179                 power = 0;
180                 break;
181
182             case Dsf:
183                 power = 0;
184                 break;
185
186             case Truewave:
187                 power = 0;
188                 break;
189
190             case Truewavec:
191                 power = -1;
192                 break;
193
194             case NzDsf:
195                 power = 0;
196                 break;
197
198             case Ull:
199                 power = 0;
200                 break;
201
202             default:
203                 power = 0;
204                 break;
205         }
206         spanOsnrDb = constantA + constantB * power + constantC * loss + constactD * power * loss;
207         if (spanOsnrDb > upperBoundosnr) {
208             spanOsnrDb =  upperBoundosnr;
209         } else if (spanOsnrDb < lowerBoundosnr) {
210             spanOsnrDb = lowerBoundosnr;
211         }
212         spanOsnrLu = Math.pow(10, (spanOsnrDb / 10.0));
213         sum = PceConstraints.CONST_OSNR / spanOsnrLu;
214         linkOsnrLu = sum;
215         LOG.debug("In retrieveosnr: link osnr is {} dB", linkOsnrLu);
216         return linkOsnrLu;
217     }
218
219     public LinkId getOppositeLink() {
220         return oppositeLink;
221     }
222
223     public Object getSourceTP() {
224         return sourceTP;
225     }
226
227     public Object getDestTP() {
228         return destTP;
229     }
230
231     public OpenroadmLinkType getlinkType() {
232         return linkType;
233     }
234
235     public LinkId getLinkId() {
236         return linkId;
237     }
238
239     public NodeId getSourceId() {
240         return sourceId;
241     }
242
243     public NodeId getDestId() {
244         return destId;
245     }
246
247     public String getClient() {
248         return client;
249     }
250
251     public void setClient(String client) {
252         this.client = client;
253     }
254
255     // Double for transformer of JUNG graph
256     public Double getLatency() {
257         return latency.doubleValue();
258     }
259
260     public String getsourceSupNodeId() {
261         return sourceSupNodeId;
262     }
263
264     public String getdestSupNodeId() {
265         return destSupNodeId;
266     }
267
268     public List<Long> getsrlgList() {
269         return srlgList;
270     }
271
272     public double getosnr() {
273         return osnr;
274     }
275
276     public String getsourceCLLI() {
277         return sourceCLLI;
278     }
279
280     public String getdestCLLI() {
281         return destCLLI;
282     }
283
284     public boolean isValid() {
285         if ((this.linkId == null) || (this.linkType == null)
286                 || (this.oppositeLink == null)) {
287             isValid = false;
288             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
289         }
290         if ((this.sourceId == null) || (this.destId == null)
291                 || (this.sourceTP == null) || (this.destTP == null)) {
292             isValid = false;
293             LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", linkId);
294         }
295         if ((this.sourceSupNodeId.equals("")) || (this.destSupNodeId.equals(""))) {
296             isValid = false;
297             LOG.error("PceLink: No Link source SuppNodeID or destination SuppNodeID is available. Link is ignored {}",
298                 linkId);
299         }
300         if ((this.sourceCLLI.equals("")) || (this.destCLLI.equals(""))) {
301             isValid = false;
302             LOG.error("PceLink: No Link source CLLI or destination CLLI is available. Link is ignored {}", linkId);
303         }
304         if ((this.omsAttributesSpan == null) && (this.linkType == OpenroadmLinkType.ROADMTOROADM)) {
305             isValid = false;
306             LOG.error("PceLink: Error reading Span for OMS link. Link is ignored {}", linkId);
307         }
308         if ((this.srlgList != null) && (this.srlgList.isEmpty())) {
309             isValid = false;
310             LOG.error("PceLink: Empty srlgList for OMS link. Link is ignored {}", linkId);
311         }
312         return isValid;
313     }
314
315     public String toString() {
316         return "PceLink type=" + linkType + " ID=" + linkId.getValue() + " latecy=" + latency;
317     }
318
319 }