2fb29265b5c165d54f9e85cd208da24499458b79
[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         double sum = 0;        // sum of 1 over the span OSNRs (linear units)
139         double linkOsnrDb;     // link OSNR, in dB
140         double linkOsnrLu;     // link OSNR, in dB
141         double spanOsnrDb;     // span OSNR, in dB
142         double spanOsnrLu;     // span OSNR, in linear units
143         double ampNoise = 5.5; // default amplifier noise value, in dB
144         double loss;           // fiber span measured loss, in dB
145         double power;          // launch power, in dB
146         double constantA = 38.97293;
147         double constantB = 0.72782;
148         double constantC = -0.532331;
149         double constactD = -0.019549;
150         double upperBoundOSNR = 33;
151         double lowerBoundOSNR = 0.1;
152
153         if (omsAttributesSpan ==  null) {
154             return 0L; // indicates no data or N/A
155         }
156         loss = omsAttributesSpan.getSpanlossCurrent().getValue().doubleValue();
157         switch (omsAttributesSpan.getLinkConcatenation().get(0).getFiberType()) {
158             case Smf:
159                 power = 2;
160                 break;
161
162             case Eleaf:
163                 power = 1;
164                 break;
165
166             case Oleaf:
167                 power = 0;
168                 break;
169
170             case Dsf:
171                 power = 0;
172                 break;
173
174             case Truewave:
175                 power = 0;
176                 break;
177
178             case Truewavec:
179                 power = -1;
180                 break;
181
182             case NzDsf:
183                 power = 0;
184                 break;
185
186             case Ull:
187                 power = 0;
188                 break;
189
190             default:
191                 power = 0;
192                 break;
193         }
194         spanOsnrDb = constantA + constantB * power + constantC * loss + constactD * power * loss;
195         if (spanOsnrDb > upperBoundOSNR) {
196             spanOsnrDb =  upperBoundOSNR;
197         } else if (spanOsnrDb < lowerBoundOSNR) {
198             spanOsnrDb = lowerBoundOSNR;
199         }
200         spanOsnrLu = Math.pow(10, (spanOsnrDb / 10.0));
201         sum = PceConstraints.CONST_OSNR / spanOsnrLu;
202         linkOsnrLu = sum;
203         //link_OSNR_dB = 10 * Math.log10(1 / sum);
204         LOG.debug("In retrieveOSNR: link OSNR is {} dB", linkOsnrLu);
205         return linkOsnrLu;
206     }
207
208
209     public LinkId getOppositeLink() {
210         return this.oppositeLink;
211     }
212
213     public Object getSourceTP() {
214         return this.sourceTP;
215     }
216
217     public Object getDestTP() {
218         return this.destTP;
219     }
220
221     public OpenroadmLinkType getLinkType() {
222         return this.linkType;
223     }
224
225     public LinkId getLinkId() {
226         return this.linkId;
227     }
228
229     public NodeId getSourceId() {
230         return this.sourceId;
231     }
232
233     public NodeId getDestId() {
234         return this.destId;
235     }
236
237     public String getClient() {
238         return this.client;
239     }
240
241     public void setClient(String client) {
242         this.client = client;
243     }
244
245     // Double for transformer of JUNG graph
246     public Double getLatency() {
247         return this.latency.doubleValue();
248     }
249
250     public boolean isValid() {
251         if ((this.linkId == null) || (this.linkType == null) || (this.oppositeLink == null)) {
252             this.isValid = false;
253             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", this.linkId);
254         }
255         if ((this.sourceId == null) || (this.destId == null) || (this.sourceTP == null) || (this.destTP == null)) {
256             this.isValid = false;
257             LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", this.linkId);
258         }
259
260         return this.isValid;
261     }
262
263     @Override
264     public String toString() {
265         return "PceLink type=" + this.linkType + " ID=" + this.linkId.toString() + " latecy=" + this.latency;
266     }
267
268 }