blob: 4408f3fb6ac65c1542f1eebe585c6c55551e547a [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;
10import net.onrc.onos.intent.IntentOperationList;
Toshio Koidec87810e2014-02-11 13:03:21 -080011import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080012import net.onrc.onos.intent.PathIntentMap;
Toshio Koidec87810e2014-02-11 13:03:21 -080013import net.onrc.onos.intent.ShortestPathIntent;
Toshio Koide0c9106d2014-02-19 15:26:38 -080014import net.onrc.onos.intent.IntentOperation.Operator;
Toshio Koidec87810e2014-02-11 13:03:21 -080015import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
16import net.onrc.onos.ofcontroller.networkgraph.Path;
17import net.onrc.onos.ofcontroller.networkgraph.Switch;
18
19/**
20 * @author Toshio Koide (t-koide@onlab.us)
21 */
Toshio Koide4f308732014-02-18 15:19:48 -080022public class PathCalcRuntime implements IFloodlightService {
23 private NetworkGraph graph;
Toshio Koidec87810e2014-02-11 13:03:21 -080024 public PathCalcRuntime(NetworkGraph g) {
25 this.graph = g;
26 }
27
Toshio Koide27ffd412014-02-18 19:15:27 -080028 /**
29 * calculate shortest-path and constrained-shortest-path intents into low-level path intents
30 * @param intentOpList IntentOperationList having instances of ShortestPathIntent/ConstrainedShortestPathIntent
31 * @param pathIntents a set of current low-level intents
32 * @return IntentOperationList for PathIntent instances
33 */
34 public IntentOperationList calcPathIntents(IntentOperationList intentOpList, PathIntentMap pathIntents) {
35 IntentOperationList pathIntentOpList = new IntentOperationList();
36 HashMap<Switch, ConstrainedBFSTree> spfTrees = new HashMap<>();
Toshio Koidec87810e2014-02-11 13:03:21 -080037
Toshio Koide27ffd412014-02-18 19:15:27 -080038 for (IntentOperation intentOp: intentOpList) {
39 switch (intentOp.operator) {
40 case ADD:
41 if (!(intentOp.intent instanceof ShortestPathIntent)) {
42 // unsupported intent type.
43 // TODO should push back the intent to caller
44 continue;
45 }
Toshio Koidec87810e2014-02-11 13:03:21 -080046
Toshio Koide27ffd412014-02-18 19:15:27 -080047 ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent;
48 Switch srcSwitch = graph.getSwitch(spIntent.getSrcSwitchDpid());
49 Switch dstSwitch = graph.getSwitch(spIntent.getDstSwitchDpid());
50 if (srcSwitch == null || dstSwitch == null) {
51 // incomplete intent.
52 // TODO should push back the intent to caller
53 continue;
54 }
Toshio Koidec87810e2014-02-11 13:03:21 -080055
Toshio Koide27ffd412014-02-18 19:15:27 -080056 double bandwidth = 0.0;
57 ConstrainedBFSTree tree = null;
58 if (intentOp.intent instanceof ConstrainedShortestPathIntent) {
59 bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth();
60 tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth);
61 }
62 else {
63 tree = spfTrees.get(srcSwitch);
64 if (tree == null) {
65 tree = new ConstrainedBFSTree(srcSwitch);
66 spfTrees.put(srcSwitch, tree);
67 }
68 }
69 Path path = tree.getPath(dstSwitch);
70 if (path == null) {
71 // path not found.
72 // TODO should push back the intent to caller
73 continue;
74 }
75 PathIntent pathIntent = new PathIntent("pi" + intentOp.intent.getId(), path, bandwidth, intentOp.intent);
76 pathIntentOpList.add(new IntentOperation(IntentOperation.Operator.ADD, pathIntent));
77 break;
78 case REMOVE:
Toshio Koide0c9106d2014-02-19 15:26:38 -080079 pathIntentOpList.add(Operator.REMOVE, new Intent("pi" + intentOp.intent.getId()));
Toshio Koide27ffd412014-02-18 19:15:27 -080080 break;
Toshio Koidec87810e2014-02-11 13:03:21 -080081 }
Toshio Koidec87810e2014-02-11 13:03:21 -080082 }
Toshio Koide27ffd412014-02-18 19:15:27 -080083 return pathIntentOpList;
Toshio Koidec87810e2014-02-11 13:03:21 -080084 }
85}