blob: 3de671e0ff798dc96d656a9457dc0430d4c94a12 [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();
Toshio Koide240b1302014-02-26 20:08:41 -080048 log.debug("NetworkGraph: {}", graph.getLinks());
Toshio Koide59bc8ea2014-02-21 17:48:46 -080049
Toshio Koide27ffd412014-02-18 19:15:27 -080050 for (IntentOperation intentOp: intentOpList) {
51 switch (intentOp.operator) {
52 case ADD:
53 if (!(intentOp.intent instanceof ShortestPathIntent)) {
Toshio Koidedf2eab92014-02-20 11:24:59 -080054 log.error("Unsupported intent type: {}", intentOp.intent.getClass().getName());
55 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
56 ErrorType.UNSUPPORTED_INTENT,
57 "Unsupported intent type.",
58 intentOp.intent));
Toshio Koide27ffd412014-02-18 19:15:27 -080059 continue;
60 }
Toshio Koidec87810e2014-02-11 13:03:21 -080061
Toshio Koide27ffd412014-02-18 19:15:27 -080062 ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent;
63 Switch srcSwitch = graph.getSwitch(spIntent.getSrcSwitchDpid());
64 Switch dstSwitch = graph.getSwitch(spIntent.getDstSwitchDpid());
65 if (srcSwitch == null || dstSwitch == null) {
Toshio Koide240b1302014-02-26 20:08:41 -080066 log.error("Switch not found. src:{}, dst:{}, NetworkGraph:{}",
Toshio Koideb39c9d32014-02-20 01:21:47 -080067 spIntent.getSrcSwitchDpid(),
Toshio Koide240b1302014-02-26 20:08:41 -080068 spIntent.getDstSwitchDpid(),
69 graph.getLinks());
Toshio Koidedf2eab92014-02-20 11:24:59 -080070 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
71 ErrorType.SWITCH_NOT_FOUND,
72 "Switch not found.",
Toshio Koidea10c0372014-02-20 17:28:10 -080073 spIntent));
Toshio Koide27ffd412014-02-18 19:15:27 -080074 continue;
75 }
Toshio Koidec87810e2014-02-11 13:03:21 -080076
Toshio Koide27ffd412014-02-18 19:15:27 -080077 double bandwidth = 0.0;
78 ConstrainedBFSTree tree = null;
Toshio Koidea10c0372014-02-20 17:28:10 -080079 if (spIntent instanceof ConstrainedShortestPathIntent) {
Toshio Koide27ffd412014-02-18 19:15:27 -080080 bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth();
81 tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth);
82 }
83 else {
84 tree = spfTrees.get(srcSwitch);
85 if (tree == null) {
86 tree = new ConstrainedBFSTree(srcSwitch);
87 spfTrees.put(srcSwitch, tree);
88 }
89 }
90 Path path = tree.getPath(dstSwitch);
91 if (path == null) {
Toshio Koide240b1302014-02-26 20:08:41 -080092 log.error("Path not found. Intent: {}, NetworkGraph:{}", spIntent.toString(), graph.getLinks());
Toshio Koidedf2eab92014-02-20 11:24:59 -080093 pathIntentOpList.add(Operator.ERROR, new ErrorIntent(
94 ErrorType.PATH_NOT_FOUND,
95 "Path not found.",
Toshio Koidea10c0372014-02-20 17:28:10 -080096 spIntent));
Toshio Koide27ffd412014-02-18 19:15:27 -080097 continue;
98 }
Toshio Koidea10c0372014-02-20 17:28:10 -080099
100 // generate new path-intent ID
101 String oldPathIntentId = spIntent.getPathIntentId();
102 String newPathIntentId;
103 if (oldPathIntentId == null)
104 newPathIntentId = PathIntent.createFirstId(spIntent.getId());
105 else {
106 newPathIntentId = PathIntent.createNextId(oldPathIntentId);
107
108 // Request removal of low-level intent if it exists.
109 pathIntentOpList.add(Operator.REMOVE, new Intent(oldPathIntentId));
110 }
111
112 // create new path-intent
113 PathIntent pathIntent = new PathIntent(newPathIntentId, path, bandwidth, spIntent);
114 pathIntent.setState(IntentState.INST_REQ);
115 spIntent.setPathIntent(pathIntent);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800116 pathIntentOpList.add(Operator.ADD, pathIntent);
Toshio Koidea10c0372014-02-20 17:28:10 -0800117
Toshio Koide27ffd412014-02-18 19:15:27 -0800118 break;
119 case REMOVE:
Toshio Koidedba6fef2014-02-23 15:36:21 -0800120 ShortestPathIntent targetAppIntent = (ShortestPathIntent) appIntents.getIntent(intentOp.intent.getId());
121 if (targetAppIntent != null) {
122 String pathIntentId = targetAppIntent.getPathIntentId();
123 if (pathIntentId != null) {
124 Intent targetPathIntent = pathIntents.getIntent(pathIntentId);
125 if (targetPathIntent != null) {
126 pathIntentOpList.add(Operator.REMOVE, targetPathIntent);
127 }
128 }
129 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800130 break;
Toshio Koidedf2eab92014-02-20 11:24:59 -0800131 case ERROR:
132 // just ignore
133 break;
Toshio Koidec87810e2014-02-11 13:03:21 -0800134 }
Toshio Koidec87810e2014-02-11 13:03:21 -0800135 }
Toshio Koide59bc8ea2014-02-21 17:48:46 -0800136 // TODO optimize locking of NetworkGraph
137 graph.releaseReadLock();
138
Toshio Koide27ffd412014-02-18 19:15:27 -0800139 return pathIntentOpList;
Toshio Koidec87810e2014-02-11 13:03:21 -0800140 }
141}