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