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