58215e8f29513d170fe34ed6c1ef8c934869ef8d
[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.yang.gen.v1.http.org.openroadm.common.network.rev181130.Link1;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.span.attributes.LinkConcatenation.FiberType;
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     ///////////////////////// LINKS ////////////////////
28     /*
29      * extension of Link to include constraints and Graph weight
30      */
31     double weight = 0;
32     private boolean isValid = true;
33     // this member is for XPONDER INPUT/OUTPUT links.
34     // it keeps name of client corresponding to NETWORK TP
35     private String client = "";
36     private final LinkId linkId;
37     private final OpenroadmLinkType linkType;
38     private final NodeId sourceId;
39     private final NodeId destId;
40     private final Object sourceTP;
41     private final Object destTP;
42     private final String sourceSupNodeId;
43     private final String destSupNodeId;
44     private final String sourceCLLI;
45     private final String destCLLI;
46     private final LinkId oppositeLink;
47     private final Long latency;
48     private final List<Long> srlgList;
49     private final double osnr;
50     private final Span omsAttributesSpan;
51     private static final double CELERITY = 2.99792458 * 1e5; //meter per ms
52     private static final double NOISE_MASK_A = 0.571429;
53     private static final double NOISE_MASK_B = 39.285714;
54     private static final double UPPER_BOUND_OSNR = 33;
55     private static final double LOWER_BOUND_OSNR = 0.1;
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
78         if (this.linkType == OpenroadmLinkType.ROADMTOROADM) {
79             this.omsAttributesSpan = MapUtils.getOmsAttributesSpan(link);
80             this.srlgList = MapUtils.getSRLG(link);
81             this.latency = calcLatency(link);
82             this.osnr = calcSpanOSNR();
83         } else {
84             this.omsAttributesSpan = null;
85             this.srlgList = null;
86             this.latency = 0L;
87             this.osnr = 100L; //infinite OSNR in DB
88         }
89
90         LOG.debug("PceLink: created PceLink  {}", toString());
91     }
92
93     //Retrieve the opposite link
94     private LinkId calcOpposite(Link link) {
95         LinkId tmpoppositeLink = MapUtils.extractOppositeLink(link);
96         if (tmpoppositeLink == null) {
97             LOG.error("PceLink: Error calcOpposite. Link is ignored {}", link.getLinkId().getValue());
98             isValid = false;
99         }
100         return tmpoppositeLink;
101     }
102
103     //Compute the link latency : if the latency is not defined, the latency it is computed from the omsAttributesSpan
104     private Long calcLatency(Link link) {
105         Link1 link1 = null;
106         link1 = link.augmentation(Link1.class);
107         Long tmplatency = link1.getLinkLatency();
108         if (tmplatency != null) {
109             return tmplatency;
110         }
111
112         try {
113             double tmp = 0;
114             for (int i = 0; i < this.omsAttributesSpan.getLinkConcatenation().size(); i++) {
115                 //Length is expressed in meter and latency is expressed in ms according to OpenROADM MSA
116                 tmp += this.omsAttributesSpan.getLinkConcatenation().get(i).getSRLGLength() / CELERITY;
117                 LOG.info("In PceLink: The latency of link {} == {}",link.getLinkId(),tmplatency);
118             }
119             tmplatency = (long) Math.ceil(tmp);
120         } catch (NullPointerException e) {
121             LOG.debug("In PceLink: cannot compute the latency for the link {}",link.getLinkId().getValue());
122             tmplatency = 1L;
123         }
124         return tmplatency;
125     }
126
127     //Compute the OSNR of a span
128     public double calcSpanOSNR() {
129         try {
130             double pout; //power on the output of the previous ROADM (dBm)
131             pout = retrievePower(this.omsAttributesSpan.getLinkConcatenation().get(0).getFiberType());
132             double spanLoss = this.omsAttributesSpan.getSpanlossCurrent().getValue().doubleValue(); // span loss (dB)
133             double pin = pout - spanLoss; //power on the input of the current ROADM (dBm)
134             double spanOsnrDb;
135             spanOsnrDb = NOISE_MASK_A * pin + NOISE_MASK_B;
136             if (spanOsnrDb > UPPER_BOUND_OSNR) {
137                 spanOsnrDb =  UPPER_BOUND_OSNR;
138             } else if (spanOsnrDb < LOWER_BOUND_OSNR) {
139                 spanOsnrDb = LOWER_BOUND_OSNR;
140             }
141             return spanOsnrDb;
142         } catch (NullPointerException e) {
143             LOG.error("in PceLink : Null field in the OmsAttrubtesSpan");
144             return 0L;
145         }
146     }
147
148     private double retrievePower(FiberType fiberType) {
149         double power;
150         switch (fiberType) {
151             case Smf:
152                 power = 2;
153                 break;
154             case Eleaf:
155                 power = 1;
156                 break;
157             case Truewavec:
158                 power = -1;
159                 break;
160             case Oleaf:
161             case Dsf:
162             case Truewave:
163             case NzDsf:
164             case Ull:
165             default:
166                 power = 0;
167                 break;
168         }
169         return power;
170     }
171
172     public LinkId getOppositeLink() {
173         return oppositeLink;
174     }
175
176     public Object getSourceTP() {
177         return sourceTP;
178     }
179
180     public Object getDestTP() {
181         return destTP;
182     }
183
184     public OpenroadmLinkType getlinkType() {
185         return linkType;
186     }
187
188     public LinkId getLinkId() {
189         return linkId;
190     }
191
192     public NodeId getSourceId() {
193         return sourceId;
194     }
195
196     public NodeId getDestId() {
197         return destId;
198     }
199
200     public String getClient() {
201         return client;
202     }
203
204     public void setClient(String client) {
205         this.client = client;
206     }
207
208     // Double for transformer of JUNG graph
209     public Double getLatency() {
210         return latency.doubleValue();
211     }
212
213     public String getsourceSupNodeId() {
214         return sourceSupNodeId;
215     }
216
217     public String getdestSupNodeId() {
218         return destSupNodeId;
219     }
220
221     public List<Long> getsrlgList() {
222         return srlgList;
223     }
224
225     public double getosnr() {
226         return osnr;
227     }
228
229     public String getsourceCLLI() {
230         return sourceCLLI;
231     }
232
233     public String getdestCLLI() {
234         return destCLLI;
235     }
236
237     public boolean isValid() {
238         if ((this.linkId == null) || (this.linkType == null)
239                 || (this.oppositeLink == null)) {
240             isValid = false;
241             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
242         }
243         if ((this.sourceId == null) || (this.destId == null)
244                 || (this.sourceTP == null) || (this.destTP == null)) {
245             isValid = false;
246             LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", linkId);
247         }
248         if ((this.sourceSupNodeId.equals("")) || (this.destSupNodeId.equals(""))) {
249             isValid = false;
250             LOG.error("PceLink: No Link source SuppNodeID or destination SuppNodeID is available. Link is ignored {}",
251                 linkId);
252         }
253         if ((this.sourceCLLI.equals("")) || (this.destCLLI.equals(""))) {
254             isValid = false;
255             LOG.error("PceLink: No Link source CLLI or destination CLLI is available. Link is ignored {}", linkId);
256         }
257         if ((this.omsAttributesSpan == null) && (this.linkType == OpenroadmLinkType.ROADMTOROADM)) {
258             isValid = false;
259             LOG.error("PceLink: Error reading Span for OMS link. Link is ignored {}", linkId);
260         }
261         if ((this.srlgList != null) && (this.srlgList.isEmpty())) {
262             isValid = false;
263             LOG.error("PceLink: Empty srlgList for OMS link. Link is ignored {}", linkId);
264         }
265         return isValid;
266     }
267
268     public String toString() {
269         return "PceLink type=" + linkType + " ID=" + linkId.getValue() + " latency=" + latency;
270     }
271
272 }