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