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.http.org.openroadm.common.state.types.rev191129.State;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class PceGraph {
34 private static final Logger LOG = LoggerFactory.getLogger(PceGraph.class);
36 ////////////////////////// for Graph ///////////////////////////
37 // how many paths to bring
38 private int kpathsToBring = 10;
41 private int mhopsPerPath = 50;
44 private Map<NodeId, PceNode> allPceNodes = new HashMap<>();
45 private PceNode apceNode = null;
46 private PceNode zpceNode = null;
47 private String serviceType = "";
49 PceConstraints pceHardConstraints;
50 PceConstraints pceSoftConstraints;
53 private PceResult pceResult = null;
54 private List<PceLink> shortestPathAtoZ = null;
56 // for path calculation
57 List<GraphPath<String, PceGraphEdge>> allWPaths = null;
59 private List<PceLink> pathAtoZ = new ArrayList<>();
61 public PceGraph(PceNode aendNode, PceNode zendNode, Map<NodeId, PceNode> allPceNodes,
62 PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult pceResult,
65 this.apceNode = aendNode;
66 this.zpceNode = zendNode;
67 this.allPceNodes = allPceNodes;
68 this.pceResult = pceResult;
69 this.pceHardConstraints = pceHardConstraints;
70 this.pceSoftConstraints = pceSoftConstraints;
71 this.serviceType = serviceType;
73 LOG.info("In GraphCalculator: A and Z = {} / {} ", aendNode, zendNode);
74 LOG.debug("In GraphCalculator: allPceNodes size {}, nodes {} ", allPceNodes.size(), allPceNodes);
77 public boolean calcPath() {
79 LOG.info(" In PCE GRAPH calcPath : K SHORT PATHS algorithm ");
81 DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph =
82 new DefaultDirectedWeightedGraph<>(PceGraphEdge.class);
83 populateWithNodes(weightedGraph);
84 populateWithLinks(weightedGraph);
86 if (!runKgraphs(weightedGraph)) {
87 LOG.info("In calcPath : pceResult {}", pceResult);
91 // validate found paths
92 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
93 for (GraphPath<String, PceGraphEdge> path : allWPaths) {
94 PostAlgoPathValidator papv = new PostAlgoPathValidator();
95 pceResult = papv.checkPath(path, allPceNodes, pceResult, pceHardConstraints, serviceType);
96 LOG.info("In calcPath after PostAlgoPathValidator {} {}",
97 pceResult.getResponseCode(), ResponseCodes.RESPONSE_OK);
99 if (!pceResult.getResponseCode().equals(ResponseCodes.RESPONSE_OK)) {
100 LOG.warn("In calcPath: post algo validations DROPPED the path {}", path);
106 for (PceGraphEdge edge : path.getEdgeList()) {
107 pathAtoZ.add(edge.link());
110 shortestPathAtoZ = new ArrayList<>(pathAtoZ);
111 switch (serviceType) {
113 case StringConstants.SERVICE_TYPE_100GE:
114 case StringConstants.SERVICE_TYPE_400GE:
115 case StringConstants.SERVICE_TYPE_OTU4:
117 "In calcPath Path FOUND path for wl [{}], min Freq assignment {}, max Freq assignment {},"
118 + " hops {}, distance per metrics {}, path AtoZ {}",
119 pceResult.getResultWavelength(), pceResult.getMinFreq(), pceResult.getMaxFreq(),
120 pathAtoZ.size(), path.getWeight(), pathAtoZ);
125 "In calcPath Path FOUND path for hops {}, distance per metrics {}, path AtoZ {}",
126 pathAtoZ.size(), path.getWeight(), pathAtoZ);
133 if (shortestPathAtoZ != null) {
134 LOG.info("In calcPath CHOOSEN PATH for wl [{}], min freq {}, max freq {}, hops {}, path AtoZ {}",
135 pceResult.getResultWavelength(), pceResult.getMinFreq(), pceResult.getMaxFreq(),
136 shortestPathAtoZ.size(), shortestPathAtoZ);
138 LOG.info("In calcPath : pceResult {}", pceResult);
139 return (pceResult.getStatus());
142 private boolean runKgraphs(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
144 if (weightedGraph.edgeSet().isEmpty() || weightedGraph.vertexSet().isEmpty()) {
147 PathValidator<String, PceGraphEdge> wpv = new InAlgoPathValidator();
149 // KShortestPaths on weightedGraph
150 KShortestSimplePaths<String, PceGraphEdge> swp =
151 new KShortestSimplePaths<>(weightedGraph, mhopsPerPath, wpv);
152 allWPaths = swp.getPaths(apceNode.getNodeId().getValue(), zpceNode.getNodeId().getValue(), kpathsToBring);
154 if (allWPaths.isEmpty()) {
155 LOG.info(" In runKgraphs : algorithm didn't find any path");
156 pceResult.setLocalCause(LocalCause.NO_PATH_EXISTS);
157 pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
162 for (GraphPath<String, PceGraphEdge> path : allWPaths) {
163 LOG.debug("path Weight: {} : {}", path.getWeight(), path.getVertexList());
169 private boolean validateLinkforGraph(PceLink pcelink) {
171 PceNode source = allPceNodes.get(pcelink.getSourceId());
172 PceNode dest = allPceNodes.get(pcelink.getDestId());
174 if (source == null) {
175 LOG.error("In addLinkToGraph link source node is null : {}", pcelink);
179 LOG.error("In addLinkToGraph link dest node is null : {}", pcelink);
182 LOG.debug("In addLinkToGraph link to nodes : {}{} {}", pcelink, source, dest);
186 private void populateWithNodes(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
187 Iterator<Map.Entry<NodeId, PceNode>> nodes = allPceNodes.entrySet().iterator();
188 while (nodes.hasNext()) {
189 Map.Entry<NodeId, PceNode> node = nodes.next();
190 if (State.InService.equals(node.getValue().getState())) {
191 weightedGraph.addVertex(node.getValue().getNodeId().getValue());
192 LOG.debug("In populateWithNodes in node : {}", node.getValue());
197 private boolean populateWithLinks(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
199 Iterator<Map.Entry<NodeId, PceNode>> nodes = allPceNodes.entrySet().iterator();
200 while (nodes.hasNext()) {
202 Map.Entry<NodeId, PceNode> node = nodes.next();
204 PceNode pcenode = node.getValue();
205 List<PceLink> links = pcenode.getOutgoingLinks();
207 LOG.debug("In populateGraph: use node for graph {}", pcenode);
209 for (PceLink link : links) {
210 LOG.debug("In populateGraph node {} : add edge to graph {}", pcenode, link);
212 if (!validateLinkforGraph(link)) {
215 if (State.InService.equals(link.getState())) {
216 PceGraphEdge graphLink = new PceGraphEdge(link);
217 weightedGraph.addEdge(link.getSourceId().getValue(), link.getDestId().getValue(), graphLink);
219 weightedGraph.setEdgeWeight(graphLink, chooseWeight(link));
226 private double chooseWeight(PceLink link) {
227 // HopCount is default
229 switch (pceHardConstraints.getPceMetrics()) {
232 LOG.debug("In PceGraph HopCount is used as a metrics. {}", link);
234 case PropagationDelay :
235 weight = link.getLatency();
236 LOG.debug("In PceGraph PropagationDelay is used as a metrics. {}", link);
238 && ("1GE".equals(serviceType) || "10GE".equals(serviceType) || "ODU4".equals(serviceType))) {
239 LOG.warn("PropagationDelay set as metric, but latency is null: is latency set for OTN link {}?",
243 // TODO implement IGPMetric and TEMetric - low priority.
247 LOG.warn("In PceGraph {} not implemented. HopCount works as a default",
248 pceHardConstraints.getPceMetrics());
254 public int getKpathsToBring() {
255 return kpathsToBring;
258 public void setKpathsToBring(int kpathsToBring) {
259 this.kpathsToBring = kpathsToBring;
262 public void setMhopsPerPath(int mhopsPerPath) {
263 this.mhopsPerPath = mhopsPerPath;
266 public List<PceLink> getPathAtoZ() {
267 return shortestPathAtoZ;
270 public PceResult getReturnStructure() {
274 public void setConstrains(PceConstraints pceHardConstraintsInput, PceConstraints pceSoftConstraintsInput) {
275 this.pceHardConstraints = pceHardConstraintsInput;
276 this.pceSoftConstraints = pceSoftConstraintsInput;