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