Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 1 | package net.onrc.onos.intent.runtime; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 6 | import java.util.HashSet; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 7 | import java.util.Iterator; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 8 | import java.util.Map; |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 9 | import java.util.Map.Entry; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 10 | |
| 11 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 12 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 13 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 14 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 15 | import net.onrc.onos.datagrid.IDatagridService; |
| 16 | import net.onrc.onos.datagrid.IEventChannel; |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 17 | import net.onrc.onos.datagrid.IEventChannelListener; |
| 18 | import net.onrc.onos.intent.Intent; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 19 | import net.onrc.onos.intent.Intent.IntentState; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 20 | import net.onrc.onos.intent.IntentMap; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 21 | import net.onrc.onos.intent.IntentOperation; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 22 | import net.onrc.onos.intent.IntentOperation.Operator; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 23 | import net.onrc.onos.intent.IntentOperationList; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 24 | import net.onrc.onos.intent.PathIntent; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 25 | import net.onrc.onos.intent.PathIntentMap; |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 26 | import net.onrc.onos.intent.ShortestPathIntent; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 27 | import net.onrc.onos.intent.persist.PersistIntent; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent; |
| 29 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
| 32 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent; |
| 33 | import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 34 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 35 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 36 | /** |
| 37 | * @author Toshio Koide (t-koide@onlab.us) |
| 38 | */ |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 39 | public class PathCalcRuntimeModule implements IFloodlightModule, IPathCalcRuntimeService, INetworkGraphListener, IEventChannelListener<Long, IntentStateList> { |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 40 | private PathCalcRuntime runtime; |
| 41 | private IDatagridService datagridService; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 42 | private INetworkGraphService networkGraphService; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 43 | private IntentMap highLevelIntents; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 44 | private PathIntentMap pathIntents; |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 45 | private IControllerRegistryService controllerRegistry; |
| 46 | private PersistIntent persistIntent; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 47 | |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 48 | private IEventChannel<Long, IntentOperationList> opEventChannel; |
| 49 | private static final String INTENT_OP_EVENT_CHANNEL_NAME = "onos.pathintent"; |
| 50 | private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state"; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 51 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 52 | // ================================================================================ |
| 53 | // private methods |
| 54 | // ================================================================================ |
| 55 | |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 56 | private void reroutePaths(Collection<PathIntent> oldPaths) { |
| 57 | if (oldPaths == null || oldPaths.isEmpty()) |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 58 | return; |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 59 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 60 | IntentOperationList reroutingOperation = new IntentOperationList(); |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 61 | for (PathIntent pathIntent : oldPaths) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 62 | reroutingOperation.add(Operator.ADD, pathIntent.getParentIntent()); |
| 63 | } |
| 64 | executeIntentOperations(reroutingOperation); |
| 65 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 66 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 67 | // ================================================================================ |
| 68 | // IFloodlightModule implementations |
| 69 | // ================================================================================ |
| 70 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 71 | @Override |
| 72 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 73 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(1); |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 74 | l.add(IPathCalcRuntimeService.class); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 75 | return l; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 80 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 81 | m.put(IPathCalcRuntimeService.class, this); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 82 | return m; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 87 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(2); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 88 | l.add(IDatagridService.class); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 89 | l.add(INetworkGraphService.class); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 90 | return l; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
| 95 | datagridService = context.getServiceImpl(IDatagridService.class); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 96 | networkGraphService = context.getServiceImpl(INetworkGraphService.class); |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 97 | controllerRegistry = context.getServiceImpl(IControllerRegistryService.class); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void startUp(FloodlightModuleContext context) { |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 102 | highLevelIntents = new IntentMap(); |
| 103 | runtime = new PathCalcRuntime(networkGraphService.getNetworkGraph()); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 104 | pathIntents = new PathIntentMap(); |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 105 | opEventChannel = datagridService.createChannel(INTENT_OP_EVENT_CHANNEL_NAME, Long.class, IntentOperationList.class); |
| 106 | datagridService.addListener(INTENT_STATE_EVENT_CHANNEL_NAME, this, Long.class, IntentStateList.class); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 107 | networkGraphService.registerNetworkGraphListener(this); |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 108 | persistIntent = new PersistIntent(controllerRegistry, networkGraphService); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 109 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 110 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 111 | // ================================================================================ |
| 112 | // IPathCalcRuntimeService implementations |
| 113 | // ================================================================================ |
| 114 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 115 | @Override |
| 116 | public IntentOperationList executeIntentOperations(IntentOperationList list) { |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 117 | // update the map of high-level intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 118 | highLevelIntents.executeOperations(list); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 119 | |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 120 | // change states of high-level intents |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 121 | IntentStateList states = new IntentStateList(); |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 122 | for (IntentOperation op : list) { |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 123 | String id = op.intent.getId(); |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 124 | if (op.intent.getState().equals(IntentState.INST_ACK)) |
| 125 | states.put(id, IntentState.REROUTE_REQ); |
| 126 | else |
| 127 | states.put(id, IntentState.INST_REQ); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 128 | } |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 129 | highLevelIntents.changeStates(states); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 130 | |
| 131 | // calculate path-intents (low-level operations) |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 132 | IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, pathIntents); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 133 | |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 134 | // persist calculated low-level operations into data store |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 135 | long key = persistIntent.getKey(); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 136 | persistIntent.persistIfLeader(key, pathIntentOperations); |
| 137 | |
| 138 | // remove error-intents and reflect them to high-level intents |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 139 | states.clear(); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 140 | Iterator<IntentOperation> i = pathIntentOperations.iterator(); |
| 141 | while (i.hasNext()) { |
| 142 | IntentOperation op = i.next(); |
| 143 | if (op.operator.equals(Operator.ERROR)) { |
| 144 | states.put(op.intent.getId(), IntentState.INST_NACK); |
| 145 | i.remove(); |
| 146 | } |
| 147 | } |
| 148 | highLevelIntents.changeStates(states); |
| 149 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 150 | // update the map of low-level intents and publish the low-level |
| 151 | // operations |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 152 | pathIntents.executeOperations(pathIntentOperations); |
Toshio Koide | 600ae5f | 2014-02-20 18:42:00 -0800 | [diff] [blame] | 153 | |
| 154 | // send remove operation includes intent which has a complete path |
| 155 | // TODO need optimization |
| 156 | for (IntentOperation op: pathIntentOperations) { |
| 157 | if(op.operator.equals(Operator.REMOVE)) { |
| 158 | op.intent = pathIntents.getIntent(op.intent.getId()); |
| 159 | } |
| 160 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 161 | opEventChannel.addEntry(key, pathIntentOperations); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 162 | return pathIntentOperations; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public IntentMap getHighLevelIntents() { |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 167 | return highLevelIntents; |
| 168 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 169 | |
| 170 | @Override |
| 171 | public IntentMap getPathIntents() { |
| 172 | return pathIntents; |
| 173 | } |
| 174 | |
| 175 | @Override |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 176 | public void purgeIntents() { |
| 177 | highLevelIntents.purge(); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 178 | pathIntents.purge(); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 179 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 180 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 181 | // ================================================================================ |
| 182 | // INetworkGraphListener implementations |
| 183 | // ================================================================================ |
| 184 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 185 | @Override |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 186 | public void networkGraphEvents(Collection<SwitchEvent> addedSwitchEvents, |
| 187 | Collection<SwitchEvent> removedSwitchEvents, |
| 188 | Collection<PortEvent> addedPortEvents, |
| 189 | Collection<PortEvent> removedPortEvents, |
| 190 | Collection<LinkEvent> addedLinkEvents, |
| 191 | Collection<LinkEvent> removedLinkEvents, |
| 192 | Collection<DeviceEvent> addedDeviceEvents, |
| 193 | Collection<DeviceEvent> removedDeviceEvents) { |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 194 | |
| 195 | HashSet<PathIntent> affectedPaths = new HashSet<>(); |
| 196 | |
| 197 | for (SwitchEvent switchEvent: removedSwitchEvents) |
| 198 | affectedPaths.addAll(pathIntents.getIntentsByDpid(switchEvent.getDpid())); |
| 199 | |
| 200 | for (PortEvent portEvent: removedPortEvents) |
| 201 | affectedPaths.addAll(pathIntents.getIntentsByPort(portEvent.getDpid(), portEvent.getNumber())); |
| 202 | |
| 203 | for (LinkEvent linkEvent: removedLinkEvents) |
| 204 | affectedPaths.addAll(pathIntents.getIntentsByLink(linkEvent)); |
| 205 | |
| 206 | reroutePaths(affectedPaths); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 207 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 208 | |
| 209 | // ================================================================================ |
| 210 | // IEventChannelListener implementations |
| 211 | // ================================================================================ |
| 212 | |
| 213 | @Override |
| 214 | public void entryAdded(IntentStateList value) { |
| 215 | entryUpdated(value); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void entryRemoved(IntentStateList value) { |
| 220 | // do nothing |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void entryUpdated(IntentStateList value) { |
| 225 | // reflect state changes of path-level intent into application-level intents |
| 226 | IntentStateList parentStates = new IntentStateList(); |
| 227 | for (Entry<String, IntentState> entry: value.entrySet()) { |
| 228 | PathIntent pathIntent = (PathIntent) pathIntents.getIntent(entry.getKey()); |
| 229 | if (pathIntent == null) continue; |
| 230 | |
| 231 | Intent parentIntent = pathIntent.getParentIntent(); |
| 232 | if (parentIntent == null || |
| 233 | !(parentIntent instanceof ShortestPathIntent) || |
| 234 | !((ShortestPathIntent) parentIntent).getPathIntentId().equals(pathIntent.getId())) |
| 235 | continue; |
| 236 | |
| 237 | IntentState state = entry.getValue(); |
| 238 | switch (state) { |
| 239 | case INST_ACK: |
| 240 | case INST_NACK: |
| 241 | case DEL_ACK: |
| 242 | case DEL_PENDING: |
| 243 | parentStates.put(parentIntent.getId(), state); |
| 244 | break; |
| 245 | default: |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | highLevelIntents.changeStates(parentStates); |
| 250 | pathIntents.changeStates(value); |
| 251 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 252 | } |