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