Upgrade to OpenROADM 7.1.0 network models
[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.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.Link1;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev200529.span.attributes.LinkConcatenation;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev200529.span.attributes.LinkConcatenation.FiberType;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev200529.span.attributes.LinkConcatenationKey;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev200529.networks.network.link.oms.attributes.Span;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmLinkType;
25 import org.opendaylight.yang.gen.v1.http.transportpce.topology.rev201019.OtnLinkType;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @SuppressWarnings("serial")
33 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
34     value = "SE_NO_SERIALVERSIONID",
35     justification = "https://github.com/rzwitserloot/lombok/wiki/WHY-NOT:-serialVersionUID")
36 public class PceLink implements Serializable {
37
38     /* Logging. */
39     private static final Logger LOG = LoggerFactory.getLogger(PceLink.class);
40     ///////////////////////// LINKS ////////////////////
41     /*
42      * extension of Link to include constraints and Graph weight
43      */
44     double weight = 0;
45     private boolean isValid = true;
46
47     // this member is for XPONDER INPUT/OUTPUT links.
48     // it keeps name of client corresponding to NETWORK TP
49     private String client = "";
50     private final LinkId linkId;
51     private final OpenroadmLinkType linkType;
52     private final NodeId sourceId;
53     private final NodeId destId;
54     private transient Object sourceTP;
55     private transient Object destTP;
56     private final String sourceNetworkSupNodeId;
57     private final String destNetworkSupNodeId;
58     private final String sourceCLLI;
59     private final String destCLLI;
60     private final LinkId oppositeLink;
61     private final Long latency;
62     private final Long availableBandwidth;
63     private final Long usedBandwidth;
64     private final List<Long> srlgList;
65     private final double osnr;
66     private final transient Span omsAttributesSpan;
67     //meter per ms
68     private static final double CELERITY = 2.99792458 * 1e5;
69     private static final double NOISE_MASK_A = 0.571429;
70     private static final double NOISE_MASK_B = 39.285714;
71     private static final double UPPER_BOUND_OSNR = 33;
72     private static final double LOWER_BOUND_OSNR = 0.1;
73
74     public PceLink(Link link, PceNode source, PceNode dest) {
75         LOG.info("PceLink: : PceLink start ");
76
77         this.linkId = link.getLinkId();
78
79         this.sourceId = link.getSource().getSourceNode();
80         this.destId = link.getDestination().getDestNode();
81
82         this.sourceTP = link.getSource().getSourceTp();
83         this.destTP = link.getDestination().getDestTp();
84
85         this.sourceNetworkSupNodeId = source.getSupNetworkNodeId();
86         this.destNetworkSupNodeId = dest.getSupNetworkNodeId();
87
88         this.sourceCLLI = source.getSupClliNodeId();
89         this.destCLLI = dest.getSupClliNodeId();
90
91         this.linkType = MapUtils.calcType(link);
92
93         this.oppositeLink = calcOpposite(link);
94
95         if (this.linkType == OpenroadmLinkType.ROADMTOROADM) {
96             this.omsAttributesSpan = MapUtils.getOmsAttributesSpan(link);
97             this.srlgList = MapUtils.getSRLG(link);
98             this.latency = calcLatency(link);
99             this.osnr = calcSpanOSNR();
100             this.availableBandwidth = 0L;
101             this.usedBandwidth = 0L;
102         } else if (this.linkType == OpenroadmLinkType.OTNLINK) {
103             this.availableBandwidth = MapUtils.getAvailableBandwidth(link);
104             this.usedBandwidth = MapUtils.getUsedBandwidth(link);
105             this.srlgList = MapUtils.getSRLGfromLink(link);
106             this.osnr = 0.0;
107             this.latency = 0L;
108             this.omsAttributesSpan = null;
109         } else {
110             this.omsAttributesSpan = null;
111             this.srlgList = null;
112             this.latency = 0L;
113             //infinite OSNR in DB
114             this.osnr = 100L;
115             this.availableBandwidth = 0L;
116             this.usedBandwidth = 0L;
117         }
118         LOG.debug("PceLink: created PceLink  {}", linkId);
119     }
120
121     //Retrieve the opposite link
122     private LinkId calcOpposite(Link link) {
123         LinkId tmpoppositeLink = MapUtils.extractOppositeLink(link);
124         if (tmpoppositeLink == null) {
125             LOG.error("PceLink: Error calcOpposite. Link is ignored {}", link.getLinkId().getValue());
126             isValid = false;
127         }
128         return tmpoppositeLink;
129     }
130
131     //Compute the link latency : if the latency is not defined, the latency is computed from the omsAttributesSpan
132     private Long calcLatency(Link link) {
133         Link1 link1 = link.augmentation(Link1.class);
134         if (link1.getLinkLatency() != null) {
135             return link1.getLinkLatency().toJava();
136         }
137         if (this.omsAttributesSpan == null) {
138             return 1L;
139         }
140         double tmp = 0;
141         Map<LinkConcatenationKey, LinkConcatenation> linkConcatenationMap = this.omsAttributesSpan
142                 .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             if (entry == null || entry.getValue() == null || entry.getValue().getSRLGLength() == null) {
146                 LOG.debug("In PceLink: cannot compute the latency for the link {}", link.getLinkId().getValue());
147                 return 1L;
148             }
149             tmp += entry.getValue().getSRLGLength().toJava() / CELERITY;
150             LOG.info("In PceLink: The latency of link {} == {}", link.getLinkId(), tmp);
151         }
152         return (long) Math.ceil(tmp);
153     }
154
155     //Compute the OSNR of a span
156     public double calcSpanOSNR() {
157         if (this.omsAttributesSpan == null) {
158             return 0L;
159         }
160         Collection<LinkConcatenation> linkConcatenationList =
161             this.omsAttributesSpan.nonnullLinkConcatenation().values();
162         if (linkConcatenationList == null) {
163             LOG.error("in PceLink : Null field in the OmsAttrubtesSpan");
164             return 0L;
165         }
166         Iterator<LinkConcatenation> linkConcatenationiterator = linkConcatenationList.iterator();
167         if (!linkConcatenationiterator.hasNext()) {
168             return 0L;
169         }
170         // power on the output of the previous ROADM (dBm)
171         double pout = retrievePower(linkConcatenationiterator.next().getFiberType());
172         // span loss (dB)
173         double spanLoss = this.omsAttributesSpan.getSpanlossCurrent().getValue().doubleValue();
174         // power on the input of the current ROADM (dBm)
175         double pin = pout - spanLoss;
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         isValid = checkParams();
288         if ((this.omsAttributesSpan == null) && (this.linkType == OpenroadmLinkType.ROADMTOROADM)) {
289             isValid = false;
290             LOG.error("PceLink: Error reading Span for OMS link. Link is ignored {}", linkId);
291         }
292         if ((this.srlgList != null) && (this.srlgList.isEmpty())) {
293             isValid = false;
294             LOG.error("PceLink: Empty srlgList for OMS link. Link is ignored {}", linkId);
295         }
296         return isValid;
297     }
298
299     public boolean isOtnValid(Link link, String serviceType) {
300
301         if (this.linkType != OpenroadmLinkType.OTNLINK) {
302             LOG.error("PceLink: Not an OTN link. Link is ignored {}", linkId);
303             return false;
304         }
305
306         OtnLinkType otnLinkType = link
307             .augmentation(org.opendaylight.yang.gen.v1.http.transportpce.topology.rev201019.Link1.class)
308             .getOtnLinkType();
309         if (this.availableBandwidth == 0L) {
310             LOG.error("PceLink: No bandwidth available for OTN Link, link {}  is ignored ", linkId);
311             return false;
312         }
313
314         long neededBW;
315         OtnLinkType neededType = null;
316         switch (serviceType) {
317
318             case "ODU4":
319                 if (this.usedBandwidth != 0L) {
320                     return false;
321                 }
322                 neededBW = 100000L;
323                 neededType = OtnLinkType.OTU4;
324                 break;
325             case "ODU2":
326             case "ODU2e":
327                 neededBW = 12500L;
328                 break;
329             case "ODU0":
330                 neededBW = 1250L;
331                 break;
332             case "ODU1":
333                 neededBW = 2500L;
334                 break;
335             case "10GE":
336                 neededBW = 10000L;
337                 neededType = OtnLinkType.ODTU4;
338                 break;
339             case "1GE":
340                 neededBW = 1000L;
341                 neededType = OtnLinkType.ODTU4;
342                 break;
343             default:
344                 LOG.error("PceLink: isOtnValid Link {} unsupported serviceType {} ", linkId, serviceType);
345                 return false;
346         }
347
348         if ((this.availableBandwidth >= neededBW)
349             && ((neededType == null) || (neededType.equals(otnLinkType)))) {
350             LOG.info("PceLink: Selected Link {} has available bandwidth and is eligible for {} creation ",
351                 linkId, serviceType);
352         }
353
354         return checkParams();
355     }
356
357     private boolean checkParams() {
358         if ((this.linkId == null) || (this.linkType == null) || (this.oppositeLink == null)) {
359             LOG.error("PceLink: No Link type or opposite link is available. Link is ignored {}", linkId);
360             return false;
361         }
362         if ((this.sourceId == null) || (this.destId == null) || (this.sourceTP == null) || (this.destTP == null)) {
363             LOG.error("PceLink: No Link source or destination is available. Link is ignored {}", linkId);
364             return false;
365         }
366         if ((this.sourceNetworkSupNodeId.equals("")) || (this.destNetworkSupNodeId.equals(""))) {
367             LOG.error("PceLink: No Link source SuppNodeID or destination SuppNodeID is available. Link is ignored {}",
368                 linkId);
369             return false;
370         }
371         if ((this.sourceCLLI.equals("")) || (this.destCLLI.equals(""))) {
372             LOG.error("PceLink: No Link source CLLI or destination CLLI is available. Link is ignored {}", linkId);
373             return false;
374         }
375
376         return true;
377     }
378
379     @Override
380     public String toString() {
381         return "PceLink type=" + linkType + " ID=" + linkId.getValue() + " latency=" + latency;
382     }
383
384     private void writeObject(ObjectOutputStream out) throws IOException {
385         out.defaultWriteObject();
386         out.writeObject(this.sourceTP);
387         out.writeObject(this.destTP);
388     }
389
390     private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
391         in.defaultReadObject();
392         this.sourceTP = in.readObject();
393         this.destTP = in.readObject();
394     }
395 }