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; |
| 9 | |
| 10 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 11 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 12 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 13 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 14 | import net.onrc.onos.datagrid.IDatagridService; |
| 15 | import net.onrc.onos.datagrid.IEventChannel; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 16 | import net.onrc.onos.intent.Intent.IntentState; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 17 | import net.onrc.onos.intent.IntentMap; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 18 | import net.onrc.onos.intent.IntentOperation; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 19 | import net.onrc.onos.intent.IntentOperation.Operator; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 20 | import net.onrc.onos.intent.IntentOperationList; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 21 | import net.onrc.onos.intent.PathIntent; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 22 | import net.onrc.onos.intent.PathIntentMap; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 23 | import net.onrc.onos.intent.persist.PersistIntent; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 24 | import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent; |
| 25 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphListener; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 26 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
| 28 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent; |
| 29 | import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 30 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 31 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 32 | /** |
| 33 | * @author Toshio Koide (t-koide@onlab.us) |
| 34 | */ |
| 35 | public class PathCalcRuntimeModule implements IFloodlightModule, IPathCalcRuntimeService, INetworkGraphListener { |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 36 | private PathCalcRuntime runtime; |
| 37 | private IDatagridService datagridService; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 38 | private INetworkGraphService networkGraphService; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 39 | private IntentMap highLevelIntents; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 40 | private PathIntentMap pathIntents; |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 41 | private IControllerRegistryService controllerRegistry; |
| 42 | private PersistIntent persistIntent; |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 43 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 44 | private IEventChannel<Long, IntentOperationList> eventChannel; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 45 | private static final String EVENT_CHANNEL_NAME = "onos.pathintent"; |
| 46 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 47 | // ================================================================================ |
| 48 | // private methods |
| 49 | // ================================================================================ |
| 50 | |
| 51 | private void reroutePaths(Collection<LinkEvent> removedLinkEvents) { |
| 52 | HashSet<PathIntent> oldPaths = new HashSet<>(); |
| 53 | for (LinkEvent linkEvent : removedLinkEvents) { |
| 54 | Collection<PathIntent> intents = pathIntents.getIntentsByLink(linkEvent); |
| 55 | if (intents == null) |
| 56 | continue; |
| 57 | oldPaths.addAll(intents); |
| 58 | } |
| 59 | |
| 60 | if (oldPaths.isEmpty()) |
| 61 | return; |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 62 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 63 | IntentOperationList reroutingOperation = new IntentOperationList(); |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 64 | for (PathIntent pathIntent : oldPaths) { |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 65 | reroutingOperation.add(Operator.ADD, pathIntent.getParentIntent()); |
| 66 | } |
| 67 | executeIntentOperations(reroutingOperation); |
| 68 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 69 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 70 | // ================================================================================ |
| 71 | // IFloodlightModule implementations |
| 72 | // ================================================================================ |
| 73 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 74 | @Override |
| 75 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 76 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(1); |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 77 | l.add(IPathCalcRuntimeService.class); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 78 | return l; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 83 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(1); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 84 | m.put(IPathCalcRuntimeService.class, this); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 85 | return m; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 90 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(); |
| 91 | l.add(IDatagridService.class); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 92 | l.add(INetworkGraphService.class); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 93 | return l; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
| 98 | datagridService = context.getServiceImpl(IDatagridService.class); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 99 | networkGraphService = context.getServiceImpl(INetworkGraphService.class); |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 100 | controllerRegistry = context.getServiceImpl(IControllerRegistryService.class); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void startUp(FloodlightModuleContext context) { |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 105 | highLevelIntents = new IntentMap(); |
| 106 | runtime = new PathCalcRuntime(networkGraphService.getNetworkGraph()); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 107 | pathIntents = new PathIntentMap(); |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 108 | eventChannel = datagridService.createChannel(EVENT_CHANNEL_NAME, Long.class, IntentOperationList.class); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 109 | networkGraphService.registerNetworkGraphListener(this); |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 110 | persistIntent = new PersistIntent(controllerRegistry, networkGraphService); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 111 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 112 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 113 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 114 | // ================================================================================ |
| 115 | // IPathCalcRuntimeService implementations |
| 116 | // ================================================================================ |
| 117 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 118 | @Override |
| 119 | public IntentOperationList executeIntentOperations(IntentOperationList list) { |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 120 | // update the map of high-level intents |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 121 | highLevelIntents.executeOperations(list); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 122 | |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 123 | // change states of high-level intents |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 124 | HashMap<String, IntentState> states = new HashMap<>(); |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 125 | for (IntentOperation op : list) { |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 126 | String id = op.intent.getId(); |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 127 | if (op.intent.getState().equals(IntentState.INST_ACK)) |
| 128 | states.put(id, IntentState.REROUTE_REQ); |
| 129 | else |
| 130 | states.put(id, IntentState.INST_REQ); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 131 | } |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 132 | highLevelIntents.changeStates(states); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 133 | |
| 134 | // calculate path-intents (low-level operations) |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 135 | IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, pathIntents); |
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 | // persist calculated low-level operations into data store |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 138 | long key = persistIntent.getKey(); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 139 | persistIntent.persistIfLeader(key, pathIntentOperations); |
| 140 | |
| 141 | // remove error-intents and reflect them to high-level intents |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 142 | states.clear(); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 143 | Iterator<IntentOperation> i = pathIntentOperations.iterator(); |
| 144 | while (i.hasNext()) { |
| 145 | IntentOperation op = i.next(); |
| 146 | if (op.operator.equals(Operator.ERROR)) { |
| 147 | states.put(op.intent.getId(), IntentState.INST_NACK); |
| 148 | i.remove(); |
| 149 | } |
| 150 | } |
| 151 | highLevelIntents.changeStates(states); |
| 152 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 153 | // update the map of low-level intents and publish the low-level |
| 154 | // operations |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 155 | pathIntents.executeOperations(pathIntentOperations); |
Toshio Koide | 600ae5f | 2014-02-20 18:42:00 -0800 | [diff] [blame] | 156 | |
| 157 | // send remove operation includes intent which has a complete path |
| 158 | // TODO need optimization |
| 159 | for (IntentOperation op: pathIntentOperations) { |
| 160 | if(op.operator.equals(Operator.REMOVE)) { |
| 161 | op.intent = pathIntents.getIntent(op.intent.getId()); |
| 162 | } |
| 163 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 164 | eventChannel.addEntry(key, pathIntentOperations); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 165 | return pathIntentOperations; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public IntentMap getHighLevelIntents() { |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 170 | return highLevelIntents; |
| 171 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 172 | |
| 173 | @Override |
| 174 | public IntentMap getPathIntents() { |
| 175 | return pathIntents; |
| 176 | } |
| 177 | |
| 178 | @Override |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 179 | public void purgeIntents() { |
| 180 | highLevelIntents.purge(); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 181 | pathIntents.purge(); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 182 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 183 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 184 | // ================================================================================ |
| 185 | // INetworkGraphListener implementations |
| 186 | // ================================================================================ |
| 187 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 188 | @Override |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 189 | public void networkGraphEvents(Collection<SwitchEvent> addedSwitchEvents, |
| 190 | Collection<SwitchEvent> removedSwitchEvents, |
| 191 | Collection<PortEvent> addedPortEvents, |
| 192 | Collection<PortEvent> removedPortEvents, |
| 193 | Collection<LinkEvent> addedLinkEvents, |
| 194 | Collection<LinkEvent> removedLinkEvents, |
| 195 | Collection<DeviceEvent> addedDeviceEvents, |
| 196 | Collection<DeviceEvent> removedDeviceEvents) { |
| 197 | // TODO need optimization. |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 198 | // Only high-level intents that affected by link down are rerouted. |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 199 | reroutePaths(removedLinkEvents); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 200 | } |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 201 | } |