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