blob: 37015cb6a1d9e9c38c7228d2d4a04c92974db6a7 [file] [log] [blame]
Toshio Koide4f308732014-02-18 15:19:48 -08001package net.onrc.onos.intent.runtime;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.HashMap;
Toshio Koide798bc1b2014-02-20 14:02:40 -08006import java.util.HashSet;
Toshio Koidedf2eab92014-02-20 11:24:59 -08007import java.util.Iterator;
Toshio Koidebf875662014-02-24 12:19:15 -08008import java.util.LinkedList;
Toshio Koide4f308732014-02-18 15:19:48 -08009import java.util.Map;
Toshio Koide066506e2014-02-20 19:52:09 -080010import java.util.Map.Entry;
Toshio Koide93be5d62014-02-23 19:30:57 -080011import java.util.concurrent.locks.ReentrantLock;
Toshio Koide4f308732014-02-18 15:19:48 -080012
Toshio Koidea94060f2014-02-21 22:58:32 -080013import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
15
Toshio Koide4f308732014-02-18 15:19:48 -080016import net.floodlightcontroller.core.module.FloodlightModuleContext;
17import net.floodlightcontroller.core.module.FloodlightModuleException;
18import net.floodlightcontroller.core.module.IFloodlightModule;
19import net.floodlightcontroller.core.module.IFloodlightService;
20import net.onrc.onos.datagrid.IDatagridService;
21import net.onrc.onos.datagrid.IEventChannel;
Toshio Koide066506e2014-02-20 19:52:09 -080022import net.onrc.onos.datagrid.IEventChannelListener;
23import net.onrc.onos.intent.Intent;
Toshio Koidedf2eab92014-02-20 11:24:59 -080024import net.onrc.onos.intent.Intent.IntentState;
Toshio Koide4f308732014-02-18 15:19:48 -080025import net.onrc.onos.intent.IntentMap;
Toshio Koidedf2eab92014-02-20 11:24:59 -080026import net.onrc.onos.intent.IntentOperation;
Toshio Koide0c9106d2014-02-19 15:26:38 -080027import net.onrc.onos.intent.IntentOperation.Operator;
Toshio Koide4f308732014-02-18 15:19:48 -080028import net.onrc.onos.intent.IntentOperationList;
Toshio Koide0c9106d2014-02-19 15:26:38 -080029import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080030import net.onrc.onos.intent.PathIntentMap;
Toshio Koide066506e2014-02-20 19:52:09 -080031import net.onrc.onos.intent.ShortestPathIntent;
Toshio Koidedf2eab92014-02-20 11:24:59 -080032import net.onrc.onos.intent.persist.PersistIntent;
Toshio Koide0c9106d2014-02-19 15:26:38 -080033import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
34import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
Toshio Koide27ffd412014-02-18 19:15:27 -080035import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Toshio Koide0c9106d2014-02-19 15:26:38 -080036import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
37import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
38import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080039import net.onrc.onos.registry.controller.IControllerRegistryService;
Toshio Koide4f308732014-02-18 15:19:48 -080040
Toshio Koide0c9106d2014-02-19 15:26:38 -080041/**
42 * @author Toshio Koide (t-koide@onlab.us)
43 */
Toshio Koide066506e2014-02-20 19:52:09 -080044public class PathCalcRuntimeModule implements IFloodlightModule, IPathCalcRuntimeService, INetworkGraphListener, IEventChannelListener<Long, IntentStateList> {
Toshio Koidebf875662014-02-24 12:19:15 -080045 class PerfLog {
46 private String step;
47 private long time;
48
49 public PerfLog(String step) {
50 this.step = step;
51 this.time = System.nanoTime();
52 }
53
54 public void logThis() {
55 log.error("Time:{}, Step:{}", time, step);
56 }
57 }
58 class PerfLogger {
59 private LinkedList<PerfLog> logData = new LinkedList<>();
60
61 public PerfLogger(String logPhase) {
62 log("start_" + logPhase);
63 }
64
65 public void log(String step) {
66 logData.add(new PerfLog(step));
67 }
68
69 public void flushLog() {
70 log("finish");
71 for (PerfLog log: logData) {
72 log.logThis();
73 }
74 logData.clear();
75 }
76 }
Toshio Koide4f308732014-02-18 15:19:48 -080077 private PathCalcRuntime runtime;
78 private IDatagridService datagridService;
Toshio Koide27ffd412014-02-18 19:15:27 -080079 private INetworkGraphService networkGraphService;
Toshio Koide4f308732014-02-18 15:19:48 -080080 private IntentMap highLevelIntents;
Toshio Koide27ffd412014-02-18 19:15:27 -080081 private PathIntentMap pathIntents;
Toshio Koide798bc1b2014-02-20 14:02:40 -080082 private IControllerRegistryService controllerRegistry;
83 private PersistIntent persistIntent;
Toshio Koide27ffd412014-02-18 19:15:27 -080084
Toshio Koide066506e2014-02-20 19:52:09 -080085 private IEventChannel<Long, IntentOperationList> opEventChannel;
Toshio Koide93be5d62014-02-23 19:30:57 -080086 private final ReentrantLock lock = new ReentrantLock();
Toshio Koide066506e2014-02-20 19:52:09 -080087 private static final String INTENT_OP_EVENT_CHANNEL_NAME = "onos.pathintent";
88 private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state";
Toshio Koidea94060f2014-02-21 22:58:32 -080089 private static final Logger log = LoggerFactory.getLogger(PathCalcRuntimeModule.class);
Toshio Koide4f308732014-02-18 15:19:48 -080090
Toshio Koide798bc1b2014-02-20 14:02:40 -080091 // ================================================================================
92 // private methods
93 // ================================================================================
94
Toshio Koidea94060f2014-02-21 22:58:32 -080095 private void reroutePaths(Collection<Intent> oldPaths) {
Toshio Koidea9078af2014-02-21 16:57:04 -080096 if (oldPaths == null || oldPaths.isEmpty())
Toshio Koide798bc1b2014-02-20 14:02:40 -080097 return;
Toshio Koidea10c0372014-02-20 17:28:10 -080098
Toshio Koide0c9106d2014-02-19 15:26:38 -080099 IntentOperationList reroutingOperation = new IntentOperationList();
Toshio Koide982c81f2014-02-23 16:53:10 -0800100 for (Intent intent : oldPaths) {
101 PathIntent pathIntent = (PathIntent) intent;
102 if (pathIntent.getState().equals(IntentState.INST_ACK) &&
103 !reroutingOperation.contains(pathIntent)) {
104 reroutingOperation.add(Operator.ADD, pathIntent.getParentIntent());
105 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800106 }
Toshio Koide8315d7d2014-02-21 22:58:32 -0800107 executeIntentOperations(reroutingOperation);
Toshio Koidea94060f2014-02-21 22:58:32 -0800108 }
109
Toshio Koide27ffd412014-02-18 19:15:27 -0800110
Toshio Koide798bc1b2014-02-20 14:02:40 -0800111 // ================================================================================
112 // IFloodlightModule implementations
113 // ================================================================================
114
Toshio Koide4f308732014-02-18 15:19:48 -0800115 @Override
116 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
117 Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(1);
Toshio Koideeb90d912014-02-18 21:30:22 -0800118 l.add(IPathCalcRuntimeService.class);
Toshio Koide4f308732014-02-18 15:19:48 -0800119 return l;
120 }
121
122 @Override
123 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Toshio Koidea9078af2014-02-21 16:57:04 -0800124 Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>();
Toshio Koide27ffd412014-02-18 19:15:27 -0800125 m.put(IPathCalcRuntimeService.class, this);
Toshio Koide4f308732014-02-18 15:19:48 -0800126 return m;
127 }
128
129 @Override
130 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Toshio Koidea9078af2014-02-21 16:57:04 -0800131 Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(2);
Toshio Koide4f308732014-02-18 15:19:48 -0800132 l.add(IDatagridService.class);
Toshio Koide27ffd412014-02-18 19:15:27 -0800133 l.add(INetworkGraphService.class);
Toshio Koide4f308732014-02-18 15:19:48 -0800134 return l;
135 }
136
137 @Override
138 public void init(FloodlightModuleContext context) throws FloodlightModuleException {
139 datagridService = context.getServiceImpl(IDatagridService.class);
Toshio Koided9fa2a82014-02-19 17:35:18 -0800140 networkGraphService = context.getServiceImpl(INetworkGraphService.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800141 controllerRegistry = context.getServiceImpl(IControllerRegistryService.class);
Toshio Koide4f308732014-02-18 15:19:48 -0800142 }
143
144 @Override
145 public void startUp(FloodlightModuleContext context) {
Toshio Koideeb90d912014-02-18 21:30:22 -0800146 highLevelIntents = new IntentMap();
147 runtime = new PathCalcRuntime(networkGraphService.getNetworkGraph());
Toshio Koide0c9106d2014-02-19 15:26:38 -0800148 pathIntents = new PathIntentMap();
Toshio Koide066506e2014-02-20 19:52:09 -0800149 opEventChannel = datagridService.createChannel(INTENT_OP_EVENT_CHANNEL_NAME, Long.class, IntentOperationList.class);
150 datagridService.addListener(INTENT_STATE_EVENT_CHANNEL_NAME, this, Long.class, IntentStateList.class);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800151 networkGraphService.registerNetworkGraphListener(this);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800152 persistIntent = new PersistIntent(controllerRegistry, networkGraphService);
Toshio Koide4f308732014-02-18 15:19:48 -0800153 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800154
Toshio Koide798bc1b2014-02-20 14:02:40 -0800155 // ================================================================================
156 // IPathCalcRuntimeService implementations
157 // ================================================================================
158
Toshio Koide27ffd412014-02-18 19:15:27 -0800159 @Override
160 public IntentOperationList executeIntentOperations(IntentOperationList list) {
Toshio Koidebf875662014-02-24 12:19:15 -0800161 if (list == null || list.size() == 0)
162 return null;
163 PerfLogger p = new PerfLogger("executeIntentOperations_" + list.get(0).operator);
Toshio Koide275d8142014-02-24 16:41:52 -0800164
165 lock.lock(); // TODO optimize locking using smaller steps
Toshio Koide93be5d62014-02-23 19:30:57 -0800166 try {
167 // update the map of high-level intents
Toshio Koidebf875662014-02-24 12:19:15 -0800168 p.log("begin_updateInMemoryIntents");
Toshio Koide93be5d62014-02-23 19:30:57 -0800169 highLevelIntents.executeOperations(list);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800170
Toshio Koide93be5d62014-02-23 19:30:57 -0800171 // change states of high-level intents
172 IntentStateList states = new IntentStateList();
173 for (IntentOperation op : list) {
174 if (op.intent.getState().equals(IntentState.INST_ACK))
175 states.put(op.intent.getId(), IntentState.REROUTE_REQ);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800176 }
Toshio Koide93be5d62014-02-23 19:30:57 -0800177 highLevelIntents.changeStates(states);
Toshio Koidebf875662014-02-24 12:19:15 -0800178 p.log("end_updateInMemoryIntents");
Toshio Koidedf2eab92014-02-20 11:24:59 -0800179
Toshio Koide93be5d62014-02-23 19:30:57 -0800180 // calculate path-intents (low-level operations)
Toshio Koidebf875662014-02-24 12:19:15 -0800181 p.log("begin_calcPathIntents");
Toshio Koide93be5d62014-02-23 19:30:57 -0800182 IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, highLevelIntents, pathIntents);
Toshio Koidebf875662014-02-24 12:19:15 -0800183 p.log("end_calcPathIntents");
Toshio Koide600ae5f2014-02-20 18:42:00 -0800184
Toshio Koide93be5d62014-02-23 19:30:57 -0800185 // persist calculated low-level operations into data store
Toshio Koidebf875662014-02-24 12:19:15 -0800186 p.log("begin_persistPathIntents");
Toshio Koide93be5d62014-02-23 19:30:57 -0800187 long key = persistIntent.getKey();
188 persistIntent.persistIfLeader(key, pathIntentOperations);
Toshio Koidebf875662014-02-24 12:19:15 -0800189 p.log("end_persistPathIntents");
Toshio Koide93be5d62014-02-23 19:30:57 -0800190
191 // remove error-intents and reflect them to high-level intents
Toshio Koidebf875662014-02-24 12:19:15 -0800192 p.log("begin_removeErrorIntents");
Toshio Koide93be5d62014-02-23 19:30:57 -0800193 states.clear();
194 Iterator<IntentOperation> i = pathIntentOperations.iterator();
195 while (i.hasNext()) {
196 IntentOperation op = i.next();
197 if (op.operator.equals(Operator.ERROR)) {
198 states.put(op.intent.getId(), IntentState.INST_NACK);
199 i.remove();
200 }
Toshio Koide600ae5f2014-02-20 18:42:00 -0800201 }
Toshio Koide93be5d62014-02-23 19:30:57 -0800202 highLevelIntents.changeStates(states);
Toshio Koidebf875662014-02-24 12:19:15 -0800203 p.log("end_removeErrorIntents");
Toshio Koidea94060f2014-02-21 22:58:32 -0800204
Toshio Koide93be5d62014-02-23 19:30:57 -0800205 // update the map of path intents and publish the path operations
Toshio Koidebf875662014-02-24 12:19:15 -0800206 p.log("begin_updateInMemoryPathIntents");
Toshio Koide93be5d62014-02-23 19:30:57 -0800207 pathIntents.executeOperations(pathIntentOperations);
Toshio Koidebf875662014-02-24 12:19:15 -0800208 p.log("end_updateInMemoryPathIntents");
Toshio Koide93be5d62014-02-23 19:30:57 -0800209
210 // Demo special: add a complete path to remove operation
Toshio Koidebf875662014-02-24 12:19:15 -0800211 p.log("begin_addPathToRemoveOperation");
Toshio Koide93be5d62014-02-23 19:30:57 -0800212 for (IntentOperation op: pathIntentOperations) {
213 if(op.operator.equals(Operator.REMOVE)) {
214 op.intent = pathIntents.getIntent(op.intent.getId());
215 }
216 if (op.intent instanceof PathIntent) {
217 log.debug("operation: {}, intent:{}", op.operator, op.intent);
218 }
219 }
Toshio Koidebf875662014-02-24 12:19:15 -0800220 p.log("end_addPathToRemoveOperation");
Toshio Koide93be5d62014-02-23 19:30:57 -0800221
222 // send notification
Toshio Koidebf875662014-02-24 12:19:15 -0800223 p.log("begin_sendNotification");
Toshio Koide93be5d62014-02-23 19:30:57 -0800224 opEventChannel.addEntry(key, pathIntentOperations);
Toshio Koidebf875662014-02-24 12:19:15 -0800225 p.log("end_sendNotification");
226 opEventChannel.removeEntry(key);
Toshio Koide93be5d62014-02-23 19:30:57 -0800227 return pathIntentOperations;
228 }
229 finally {
Toshio Koidebf875662014-02-24 12:19:15 -0800230 p.flushLog();
Toshio Koide93be5d62014-02-23 19:30:57 -0800231 lock.unlock();
232 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800233 }
234
235 @Override
236 public IntentMap getHighLevelIntents() {
Toshio Koide4f308732014-02-18 15:19:48 -0800237 return highLevelIntents;
238 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800239
240 @Override
241 public IntentMap getPathIntents() {
242 return pathIntents;
243 }
244
245 @Override
Toshio Koide4f308732014-02-18 15:19:48 -0800246 public void purgeIntents() {
247 highLevelIntents.purge();
Toshio Koide27ffd412014-02-18 19:15:27 -0800248 pathIntents.purge();
Toshio Koide4f308732014-02-18 15:19:48 -0800249 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800250
Toshio Koide798bc1b2014-02-20 14:02:40 -0800251 // ================================================================================
252 // INetworkGraphListener implementations
253 // ================================================================================
254
Toshio Koide0c9106d2014-02-19 15:26:38 -0800255 @Override
Toshio Koide798bc1b2014-02-20 14:02:40 -0800256 public void networkGraphEvents(Collection<SwitchEvent> addedSwitchEvents,
257 Collection<SwitchEvent> removedSwitchEvents,
258 Collection<PortEvent> addedPortEvents,
259 Collection<PortEvent> removedPortEvents,
260 Collection<LinkEvent> addedLinkEvents,
261 Collection<LinkEvent> removedLinkEvents,
262 Collection<DeviceEvent> addedDeviceEvents,
263 Collection<DeviceEvent> removedDeviceEvents) {
Toshio Koidea9078af2014-02-21 16:57:04 -0800264
Toshio Koidebf875662014-02-24 12:19:15 -0800265 PerfLogger p = new PerfLogger("networkGraphEvents");
Toshio Koidea94060f2014-02-21 22:58:32 -0800266 HashSet<Intent> affectedPaths = new HashSet<>();
Toshio Koidea9078af2014-02-21 16:57:04 -0800267
Toshio Koidebf875662014-02-24 12:19:15 -0800268 if (addedLinkEvents.size() > 0) { // ||
269// addedPortEvents.size() > 0 ||
270// addedSwitchEvents.size() > 0) {
271 p.log("begin_getAllIntents");
Toshio Koidea94060f2014-02-21 22:58:32 -0800272 affectedPaths.addAll(getPathIntents().getAllIntents());
Toshio Koidebf875662014-02-24 12:19:15 -0800273 p.log("end_getAllIntents");
Toshio Koidea94060f2014-02-21 22:58:32 -0800274 }
275 else {
Toshio Koidebf875662014-02-24 12:19:15 -0800276 p.log("begin_getIntentsByLink");
Toshio Koidea94060f2014-02-21 22:58:32 -0800277 for (LinkEvent linkEvent: removedLinkEvents)
278 affectedPaths.addAll(pathIntents.getIntentsByLink(linkEvent));
Toshio Koidebf875662014-02-24 12:19:15 -0800279 p.log("end_getIntentsByLink");
Toshio Koidea9078af2014-02-21 16:57:04 -0800280
Toshio Koidebf875662014-02-24 12:19:15 -0800281 p.log("begin_getIntentsByPort");
Toshio Koidea94060f2014-02-21 22:58:32 -0800282 for (PortEvent portEvent: removedPortEvents)
283 affectedPaths.addAll(pathIntents.getIntentsByPort(portEvent.getDpid(), portEvent.getNumber()));
Toshio Koidebf875662014-02-24 12:19:15 -0800284 p.log("end_getIntentsByPort");
Toshio Koidea9078af2014-02-21 16:57:04 -0800285
Toshio Koidebf875662014-02-24 12:19:15 -0800286 p.log("begin_getIntentsByDpid");
Toshio Koidea94060f2014-02-21 22:58:32 -0800287 for (SwitchEvent switchEvent: removedSwitchEvents)
288 affectedPaths.addAll(pathIntents.getIntentsByDpid(switchEvent.getDpid()));
Toshio Koidebf875662014-02-24 12:19:15 -0800289 p.log("end_getIntentsByDpid");
Toshio Koidea94060f2014-02-21 22:58:32 -0800290 }
Toshio Koidea9078af2014-02-21 16:57:04 -0800291 reroutePaths(affectedPaths);
Toshio Koidebf875662014-02-24 12:19:15 -0800292 p.flushLog();
Toshio Koide0c9106d2014-02-19 15:26:38 -0800293 }
Toshio Koide066506e2014-02-20 19:52:09 -0800294
295 // ================================================================================
296 // IEventChannelListener implementations
297 // ================================================================================
298
299 @Override
300 public void entryAdded(IntentStateList value) {
301 entryUpdated(value);
302 }
303
304 @Override
305 public void entryRemoved(IntentStateList value) {
306 // do nothing
307 }
308
309 @Override
310 public void entryUpdated(IntentStateList value) {
Toshio Koide8315d7d2014-02-21 22:58:32 -0800311 // TODO draw state transition diagram in multiple ONOS instances and update this method
Toshio Koidebf875662014-02-24 12:19:15 -0800312 PerfLogger p = new PerfLogger("entryUpdated");
Toshio Koide93be5d62014-02-23 19:30:57 -0800313 lock.lock(); // TODO optimize locking using smaller steps
314 try {
Toshio Koide93be5d62014-02-23 19:30:57 -0800315 // reflect state changes of path-level intent into application-level intents
Toshio Koidebf875662014-02-24 12:19:15 -0800316 p.log("begin_changeStateByNotification");
Toshio Koide93be5d62014-02-23 19:30:57 -0800317 IntentStateList parentStates = new IntentStateList();
318 for (Entry<String, IntentState> entry: value.entrySet()) {
319 PathIntent pathIntent = (PathIntent) pathIntents.getIntent(entry.getKey());
320 if (pathIntent == null) continue;
Toshio Koide8315d7d2014-02-21 22:58:32 -0800321
Toshio Koide93be5d62014-02-23 19:30:57 -0800322 Intent parentIntent = pathIntent.getParentIntent();
323 if (parentIntent == null ||
324 !(parentIntent instanceof ShortestPathIntent) ||
325 !((ShortestPathIntent) parentIntent).getPathIntentId().equals(pathIntent.getId()))
326 continue;
Toshio Koide066506e2014-02-20 19:52:09 -0800327
Toshio Koide93be5d62014-02-23 19:30:57 -0800328 IntentState state = entry.getValue();
329 switch (state) {
330 case INST_REQ:
331 case INST_ACK:
332 case INST_NACK:
333 case DEL_REQ:
334 case DEL_ACK:
335 case DEL_PENDING:
336 parentStates.put(parentIntent.getId(), state);
337 break;
338 default:
339 break;
340 }
Toshio Koide066506e2014-02-20 19:52:09 -0800341 }
Toshio Koide93be5d62014-02-23 19:30:57 -0800342 highLevelIntents.changeStates(parentStates);
343 pathIntents.changeStates(value);
Toshio Koidebf875662014-02-24 12:19:15 -0800344 p.log("end_changeStateByNotification");
Toshio Koide066506e2014-02-20 19:52:09 -0800345 }
Toshio Koide93be5d62014-02-23 19:30:57 -0800346 finally {
Toshio Koidebf875662014-02-24 12:19:15 -0800347 p.flushLog();
Toshio Koide93be5d62014-02-23 19:30:57 -0800348 lock.unlock();
349 }
Toshio Koide066506e2014-02-20 19:52:09 -0800350 }
Toshio Koidea9078af2014-02-21 16:57:04 -0800351}