blob: 3dfdc84d0db22c75074c7d600ef61554d3c691f0 [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 Koide4f308732014-02-18 15:19:48 -08008import java.util.Map;
Toshio Koide066506e2014-02-20 19:52:09 -08009import java.util.Map.Entry;
Toshio Koide4f308732014-02-18 15:19:48 -080010
Toshio Koidea94060f2014-02-21 22:58:32 -080011import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13
Toshio Koide4f308732014-02-18 15:19:48 -080014import net.floodlightcontroller.core.module.FloodlightModuleContext;
15import net.floodlightcontroller.core.module.FloodlightModuleException;
16import net.floodlightcontroller.core.module.IFloodlightModule;
17import net.floodlightcontroller.core.module.IFloodlightService;
18import net.onrc.onos.datagrid.IDatagridService;
19import net.onrc.onos.datagrid.IEventChannel;
Toshio Koide066506e2014-02-20 19:52:09 -080020import net.onrc.onos.datagrid.IEventChannelListener;
21import net.onrc.onos.intent.Intent;
Toshio Koidedf2eab92014-02-20 11:24:59 -080022import net.onrc.onos.intent.Intent.IntentState;
Toshio Koide4f308732014-02-18 15:19:48 -080023import net.onrc.onos.intent.IntentMap;
Toshio Koidedf2eab92014-02-20 11:24:59 -080024import net.onrc.onos.intent.IntentOperation;
Toshio Koide0c9106d2014-02-19 15:26:38 -080025import net.onrc.onos.intent.IntentOperation.Operator;
Toshio Koide4f308732014-02-18 15:19:48 -080026import net.onrc.onos.intent.IntentOperationList;
Toshio Koide0c9106d2014-02-19 15:26:38 -080027import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080028import net.onrc.onos.intent.PathIntentMap;
Toshio Koide066506e2014-02-20 19:52:09 -080029import net.onrc.onos.intent.ShortestPathIntent;
Toshio Koidedf2eab92014-02-20 11:24:59 -080030import net.onrc.onos.intent.persist.PersistIntent;
Toshio Koide0c9106d2014-02-19 15:26:38 -080031import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
32import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener;
Toshio Koide27ffd412014-02-18 19:15:27 -080033import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Toshio Koide0c9106d2014-02-19 15:26:38 -080034import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
35import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
36import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080037import net.onrc.onos.registry.controller.IControllerRegistryService;
Toshio Koide4f308732014-02-18 15:19:48 -080038
Toshio Koide0c9106d2014-02-19 15:26:38 -080039/**
40 * @author Toshio Koide (t-koide@onlab.us)
41 */
Toshio Koide066506e2014-02-20 19:52:09 -080042public class PathCalcRuntimeModule implements IFloodlightModule, IPathCalcRuntimeService, INetworkGraphListener, IEventChannelListener<Long, IntentStateList> {
Toshio Koide4f308732014-02-18 15:19:48 -080043 private PathCalcRuntime runtime;
44 private IDatagridService datagridService;
Toshio Koide27ffd412014-02-18 19:15:27 -080045 private INetworkGraphService networkGraphService;
Toshio Koide4f308732014-02-18 15:19:48 -080046 private IntentMap highLevelIntents;
Toshio Koide27ffd412014-02-18 19:15:27 -080047 private PathIntentMap pathIntents;
Toshio Koide798bc1b2014-02-20 14:02:40 -080048 private IControllerRegistryService controllerRegistry;
49 private PersistIntent persistIntent;
Toshio Koide27ffd412014-02-18 19:15:27 -080050
Toshio Koide066506e2014-02-20 19:52:09 -080051 private IEventChannel<Long, IntentOperationList> opEventChannel;
52 private static final String INTENT_OP_EVENT_CHANNEL_NAME = "onos.pathintent";
53 private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state";
Toshio Koidea94060f2014-02-21 22:58:32 -080054 private static final Logger log = LoggerFactory.getLogger(PathCalcRuntimeModule.class);
Toshio Koide4f308732014-02-18 15:19:48 -080055
Toshio Koide798bc1b2014-02-20 14:02:40 -080056 // ================================================================================
57 // private methods
58 // ================================================================================
59
Toshio Koidea94060f2014-02-21 22:58:32 -080060 private void reroutePaths(Collection<Intent> oldPaths) {
Toshio Koidea9078af2014-02-21 16:57:04 -080061 if (oldPaths == null || oldPaths.isEmpty())
Toshio Koide798bc1b2014-02-20 14:02:40 -080062 return;
Toshio Koidea10c0372014-02-20 17:28:10 -080063
Toshio Koide0c9106d2014-02-19 15:26:38 -080064 IntentOperationList reroutingOperation = new IntentOperationList();
Toshio Koidea94060f2014-02-21 22:58:32 -080065 for (Intent pathIntent : oldPaths) {
66 reroutingOperation.add(Operator.ADD, ((PathIntent) pathIntent).getParentIntent());
Toshio Koide0c9106d2014-02-19 15:26:38 -080067 }
Toshio Koide8315d7d2014-02-21 22:58:32 -080068 executeIntentOperations(reroutingOperation);
Toshio Koidea94060f2014-02-21 22:58:32 -080069 }
70
71 private void log(String step) {
72 log.error("Step:{}, Time:{}", step, System.nanoTime());
Toshio Koide0c9106d2014-02-19 15:26:38 -080073 }
Toshio Koide27ffd412014-02-18 19:15:27 -080074
Toshio Koide798bc1b2014-02-20 14:02:40 -080075 // ================================================================================
76 // IFloodlightModule implementations
77 // ================================================================================
78
Toshio Koide4f308732014-02-18 15:19:48 -080079 @Override
80 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
81 Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(1);
Toshio Koideeb90d912014-02-18 21:30:22 -080082 l.add(IPathCalcRuntimeService.class);
Toshio Koide4f308732014-02-18 15:19:48 -080083 return l;
84 }
85
86 @Override
87 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Toshio Koidea9078af2014-02-21 16:57:04 -080088 Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>();
Toshio Koide27ffd412014-02-18 19:15:27 -080089 m.put(IPathCalcRuntimeService.class, this);
Toshio Koide4f308732014-02-18 15:19:48 -080090 return m;
91 }
92
93 @Override
94 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Toshio Koidea9078af2014-02-21 16:57:04 -080095 Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(2);
Toshio Koide4f308732014-02-18 15:19:48 -080096 l.add(IDatagridService.class);
Toshio Koide27ffd412014-02-18 19:15:27 -080097 l.add(INetworkGraphService.class);
Toshio Koide4f308732014-02-18 15:19:48 -080098 return l;
99 }
100
101 @Override
102 public void init(FloodlightModuleContext context) throws FloodlightModuleException {
103 datagridService = context.getServiceImpl(IDatagridService.class);
Toshio Koided9fa2a82014-02-19 17:35:18 -0800104 networkGraphService = context.getServiceImpl(INetworkGraphService.class);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800105 controllerRegistry = context.getServiceImpl(IControllerRegistryService.class);
Toshio Koide4f308732014-02-18 15:19:48 -0800106 }
107
108 @Override
109 public void startUp(FloodlightModuleContext context) {
Toshio Koideeb90d912014-02-18 21:30:22 -0800110 highLevelIntents = new IntentMap();
111 runtime = new PathCalcRuntime(networkGraphService.getNetworkGraph());
Toshio Koide0c9106d2014-02-19 15:26:38 -0800112 pathIntents = new PathIntentMap();
Toshio Koide066506e2014-02-20 19:52:09 -0800113 opEventChannel = datagridService.createChannel(INTENT_OP_EVENT_CHANNEL_NAME, Long.class, IntentOperationList.class);
114 datagridService.addListener(INTENT_STATE_EVENT_CHANNEL_NAME, this, Long.class, IntentStateList.class);
Toshio Koide0c9106d2014-02-19 15:26:38 -0800115 networkGraphService.registerNetworkGraphListener(this);
Toshio Koide798bc1b2014-02-20 14:02:40 -0800116 persistIntent = new PersistIntent(controllerRegistry, networkGraphService);
Toshio Koide4f308732014-02-18 15:19:48 -0800117 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800118
Toshio Koide798bc1b2014-02-20 14:02:40 -0800119 // ================================================================================
120 // IPathCalcRuntimeService implementations
121 // ================================================================================
122
Toshio Koide27ffd412014-02-18 19:15:27 -0800123 @Override
124 public IntentOperationList executeIntentOperations(IntentOperationList list) {
Toshio Koidedf2eab92014-02-20 11:24:59 -0800125 // update the map of high-level intents
Toshio Koidea94060f2014-02-21 22:58:32 -0800126 log("begin_updateInMemoryIntents");
Toshio Koide27ffd412014-02-18 19:15:27 -0800127 highLevelIntents.executeOperations(list);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800128
Toshio Koidea10c0372014-02-20 17:28:10 -0800129 // change states of high-level intents
Toshio Koide066506e2014-02-20 19:52:09 -0800130 IntentStateList states = new IntentStateList();
Toshio Koide798bc1b2014-02-20 14:02:40 -0800131 for (IntentOperation op : list) {
Toshio Koidea10c0372014-02-20 17:28:10 -0800132 if (op.intent.getState().equals(IntentState.INST_ACK))
Toshio Koided7476d02014-02-22 15:59:04 -0800133 states.put(op.intent.getId(), IntentState.REROUTE_REQ);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800134 }
Toshio Koidea10c0372014-02-20 17:28:10 -0800135 highLevelIntents.changeStates(states);
Toshio Koidea94060f2014-02-21 22:58:32 -0800136 log("end_updateInMemoryIntents");
Toshio Koidedf2eab92014-02-20 11:24:59 -0800137
138 // calculate path-intents (low-level operations)
Toshio Koidea94060f2014-02-21 22:58:32 -0800139 log("begin_calcPathIntents");
Toshio Koidedba6fef2014-02-23 15:36:21 -0800140 IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, highLevelIntents, pathIntents);
Toshio Koidea94060f2014-02-21 22:58:32 -0800141 log("end_calcPathIntents");
Toshio Koidedf2eab92014-02-20 11:24:59 -0800142
Toshio Koidea10c0372014-02-20 17:28:10 -0800143 // persist calculated low-level operations into data store
Toshio Koidea94060f2014-02-21 22:58:32 -0800144 log("begin_persistPathIntents");
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800145 long key = persistIntent.getKey();
Toshio Koidedf2eab92014-02-20 11:24:59 -0800146 persistIntent.persistIfLeader(key, pathIntentOperations);
Toshio Koidea94060f2014-02-21 22:58:32 -0800147 log("end_persistPathIntents");
Toshio Koidedf2eab92014-02-20 11:24:59 -0800148
149 // remove error-intents and reflect them to high-level intents
Toshio Koidea94060f2014-02-21 22:58:32 -0800150 log("begin_removeErrorIntents");
Toshio Koidea10c0372014-02-20 17:28:10 -0800151 states.clear();
Toshio Koidedf2eab92014-02-20 11:24:59 -0800152 Iterator<IntentOperation> i = pathIntentOperations.iterator();
153 while (i.hasNext()) {
154 IntentOperation op = i.next();
155 if (op.operator.equals(Operator.ERROR)) {
156 states.put(op.intent.getId(), IntentState.INST_NACK);
157 i.remove();
158 }
159 }
160 highLevelIntents.changeStates(states);
Toshio Koidea94060f2014-02-21 22:58:32 -0800161 log("end_removeErrorIntents");
Toshio Koidedf2eab92014-02-20 11:24:59 -0800162
Toshio Koidea94060f2014-02-21 22:58:32 -0800163 // update the map of path intents and publish the path operations
164 log("begin_updateInMemoryPathIntents");
Toshio Koide27ffd412014-02-18 19:15:27 -0800165 pathIntents.executeOperations(pathIntentOperations);
Toshio Koidea94060f2014-02-21 22:58:32 -0800166 log("end_updateInMemoryPathIntents");
Toshio Koide600ae5f2014-02-20 18:42:00 -0800167
Toshio Koidea94060f2014-02-21 22:58:32 -0800168 // Demo special: add a complete path to remove operation
169 log("begin_addPathToRemoveOperation");
Toshio Koide600ae5f2014-02-20 18:42:00 -0800170 for (IntentOperation op: pathIntentOperations) {
171 if(op.operator.equals(Operator.REMOVE)) {
172 op.intent = pathIntents.getIntent(op.intent.getId());
173 }
174 }
Toshio Koidea94060f2014-02-21 22:58:32 -0800175 log("end_addPathToRemoveOperation");
176
177 // send notification
178 log("begin_sendNotification");
Toshio Koide066506e2014-02-20 19:52:09 -0800179 opEventChannel.addEntry(key, pathIntentOperations);
Toshio Koidea94060f2014-02-21 22:58:32 -0800180 log("end_sendNotification");
Toshio Koide27ffd412014-02-18 19:15:27 -0800181 return pathIntentOperations;
182 }
183
184 @Override
185 public IntentMap getHighLevelIntents() {
Toshio Koide4f308732014-02-18 15:19:48 -0800186 return highLevelIntents;
187 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800188
189 @Override
190 public IntentMap getPathIntents() {
191 return pathIntents;
192 }
193
194 @Override
Toshio Koide4f308732014-02-18 15:19:48 -0800195 public void purgeIntents() {
196 highLevelIntents.purge();
Toshio Koide27ffd412014-02-18 19:15:27 -0800197 pathIntents.purge();
Toshio Koide4f308732014-02-18 15:19:48 -0800198 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800199
Toshio Koide798bc1b2014-02-20 14:02:40 -0800200 // ================================================================================
201 // INetworkGraphListener implementations
202 // ================================================================================
203
Toshio Koide0c9106d2014-02-19 15:26:38 -0800204 @Override
Toshio Koide798bc1b2014-02-20 14:02:40 -0800205 public void networkGraphEvents(Collection<SwitchEvent> addedSwitchEvents,
206 Collection<SwitchEvent> removedSwitchEvents,
207 Collection<PortEvent> addedPortEvents,
208 Collection<PortEvent> removedPortEvents,
209 Collection<LinkEvent> addedLinkEvents,
210 Collection<LinkEvent> removedLinkEvents,
211 Collection<DeviceEvent> addedDeviceEvents,
212 Collection<DeviceEvent> removedDeviceEvents) {
Toshio Koidea9078af2014-02-21 16:57:04 -0800213
Toshio Koidea94060f2014-02-21 22:58:32 -0800214 log("called_networkGraphEvents");
215 HashSet<Intent> affectedPaths = new HashSet<>();
Toshio Koidea9078af2014-02-21 16:57:04 -0800216
Toshio Koidea94060f2014-02-21 22:58:32 -0800217 if (addedLinkEvents.size() > 0 ||
218 addedPortEvents.size() > 0 ||
219 addedSwitchEvents.size() > 0) {
220 log("begin_getAllIntents");
221 affectedPaths.addAll(getPathIntents().getAllIntents());
222 log("end_getAllIntents");
223 }
224 else {
225 log("begin_getIntentsByLink");
226 for (LinkEvent linkEvent: removedLinkEvents)
227 affectedPaths.addAll(pathIntents.getIntentsByLink(linkEvent));
228 log("end_getIntentsByLink");
Toshio Koidea9078af2014-02-21 16:57:04 -0800229
Toshio Koidea94060f2014-02-21 22:58:32 -0800230 log("begin_getIntentsByPort");
231 for (PortEvent portEvent: removedPortEvents)
232 affectedPaths.addAll(pathIntents.getIntentsByPort(portEvent.getDpid(), portEvent.getNumber()));
233 log("end_getIntentsByPort");
Toshio Koidea9078af2014-02-21 16:57:04 -0800234
Toshio Koidea94060f2014-02-21 22:58:32 -0800235 log("begin_getIntentsByDpid");
236 for (SwitchEvent switchEvent: removedSwitchEvents)
237 affectedPaths.addAll(pathIntents.getIntentsByDpid(switchEvent.getDpid()));
238 log("end_getIntentsByDpid");
239 }
Toshio Koidea9078af2014-02-21 16:57:04 -0800240 reroutePaths(affectedPaths);
Toshio Koidea94060f2014-02-21 22:58:32 -0800241 log("finished_networkGraphEvents");
Toshio Koide0c9106d2014-02-19 15:26:38 -0800242 }
Toshio Koide066506e2014-02-20 19:52:09 -0800243
244 // ================================================================================
245 // IEventChannelListener implementations
246 // ================================================================================
247
248 @Override
249 public void entryAdded(IntentStateList value) {
Toshio Koidea94060f2014-02-21 22:58:32 -0800250 log("called_EntryAdded");
Toshio Koide066506e2014-02-20 19:52:09 -0800251 entryUpdated(value);
252 }
253
254 @Override
255 public void entryRemoved(IntentStateList value) {
256 // do nothing
257 }
258
259 @Override
260 public void entryUpdated(IntentStateList value) {
Toshio Koide8315d7d2014-02-21 22:58:32 -0800261 // TODO draw state transition diagram in multiple ONOS instances and update this method
262
Toshio Koidea94060f2014-02-21 22:58:32 -0800263 log("called_EntryUpdated");
Toshio Koide066506e2014-02-20 19:52:09 -0800264 // reflect state changes of path-level intent into application-level intents
Toshio Koidea94060f2014-02-21 22:58:32 -0800265 log("begin_changeStateByNotification");
Toshio Koide066506e2014-02-20 19:52:09 -0800266 IntentStateList parentStates = new IntentStateList();
267 for (Entry<String, IntentState> entry: value.entrySet()) {
268 PathIntent pathIntent = (PathIntent) pathIntents.getIntent(entry.getKey());
269 if (pathIntent == null) continue;
270
271 Intent parentIntent = pathIntent.getParentIntent();
272 if (parentIntent == null ||
273 !(parentIntent instanceof ShortestPathIntent) ||
274 !((ShortestPathIntent) parentIntent).getPathIntentId().equals(pathIntent.getId()))
275 continue;
276
277 IntentState state = entry.getValue();
278 switch (state) {
279 case INST_ACK:
280 case INST_NACK:
281 case DEL_ACK:
282 case DEL_PENDING:
283 parentStates.put(parentIntent.getId(), state);
284 break;
285 default:
286 break;
287 }
288 }
289 highLevelIntents.changeStates(parentStates);
290 pathIntents.changeStates(value);
Toshio Koidea94060f2014-02-21 22:58:32 -0800291 log("end_changeStateByNotification");
Toshio Koide066506e2014-02-20 19:52:09 -0800292 }
Toshio Koidea9078af2014-02-21 16:57:04 -0800293}