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.graph;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
16 import org.jgrapht.GraphPath;
17 import org.jgrapht.alg.shortestpath.KShortestSimplePaths;
18 import org.jgrapht.alg.shortestpath.PathValidator;
19 import org.jgrapht.graph.DefaultDirectedWeightedGraph;
20 import org.opendaylight.transportpce.common.ResponseCodes;
21 import org.opendaylight.transportpce.common.StringConstants;
22 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
23 import org.opendaylight.transportpce.pce.networkanalyzer.PceLink;
24 import org.opendaylight.transportpce.pce.networkanalyzer.PceNode;
25 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
26 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult.LocalCause;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class PceGraph {
33 private static final Logger LOG = LoggerFactory.getLogger(PceGraph.class);
35 ////////////////////////// for Graph ///////////////////////////
36 // how many paths to bring
37 private int kpathsToBring = 10;
40 private int mhopsPerPath = 50;
43 private Map<NodeId, PceNode> allPceNodes = new HashMap<>();
44 private PceNode apceNode = null;
45 private PceNode zpceNode = null;
46 private String serviceType = "";
48 PceConstraints pceHardConstraints;
49 PceConstraints pceSoftConstraints;
52 private PceResult pceResult = null;
53 private List<PceLink> shortestPathAtoZ = null;
55 // for path calculation
56 List<GraphPath<String, PceGraphEdge>> allWPaths = null;
58 private List<PceLink> pathAtoZ = new ArrayList<>();
60 public PceGraph(PceNode aendNode, PceNode zendNode, Map<NodeId, PceNode> allPceNodes,
61 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;
70 this.serviceType = serviceType;
72 LOG.info("In GraphCalculator: A and Z = {} / {} ", aendNode, zendNode);
73 LOG.debug("In GraphCalculator: allPceNodes size {}, nodes {} ", allPceNodes.size(), allPceNodes);
76 public boolean calcPath() {
78 LOG.info(" In PCE GRAPH calcPath : K SHORT PATHS algorithm ");
80 DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph =
81 new DefaultDirectedWeightedGraph<>(PceGraphEdge.class);
82 populateWithNodes(weightedGraph);
83 populateWithLinks(weightedGraph);
85 if (!runKgraphs(weightedGraph)) {
86 LOG.info("In calcPath : pceResult {}", pceResult);
90 // validate found paths
91 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
92 for (GraphPath<String, PceGraphEdge> path : allWPaths) {
93 PostAlgoPathValidator papv = new PostAlgoPathValidator();
94 pceResult = papv.checkPath(path, allPceNodes, pceResult, pceHardConstraints, serviceType);
95 LOG.info("In calcPath after PostAlgoPathValidator {} {}",
96 pceResult.getResponseCode(), ResponseCodes.RESPONSE_OK);
98 if (!pceResult.getResponseCode().equals(ResponseCodes.RESPONSE_OK)) {
99 LOG.info("In calcPath: post algo validations DROPPED the path {}", path);
105 for (PceGraphEdge edge : path.getEdgeList()) {
106 pathAtoZ.add(edge.link());
109 shortestPathAtoZ = new ArrayList<>(pathAtoZ);
110 if ((StringConstants.SERVICE_TYPE_100GE.equals(serviceType))
111 || (StringConstants.SERVICE_TYPE_OTU4.equals(serviceType))) {
112 LOG.info("In calcPath Path FOUND path for wl [{}], min Freq assignment {}, max Freq assignment {},"
113 + " hops {}, distance per metrics {}, path AtoZ {}",
114 pceResult.getResultWavelength(), pceResult.getMinFreq(), pceResult.getMaxFreq(),
115 pathAtoZ.size(), path.getWeight(), pathAtoZ);
118 // Service is at OTN layer and is relying on a supporting wavelength service
119 LOG.info("In calcPath Path FOUND path for hops {}, distance per metrics {}, path AtoZ {}",
120 pathAtoZ.size(), path.getWeight(), pathAtoZ);
126 if (shortestPathAtoZ != null) {
127 LOG.info("In calcPath CHOOSEN PATH for wl [{}], min freq {}, max freq {}, hops {}, path AtoZ {}",
128 pceResult.getResultWavelength(), pceResult.getMinFreq(), pceResult.getMaxFreq(),
129 shortestPathAtoZ.size(), shortestPathAtoZ);
131 LOG.info("In calcPath : pceResult {}", pceResult);
132 return (pceResult.getStatus());
135 private boolean runKgraphs(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
137 if (weightedGraph.edgeSet().isEmpty() || weightedGraph.vertexSet().isEmpty()) {
140 PathValidator<String, PceGraphEdge> wpv = new InAlgoPathValidator();
142 // KShortestPaths on weightedGraph
143 KShortestSimplePaths<String, PceGraphEdge> swp =
144 new KShortestSimplePaths<>(weightedGraph, mhopsPerPath, wpv);
145 allWPaths = swp.getPaths(apceNode.getNodeId().getValue(), zpceNode.getNodeId().getValue(), kpathsToBring);
147 if (allWPaths.isEmpty()) {
148 LOG.info(" In runKgraphs : algorithm didn't find any path");
149 pceResult.setLocalCause(LocalCause.NO_PATH_EXISTS);
150 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
155 for (GraphPath<String, PceGraphEdge> path : allWPaths) {
156 LOG.debug("path Weight: {} : {}", path.getWeight(), path.getVertexList());
162 private boolean validateLinkforGraph(PceLink pcelink) {
164 PceNode source = allPceNodes.get(pcelink.getSourceId());
165 PceNode dest = allPceNodes.get(pcelink.getDestId());
167 if (source == null) {
168 LOG.error("In addLinkToGraph link source node is null : {}", pcelink);
172 LOG.error("In addLinkToGraph link dest node is null : {}", pcelink);
175 LOG.debug("In addLinkToGraph link to nodes : {}{} {}", pcelink, source, dest);
179 private void populateWithNodes(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
180 Iterator<Map.Entry<NodeId, PceNode>> nodes = allPceNodes.entrySet().iterator();
181 while (nodes.hasNext()) {
182 Map.Entry<NodeId, PceNode> node = nodes.next();
183 weightedGraph.addVertex(node.getValue().getNodeId().getValue());
184 LOG.debug("In populateWithNodes in node : {}", node.getValue());
188 private boolean populateWithLinks(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
190 Iterator<Map.Entry<NodeId, PceNode>> nodes = allPceNodes.entrySet().iterator();
191 while (nodes.hasNext()) {
193 Map.Entry<NodeId, PceNode> node = nodes.next();
195 PceNode pcenode = node.getValue();
196 List<PceLink> links = pcenode.getOutgoingLinks();
198 LOG.debug("In populateGraph: use node for graph {}", pcenode);
200 for (PceLink link : links) {
201 LOG.debug("In populateGraph node {} : add edge to graph {}", pcenode, link);
203 if (!validateLinkforGraph(link)) {
207 PceGraphEdge graphLink = new PceGraphEdge(link);
208 weightedGraph.addEdge(link.getSourceId().getValue(), link.getDestId().getValue(), graphLink);
210 weightedGraph.setEdgeWeight(graphLink, chooseWeight(link));
216 private double chooseWeight(PceLink link) {
217 // HopCount is default
219 switch (pceHardConstraints.getPceMetrics()) {
222 LOG.debug("In PceGraph HopCount is used as a metrics. {}", link);
224 case PropagationDelay :
225 weight = link.getLatency();
226 LOG.debug("In PceGraph PropagationDelay is used as a metrics. {}", link);
227 if ((("1GE".equals(serviceType)) || ("10GE".equals(serviceType)) || ("ODU4".equals(serviceType)))
229 LOG.warn("PropagationDelay set as metric, but latency is null: is latency set for OTN link {}?",
233 // TODO implement IGPMetric and TEMetric - low priority.
237 LOG.warn("In PceGraph {} not implemented. HopCount works as a default",
238 pceHardConstraints.getPceMetrics());
244 public int getKpathsToBring() {
245 return kpathsToBring;
248 public void setKpathsToBring(int kpathsToBring) {
249 this.kpathsToBring = kpathsToBring;
252 public void setMhopsPerPath(int mhopsPerPath) {
253 this.mhopsPerPath = mhopsPerPath;
256 public List<PceLink> getPathAtoZ() {
257 return shortestPathAtoZ;
260 public PceResult getReturnStructure() {
264 public void setConstrains(PceConstraints pceHardConstraintsInput, PceConstraints pceSoftConstraintsInput) {
265 this.pceHardConstraints = pceHardConstraintsInput;
266 this.pceSoftConstraints = pceSoftConstraintsInput;