blob: 6ff92db7c1041bc6c4bb3f895b53313eaece1f3c [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 Koidedba6fef2014-02-23 15:36:21 -080012import net.onrc.onos.intent.IntentMap;
Toshio Koide4f308732014-02-18 15:19:48 -080013import net.onrc.onos.intent.IntentOperation;
Toshio Koided9fa2a82014-02-19 17:35:18 -080014import net.onrc.onos.intent.IntentOperation.Operator;
Toshio Koide4f308732014-02-18 15:19:48 -080015import net.onrc.onos.intent.IntentOperationList;
Toshio Koidec87810e2014-02-11 13:03:21 -080016import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080017import net.onrc.onos.intent.PathIntentMap;
Toshio Koidec87810e2014-02-11 13:03:21 -080018import net.onrc.onos.intent.ShortestPathIntent;
19import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
20import net.onrc.onos.ofcontroller.networkgraph.Path;
21import net.onrc.onos.ofcontroller.networkgraph.Switch;
22
Toshio Koideb39c9d32014-02-20 01:21:47 -080023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Toshio Koidec87810e2014-02-11 13:03:21 -080026/**
27 * @author Toshio Koide (t-koide@onlab.us)
28 */
Toshio Koide4f308732014-02-18 15:19:48 -080029public class PathCalcRuntime implements IFloodlightService {
30 private NetworkGraph graph;
Toshio Koideb39c9d32014-02-20 01:21:47 -080031 private final static Logger log = LoggerFactory.getLogger(PathCalcRuntime.class);
Toshio Koidec87810e2014-02-11 13:03:21 -080032 public PathCalcRuntime(NetworkGraph g) {
33 this.graph = g;
34 }
35
Toshio Koide27ffd412014-02-18 19:15:27 -080036 /**
37 * calculate shortest-path and constrained-shortest-path intents into low-level path intents
38 * @param intentOpList IntentOperationList having instances of ShortestPathIntent/ConstrainedShortestPathIntent
39 * @param pathIntents a set of current low-level intents
Toshio Koidedf2eab92014-02-20 11:24:59 -080040 * @return IntentOperationList. PathIntent and/or ErrorIntent instances.
Toshio Koide27ffd412014-02-18 19:15:27 -080041 */
Toshio Koidedba6fef2014-02-23 15:36:21 -080042 public IntentOperationList calcPathIntents(final IntentOperationList intentOpList, final IntentMap appIntents, final PathIntentMap pathIntents) {
Toshio Koide27ffd412014-02-18 19:15:27 -080043 IntentOperationList pathIntentOpList = new IntentOperationList();
44 HashMap<Switch, ConstrainedBFSTree> spfTrees = new HashMap<>();
Toshio Koidec87810e2014-02-11 13:03:21 -080045
Toshio Koide59bc8ea2014-02-21 17:48:46 -080046 // TODO optimize locking of NetworkGraph
47 graph.acquireReadLock();
48
Toshio Koide27ffd412014-02-18 19:15:27 -080049 for (IntentOperation intentOp: intentOpList) {
50 switch (intentOp.operator) {
51 case ADD:
52 if (!(intentOp.intent instanceof ShortestPathIntent)) {
Toshio Koidedf2eab92014-02-20 11:24:59 -080053 log.error("Unsupported intent type: {}", intentOp.intent.getClass().getName());
54 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
55 ErrorType.UNSUPPORTED_INTENT,
56 "Unsupported intent type.",
57 intentOp.intent));
Toshio Koide27ffd412014-02-18 19:15:27 -080058 continue;
59 }
Toshio Koidec87810e2014-02-11 13:03:21 -080060
Toshio Koide27ffd412014-02-18 19:15:27 -080061 ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent;
62 Switch srcSwitch = graph.getSwitch(spIntent.getSrcSwitchDpid());
63 Switch dstSwitch = graph.getSwitch(spIntent.getDstSwitchDpid());
64 if (srcSwitch == null || dstSwitch == null) {
Toshio Koideb39c9d32014-02-20 01:21:47 -080065 log.error("Switch not found: {}, {}",
66 spIntent.getSrcSwitchDpid(),
67 spIntent.getDstSwitchDpid());
Toshio Koidedf2eab92014-02-20 11:24:59 -080068 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
69 ErrorType.SWITCH_NOT_FOUND,
70 "Switch not found.",
Toshio Koidea10c0372014-02-20 17:28:10 -080071 spIntent));
Toshio Koide27ffd412014-02-18 19:15:27 -080072 continue;
73 }
Toshio Koidec87810e2014-02-11 13:03:21 -080074
Toshio Koide27ffd412014-02-18 19:15:27 -080075 double bandwidth = 0.0;
76 ConstrainedBFSTree tree = null;
Toshio Koidea10c0372014-02-20 17:28:10 -080077 if (spIntent instanceof ConstrainedShortestPathIntent) {
Toshio Koide27ffd412014-02-18 19:15:27 -080078 bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth();
79 tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth);
80 }
81 else {
82 tree = spfTrees.get(srcSwitch);
83 if (tree == null) {
84 tree = new ConstrainedBFSTree(srcSwitch);
85 spfTrees.put(srcSwitch, tree);
86 }
87 }
88 Path path = tree.getPath(dstSwitch);
89 if (path == null) {
Toshio Koidea10c0372014-02-20 17:28:10 -080090 log.error("Path not found: {}", spIntent.toString());
Toshio Koidedf2eab92014-02-20 11:24:59 -080091 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
92 ErrorType.PATH_NOT_FOUND,
93 "Path not found.",
Toshio Koidea10c0372014-02-20 17:28:10 -080094 spIntent));
Toshio Koide27ffd412014-02-18 19:15:27 -080095 continue;
96 }
Toshio Koidea10c0372014-02-20 17:28:10 -080097
98 // generate new path-intent ID
99 String oldPathIntentId = spIntent.getPathIntentId();
100 String newPathIntentId;
101 if (oldPathIntentId == null)
102 newPathIntentId = PathIntent.createFirstId(spIntent.getId());
103 else {
104 newPathIntentId = PathIntent.createNextId(oldPathIntentId);
105
106 // Request removal of low-level intent if it exists.
107 pathIntentOpList.add(Operator.REMOVE, new Intent(oldPathIntentId));
108 }
109
110 // create new path-intent
111 PathIntent pathIntent = new PathIntent(newPathIntentId, path, bandwidth, spIntent);
112 pathIntent.setState(IntentState.INST_REQ);
113 spIntent.setPathIntent(pathIntent);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800114 pathIntentOpList.add(Operator.ADD, pathIntent);
Toshio Koidea10c0372014-02-20 17:28:10 -0800115
Toshio Koide27ffd412014-02-18 19:15:27 -0800116 break;
117 case REMOVE:
Toshio Koidedba6fef2014-02-23 15:36:21 -0800118 ShortestPathIntent targetAppIntent = (ShortestPathIntent) appIntents.getIntent(intentOp.intent.getId());
119 if (targetAppIntent != null) {
120 String pathIntentId = targetAppIntent.getPathIntentId();
121 if (pathIntentId != null) {
122 Intent targetPathIntent = pathIntents.getIntent(pathIntentId);
123 if (targetPathIntent != null) {
124 pathIntentOpList.add(Operator.REMOVE, targetPathIntent);
125 }
126 }
127 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800128 break;
Toshio Koidedf2eab92014-02-20 11:24:59 -0800129 case ERROR:
130 // just ignore
131 break;
Toshio Koidec87810e2014-02-11 13:03:21 -0800132 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800133 }
Toshio Koide59bc8ea2014-02-21 17:48:46 -0800134 // TODO optimize locking of NetworkGraph
135 graph.releaseReadLock();
136
Toshio Koide27ffd412014-02-18 19:15:27 -0800137 return pathIntentOpList;
Toshio Koidec87810e2014-02-11 13:03:21 -0800138 }
139}