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