blob: b7c355d27efd42fc59410b9a6d4d95e52ac63e06 [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 Koidea94060f2014-02-21 22:58:32 -0800128 log("end_updateInMemoryIntents");
Toshio Koidedf2eab92014-02-20 11:24:59 -0800129
Toshio Koidea10c0372014-02-20 17:28:10 -0800130 // change states of high-level intents
Toshio Koidea94060f2014-02-21 22:58:32 -0800131 log("begin_updateInMemoryIntents");
Toshio Koide066506e2014-02-20 19:52:09 -0800132 IntentStateList states = new IntentStateList();
Toshio Koide798bc1b2014-02-20 14:02:40 -0800133 for (IntentOperation op : list) {
Toshio Koidedf2eab92014-02-20 11:24:59 -0800134 String id = op.intent.getId();
Toshio Koidea10c0372014-02-20 17:28:10 -0800135 if (op.intent.getState().equals(IntentState.INST_ACK))
136 states.put(id, IntentState.REROUTE_REQ);
137 else
138 states.put(id, IntentState.INST_REQ);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800139 }
Toshio Koidea10c0372014-02-20 17:28:10 -0800140 highLevelIntents.changeStates(states);
Toshio Koidea94060f2014-02-21 22:58:32 -0800141 log("end_updateInMemoryIntents");
Toshio Koidedf2eab92014-02-20 11:24:59 -0800142
143 // calculate path-intents (low-level operations)
Toshio Koidea94060f2014-02-21 22:58:32 -0800144 log("begin_calcPathIntents");
Toshio Koide27ffd412014-02-18 19:15:27 -0800145 IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, pathIntents);
Toshio Koidea94060f2014-02-21 22:58:32 -0800146 log("end_calcPathIntents");
Toshio Koidedf2eab92014-02-20 11:24:59 -0800147
Toshio Koidea10c0372014-02-20 17:28:10 -0800148 // persist calculated low-level operations into data store
Toshio Koidea94060f2014-02-21 22:58:32 -0800149 log("begin_persistPathIntents");
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800150 long key = persistIntent.getKey();
Toshio Koidedf2eab92014-02-20 11:24:59 -0800151 persistIntent.persistIfLeader(key, pathIntentOperations);
Toshio Koidea94060f2014-02-21 22:58:32 -0800152 log("end_persistPathIntents");
Toshio Koidedf2eab92014-02-20 11:24:59 -0800153
154 // remove error-intents and reflect them to high-level intents
Toshio Koidea94060f2014-02-21 22:58:32 -0800155 log("begin_removeErrorIntents");
Toshio Koidea10c0372014-02-20 17:28:10 -0800156 states.clear();
Toshio Koidedf2eab92014-02-20 11:24:59 -0800157 Iterator<IntentOperation> i = pathIntentOperations.iterator();
158 while (i.hasNext()) {
159 IntentOperation op = i.next();
160 if (op.operator.equals(Operator.ERROR)) {
161 states.put(op.intent.getId(), IntentState.INST_NACK);
162 i.remove();
163 }
164 }
165 highLevelIntents.changeStates(states);
Toshio Koidea94060f2014-02-21 22:58:32 -0800166 log("end_removeErrorIntents");
Toshio Koidedf2eab92014-02-20 11:24:59 -0800167
Toshio Koidea94060f2014-02-21 22:58:32 -0800168 // update the map of path intents and publish the path operations
169 log("begin_updateInMemoryPathIntents");
Toshio Koide27ffd412014-02-18 19:15:27 -0800170 pathIntents.executeOperations(pathIntentOperations);
Toshio Koidea94060f2014-02-21 22:58:32 -0800171 log("end_updateInMemoryPathIntents");
Toshio Koide600ae5f2014-02-20 18:42:00 -0800172
Toshio Koidea94060f2014-02-21 22:58:32 -0800173 // Demo special: add a complete path to remove operation
174 log("begin_addPathToRemoveOperation");
Toshio Koide600ae5f2014-02-20 18:42:00 -0800175 for (IntentOperation op: pathIntentOperations) {
176 if(op.operator.equals(Operator.REMOVE)) {
177 op.intent = pathIntents.getIntent(op.intent.getId());
178 }
179 }
Toshio Koidea94060f2014-02-21 22:58:32 -0800180 log("end_addPathToRemoveOperation");
181
182 // send notification
183 log("begin_sendNotification");
Toshio Koide066506e2014-02-20 19:52:09 -0800184 opEventChannel.addEntry(key, pathIntentOperations);
Toshio Koidea94060f2014-02-21 22:58:32 -0800185 log("end_sendNotification");
Toshio Koide27ffd412014-02-18 19:15:27 -0800186 return pathIntentOperations;
187 }
188
189 @Override
190 public IntentMap getHighLevelIntents() {
Toshio Koide4f308732014-02-18 15:19:48 -0800191 return highLevelIntents;
192 }
Toshio Koide27ffd412014-02-18 19:15:27 -0800193
194 @Override
195 public IntentMap getPathIntents() {
196 return pathIntents;
197 }
198
199 @Override
Toshio Koide4f308732014-02-18 15:19:48 -0800200 public void purgeIntents() {
201 highLevelIntents.purge();
Toshio Koide27ffd412014-02-18 19:15:27 -0800202 pathIntents.purge();
Toshio Koide4f308732014-02-18 15:19:48 -0800203 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800204
Toshio Koide798bc1b2014-02-20 14:02:40 -0800205 // ================================================================================
206 // INetworkGraphListener implementations
207 // ================================================================================
208
Toshio Koide0c9106d2014-02-19 15:26:38 -0800209 @Override
Toshio Koide798bc1b2014-02-20 14:02:40 -0800210 public void networkGraphEvents(Collection<SwitchEvent> addedSwitchEvents,
211 Collection<SwitchEvent> removedSwitchEvents,
212 Collection<PortEvent> addedPortEvents,
213 Collection<PortEvent> removedPortEvents,
214 Collection<LinkEvent> addedLinkEvents,
215 Collection<LinkEvent> removedLinkEvents,
216 Collection<DeviceEvent> addedDeviceEvents,
217 Collection<DeviceEvent> removedDeviceEvents) {
Toshio Koidea9078af2014-02-21 16:57:04 -0800218
Toshio Koidea94060f2014-02-21 22:58:32 -0800219 log("called_networkGraphEvents");
220 HashSet<Intent> affectedPaths = new HashSet<>();
Toshio Koidea9078af2014-02-21 16:57:04 -0800221
Toshio Koidea94060f2014-02-21 22:58:32 -0800222 if (addedLinkEvents.size() > 0 ||
223 addedPortEvents.size() > 0 ||
224 addedSwitchEvents.size() > 0) {
225 log("begin_getAllIntents");
226 affectedPaths.addAll(getPathIntents().getAllIntents());
227 log("end_getAllIntents");
228 }
229 else {
230 log("begin_getIntentsByLink");
231 for (LinkEvent linkEvent: removedLinkEvents)
232 affectedPaths.addAll(pathIntents.getIntentsByLink(linkEvent));
233 log("end_getIntentsByLink");
Toshio Koidea9078af2014-02-21 16:57:04 -0800234
Toshio Koidea94060f2014-02-21 22:58:32 -0800235 log("begin_getIntentsByPort");
236 for (PortEvent portEvent: removedPortEvents)
237 affectedPaths.addAll(pathIntents.getIntentsByPort(portEvent.getDpid(), portEvent.getNumber()));
238 log("end_getIntentsByPort");
Toshio Koidea9078af2014-02-21 16:57:04 -0800239
Toshio Koidea94060f2014-02-21 22:58:32 -0800240 log("begin_getIntentsByDpid");
241 for (SwitchEvent switchEvent: removedSwitchEvents)
242 affectedPaths.addAll(pathIntents.getIntentsByDpid(switchEvent.getDpid()));
243 log("end_getIntentsByDpid");
244 }
Toshio Koidea9078af2014-02-21 16:57:04 -0800245 reroutePaths(affectedPaths);
Toshio Koidea94060f2014-02-21 22:58:32 -0800246 log("finished_networkGraphEvents");
Toshio Koide0c9106d2014-02-19 15:26:38 -0800247 }
Toshio Koide066506e2014-02-20 19:52:09 -0800248
249 // ================================================================================
250 // IEventChannelListener implementations
251 // ================================================================================
252
253 @Override
254 public void entryAdded(IntentStateList value) {
Toshio Koidea94060f2014-02-21 22:58:32 -0800255 log("called_EntryAdded");
Toshio Koide066506e2014-02-20 19:52:09 -0800256 entryUpdated(value);
257 }
258
259 @Override
260 public void entryRemoved(IntentStateList value) {
261 // do nothing
262 }
263
264 @Override
265 public void entryUpdated(IntentStateList value) {
Toshio Koide8315d7d2014-02-21 22:58:32 -0800266 // TODO draw state transition diagram in multiple ONOS instances and update this method
267
Toshio Koidea94060f2014-02-21 22:58:32 -0800268 log("called_EntryUpdated");
Toshio Koide066506e2014-02-20 19:52:09 -0800269 // reflect state changes of path-level intent into application-level intents
Toshio Koidea94060f2014-02-21 22:58:32 -0800270 log("begin_changeStateByNotification");
Toshio Koide066506e2014-02-20 19:52:09 -0800271 IntentStateList parentStates = new IntentStateList();
272 for (Entry<String, IntentState> entry: value.entrySet()) {
273 PathIntent pathIntent = (PathIntent) pathIntents.getIntent(entry.getKey());
274 if (pathIntent == null) continue;
275
276 Intent parentIntent = pathIntent.getParentIntent();
277 if (parentIntent == null ||
278 !(parentIntent instanceof ShortestPathIntent) ||
279 !((ShortestPathIntent) parentIntent).getPathIntentId().equals(pathIntent.getId()))
280 continue;
281
282 IntentState state = entry.getValue();
283 switch (state) {
284 case INST_ACK:
285 case INST_NACK:
286 case DEL_ACK:
287 case DEL_PENDING:
288 parentStates.put(parentIntent.getId(), state);
289 break;
290 default:
291 break;
292 }
293 }
294 highLevelIntents.changeStates(parentStates);
295 pathIntents.changeStates(value);
Toshio Koidea94060f2014-02-21 22:58:32 -0800296 log("end_changeStateByNotification");
Toshio Koide066506e2014-02-20 19:52:09 -0800297 }
Toshio Koidea9078af2014-02-21 16:57:04 -0800298}