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