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