2 * Copyright © 2017 AT&T, Inc. and others. All rights reserved.
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
9 package org.opendaylight.transportpce.pce;
11 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
12 import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
18 import org.apache.commons.collections15.Transformer;
19 import org.opendaylight.transportpce.common.ResponseCodes;
20 import org.opendaylight.transportpce.pce.PceResult.LocalCause;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 public class PceGraph {
28 private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
30 ////////////////////////// for Graph ///////////////////////////
31 private DirectedSparseMultigraph<PceNode, PceLink> nwGraph = new DirectedSparseMultigraph<PceNode, PceLink>();
32 private DijkstraShortestPath<PceNode, PceLink> shortestPath = null;
35 private Map<NodeId, PceNode> allPceNodes = new HashMap<NodeId, PceNode>();
36 private PceNode apceNode = null;
37 private PceNode zpceNode = null;
39 PceConstraints pceHardConstraints;
40 PceConstraints pceSoftConstraints;
43 private PceResult pceResult = null;
44 private List<PceLink> shortestPathAtoZ = null;
47 private static final int MAX_WAWELENGTH = 96;
49 // for path calculation
50 private List<PceLink> pathAtoZ = null;
51 private int minFoundDistance;
52 private int tmpAtozDistance = 0;
53 private int tmpAtozLatency = 0;
54 private int bestDistance;
55 private boolean noPathExists = false;
57 private boolean foundButTooHighLatency = false;
59 private List<ListOfNodes> listOfNodesPerWL = new ArrayList<ListOfNodes>();
61 public PceGraph(PceNode aendNode, PceNode zendNode, Map<NodeId, PceNode> allPceNodes,
62 PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult pceResult) {
64 this.apceNode = aendNode;
65 this.zpceNode = zendNode;
66 this.allPceNodes = allPceNodes;
67 this.pceResult = pceResult;
68 this.pceHardConstraints = pceHardConstraints;
69 this.pceSoftConstraints = pceSoftConstraints;
71 // TODO - fix the assumption that wavelengths are from 1 to 96 and can be used
73 this.listOfNodesPerWL.add(new ListOfNodes());
74 for (int i = 1; i <= MAX_WAWELENGTH; i++) {
75 // create list of nodes per wavelength
76 ListOfNodes wls = new ListOfNodes();
77 this.listOfNodesPerWL.add(wls);
80 LOG.debug("In GraphCalculator: A and Z = {} / {}", aendNode.toString(), zendNode.toString());
81 LOG.debug("In GraphCalculator: allPceNodes = {}", allPceNodes.toString());
84 public boolean calcPath() {
86 LOG.info("In calcPath: metric {} is used ", this.pceHardConstraints.getPceMetrics());
88 populateGraph(this.allPceNodes);
90 LOG.info(" In PCE GRAPH : QUICK algorithm ");
95 this.bestDistance = this.tmpAtozDistance;
97 if (chooseWavelength()) {
98 this.pceResult.setRC(ResponseCodes.RESPONSE_OK);
99 this.shortestPathAtoZ = this.pathAtoZ;
100 LOG.info("In GraphCalculator QUICK CalcPath: AtoZ {}", this.pathAtoZ.toString());
101 LOG.info("In GraphCalculator QUICK CalcPath: pceResult {}", this.pceResult.toString());
105 // continue work per wavelength
106 LOG.warn(" In PCE GRAPH : QUICK algorithm didn't find shared wavelength over the shortest path");
110 LOG.warn(" In PCE GRAPH : QUICK algorithm didn't find shortest path with single wavelength");
111 if (this.noPathExists) {
112 // quick algo looks for path independently on wavelength. therefore no path
113 // means fatal problem
114 LOG.warn(" In PCE GRAPH : QUICK algorithm didn't find any path");
115 this.pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
119 // rearrange all nodes per the relevant wavelength indexes
120 extractWLs(this.allPceNodes);
122 this.pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
123 boolean firstPath = true;
125 for (int i = 1; i <= MAX_WAWELENGTH; i++) {
126 LOG.info(" In PCE GRAPH : FUll algorithm for WL {}", i);
127 List<PceNode> nodes = this.listOfNodesPerWL.get(i).getNodes();
128 populateGraph(nodes);
135 // set minFoundDistance for the first time
140 if (this.tmpAtozDistance < this.minFoundDistance) {
144 if (this.tmpAtozDistance == this.bestDistance) {
145 // optimization: stop on the first WL with result == the best
150 // return codes can come in different orders. this method fixes it a bit
151 // TODO build it better
154 LOG.info("In GraphCalculator FUll CalcPath: pceResult {}", this.pceResult.toString());
155 return (this.pceResult.getStatus());
158 private boolean populateGraph(Map<NodeId, PceNode> allNodes) {
162 Iterator<Map.Entry<NodeId, PceNode>> nodes = allNodes.entrySet().iterator();
163 while (nodes.hasNext()) {
165 Map.Entry<NodeId, PceNode> node = nodes.next();
167 PceNode pcenode = node.getValue();
168 List<PceLink> links = pcenode.getOutgoingLinks();
170 LOG.info("In populateGraph: use node for graph {}", pcenode.toString());
172 for (PceLink link : links) {
173 LOG.info("In populateGraph: add edge to graph {}", link.toString());
174 addLinkToGraph(link);
183 private boolean populateGraph(List<PceNode> allNodes) {
187 for (PceNode node : allNodes) {
188 List<PceLink> links = node.getOutgoingLinks();
190 LOG.debug("In populateGraph: use node for graph {}", node.toString());
192 for (PceLink link : links) {
193 LOG.debug("In populateGraph: add edge to graph {}", link.toString());
194 addLinkToGraph(link);
203 private boolean runGraph() {
204 LOG.info("In runGraph Vertices: {}; Eges: {} ", this.nwGraph.getVertexCount(), this.nwGraph.getEdgeCount());
206 this.pathAtoZ = null;
209 this.shortestPath = calcAlgo();
211 if (this.shortestPath == null) {
212 this.noPathExists = true;
213 LOG.error("In runGraph: shortest path alg is null ");// ,
217 this.pathAtoZ = this.shortestPath.getPath(this.apceNode, this.zpceNode);
219 if ((this.pathAtoZ == null) || (this.pathAtoZ.size() == 0)) {
220 LOG.info("In runGraph: AtoZ path is empty");
221 this.pceResult.setLocalCause(LocalCause.NO_PATH_EXISTS);
225 pathMetricsToCompare();
227 return compareMaxLatency();
229 } catch (IllegalArgumentException e) {
230 LOG.error("In runGraph: can't calculate the path. A or Z node don't have any links {}", e);
231 this.noPathExists = true;
237 private DijkstraShortestPath<PceNode, PceLink> calcAlgo() {
239 Transformer<PceLink, Double> wtTransformer = new Transformer<PceLink, Double>() {
241 public Double transform(PceLink link) {
242 return link.getLatency();
246 this.shortestPath = null;
248 switch (this.pceHardConstraints.getPceMetrics()) {
249 case PropagationDelay:
250 this.shortestPath = new DijkstraShortestPath<>(this.nwGraph, wtTransformer);
251 LOG.debug("In calcShortestPath: PropagationDelay method run ");
254 this.shortestPath = new DijkstraShortestPath<>(this.nwGraph);
255 LOG.debug("In calcShortestPath: HopCount method run ");
259 this.shortestPath = new DijkstraShortestPath<>(this.nwGraph);
260 LOG.warn("In calcShortestPath: instead IGPMetric/TEMetric method Hop-Count runs as a default ");
264 return this.shortestPath;
268 private void addLinkToGraph(PceLink pcelink) {
270 PceNode source = this.allPceNodes.get(pcelink.getSourceId());
271 PceNode dest = this.allPceNodes.get(pcelink.getDestId());
273 if (source == null) {
274 LOG.error("In addLinkToGraph link source node is null : {}", pcelink.toString());
278 LOG.error("In addLinkToGraph link dest node is null : {}", pcelink.toString());
282 LOG.debug("In addLinkToGraph link and nodes : {} ; {} / {}", pcelink.toString(), source.toString(),
284 this.nwGraph.addEdge(pcelink, source, dest);
289 * "QUICK" approach build shortest path. and then look for a single wavelength
292 private boolean chooseWavelength() {
293 for (long i = 1; i <= MAX_WAWELENGTH; i++) {
294 boolean completed = true;
295 for (PceLink link : this.pathAtoZ) {
296 PceNode pceNode = this.allPceNodes.get(link.getSourceId());
297 if (!pceNode.checkWL(i)) {
303 this.pceResult.setResultWavelength(i);
307 return (this.pceResult.getResultWavelength() > 0);
310 public List<PceLink> getPathAtoZ() {
311 return this.shortestPathAtoZ;
314 public PceResult getReturnStructure() {
315 return this.pceResult;
318 // TODO build ordered set ordered per the index. Current assumption is that
319 // wavelenght serves as an index
320 private class ListOfNodes {
321 private List<PceNode> listOfNodes = new ArrayList<PceNode>();
323 private void addNodetoWL(PceNode node) {
324 this.listOfNodes.add(node);
327 private List<PceNode> getNodes() {
328 return this.listOfNodes;
333 private boolean extractWLs(Map<NodeId, PceNode> allNodes) {
335 Iterator<Map.Entry<NodeId, PceNode>> nodes = allNodes.entrySet().iterator();
336 while (nodes.hasNext()) {
338 Map.Entry<NodeId, PceNode> node = nodes.next();
340 PceNode pcenode = node.getValue();
341 List<Long> wls = pcenode.getAvailableWLs();
343 LOG.debug("In extractWLs wls in node : {} {}", pcenode.toString(), wls.size());
344 LOG.debug("In extractWLs listOfWLs total : {}", this.listOfNodesPerWL.size());
346 LOG.debug("In extractWLs i in wls : {}", i);
347 ListOfNodes lwl = this.listOfNodesPerWL.get(i.intValue());
348 lwl.addNodetoWL(pcenode);
355 private void cleanupGraph() {
356 LOG.debug("In cleanupGraph remove {} nodes ", this.nwGraph.getEdgeCount());
357 Iterable<PceNode> toRemove = new ArrayList<PceNode>(this.nwGraph.getVertices());
358 for (PceNode node : toRemove) {
359 this.nwGraph.removeVertex(node);
361 LOG.debug("In cleanupGraph after {} removed ", this.nwGraph.getEdgeCount());
364 private void analyzeResult() {
365 // very simple for the start
367 if (this.pceResult.getStatus()) {
371 // if request is rejected but at least once there was path found, try to save
372 // the real reason of reject
373 if (this.foundButTooHighLatency) {
374 this.pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
375 this.pceResult.setLocalCause(LocalCause.TOO_HIGH_LATENCY);
376 this.pceResult.setCalcMessage("No path available due to constraint Hard/Latency");
381 private boolean compareMaxLatency() {
383 Long latencyConstraint = this.pceHardConstraints.getMaxLatency();
385 if (latencyConstraint > 0) {
386 if (this.tmpAtozLatency > latencyConstraint) {
387 this.foundButTooHighLatency = true;
388 this.pceResult.setLocalCause(LocalCause.TOO_HIGH_LATENCY);
389 LOG.info("In validateLatency: AtoZ path has too high LATENCY {} > {}", this.tmpAtozLatency,
394 LOG.info("In validateLatency: AtoZ path is {}", this.pathAtoZ.toString());
398 private void pathMetricsToCompare() {
400 this.tmpAtozDistance = this.shortestPath.getDistance(this.apceNode, this.zpceNode).intValue();
402 // TODO this code is for HopCount. excluded from switch for not implemented
403 // IGPMetric and TEMetric
404 this.tmpAtozLatency = 0;
405 for (PceLink pcelink : this.pathAtoZ) {
406 this.tmpAtozLatency = this.tmpAtozLatency + pcelink.getLatency().intValue();
409 switch (this.pceHardConstraints.getPceMetrics()) {
411 // TODO implement IGPMetric - low priority
412 LOG.error("In PceGraph not implemented IGPMetric. HopCount works as a default");
416 // TODO implement TEMetric - low priority
417 LOG.error("In PceGraph not implemented TEMetric. HopCount works as a default");
423 case PropagationDelay:
424 this.tmpAtozLatency = this.tmpAtozDistance;
428 LOG.error("In PceGraph {}: unknown metric. ", this.pceHardConstraints.getPceMetrics());
432 LOG.info("In runGraph: AtoZ size {}, distance {}, latency {} ", this.pathAtoZ.size(), this.tmpAtozDistance,
433 this.tmpAtozLatency);
434 LOG.debug("In runGraph: AtoZ {}", this.pathAtoZ.toString());
439 private void rememberPath(int index) {
440 this.minFoundDistance = this.tmpAtozDistance;
441 this.shortestPathAtoZ = this.pathAtoZ;
442 this.pceResult.setResultWavelength(Long.valueOf(index));
443 this.pceResult.setRC(ResponseCodes.RESPONSE_OK);
444 LOG.info("In GraphCalculator FUll CalcPath for wl [{}]: found AtoZ {}", index, this.pathAtoZ.toString());
448 public void setConstrains(PceConstraints pceHardConstraintsIn, PceConstraints pceSoftConstraintsIn) {
449 this.pceHardConstraints = pceHardConstraintsIn;
450 this.pceSoftConstraints = pceSoftConstraintsIn;