blob: 043a006f936189e2b17e968f33137dbfb21711c4 [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 Koidedf2eab92014-02-20 11:24:59 -08008import net.onrc.onos.intent.ErrorIntent;
9import net.onrc.onos.intent.ErrorIntent.ErrorType;
Toshio Koide0c9106d2014-02-19 15:26:38 -080010import net.onrc.onos.intent.Intent;
Toshio Koidea10c0372014-02-20 17:28:10 -080011import net.onrc.onos.intent.Intent.IntentState;
Toshio Koide4f308732014-02-18 15:19:48 -080012import net.onrc.onos.intent.IntentOperation;
Toshio Koided9fa2a82014-02-19 17:35:18 -080013import net.onrc.onos.intent.IntentOperation.Operator;
Toshio Koide4f308732014-02-18 15:19:48 -080014import net.onrc.onos.intent.IntentOperationList;
Toshio Koidec87810e2014-02-11 13:03:21 -080015import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080016import net.onrc.onos.intent.PathIntentMap;
Toshio Koidec87810e2014-02-11 13:03:21 -080017import net.onrc.onos.intent.ShortestPathIntent;
18import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
19import net.onrc.onos.ofcontroller.networkgraph.Path;
20import net.onrc.onos.ofcontroller.networkgraph.Switch;
21
Toshio Koideb39c9d32014-02-20 01:21:47 -080022import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
Toshio Koidec87810e2014-02-11 13:03:21 -080025/**
26 * @author Toshio Koide (t-koide@onlab.us)
27 */
Toshio Koide4f308732014-02-18 15:19:48 -080028public class PathCalcRuntime implements IFloodlightService {
29 private NetworkGraph graph;
Toshio Koideb39c9d32014-02-20 01:21:47 -080030 private final static Logger log = LoggerFactory.getLogger(PathCalcRuntime.class);
Toshio Koidec87810e2014-02-11 13:03:21 -080031 public PathCalcRuntime(NetworkGraph g) {
32 this.graph = g;
33 }
34
Toshio Koide27ffd412014-02-18 19:15:27 -080035 /**
36 * calculate shortest-path and constrained-shortest-path intents into low-level path intents
37 * @param intentOpList IntentOperationList having instances of ShortestPathIntent/ConstrainedShortestPathIntent
38 * @param pathIntents a set of current low-level intents
Toshio Koidedf2eab92014-02-20 11:24:59 -080039 * @return IntentOperationList. PathIntent and/or ErrorIntent instances.
Toshio Koide27ffd412014-02-18 19:15:27 -080040 */
Toshio Koidedf2eab92014-02-20 11:24:59 -080041 public IntentOperationList calcPathIntents(final IntentOperationList intentOpList, final PathIntentMap pathIntents) {
Toshio Koide27ffd412014-02-18 19:15:27 -080042 IntentOperationList pathIntentOpList = new IntentOperationList();
43 HashMap<Switch, ConstrainedBFSTree> spfTrees = new HashMap<>();
Toshio Koidec87810e2014-02-11 13:03:21 -080044
Toshio Koide27ffd412014-02-18 19:15:27 -080045 for (IntentOperation intentOp: intentOpList) {
46 switch (intentOp.operator) {
47 case ADD:
48 if (!(intentOp.intent instanceof ShortestPathIntent)) {
Toshio Koidedf2eab92014-02-20 11:24:59 -080049 log.error("Unsupported intent type: {}", intentOp.intent.getClass().getName());
50 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
51 ErrorType.UNSUPPORTED_INTENT,
52 "Unsupported intent type.",
53 intentOp.intent));
Toshio Koide27ffd412014-02-18 19:15:27 -080054 continue;
55 }
Toshio Koidec87810e2014-02-11 13:03:21 -080056
Toshio Koide27ffd412014-02-18 19:15:27 -080057 ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent;
58 Switch srcSwitch = graph.getSwitch(spIntent.getSrcSwitchDpid());
59 Switch dstSwitch = graph.getSwitch(spIntent.getDstSwitchDpid());
60 if (srcSwitch == null || dstSwitch == null) {
Toshio Koideb39c9d32014-02-20 01:21:47 -080061 log.error("Switch not found: {}, {}",
62 spIntent.getSrcSwitchDpid(),
63 spIntent.getDstSwitchDpid());
Toshio Koidedf2eab92014-02-20 11:24:59 -080064 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
65 ErrorType.SWITCH_NOT_FOUND,
66 "Switch not found.",
Toshio Koidea10c0372014-02-20 17:28:10 -080067 spIntent));
Toshio Koide27ffd412014-02-18 19:15:27 -080068 continue;
69 }
Toshio Koidec87810e2014-02-11 13:03:21 -080070
Toshio Koide27ffd412014-02-18 19:15:27 -080071 double bandwidth = 0.0;
72 ConstrainedBFSTree tree = null;
Toshio Koidea10c0372014-02-20 17:28:10 -080073 if (spIntent instanceof ConstrainedShortestPathIntent) {
Toshio Koide27ffd412014-02-18 19:15:27 -080074 bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth();
75 tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth);
76 }
77 else {
78 tree = spfTrees.get(srcSwitch);
79 if (tree == null) {
80 tree = new ConstrainedBFSTree(srcSwitch);
81 spfTrees.put(srcSwitch, tree);
82 }
83 }
84 Path path = tree.getPath(dstSwitch);
85 if (path == null) {
Toshio Koidea10c0372014-02-20 17:28:10 -080086 log.error("Path not found: {}", spIntent.toString());
Toshio Koidedf2eab92014-02-20 11:24:59 -080087 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
88 ErrorType.PATH_NOT_FOUND,
89 "Path not found.",
Toshio Koidea10c0372014-02-20 17:28:10 -080090 spIntent));
Toshio Koide27ffd412014-02-18 19:15:27 -080091 continue;
92 }
Toshio Koidea10c0372014-02-20 17:28:10 -080093
94 // generate new path-intent ID
95 String oldPathIntentId = spIntent.getPathIntentId();
96 String newPathIntentId;
97 if (oldPathIntentId == null)
98 newPathIntentId = PathIntent.createFirstId(spIntent.getId());
99 else {
100 newPathIntentId = PathIntent.createNextId(oldPathIntentId);
101
102 // Request removal of low-level intent if it exists.
103 pathIntentOpList.add(Operator.REMOVE, new Intent(oldPathIntentId));
104 }
105
106 // create new path-intent
107 PathIntent pathIntent = new PathIntent(newPathIntentId, path, bandwidth, spIntent);
108 pathIntent.setState(IntentState.INST_REQ);
109 spIntent.setPathIntent(pathIntent);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800110 pathIntentOpList.add(Operator.ADD, pathIntent);
Toshio Koidea10c0372014-02-20 17:28:10 -0800111
Toshio Koide27ffd412014-02-18 19:15:27 -0800112 break;
113 case REMOVE:
Toshio Koidea10c0372014-02-20 17:28:10 -0800114 pathIntentOpList.add(Operator.REMOVE, new Intent(
115 ((ShortestPathIntent) intentOp.intent).getPathIntentId()));
Toshio Koide27ffd412014-02-18 19:15:27 -0800116 break;
Toshio Koidedf2eab92014-02-20 11:24:59 -0800117 case ERROR:
118 // just ignore
119 break;
Toshio Koidec87810e2014-02-11 13:03:21 -0800120 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800121 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800122 return pathIntentOpList;
Toshio Koidec87810e2014-02-11 13:03:21 -0800123 }
124}