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 | 565d6dd | 2014-03-27 11:22:25 -0700 | [diff] [blame] | 103 | if (pathIntent.isPathFrozen()) |
Toshio Koide | 93797dc | 2014-02-27 23:54:26 -0800 | [diff] [blame] | 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 | 500431f | 2014-02-28 02:02:25 -0800 | [diff] [blame] | 179 | if (op.intent.getState().equals(IntentState.CREATED)) |
| 180 | states.put(op.intent.getId(), IntentState.INST_REQ); // XXX |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 181 | } |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 182 | highLevelIntents.changeStates(states); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 183 | p.log("end_updateInMemoryIntents"); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 184 | |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 185 | // calculate path-intents (low-level operations) |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 186 | p.log("begin_calcPathIntents"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 187 | IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, highLevelIntents, pathIntents); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 188 | p.log("end_calcPathIntents"); |
Toshio Koide | 600ae5f | 2014-02-20 18:42:00 -0800 | [diff] [blame] | 189 | |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 190 | // persist calculated low-level operations into data store |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 191 | p.log("begin_persistPathIntents"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 192 | long key = persistIntent.getKey(); |
| 193 | persistIntent.persistIfLeader(key, pathIntentOperations); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 194 | p.log("end_persistPathIntents"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 195 | |
| 196 | // remove error-intents and reflect them to high-level intents |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 197 | p.log("begin_removeErrorIntents"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 198 | states.clear(); |
| 199 | Iterator<IntentOperation> i = pathIntentOperations.iterator(); |
| 200 | while (i.hasNext()) { |
| 201 | IntentOperation op = i.next(); |
| 202 | if (op.operator.equals(Operator.ERROR)) { |
| 203 | states.put(op.intent.getId(), IntentState.INST_NACK); |
| 204 | i.remove(); |
| 205 | } |
Toshio Koide | 600ae5f | 2014-02-20 18:42:00 -0800 | [diff] [blame] | 206 | } |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 207 | highLevelIntents.changeStates(states); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 208 | p.log("end_removeErrorIntents"); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 209 | |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 210 | // update the map of path intents and publish the path operations |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 211 | p.log("begin_updateInMemoryPathIntents"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 212 | pathIntents.executeOperations(pathIntentOperations); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 213 | p.log("end_updateInMemoryPathIntents"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 214 | |
Toshio Koide | 500431f | 2014-02-28 02:02:25 -0800 | [diff] [blame] | 215 | // XXX Demo special: add a complete path to remove operation |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 216 | p.log("begin_addPathToRemoveOperation"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 217 | for (IntentOperation op: pathIntentOperations) { |
| 218 | if(op.operator.equals(Operator.REMOVE)) { |
| 219 | op.intent = pathIntents.getIntent(op.intent.getId()); |
| 220 | } |
| 221 | if (op.intent instanceof PathIntent) { |
| 222 | log.debug("operation: {}, intent:{}", op.operator, op.intent); |
| 223 | } |
| 224 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 225 | p.log("end_addPathToRemoveOperation"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 226 | |
| 227 | // send notification |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 228 | p.log("begin_sendNotification"); |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 229 | // XXX: Send notifications using the same key every time |
| 230 | // and receive them by entryAdded() and entryUpdated() |
| 231 | opEventChannel.addEntry(0L, pathIntentOperations); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 232 | p.log("end_sendNotification"); |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 233 | //opEventChannel.removeEntry(key); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 234 | return pathIntentOperations; |
| 235 | } |
| 236 | finally { |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 237 | p.flushLog(); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 238 | lock.unlock(); |
| 239 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public IntentMap getHighLevelIntents() { |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 244 | return highLevelIntents; |
| 245 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 246 | |
| 247 | @Override |
| 248 | public IntentMap getPathIntents() { |
| 249 | return pathIntents; |
| 250 | } |
| 251 | |
| 252 | @Override |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 253 | public void purgeIntents() { |
| 254 | highLevelIntents.purge(); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 255 | pathIntents.purge(); |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 256 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 257 | |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 258 | // ================================================================================ |
| 259 | // INetworkGraphListener implementations |
| 260 | // ================================================================================ |
| 261 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 262 | @Override |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 263 | public void networkGraphEvents(Collection<SwitchEvent> addedSwitchEvents, |
| 264 | Collection<SwitchEvent> removedSwitchEvents, |
| 265 | Collection<PortEvent> addedPortEvents, |
| 266 | Collection<PortEvent> removedPortEvents, |
| 267 | Collection<LinkEvent> addedLinkEvents, |
| 268 | Collection<LinkEvent> removedLinkEvents, |
| 269 | Collection<DeviceEvent> addedDeviceEvents, |
| 270 | Collection<DeviceEvent> removedDeviceEvents) { |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 271 | |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 272 | PerfLogger p = new PerfLogger("networkGraphEvents"); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 273 | HashSet<Intent> affectedPaths = new HashSet<>(); |
Toshio Koide | 93797dc | 2014-02-27 23:54:26 -0800 | [diff] [blame] | 274 | |
Brian O'Connor | 5518d28 | 2014-02-25 22:25:58 -0800 | [diff] [blame] | 275 | boolean rerouteAll = false; |
| 276 | for(LinkEvent le : addedLinkEvents) { |
| 277 | LinkEvent rev = new LinkEvent(le.getDst().getDpid(), le.getDst().getNumber(), le.getSrc().getDpid(), le.getSrc().getNumber()); |
| 278 | if(unmatchedLinkEvents.contains(rev)) { |
| 279 | rerouteAll = true; |
| 280 | unmatchedLinkEvents.remove(rev); |
| 281 | log.debug("Found matched LinkEvent: {} {}", rev, le); |
| 282 | } |
| 283 | else { |
| 284 | unmatchedLinkEvents.add(le); |
| 285 | log.debug("Adding unmatched LinkEvent: {}", le); |
| 286 | } |
| 287 | } |
| 288 | for(LinkEvent le : removedLinkEvents) { |
| 289 | if (unmatchedLinkEvents.contains(le)) { |
| 290 | unmatchedLinkEvents.remove(le); |
| 291 | log.debug("Removing LinkEvent: {}", le); |
| 292 | } |
| 293 | } |
| 294 | if(unmatchedLinkEvents.size() > 0) { |
| 295 | log.debug("Unmatched link events: {} events", unmatchedLinkEvents.size()); |
| 296 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 297 | |
Brian O'Connor | 5518d28 | 2014-02-25 22:25:58 -0800 | [diff] [blame] | 298 | if ( rerouteAll ) {//addedLinkEvents.size() > 0) { // || |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 299 | // addedPortEvents.size() > 0 || |
| 300 | // addedSwitchEvents.size() > 0) { |
| 301 | p.log("begin_getAllIntents"); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 302 | affectedPaths.addAll(getPathIntents().getAllIntents()); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 303 | p.log("end_getAllIntents"); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 304 | } |
Brian O'Connor | 5518d28 | 2014-02-25 22:25:58 -0800 | [diff] [blame] | 305 | else if (removedSwitchEvents.size() > 0 || |
| 306 | removedLinkEvents.size() > 0 || |
| 307 | removedPortEvents.size() > 0) { |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 308 | p.log("begin_getIntentsByLink"); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 309 | for (LinkEvent linkEvent: removedLinkEvents) |
| 310 | affectedPaths.addAll(pathIntents.getIntentsByLink(linkEvent)); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 311 | p.log("end_getIntentsByLink"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 312 | |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 313 | p.log("begin_getIntentsByPort"); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 314 | for (PortEvent portEvent: removedPortEvents) |
| 315 | affectedPaths.addAll(pathIntents.getIntentsByPort(portEvent.getDpid(), portEvent.getNumber())); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 316 | p.log("end_getIntentsByPort"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 317 | |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 318 | p.log("begin_getIntentsByDpid"); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 319 | for (SwitchEvent switchEvent: removedSwitchEvents) |
| 320 | affectedPaths.addAll(pathIntents.getIntentsByDpid(switchEvent.getDpid())); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 321 | p.log("end_getIntentsByDpid"); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 322 | } |
Toshio Koide | 240b130 | 2014-02-26 20:08:41 -0800 | [diff] [blame] | 323 | p.log("begin_reroutePaths"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 324 | reroutePaths(affectedPaths); |
Toshio Koide | 240b130 | 2014-02-26 20:08:41 -0800 | [diff] [blame] | 325 | p.log("end_reroutePaths"); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 326 | p.flushLog(); |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 327 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 328 | |
| 329 | // ================================================================================ |
| 330 | // IEventChannelListener implementations |
| 331 | // ================================================================================ |
| 332 | |
| 333 | @Override |
| 334 | public void entryAdded(IntentStateList value) { |
| 335 | entryUpdated(value); |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void entryRemoved(IntentStateList value) { |
| 340 | // do nothing |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void entryUpdated(IntentStateList value) { |
Toshio Koide | 8315d7d | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 345 | // 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] | 346 | PerfLogger p = new PerfLogger("entryUpdated"); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 347 | lock.lock(); // TODO optimize locking using smaller steps |
| 348 | try { |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 349 | // reflect state changes of path-level intent into application-level intents |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 350 | p.log("begin_changeStateByNotification"); |
Toshio Koide | 500431f | 2014-02-28 02:02:25 -0800 | [diff] [blame] | 351 | IntentStateList highLevelIntentStates = new IntentStateList(); |
| 352 | IntentStateList pathIntentStates = new IntentStateList(); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 353 | for (Entry<String, IntentState> entry: value.entrySet()) { |
| 354 | PathIntent pathIntent = (PathIntent) pathIntents.getIntent(entry.getKey()); |
| 355 | if (pathIntent == null) continue; |
Toshio Koide | 8315d7d | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 356 | |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 357 | Intent parentIntent = pathIntent.getParentIntent(); |
| 358 | if (parentIntent == null || |
| 359 | !(parentIntent instanceof ShortestPathIntent) || |
| 360 | !((ShortestPathIntent) parentIntent).getPathIntentId().equals(pathIntent.getId())) |
| 361 | continue; |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 362 | |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 363 | IntentState state = entry.getValue(); |
| 364 | switch (state) { |
Toshio Koide | 500431f | 2014-02-28 02:02:25 -0800 | [diff] [blame] | 365 | //case INST_REQ: |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 366 | case INST_ACK: |
| 367 | case INST_NACK: |
Toshio Koide | 500431f | 2014-02-28 02:02:25 -0800 | [diff] [blame] | 368 | //case DEL_REQ: |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 369 | case DEL_ACK: |
| 370 | case DEL_PENDING: |
Toshio Koide | 500431f | 2014-02-28 02:02:25 -0800 | [diff] [blame] | 371 | highLevelIntentStates.put(parentIntent.getId(), state); |
| 372 | pathIntentStates.put(entry.getKey(), entry.getValue()); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 373 | break; |
| 374 | default: |
| 375 | break; |
| 376 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 377 | } |
Toshio Koide | 500431f | 2014-02-28 02:02:25 -0800 | [diff] [blame] | 378 | highLevelIntents.changeStates(highLevelIntentStates); |
| 379 | pathIntents.changeStates(pathIntentStates); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 380 | p.log("end_changeStateByNotification"); |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 381 | } |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 382 | finally { |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 383 | p.flushLog(); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 384 | lock.unlock(); |
| 385 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 386 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 387 | } |