blob: 5600e0c23c2036b46fc44d0276f21c247d389f2c [file] [log] [blame]
Toshio Koidec87810e2014-02-11 13:03:21 -08001package net.onrc.onos.intent.runtime;
2
Toshio Koide27ffd412014-02-18 19:15:27 -08003import java.util.HashMap;
Toshio Koidec87810e2014-02-11 13:03:21 -08004
Toshio Koide4f308732014-02-18 15:19:48 -08005import net.floodlightcontroller.core.module.IFloodlightService;
Toshio Koidec87810e2014-02-11 13:03:21 -08006import net.onrc.onos.intent.ConstrainedBFSTree;
7import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Toshio Koide0c9106d2014-02-19 15:26:38 -08008import net.onrc.onos.intent.Intent;
Toshio Koide4f308732014-02-18 15:19:48 -08009import net.onrc.onos.intent.IntentOperation;
Toshio Koided9fa2a82014-02-19 17:35:18 -080010import net.onrc.onos.intent.IntentOperation.Operator;
Toshio Koide4f308732014-02-18 15:19:48 -080011import net.onrc.onos.intent.IntentOperationList;
Toshio Koidec87810e2014-02-11 13:03:21 -080012import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080013import net.onrc.onos.intent.PathIntentMap;
Toshio Koidec87810e2014-02-11 13:03:21 -080014import net.onrc.onos.intent.ShortestPathIntent;
15import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
16import net.onrc.onos.ofcontroller.networkgraph.Path;
17import net.onrc.onos.ofcontroller.networkgraph.Switch;
18
Toshio Koideb39c9d32014-02-20 01:21:47 -080019import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21
Toshio Koidec87810e2014-02-11 13:03:21 -080022/**
23 * @author Toshio Koide (t-koide@onlab.us)
24 */
Toshio Koide4f308732014-02-18 15:19:48 -080025public class PathCalcRuntime implements IFloodlightService {
26 private NetworkGraph graph;
Toshio Koideb39c9d32014-02-20 01:21:47 -080027 private final static Logger log = LoggerFactory.getLogger(PathCalcRuntime.class);
Toshio Koidec87810e2014-02-11 13:03:21 -080028 public PathCalcRuntime(NetworkGraph g) {
29 this.graph = g;
30 }
31
Toshio Koide27ffd412014-02-18 19:15:27 -080032 /**
33 * calculate shortest-path and constrained-shortest-path intents into low-level path intents
34 * @param intentOpList IntentOperationList having instances of ShortestPathIntent/ConstrainedShortestPathIntent
35 * @param pathIntents a set of current low-level intents
36 * @return IntentOperationList for PathIntent instances
37 */
38 public IntentOperationList calcPathIntents(IntentOperationList intentOpList, PathIntentMap pathIntents) {
39 IntentOperationList pathIntentOpList = new IntentOperationList();
40 HashMap<Switch, ConstrainedBFSTree> spfTrees = new HashMap<>();
Toshio Koidec87810e2014-02-11 13:03:21 -080041
Toshio Koide27ffd412014-02-18 19:15:27 -080042 for (IntentOperation intentOp: intentOpList) {
43 switch (intentOp.operator) {
44 case ADD:
45 if (!(intentOp.intent instanceof ShortestPathIntent)) {
Toshio Koideb39c9d32014-02-20 01:21:47 -080046 log.error("unsupported intent type: {}", intentOp.intent.getClass().getName());
Toshio Koide27ffd412014-02-18 19:15:27 -080047 // TODO should push back the intent to caller
48 continue;
49 }
Toshio Koidec87810e2014-02-11 13:03:21 -080050
Toshio Koide27ffd412014-02-18 19:15:27 -080051 ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent;
52 Switch srcSwitch = graph.getSwitch(spIntent.getSrcSwitchDpid());
53 Switch dstSwitch = graph.getSwitch(spIntent.getDstSwitchDpid());
54 if (srcSwitch == null || dstSwitch == null) {
Toshio Koideb39c9d32014-02-20 01:21:47 -080055 log.error("Switch not found: {}, {}",
56 spIntent.getSrcSwitchDpid(),
57 spIntent.getDstSwitchDpid());
Toshio Koide27ffd412014-02-18 19:15:27 -080058 // TODO should push back the intent to caller
59 continue;
60 }
Toshio Koidec87810e2014-02-11 13:03:21 -080061
Toshio Koide27ffd412014-02-18 19:15:27 -080062 double bandwidth = 0.0;
63 ConstrainedBFSTree tree = null;
64 if (intentOp.intent instanceof ConstrainedShortestPathIntent) {
65 bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth();
66 tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth);
67 }
68 else {
69 tree = spfTrees.get(srcSwitch);
70 if (tree == null) {
71 tree = new ConstrainedBFSTree(srcSwitch);
72 spfTrees.put(srcSwitch, tree);
73 }
74 }
75 Path path = tree.getPath(dstSwitch);
76 if (path == null) {
Toshio Koideb39c9d32014-02-20 01:21:47 -080077 log.error("Path not found: {}", intentOp.intent.toString());
Toshio Koide27ffd412014-02-18 19:15:27 -080078 // TODO should push back the intent to caller
79 continue;
80 }
81 PathIntent pathIntent = new PathIntent("pi" + intentOp.intent.getId(), path, bandwidth, intentOp.intent);
82 pathIntentOpList.add(new IntentOperation(IntentOperation.Operator.ADD, pathIntent));
83 break;
84 case REMOVE:
Toshio Koide0c9106d2014-02-19 15:26:38 -080085 pathIntentOpList.add(Operator.REMOVE, new Intent("pi" + intentOp.intent.getId()));
Toshio Koide27ffd412014-02-18 19:15:27 -080086 break;
Toshio Koidec87810e2014-02-11 13:03:21 -080087 }
Toshio Koidec87810e2014-02-11 13:03:21 -080088 }
Toshio Koide27ffd412014-02-18 19:15:27 -080089 return pathIntentOpList;
Toshio Koidec87810e2014-02-11 13:03:21 -080090 }
91}