Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 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 | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 8 | import java.util.LinkedList; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 9 | import java.util.Map; |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 10 | import java.util.Map.Entry; |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 11 | import java.util.concurrent.locks.ReentrantLock; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 12 | |
| 13 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 14 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 18 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 19 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.intent.Intent; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 21 | import net.onrc.onos.core.intent.Intent.IntentState; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.intent.IntentMap; |
| 23 | import net.onrc.onos.core.intent.IntentOperation; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.intent.IntentOperation.Operator; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.intent.IntentOperationList; |
| 26 | import net.onrc.onos.core.intent.PathIntent; |
| 27 | import net.onrc.onos.core.intent.PathIntentMap; |
| 28 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.topology.DeviceEvent; |
| 31 | import net.onrc.onos.core.topology.INetworkGraphListener; |
| 32 | import net.onrc.onos.core.topology.INetworkGraphService; |
| 33 | import net.onrc.onos.core.topology.LinkEvent; |
| 34 | import net.onrc.onos.core.topology.PortEvent; |
| 35 | import net.onrc.onos.core.topology.SwitchEvent; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 36 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 40 | /** |
| 41 | * @author Toshio Koide (t-koide@onlab.us) |
| 42 | */ |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 43 | public class PathCalcRuntimeModule implements IFloodlightModule, IPathCalcRuntimeService, INetworkGraphListener, IEventChannelListener<Long, IntentStateList> { |
Pavlin Radoslavov | fee8098 | 2014-04-10 12:12:04 -0700 | [diff] [blame] | 44 | static class PerfLog { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 45 | private String step; |
| 46 | private long time; |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 47 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | public PerfLog(String step) { |
| 49 | this.step = step; |
| 50 | this.time = System.nanoTime(); |
| 51 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 52 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | public void logThis() { |
| 54 | log.error("Time:{}, Step:{}", time, step); |
| 55 | } |
| 56 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 57 | |
Pavlin Radoslavov | 53ad5e3 | 2014-04-10 14:24:26 -0700 | [diff] [blame] | 58 | static class PerfLogger { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | private LinkedList<PerfLog> logData = new LinkedList<>(); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 60 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | public PerfLogger(String logPhase) { |
| 62 | log("start_" + logPhase); |
| 63 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 64 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 65 | public void log(String step) { |
| 66 | logData.add(new PerfLog(step)); |
| 67 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 68 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | public void flushLog() { |
| 70 | log("finish"); |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 71 | for (PerfLog perfLog : logData) { |
| 72 | perfLog.logThis(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | } |
| 74 | logData.clear(); |
| 75 | } |
| 76 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 77 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | private PathCalcRuntime runtime; |
| 79 | private IDatagridService datagridService; |
| 80 | private INetworkGraphService networkGraphService; |
| 81 | private IntentMap highLevelIntents; |
| 82 | private PathIntentMap pathIntents; |
| 83 | private IControllerRegistryService controllerRegistry; |
| 84 | private PersistIntent persistIntent; |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 85 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | private IEventChannel<Long, IntentOperationList> opEventChannel; |
| 87 | private final ReentrantLock lock = new ReentrantLock(); |
| 88 | private HashSet<LinkEvent> unmatchedLinkEvents = new HashSet<>(); |
| 89 | private static final String INTENT_OP_EVENT_CHANNEL_NAME = "onos.pathintent"; |
| 90 | private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state"; |
| 91 | private static final Logger log = LoggerFactory.getLogger(PathCalcRuntimeModule.class); |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 92 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 93 | // ================================================================================ |
| 94 | // private methods |
| 95 | // ================================================================================ |
| 96 | |
| 97 | private void reroutePaths(Collection<Intent> oldPaths) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 98 | if (oldPaths == null || oldPaths.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | return; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 100 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | |
| 102 | IntentOperationList reroutingOperation = new IntentOperationList(); |
| 103 | for (Intent intent : oldPaths) { |
| 104 | PathIntent pathIntent = (PathIntent) intent; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 105 | if (pathIntent.isPathFrozen()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 107 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | if (pathIntent.getState().equals(IntentState.INST_ACK) && // XXX: path intents in flight |
| 109 | !reroutingOperation.contains(pathIntent.getParentIntent())) { |
| 110 | reroutingOperation.add(Operator.ADD, pathIntent.getParentIntent()); |
| 111 | } |
| 112 | } |
| 113 | executeIntentOperations(reroutingOperation); |
| 114 | } |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 115 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 116 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 117 | // ================================================================================ |
| 118 | // IFloodlightModule implementations |
| 119 | // ================================================================================ |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 120 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 123 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(1); |
| 124 | l.add(IPathCalcRuntimeService.class); |
| 125 | return l; |
| 126 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 127 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 128 | @Override |
| 129 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 130 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(); |
| 131 | m.put(IPathCalcRuntimeService.class, this); |
| 132 | return m; |
| 133 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 134 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 137 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(2); |
| 138 | l.add(IDatagridService.class); |
| 139 | l.add(INetworkGraphService.class); |
| 140 | return l; |
| 141 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 142 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
| 145 | datagridService = context.getServiceImpl(IDatagridService.class); |
| 146 | networkGraphService = context.getServiceImpl(INetworkGraphService.class); |
| 147 | controllerRegistry = context.getServiceImpl(IControllerRegistryService.class); |
| 148 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 149 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public void startUp(FloodlightModuleContext context) { |
| 152 | highLevelIntents = new IntentMap(); |
| 153 | runtime = new PathCalcRuntime(networkGraphService.getNetworkGraph()); |
| 154 | pathIntents = new PathIntentMap(); |
| 155 | opEventChannel = datagridService.createChannel(INTENT_OP_EVENT_CHANNEL_NAME, Long.class, IntentOperationList.class); |
| 156 | datagridService.addListener(INTENT_STATE_EVENT_CHANNEL_NAME, this, Long.class, IntentStateList.class); |
| 157 | networkGraphService.registerNetworkGraphListener(this); |
Pavlin Radoslavov | 0294e05 | 2014-04-10 13:36:45 -0700 | [diff] [blame] | 158 | persistIntent = new PersistIntent(controllerRegistry); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 159 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 160 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 161 | // ================================================================================ |
| 162 | // IPathCalcRuntimeService implementations |
| 163 | // ================================================================================ |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 164 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 165 | @Override |
| 166 | public IntentOperationList executeIntentOperations(IntentOperationList list) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 167 | if (list == null || list.size() == 0) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 168 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 169 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 170 | PerfLogger p = new PerfLogger("executeIntentOperations_" + list.get(0).operator); |
Toshio Koide | 275d814 | 2014-02-24 16:41:52 -0800 | [diff] [blame] | 171 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 172 | lock.lock(); // TODO optimize locking using smaller steps |
| 173 | try { |
| 174 | // update the map of high-level intents |
| 175 | p.log("begin_updateInMemoryIntents"); |
| 176 | highLevelIntents.executeOperations(list); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 177 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | // change states of high-level intents |
| 179 | IntentStateList states = new IntentStateList(); |
| 180 | for (IntentOperation op : list) { |
| 181 | switch (op.operator) { |
| 182 | case ADD: |
| 183 | switch (op.intent.getState()) { |
| 184 | case CREATED: |
| 185 | states.put(op.intent.getId(), IntentState.INST_REQ); |
| 186 | break; |
| 187 | case INST_ACK: |
| 188 | states.put(op.intent.getId(), IntentState.REROUTE_REQ); |
| 189 | break; |
| 190 | default: |
| 191 | break; |
| 192 | } |
| 193 | break; |
| 194 | case REMOVE: |
| 195 | switch (op.intent.getState()) { |
| 196 | case CREATED: |
| 197 | states.put(op.intent.getId(), IntentState.DEL_REQ); |
| 198 | break; |
| 199 | default: |
| 200 | break; |
| 201 | } |
| 202 | break; |
| 203 | default: |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | highLevelIntents.changeStates(states); |
| 208 | p.log("end_updateInMemoryIntents"); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 209 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 210 | // calculate path-intents (low-level operations) |
| 211 | p.log("begin_calcPathIntents"); |
| 212 | IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, highLevelIntents, pathIntents); |
| 213 | p.log("end_calcPathIntents"); |
Toshio Koide | 600ae5f | 2014-02-20 18:42:00 -0800 | [diff] [blame] | 214 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 215 | // persist calculated low-level operations into data store |
| 216 | p.log("begin_persistPathIntents"); |
| 217 | long key = persistIntent.getKey(); |
| 218 | persistIntent.persistIfLeader(key, pathIntentOperations); |
| 219 | p.log("end_persistPathIntents"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 220 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 221 | // remove error-intents and reflect them to high-level intents |
| 222 | p.log("begin_removeErrorIntents"); |
| 223 | states.clear(); |
| 224 | Iterator<IntentOperation> i = pathIntentOperations.iterator(); |
| 225 | while (i.hasNext()) { |
| 226 | IntentOperation op = i.next(); |
| 227 | if (op.operator.equals(Operator.ERROR)) { |
| 228 | states.put(op.intent.getId(), IntentState.INST_NACK); |
| 229 | i.remove(); |
| 230 | } |
| 231 | } |
| 232 | highLevelIntents.changeStates(states); |
| 233 | p.log("end_removeErrorIntents"); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 234 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 235 | // update the map of path intents and publish the path operations |
| 236 | p.log("begin_updateInMemoryPathIntents"); |
| 237 | pathIntents.executeOperations(pathIntentOperations); |
| 238 | p.log("end_updateInMemoryPathIntents"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 239 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 240 | // XXX Demo special: add a complete path to remove operation |
| 241 | p.log("begin_addPathToRemoveOperation"); |
| 242 | for (IntentOperation op : pathIntentOperations) { |
| 243 | if (op.operator.equals(Operator.REMOVE)) { |
| 244 | op.intent = pathIntents.getIntent(op.intent.getId()); |
| 245 | } |
| 246 | if (op.intent instanceof PathIntent) { |
| 247 | log.debug("operation: {}, intent:{}", op.operator, op.intent); |
| 248 | } |
| 249 | } |
| 250 | p.log("end_addPathToRemoveOperation"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 251 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 252 | // send notification |
| 253 | p.log("begin_sendNotification"); |
| 254 | // XXX: Send notifications using the same key every time |
| 255 | // and receive them by entryAdded() and entryUpdated() |
| 256 | opEventChannel.addEntry(0L, pathIntentOperations); |
| 257 | p.log("end_sendNotification"); |
| 258 | //opEventChannel.removeEntry(key); |
| 259 | return pathIntentOperations; |
| 260 | } finally { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 261 | lock.unlock(); |
Pavlin Radoslavov | c68974f | 2014-04-09 17:28:38 -0700 | [diff] [blame] | 262 | p.flushLog(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 265 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 266 | @Override |
| 267 | public IntentMap getHighLevelIntents() { |
| 268 | return highLevelIntents; |
| 269 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 270 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 271 | @Override |
| 272 | public IntentMap getPathIntents() { |
| 273 | return pathIntents; |
| 274 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 275 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 276 | @Override |
| 277 | public void purgeIntents() { |
| 278 | highLevelIntents.purge(); |
| 279 | pathIntents.purge(); |
| 280 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 281 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 282 | // ================================================================================ |
| 283 | // INetworkGraphListener implementations |
| 284 | // ================================================================================ |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 285 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 286 | @Override |
| 287 | public void networkGraphEvents(Collection<SwitchEvent> addedSwitchEvents, |
| 288 | Collection<SwitchEvent> removedSwitchEvents, |
| 289 | Collection<PortEvent> addedPortEvents, |
| 290 | Collection<PortEvent> removedPortEvents, |
| 291 | Collection<LinkEvent> addedLinkEvents, |
| 292 | Collection<LinkEvent> removedLinkEvents, |
| 293 | Collection<DeviceEvent> addedDeviceEvents, |
| 294 | Collection<DeviceEvent> removedDeviceEvents) { |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 295 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 296 | PerfLogger p = new PerfLogger("networkGraphEvents"); |
| 297 | HashSet<Intent> affectedPaths = new HashSet<>(); |
Toshio Koide | 93797dc | 2014-02-27 23:54:26 -0800 | [diff] [blame] | 298 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 299 | boolean rerouteAll = false; |
| 300 | for (LinkEvent le : addedLinkEvents) { |
| 301 | LinkEvent rev = new LinkEvent(le.getDst().getDpid(), le.getDst().getNumber(), le.getSrc().getDpid(), le.getSrc().getNumber()); |
| 302 | if (unmatchedLinkEvents.contains(rev)) { |
| 303 | rerouteAll = true; |
| 304 | unmatchedLinkEvents.remove(rev); |
| 305 | log.debug("Found matched LinkEvent: {} {}", rev, le); |
| 306 | } else { |
| 307 | unmatchedLinkEvents.add(le); |
| 308 | log.debug("Adding unmatched LinkEvent: {}", le); |
| 309 | } |
| 310 | } |
| 311 | for (LinkEvent le : removedLinkEvents) { |
| 312 | if (unmatchedLinkEvents.contains(le)) { |
| 313 | unmatchedLinkEvents.remove(le); |
| 314 | log.debug("Removing LinkEvent: {}", le); |
| 315 | } |
| 316 | } |
| 317 | if (unmatchedLinkEvents.size() > 0) { |
| 318 | log.debug("Unmatched link events: {} events", unmatchedLinkEvents.size()); |
| 319 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 320 | |
Ray Milkey | 7f1567c | 2014-04-08 13:53:32 -0700 | [diff] [blame] | 321 | if (rerouteAll) { //addedLinkEvents.size() > 0) { // || |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 322 | // addedPortEvents.size() > 0 || |
| 323 | // addedSwitchEvents.size() > 0) { |
| 324 | p.log("begin_getAllIntents"); |
| 325 | affectedPaths.addAll(getPathIntents().getAllIntents()); |
| 326 | p.log("end_getAllIntents"); |
| 327 | } else if (removedSwitchEvents.size() > 0 || |
| 328 | removedLinkEvents.size() > 0 || |
| 329 | removedPortEvents.size() > 0) { |
| 330 | p.log("begin_getIntentsByLink"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 331 | for (LinkEvent linkEvent : removedLinkEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 332 | affectedPaths.addAll(pathIntents.getIntentsByLink(linkEvent)); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 333 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 334 | p.log("end_getIntentsByLink"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 335 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 336 | p.log("begin_getIntentsByPort"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 337 | for (PortEvent portEvent : removedPortEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 338 | affectedPaths.addAll(pathIntents.getIntentsByPort(portEvent.getDpid(), portEvent.getNumber())); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 339 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 340 | p.log("end_getIntentsByPort"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 341 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 342 | p.log("begin_getIntentsByDpid"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 343 | for (SwitchEvent switchEvent : removedSwitchEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 344 | affectedPaths.addAll(pathIntents.getIntentsByDpid(switchEvent.getDpid())); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 345 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 346 | p.log("end_getIntentsByDpid"); |
| 347 | } |
| 348 | p.log("begin_reroutePaths"); |
| 349 | reroutePaths(affectedPaths); |
| 350 | p.log("end_reroutePaths"); |
| 351 | p.flushLog(); |
| 352 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 353 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 354 | // ================================================================================ |
| 355 | // IEventChannelListener implementations |
| 356 | // ================================================================================ |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 357 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 358 | @Override |
| 359 | public void entryAdded(IntentStateList value) { |
| 360 | entryUpdated(value); |
| 361 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 362 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 363 | @Override |
| 364 | public void entryRemoved(IntentStateList value) { |
| 365 | // do nothing |
| 366 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 367 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 368 | @Override |
| 369 | public void entryUpdated(IntentStateList value) { |
| 370 | // TODO draw state transition diagram in multiple ONOS instances and update this method |
| 371 | PerfLogger p = new PerfLogger("entryUpdated"); |
| 372 | lock.lock(); // TODO optimize locking using smaller steps |
| 373 | try { |
| 374 | // reflect state changes of path-level intent into application-level intents |
| 375 | p.log("begin_changeStateByNotification"); |
| 376 | IntentStateList highLevelIntentStates = new IntentStateList(); |
| 377 | IntentStateList pathIntentStates = new IntentStateList(); |
| 378 | for (Entry<String, IntentState> entry : value.entrySet()) { |
| 379 | PathIntent pathIntent = (PathIntent) pathIntents.getIntent(entry.getKey()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 380 | if (pathIntent == null) { |
| 381 | continue; |
| 382 | } |
Toshio Koide | 8315d7d | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 383 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 384 | Intent parentIntent = pathIntent.getParentIntent(); |
| 385 | if (parentIntent == null || |
| 386 | !(parentIntent instanceof ShortestPathIntent) || |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 387 | !((ShortestPathIntent) parentIntent).getPathIntentId().equals(pathIntent.getId())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 388 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 389 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 390 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 391 | IntentState state = entry.getValue(); |
| 392 | switch (state) { |
| 393 | //case INST_REQ: |
| 394 | case INST_ACK: |
| 395 | case INST_NACK: |
| 396 | //case DEL_REQ: |
| 397 | case DEL_ACK: |
| 398 | case DEL_PENDING: |
| 399 | highLevelIntentStates.put(parentIntent.getId(), state); |
| 400 | pathIntentStates.put(entry.getKey(), entry.getValue()); |
| 401 | break; |
| 402 | default: |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | highLevelIntents.changeStates(highLevelIntentStates); |
| 407 | pathIntents.changeStates(pathIntentStates); |
| 408 | p.log("end_changeStateByNotification"); |
| 409 | } finally { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 410 | lock.unlock(); |
Pavlin Radoslavov | c68974f | 2014-04-09 17:28:38 -0700 | [diff] [blame] | 411 | p.flushLog(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 414 | } |