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