fix import extra separations
[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.io.IOException;
12 import java.io.ObjectInputStream;
13 import java.io.ObjectOutputStream;
14 import java.io.Serializable;
15 import java.util.List;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.Link1;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.span.attributes.LinkConcatenation.FiberType;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.networks.network.link.oms.attributes.Span;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmLinkType;
20 import org.opendaylight.yang.gen.v1.http.transportpce.topology.rev200129.OtnLinkType;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class PceLink implements Serializable {
28
29     private static final long serialVersionUID = 1L;
30     /* Logging. */
31     private static final Logger LOG = LoggerFactory.getLogger(PceLink.class);
32     ///////////////////////// LINKS ////////////////////
33     /*
34      * extension of Link to include constraints and Graph weight
35      */
36     double weight = 0;
37     private boolean isValid = true;
38     private boolean isOtnValid = true;
39
40     // this member is for XPONDER INPUT/OUTPUT links.
41     // it keeps name of client corresponding to NETWORK TP
42     private String client = "";
43     private final LinkId linkId;
44     private final OpenroadmLinkType linkType;
45     private final NodeId sourceId;
46     private final NodeId destId;
47     private transient Object sourceTP;
48     private transient Object destTP;
49     private final String sourceNetworkSupNodeId;
50     private final String destNetworkSupNodeId;
51     private final String sourceCLLI;
52     private final String destCLLI;
53     private final LinkId oppositeLink;
54     private final Long latency;
55     private final Long availableBandwidth;
56     private final Long usedBandwidth;
57     private final List<Long> srlgList;
58     private final double osnr;
59     private final transient Span omsAttributesSpan;
60     private static final double CELERITY = 2.99792458 * 1e5; //meter per ms
61     private static final double NOISE_MASK_A = 0.571429;
62     private static final double NOISE_MASK_B = 39.285714;
63     private static final double UPPER_BOUND_OSNR = 33;
64     private static final double LOWER_BOUND_OSNR = 0.1;
65
66     public PceLink(Link link, PceNode source, PceNode dest) {
67         LOG.info("PceLink: : PceLink start ");
68
69         this.linkId = link.getLinkId();
70
71         this.sourceId = link.getSource().getSourceNode();
72         this.destId = link.getDestination().getDestNode();
73
74         this.sourceTP = link.getSource().getSourceTp();
75         this.destTP = link.getDestination().getDestTp();
76
77         this.sourceNetworkSupNodeId = source.getSupNetworkNodeId();
78         this.destNetworkSupNodeId = dest.getSupNetworkNodeId();
79
80         this.sourceCLLI = source.getSupClliNodeId();
81         this.destCLLI = dest.getSupClliNodeId();
82
83         this.linkType = MapUtils.calcType(link);
84
85         this.oppositeLink = calcOpposite(link);
86
87         if (this.linkType == OpenroadmLinkType.ROADMTOROADM) {
88             this.omsAttributesSpan = MapUtils.getOmsAttributesSpan(link);
89             this.srlgList = MapUtils.getSRLG(link);
90             this.latency = calcLatency(link);
91             this.osnr = calcSpanOSNR();
92             this.availableBandwidth = 0L;
93             this.usedBandwidth = 0L;
94         } else if (this.linkType == OpenroadmLinkType.OTNLINK) {
95             this.availableBandwidth = MapUtils.getAvailableBandwidth(link);
96             this.usedBandwidth = MapUtils.getUsedBandwidth(link);
97             this.srlgList = MapUtils.getSRLGfromLink(link);
98             this.osnr = 0.0;
99             this.latency = 0L;
100             this.omsAttributesSpan = null;
101         } else {
102             this.omsAttributesSpan = null;
103             this.srlgList = null;
104             this.latency = 0L;
105             this.osnr = 100L; //infinite OSNR in DB
106             this.availableBandwidth = 0L;
107             this.usedBandwidth = 0L;
108         }
109         LOG.debug("PceLink: created PceLink  {}", linkId);
110     }
111
112     //Retrieve the opposite link
113     private LinkId calcOpposite(Link link) {
114         LinkId tmpoppositeLink = MapUtils.extractOppositeLink(link);
115         if (tmpoppositeLink == null) {
116             LOG.error("PceLink: Error calcOpposite. Link is ignored {}", link.getLinkId().getValue());
117             isValid = false;
118         }
119         return tmpoppositeLink;
120     }
121
122     //Compute the link latency : if the latency is not defined, the latency is computed from the omsAttributesSpan
123     private Long calcLatency(Link link) {
124         Link1 link1 = null;
125         Long tmplatency;
126         link1 = link.augmentation(Link1.class);
127         if (link1.getLinkLatency() != null) {
128             tmplatency = link1.getLinkLatency().toJava();
129             return tmplatency;
130         }
131
132         try {
133             double tmp = 0;
134             for (int i = 0; i < this.omsAttributesSpan.getLinkConcatenation().size(); i++) {
135                 //Length is expressed in meter and latency is expressed in ms according to OpenROADM MSA
136                 tmp += this.omsAttributesSpan.getLinkConcatenation().get(i).getSRLGLength().toJava() / CELERITY;
137                 LOG.info("In PceLink: The latency of link {} == {}",link.getLinkId(),tmp);
138             }
139             tmplatency = (long) Math.ceil(tmp);
140         } catch (NullPointerException e) {
141             LOG.debug("In PceLink: cannot compute the latency for the link {}",link.getLinkId().getValue());
142             tmplatency = 1L;
143         }
144         return tmplatency;
145     }
146
147     //Compute the OSNR of a span
148     public double calcSpanOSNR() {
149         try {
150             double pout; //power on the output of the previous ROADM (dBm)
151             pout = retrievePower(this.omsAttributesSpan.getLinkConcatenation().get(0).getFiberType());
152             double spanLoss = this.omsAttributesSpan.getSpanlossCurrent().getValue().doubleValue(); // span loss (dB)
153             double pin = pout - spanLoss; //power on the input of the current ROADM (dBm)
154             double spanOsnrDb;
155             spanOsnrDb = NOISE_MASK_A * pin + NOISE_MASK_B;
156             if (spanOsnrDb > UPPER_BOUND_OSNR) {
157                 spanOsnrDb =  UPPER_BOUND_OSNR;
158             } else if (spanOsnrDb < LOWER_BOUND_OSNR) {
159                 spanOsnrDb = LOWER_BOUND_OSNR;
160             }
161             return spanOsnrDb;
162         } catch (NullPointerException e) {
163             LOG.error("in PceLink : Null field in the OmsAttrubtesSpan");
164             return 0L;
165         }
166     }
167
168     private double retrievePower(FiberType fiberType) {
169         double power;
170         switch (fiberType) {
171             case Smf:
172                 power = 2;
173                 break;
174             case Eleaf:
175                 power = 1;
176                 break;
177             case Truewavec:
178                 power = -1;
179                 break;
180             case Oleaf:
181             case Dsf:
182             case Truewave:
183             case NzDsf:
184             case Ull:
185             default:
186                 power = 0;
187                 break;
188         }
189         return power;
190     }
191
192     public LinkId getOppositeLink() {
193         return oppositeLink;
194     }
195
196     public Object getSourceTP() {
197         return sourceTP;
198     }
199
200     public Object getDestTP() {
201         return destTP;
202     }
203
204     public OpenroadmLinkType getlinkType() {
205         return linkType;
206     }
207
208     public LinkId getLinkId() {
209         return linkId;
210     }
211
212     public NodeId getSourceId() {
213         return sourceId;
214     }
215
216     public NodeId getDestId() {
217         return destId;
218     }
219
220     public String getClient() {
221         return client;
222     }
223
224     public void setClient(String client) {
225         this.client = client;
226     }
227
228     // Double for transformer of JUNG graph
229     public Double getLatency() {
230         return latency.doubleValue();
231     }
232
233     public Long getAvailableBandwidth() {
234         return availableBandwidth;
235     }
236
237     public Long getUsedBandwidth() {
238         return usedBandwidth;
239     }
240
241     public String getsourceNetworkSupNodeId() {
242         return sourceNetworkSupNodeId;
243     }
244
245     public String getdestNetworkSupNodeId() {
246         return destNetworkSupNodeId;
247     }
248
249     public List<Long> getsrlgList() {
250         return srlgList;
251     }
252
253     public double getosnr() {
254         return osnr;
255     }
256
257     public String getsourceCLLI() {
258         return sourceCLLI;
259     }
260
261     public String getdestCLLI() {
262         return destCLLI;
263     }
264
265     public boolean isValid() {
266         if ((this.linkId == null) || (this.linkType == null) || (this.oppositeLink == null)) {
267             isValid = false;
268             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
269         }
270         if ((this.sourceId == null) || (this.destId == null) || (this.sourceTP == null) || (this.destTP == null)) {
271             isValid = false;
272             LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", linkId);
273         }
274         if ((this.sourceNetworkSupNodeId.equals("")) || (this.destNetworkSupNodeId.equals(""))) {
275             isValid = false;
276             LOG.error("PceLink: No Link source SuppNodeID or destination SuppNodeID is available. Link is ignored {}",
277                 linkId);
278         }
279         if ((this.sourceCLLI.equals("")) || (this.destCLLI.equals(""))) {
280             isValid = false;
281             LOG.error("PceLink: No Link source CLLI or destination CLLI is available. Link is ignored {}", linkId);
282         }
283         if ((this.omsAttributesSpan == null) && (this.linkType == OpenroadmLinkType.ROADMTOROADM)) {
284             isValid = false;
285             LOG.error("PceLink: Error reading Span for OMS link. Link is ignored {}", linkId);
286         }
287         if ((this.srlgList != null) && (this.srlgList.isEmpty())) {
288             isValid = false;
289             LOG.error("PceLink: Empty srlgList for OMS link. Link is ignored {}", linkId);
290         }
291         return isValid;
292     }
293
294     public boolean isOtnValid(Link link, String serviceType) {
295
296         if (this.linkType != OpenroadmLinkType.OTNLINK) {
297             LOG.error("PceLink: Not an OTN link. Link is ignored {}", linkId);
298             return false;
299         }
300
301         OtnLinkType otnLinkType = link
302             .augmentation(org.opendaylight.yang.gen.v1.http.transportpce.topology.rev200129.Link1.class)
303             .getOtnLinkType();
304         if (this.availableBandwidth == 0L) {
305             LOG.error("PceLink: No bandwidth available for OTN Link, link {}  is ignored ", linkId);
306             return false;
307         }
308
309         long neededBW;
310         OtnLinkType neededType = null;
311         switch (serviceType) {
312
313             case "ODU4":
314                 if (this.usedBandwidth != 0L) {
315                     return false;
316                 }
317                 neededBW = 100000L;
318                 neededType = OtnLinkType.OTU4;
319                 break;
320             case "ODU2":
321             case "ODU2e":
322                 neededBW = 12500L;
323                 break;
324             case "ODU0":
325                 neededBW = 1250L;
326                 break;
327             case "ODU1":
328                 neededBW = 2500L;
329                 break;
330             case "10GE":
331                 neededBW = 10000L;
332                 neededType = OtnLinkType.ODTU4;
333                 break;
334             case "1GE":
335                 neededBW = 1000L;
336                 neededType = OtnLinkType.ODTU4;
337                 break;
338             default:
339                 LOG.error("PceLink: isOtnValid Link {} unsupported serviceType {} ", linkId, serviceType);
340                 return false;
341         }
342
343         if ((this.availableBandwidth >= neededBW)
344             && ((neededType == null) || (neededType.equals(otnLinkType)))) {
345             LOG.info("PceLink: Selected Link {} has available bandwidth and is eligible for {} creation ",
346                 linkId, serviceType);
347         }
348
349         if ((this.linkId == null) || (this.linkType == null) || (this.oppositeLink == null)) {
350             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
351             return false;
352         }
353         if ((this.sourceId == null) || (this.destId == null) || (this.sourceTP == null) || (this.destTP == null)) {
354             LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", linkId);
355             return false;
356         }
357         if ((this.sourceNetworkSupNodeId.equals("")) || (this.destNetworkSupNodeId.equals(""))) {
358             LOG.error("PceLink: No Link source SuppNodeID or destination SuppNodeID is available. Link is ignored {}",
359                 linkId);
360             return false;
361         }
362         if ((this.sourceCLLI.equals("")) || (this.destCLLI.equals(""))) {
363             LOG.error("PceLink: No Link source CLLI or destination CLLI is available. Link is ignored {}", linkId);
364             return false;
365         }
366
367         return true;
368     }
369
370     @Override
371     public String toString() {
372         return "PceLink type=" + linkType + " ID=" + linkId.getValue() + " latency=" + latency;
373     }
374
375     private void writeObject(ObjectOutputStream out) throws IOException {
376         out.defaultWriteObject();
377         out.writeObject(this.sourceTP);
378         out.writeObject(this.destTP);
379     }
380
381     private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
382         in.defaultReadObject();
383         this.sourceTP = in.readObject();
384         this.destTP = in.readObject();
385     }
386 }