fix some sonar issues
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / 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;
10
11 import java.util.List;
12
13 import org.eclipse.jdt.annotation.Nullable;
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(PceCalculation.class);
27
28     ///////////////////////// LINKS ////////////////////
29     /*
30      * extension of Link to include constraints and Graph weight
31      */
32     // double capacity = 1;
33
34     double weight = 0;
35
36     private boolean isValid = true;
37
38     // this member is for XPONDER INPUT/OUTPUT links.
39     // it keeps name of client corresponding to NETWORK TP
40     private String client = "";
41
42     private final LinkId linkId;
43     private final OpenroadmLinkType linkType;
44     private final NodeId sourceId;
45     private final NodeId destId;
46     private final Object sourceTP;
47     private final Object destTP;
48     private final LinkId oppositeLink;
49     private final Long latency;
50     private final List<Long> srlg;
51     private final double osnr;
52     private final Span omsAttributesSpan;
53
54     public PceLink(Link link) {
55         LOG.debug("PceLink: : PceLink start ");
56
57         this.linkId = link.getLinkId();
58
59         this.sourceId = link.getSource().getSourceNode();
60         this.destId = link.getDestination().getDestNode();
61
62         this.sourceTP = link.getSource().getSourceTp();
63         this.destTP = link.getDestination().getDestTp();
64
65         this.linkType = calcType(link);
66
67         this.oppositeLink = calcOpposite(link);
68         this.latency = calcLatency(link);
69
70         if (this.linkType == OpenroadmLinkType.ROADMTOROADM) {
71             this.omsAttributesSpan = MapUtils.getOmsAttributesSpan(link);
72             this.srlg = MapUtils.getSRLG(link);
73             this.osnr = retrieveOSNR();
74         } else {
75             this.omsAttributesSpan = null;
76             this.srlg = null;
77             this.osnr = 0L;
78         }
79
80         LOG.debug("PceLink: created PceLink  {}", toString());
81     }
82
83     private OpenroadmLinkType calcType(Link link) {
84         org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.@Nullable Link1 link1 = null;
85         OpenroadmLinkType tmplType = null;
86
87         // ID and type
88         link1 = link.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130
89             .Link1.class);
90         if (link1 == null) {
91             this.isValid = false;
92             LOG.error("PceLink: No Link augmentation available. Link is ignored {}", this.linkId);
93             return null;
94         }
95
96         tmplType = link1.getLinkType();
97         if (tmplType == null) {
98             this.isValid = false;
99             LOG.error("PceLink: No Link type available. Link is ignored {}", this.linkId);
100             return null;
101         }
102         return tmplType;
103     }
104
105     private LinkId calcOpposite(Link link) {
106         // opposite link
107         LinkId tmpoppositeLink = null;
108         Link1 linkOpposite = link.augmentation(Link1.class);
109         if (linkOpposite.getOppositeLink() != null) {
110             tmpoppositeLink = linkOpposite.getOppositeLink();
111         } else {
112             LOG.error("link {} has no opposite link", link.getLinkId().getValue());
113         }
114         LOG.debug("PceLink: reading oppositeLink.  {}", linkOpposite.toString());
115         if (tmpoppositeLink == null) {
116             this.isValid = false;
117             LOG.error("PceLink: Error reading oppositeLink. Link is ignored {}", this.linkId);
118             return null;
119         }
120         return tmpoppositeLink;
121     }
122
123     private Long calcLatency(Link link) {
124         Long tmplatency = (long)0;
125         Link1 link1 = null;
126         // latency
127         link1 = link.augmentation(Link1.class);
128         tmplatency = link1.getLinkLatency();
129         if (tmplatency == null) {
130             tmplatency = (long) 0;
131         }
132         return tmplatency;
133
134     }
135
136     @SuppressWarnings("checkstyle:VariableDeclarationUsageDistance")
137     public double retrieveOSNR() {
138         // sum of 1 over the span OSNRs (linear units)
139         double sum = 0;
140         // link OSNR, in dB
141         double linkOsnrDb;
142         // link OSNR, in dB
143         double linkOsnrLu;
144         // span OSNR, in dB
145         double spanOsnrDb;
146         // span OSNR, in linear units
147         double spanOsnrLu;
148         // default amplifier noise value, in dB
149         double ampNoise = 5.5;
150         // fiber span measured loss, in dB
151         double loss;
152         // launch power, in dB
153         double power;
154         double constantA = 38.97293;
155         double constantB = 0.72782;
156         double constantC = -0.532331;
157         double constactD = -0.019549;
158         double upperBoundOSNR = 33;
159         double lowerBoundOSNR = 0.1;
160
161         if (omsAttributesSpan ==  null) {
162             // indicates no data or N/A
163             return 0L;
164         }
165         loss = omsAttributesSpan.getSpanlossCurrent().getValue().doubleValue();
166         switch (omsAttributesSpan.getLinkConcatenation().get(0).getFiberType()) {
167             case Smf:
168                 power = 2;
169                 break;
170
171             case Eleaf:
172                 power = 1;
173                 break;
174
175             case Oleaf:
176                 power = 0;
177                 break;
178
179             case Dsf:
180                 power = 0;
181                 break;
182
183             case Truewave:
184                 power = 0;
185                 break;
186
187             case Truewavec:
188                 power = -1;
189                 break;
190
191             case NzDsf:
192                 power = 0;
193                 break;
194
195             case Ull:
196                 power = 0;
197                 break;
198
199             default:
200                 power = 0;
201                 break;
202         }
203         spanOsnrDb = constantA + constantB * power + constantC * loss + constactD * power * loss;
204         if (spanOsnrDb > upperBoundOSNR) {
205             spanOsnrDb =  upperBoundOSNR;
206         } else if (spanOsnrDb < lowerBoundOSNR) {
207             spanOsnrDb = lowerBoundOSNR;
208         }
209         spanOsnrLu = Math.pow(10, (spanOsnrDb / 10.0));
210         sum = PceConstraints.CONST_OSNR / spanOsnrLu;
211         linkOsnrLu = sum;
212         //link_OSNR_dB = 10 * Math.log10(1 / sum);
213         LOG.debug("In retrieveOSNR: link OSNR is {} dB", linkOsnrLu);
214         return linkOsnrLu;
215     }
216
217
218     public LinkId getOppositeLink() {
219         return this.oppositeLink;
220     }
221
222     public Object getSourceTP() {
223         return this.sourceTP;
224     }
225
226     public Object getDestTP() {
227         return this.destTP;
228     }
229
230     public OpenroadmLinkType getLinkType() {
231         return this.linkType;
232     }
233
234     public LinkId getLinkId() {
235         return this.linkId;
236     }
237
238     public NodeId getSourceId() {
239         return this.sourceId;
240     }
241
242     public NodeId getDestId() {
243         return this.destId;
244     }
245
246     public String getClient() {
247         return this.client;
248     }
249
250     public void setClient(String client) {
251         this.client = client;
252     }
253
254     // Double for transformer of JUNG graph
255     public Double getLatency() {
256         return this.latency.doubleValue();
257     }
258
259     public boolean isValid() {
260         if ((this.linkId == null) || (this.linkType == null) || (this.oppositeLink == null)) {
261             this.isValid = false;
262             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", this.linkId);
263         }
264         if ((this.sourceId == null) || (this.destId == null) || (this.sourceTP == null) || (this.destTP == null)) {
265             this.isValid = false;
266             LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", this.linkId);
267         }
268
269         return this.isValid;
270     }
271
272     @Override
273     public String toString() {
274         return "PceLink type=" + this.linkType + " ID=" + this.linkId.toString() + " latecy=" + this.latency;
275     }
276
277 }