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