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) {
161 Iterator<Map.Entry<NodeId, PceNode>> nodes = allNodes.entrySet().iterator();
162 while (nodes.hasNext()) {
163 Map.Entry<NodeId, PceNode> node = nodes.next();
164 PceNode pcenode = node.getValue();
165 List<PceLink> links = pcenode.getOutgoingLinks();
166 LOG.info("In populateGraph: use node for graph {}", pcenode.toString());
167 for (PceLink link : links) {
168 LOG.info("In populateGraph: add edge to graph {}", link.toString());
169 addLinkToGraph(link);
175 private boolean populateGraph(List<PceNode> allNodes) {
179 for (PceNode node : allNodes) {
180 List<PceLink> links = node.getOutgoingLinks();
181 LOG.debug("In populateGraph: use node for graph {}", node.toString());
182 for (PceLink link : links) {
183 LOG.debug("In populateGraph: add edge to graph {}", link.toString());
184 addLinkToGraph(link);
192 private boolean runGraph() {
193 LOG.info("In runGraph Vertices: {}; Eges: {} ", this.nwGraph.getVertexCount(), this.nwGraph.getEdgeCount());
195 this.pathAtoZ = null;
198 this.shortestPath = calcAlgo();
200 if (this.shortestPath == null) {
201 this.noPathExists = true;
202 LOG.error("In runGraph: shortest path alg is null ");// ,
206 this.pathAtoZ = this.shortestPath.getPath(this.apceNode, this.zpceNode);
208 if ((this.pathAtoZ == null) || (this.pathAtoZ.size() == 0)) {
209 LOG.info("In runGraph: AtoZ path is empty");
210 this.pceResult.setLocalCause(LocalCause.NO_PATH_EXISTS);
214 pathMetricsToCompare();
216 return compareMaxLatency();
218 } catch (IllegalArgumentException e) {
219 LOG.error("In runGraph: can't calculate the path. A or Z node don't have any links {}", e);
220 this.noPathExists = true;
226 private DijkstraShortestPath<PceNode, PceLink> calcAlgo() {
228 Transformer<PceLink, Double> wtTransformer = new Transformer<PceLink, Double>() {
230 public Double transform(PceLink link) {
231 return link.getLatency();
235 this.shortestPath = null;
237 switch (this.pceHardConstraints.getPceMetrics()) {
238 case PropagationDelay:
239 this.shortestPath = new DijkstraShortestPath<>(this.nwGraph, wtTransformer);
240 LOG.debug("In calcShortestPath: PropagationDelay method run ");
243 this.shortestPath = new DijkstraShortestPath<>(this.nwGraph);
244 LOG.debug("In calcShortestPath: HopCount method run ");
248 this.shortestPath = new DijkstraShortestPath<>(this.nwGraph);
249 LOG.warn("In calcShortestPath: instead IGPMetric/TEMetric method Hop-Count runs as a default ");
253 return this.shortestPath;
257 private void addLinkToGraph(PceLink pcelink) {
259 PceNode source = this.allPceNodes.get(pcelink.getSourceId());
260 PceNode dest = this.allPceNodes.get(pcelink.getDestId());
262 if (source == null) {
263 LOG.error("In addLinkToGraph link source node is null : {}", pcelink.toString());
267 LOG.error("In addLinkToGraph link dest node is null : {}", pcelink.toString());
271 LOG.debug("In addLinkToGraph link and nodes : {} ; {} / {}", pcelink.toString(), source.toString(),
273 this.nwGraph.addEdge(pcelink, source, dest);
278 * "QUICK" approach build shortest path. and then look for a single wavelength
281 private boolean chooseWavelength() {
282 for (long i = 1; i <= MAX_WAWELENGTH; i++) {
283 boolean completed = true;
284 for (PceLink link : this.pathAtoZ) {
285 PceNode pceNode = this.allPceNodes.get(link.getSourceId());
286 if (!pceNode.checkWL(i)) {
292 this.pceResult.setResultWavelength(i);
296 return (this.pceResult.getResultWavelength() > 0);
299 public List<PceLink> getPathAtoZ() {
300 return this.shortestPathAtoZ;
303 public PceResult getReturnStructure() {
304 return this.pceResult;
307 // TODO build ordered set ordered per the index. Current assumption is that
308 // wavelenght serves as an index
309 private class ListOfNodes {
310 private List<PceNode> listOfNodes = new ArrayList<PceNode>();
312 private void addNodetoWL(PceNode node) {
313 this.listOfNodes.add(node);
316 private List<PceNode> getNodes() {
317 return this.listOfNodes;
322 private boolean extractWLs(Map<NodeId, PceNode> allNodes) {
324 Iterator<Map.Entry<NodeId, PceNode>> nodes = allNodes.entrySet().iterator();
325 while (nodes.hasNext()) {
327 Map.Entry<NodeId, PceNode> node = nodes.next();
329 PceNode pcenode = node.getValue();
330 List<Long> wls = pcenode.getAvailableWLs();
332 LOG.debug("In extractWLs wls in node : {} {}", pcenode.toString(), wls.size());
333 LOG.debug("In extractWLs listOfWLs total : {}", this.listOfNodesPerWL.size());
335 LOG.debug("In extractWLs i in wls : {}", i);
336 ListOfNodes lwl = this.listOfNodesPerWL.get(i.intValue());
337 lwl.addNodetoWL(pcenode);
344 private void cleanupGraph() {
345 LOG.debug("In cleanupGraph remove {} nodes ", this.nwGraph.getEdgeCount());
346 Iterable<PceNode> toRemove = new ArrayList<PceNode>(this.nwGraph.getVertices());
347 for (PceNode node : toRemove) {
348 this.nwGraph.removeVertex(node);
350 LOG.debug("In cleanupGraph after {} removed ", this.nwGraph.getEdgeCount());
353 private void analyzeResult() {
354 // very simple for the start
356 if (this.pceResult.getStatus()) {
360 // if request is rejected but at least once there was path found, try to save
361 // the real reason of reject
362 if (this.foundButTooHighLatency) {
363 this.pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
364 this.pceResult.setLocalCause(LocalCause.TOO_HIGH_LATENCY);
365 this.pceResult.setCalcMessage("No path available due to constraint Hard/Latency");
370 private boolean compareMaxLatency() {
372 Long latencyConstraint = this.pceHardConstraints.getMaxLatency();
374 if ((latencyConstraint > 0) && (this.tmpAtozLatency > latencyConstraint)) {
375 this.foundButTooHighLatency = true;
376 this.pceResult.setLocalCause(LocalCause.TOO_HIGH_LATENCY);
377 LOG.info("In validateLatency: AtoZ path has too high LATENCY {} > {}", this.tmpAtozLatency,
381 LOG.info("In validateLatency: AtoZ path is {}", this.pathAtoZ.toString());
385 private void pathMetricsToCompare() {
387 this.tmpAtozDistance = this.shortestPath.getDistance(this.apceNode, this.zpceNode).intValue();
389 // TODO this code is for HopCount. excluded from switch for not implemented
390 // IGPMetric and TEMetric
391 this.tmpAtozLatency = 0;
392 for (PceLink pcelink : this.pathAtoZ) {
393 this.tmpAtozLatency = this.tmpAtozLatency + pcelink.getLatency().intValue();
396 switch (this.pceHardConstraints.getPceMetrics()) {
398 // TODO implement IGPMetric - low priority
399 LOG.error("In PceGraph not implemented IGPMetric. HopCount works as a default");
403 // TODO implement TEMetric - low priority
404 LOG.error("In PceGraph not implemented TEMetric. HopCount works as a default");
410 case PropagationDelay:
411 this.tmpAtozLatency = this.tmpAtozDistance;
415 LOG.error("In PceGraph {}: unknown metric. ", this.pceHardConstraints.getPceMetrics());
419 LOG.info("In runGraph: AtoZ size {}, distance {}, latency {} ", this.pathAtoZ.size(), this.tmpAtozDistance,
420 this.tmpAtozLatency);
421 LOG.debug("In runGraph: AtoZ {}", this.pathAtoZ.toString());
426 private void rememberPath(int index) {
427 this.minFoundDistance = this.tmpAtozDistance;
428 this.shortestPathAtoZ = this.pathAtoZ;
429 this.pceResult.setResultWavelength(Long.valueOf(index));
430 this.pceResult.setRC(ResponseCodes.RESPONSE_OK);
431 LOG.info("In GraphCalculator FUll CalcPath for wl [{}]: found AtoZ {}", index, this.pathAtoZ.toString());
435 public void setConstrains(PceConstraints pceHardConstraintsIn, PceConstraints pceSoftConstraintsIn) {
436 this.pceHardConstraints = pceHardConstraintsIn;
437 this.pceSoftConstraints = pceSoftConstraintsIn;