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