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.pce.constraints.PceConstraints;
22 import org.opendaylight.transportpce.pce.networkanalyzer.PceLink;
23 import org.opendaylight.transportpce.pce.networkanalyzer.PceNode;
24 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
25 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult.LocalCause;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class PceGraph {
32 private static final Logger LOG = LoggerFactory.getLogger(PceGraph.class);
34 ////////////////////////// for Graph ///////////////////////////
35 // how many paths to bring
36 private int kpathsToBring = 10;
39 private int mhopsPerPath = 50;
42 private Map<NodeId, PceNode> allPceNodes = new HashMap<>();
43 private PceNode apceNode = null;
44 private PceNode zpceNode = null;
45 private String serviceType = "";
47 PceConstraints pceHardConstraints;
48 PceConstraints pceSoftConstraints;
51 private PceResult pceResult = null;
52 private List<PceLink> shortestPathAtoZ = null;
54 // for path calculation
55 List<GraphPath<String, PceGraphEdge>> allWPaths = null;
57 private List<PceLink> pathAtoZ = new ArrayList<>();
59 public PceGraph(PceNode aendNode, PceNode zendNode, Map<NodeId, PceNode> allPceNodes,
60 PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult pceResult,
63 this.apceNode = aendNode;
64 this.zpceNode = zendNode;
65 this.allPceNodes = allPceNodes;
66 this.pceResult = pceResult;
67 this.pceHardConstraints = pceHardConstraints;
68 this.pceSoftConstraints = pceSoftConstraints;
69 this.serviceType = serviceType;
71 LOG.info("In GraphCalculator: A and Z = {} / {} ", aendNode, zendNode);
72 LOG.debug("In GraphCalculator: allPceNodes size {}, nodes {} ", allPceNodes.size(), allPceNodes);
75 public boolean calcPath() {
77 LOG.info(" In PCE GRAPH calcPath : K SHORT PATHS algorithm ");
79 DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph =
80 new DefaultDirectedWeightedGraph<>(PceGraphEdge.class);
81 populateWithNodes(weightedGraph);
82 populateWithLinks(weightedGraph);
84 if (!runKgraphs(weightedGraph)) {
85 LOG.info("In calcPath : pceResult {}", pceResult);
89 // validate found paths
90 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
91 for (GraphPath<String, PceGraphEdge> path : allWPaths) {
92 PostAlgoPathValidator papv = new PostAlgoPathValidator();
93 pceResult = papv.checkPath(path, allPceNodes, pceResult, pceHardConstraints, serviceType);
94 LOG.info("In calcPath after PostAlgoPathValidator {} {}",
95 pceResult.getResponseCode(), ResponseCodes.RESPONSE_OK);
97 if (!pceResult.getResponseCode().equals(ResponseCodes.RESPONSE_OK)) {
98 LOG.info("In calcPath: post algo validations DROPPED the path {}", path);
104 for (PceGraphEdge edge : path.getEdgeList()) {
105 pathAtoZ.add(edge.link());
108 shortestPathAtoZ = new ArrayList<>(pathAtoZ);
109 if (("100GE".equals(serviceType)) || ("OTU4".equals(serviceType))) {
110 LOG.info("In calcPath Path FOUND path for wl [{}], hops {}, distance per metrics {}, path AtoZ {}",
111 pceResult.getResultWavelength(), pathAtoZ.size(), path.getWeight(), pathAtoZ);
114 // Service is at OTN layer and is relying on a supporting wavelength service
115 LOG.info("In calcPath Path FOUND path for hops {}, distance per metrics {}, path AtoZ {}",
116 pathAtoZ.size(), path.getWeight(), pathAtoZ);
122 if (shortestPathAtoZ != null) {
123 LOG.info("In calcPath CHOOSEN PATH for wl [{}], hops {}, path AtoZ {}",
124 pceResult.getResultWavelength(), shortestPathAtoZ.size(), shortestPathAtoZ);
126 LOG.info("In calcPath : pceResult {}", pceResult);
127 return (pceResult.getStatus());
130 private boolean runKgraphs(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
132 if (weightedGraph.edgeSet().isEmpty() || weightedGraph.vertexSet().isEmpty()) {
135 PathValidator<String, PceGraphEdge> wpv = new InAlgoPathValidator();
137 // KShortestPaths on weightedGraph
138 KShortestSimplePaths<String, PceGraphEdge> swp =
139 new KShortestSimplePaths<>(weightedGraph, mhopsPerPath, wpv);
140 allWPaths = swp.getPaths(apceNode.getNodeId().getValue(), zpceNode.getNodeId().getValue(), kpathsToBring);
142 if (allWPaths.isEmpty()) {
143 LOG.info(" In runKgraphs : algorithm didn't find any path");
144 pceResult.setLocalCause(LocalCause.NO_PATH_EXISTS);
145 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
150 for (GraphPath<String, PceGraphEdge> path : allWPaths) {
151 LOG.debug("path Weight: {} : {}", path.getWeight(), path.getVertexList());
157 private boolean validateLinkforGraph(PceLink pcelink) {
159 PceNode source = allPceNodes.get(pcelink.getSourceId());
160 PceNode dest = allPceNodes.get(pcelink.getDestId());
162 if (source == null) {
163 LOG.error("In addLinkToGraph link source node is null : {}", pcelink);
167 LOG.error("In addLinkToGraph link dest node is null : {}", pcelink);
170 LOG.debug("In addLinkToGraph link to nodes : {}{} {}", pcelink, source, dest);
174 private void populateWithNodes(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
175 Iterator<Map.Entry<NodeId, PceNode>> nodes = allPceNodes.entrySet().iterator();
176 while (nodes.hasNext()) {
177 Map.Entry<NodeId, PceNode> node = nodes.next();
178 weightedGraph.addVertex(node.getValue().getNodeId().getValue());
179 LOG.debug("In populateWithNodes in node : {}", node.getValue());
183 private boolean populateWithLinks(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
185 Iterator<Map.Entry<NodeId, PceNode>> nodes = allPceNodes.entrySet().iterator();
186 while (nodes.hasNext()) {
188 Map.Entry<NodeId, PceNode> node = nodes.next();
190 PceNode pcenode = node.getValue();
191 List<PceLink> links = pcenode.getOutgoingLinks();
193 LOG.debug("In populateGraph: use node for graph {}", pcenode);
195 for (PceLink link : links) {
196 LOG.debug("In populateGraph node {} : add edge to graph {}", pcenode, link);
198 if (!validateLinkforGraph(link)) {
202 PceGraphEdge graphLink = new PceGraphEdge(link);
203 weightedGraph.addEdge(link.getSourceId().getValue(), link.getDestId().getValue(), graphLink);
205 weightedGraph.setEdgeWeight(graphLink, chooseWeight(link));
211 private double chooseWeight(PceLink link) {
212 // HopCount is default
214 switch (pceHardConstraints.getPceMetrics()) {
217 LOG.debug("In PceGraph HopCount is used as a metrics. {}", link);
219 case PropagationDelay :
220 weight = link.getLatency();
221 LOG.debug("In PceGraph PropagationDelay is used as a metrics. {}", link);
222 if ((("1GE".equals(serviceType)) || ("10GE".equals(serviceType)) || ("ODU4".equals(serviceType)))
224 LOG.warn("PropagationDelay set as metric, but latency is null: is latency set for OTN link {}?",
228 // TODO implement IGPMetric and TEMetric - low priority.
232 LOG.warn("In PceGraph {} not implemented. HopCount works as a default",
233 pceHardConstraints.getPceMetrics());
239 public int getKpathsToBring() {
240 return kpathsToBring;
243 public void setKpathsToBring(int kpathsToBring) {
244 this.kpathsToBring = kpathsToBring;
247 public void setMhopsPerPath(int mhopsPerPath) {
248 this.mhopsPerPath = mhopsPerPath;
251 public List<PceLink> getPathAtoZ() {
252 return shortestPathAtoZ;
255 public PceResult getReturnStructure() {
259 public void setConstrains(PceConstraints pceHardConstraintsInput, PceConstraints pceSoftConstraintsInput) {
260 this.pceHardConstraints = pceHardConstraintsInput;
261 this.pceSoftConstraints = pceSoftConstraintsInput;