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; |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 9 | import java.util.List; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 10 | import java.util.Map; |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 11 | import java.util.Map.Entry; |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 12 | import java.util.Set; |
| 13 | import java.util.concurrent.ConcurrentHashMap; |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 14 | import java.util.concurrent.ConcurrentMap; |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 15 | import java.util.concurrent.locks.ReentrantLock; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 16 | |
| 17 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 18 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 19 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 20 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.restserver.IRestApiService; |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 22 | import net.floodlightcontroller.util.MACAddress; |
| 23 | import net.onrc.onos.api.intent.ApplicationIntent; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 25 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 26 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.intent.ConstrainedShortestPathIntent; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.intent.Intent; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.intent.Intent.IntentState; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.intent.IntentMap; |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.intent.IntentMap.ChangedEvent; |
| 32 | import net.onrc.onos.core.intent.IntentMap.ChangedListener; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 33 | import net.onrc.onos.core.intent.IntentOperation; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 34 | import net.onrc.onos.core.intent.IntentOperation.Operator; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.intent.IntentOperationList; |
| 36 | import net.onrc.onos.core.intent.PathIntent; |
| 37 | import net.onrc.onos.core.intent.PathIntentMap; |
| 38 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.intent.runtime.web.IntentWebRoutable; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 41 | import net.onrc.onos.core.topology.DeviceEvent; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 42 | import net.onrc.onos.core.topology.ITopologyListener; |
| 43 | import net.onrc.onos.core.topology.ITopologyService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 44 | import net.onrc.onos.core.topology.LinkEvent; |
| 45 | import net.onrc.onos.core.topology.PortEvent; |
| 46 | import net.onrc.onos.core.topology.SwitchEvent; |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 47 | import net.onrc.onos.core.util.Dpid; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 48 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | import org.slf4j.LoggerFactory; |
| 51 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 52 | /** |
| 53 | * @author Toshio Koide (t-koide@onlab.us) |
| 54 | */ |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 55 | public class PathCalcRuntimeModule implements IFloodlightModule, |
| 56 | IPathCalcRuntimeService, |
| 57 | ITopologyListener, |
| 58 | IEventChannelListener<Long, IntentStateList> { |
Pavlin Radoslavov | fee8098 | 2014-04-10 12:12:04 -0700 | [diff] [blame] | 59 | static class PerfLog { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 60 | private String step; |
| 61 | private long time; |
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 PerfLog(String step) { |
| 64 | this.step = step; |
| 65 | this.time = System.nanoTime(); |
| 66 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 67 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 68 | public void logThis() { |
Pavlin Radoslavov | 964f8ae | 2014-04-18 16:44:14 -0700 | [diff] [blame] | 69 | log.debug("Time:{}, Step:{}", time, step); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 72 | |
Pavlin Radoslavov | 53ad5e3 | 2014-04-10 14:24:26 -0700 | [diff] [blame] | 73 | static class PerfLogger { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 74 | private LinkedList<PerfLog> logData = new LinkedList<>(); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 75 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | public PerfLogger(String logPhase) { |
| 77 | log("start_" + logPhase); |
| 78 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 79 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | public void log(String step) { |
| 81 | logData.add(new PerfLog(step)); |
| 82 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 83 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | public void flushLog() { |
| 85 | log("finish"); |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 86 | for (PerfLog perfLog : logData) { |
| 87 | perfLog.logThis(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | } |
| 89 | logData.clear(); |
| 90 | } |
| 91 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 92 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 93 | /** |
| 94 | * A class to track the deletion of intents and purge them as appropriate. |
| 95 | */ |
| 96 | private class DeleteIntentsTracker implements ChangedListener { |
| 97 | @Override |
| 98 | public void intentsChange(LinkedList<ChangedEvent> events) { |
| 99 | List<String> removeIntentIds = new LinkedList<String>(); |
| 100 | List<String> removePathIds = new LinkedList<String>(); |
| 101 | |
| 102 | // |
| 103 | // Process the events one-by-one and collect the Intent IDs of |
| 104 | // those intents that should be purged. |
| 105 | // |
| 106 | for (ChangedEvent event : events) { |
| 107 | log.debug("DeleteIntentsTracker: Intent ID {}, eventType {}", |
| 108 | event.intent.getId() , event.eventType); |
| 109 | PathIntent pathIntent = (PathIntent) pathIntents.getIntent(event.intent.getId()); |
| 110 | if (pathIntent == null) { |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | // |
| 115 | // Test whether the new Intent state allows the Intent |
| 116 | // to be purged. |
| 117 | // |
| 118 | boolean shouldPurge = false; |
| 119 | switch (event.eventType) { |
| 120 | case ADDED: |
| 121 | break; |
| 122 | case REMOVED: |
| 123 | break; |
| 124 | case STATE_CHANGED: |
| 125 | IntentState state = pathIntent.getState(); |
| 126 | switch (state) { |
| 127 | case INST_REQ: |
| 128 | break; |
| 129 | case INST_ACK: |
| 130 | break; |
| 131 | case INST_NACK: |
| 132 | shouldPurge = true; |
| 133 | break; |
| 134 | case DEL_REQ: |
| 135 | break; |
| 136 | case DEL_ACK: |
| 137 | shouldPurge = true; |
| 138 | break; |
| 139 | case DEL_PENDING: |
| 140 | break; |
| 141 | default: |
| 142 | break; |
| 143 | } |
| 144 | break; |
| 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | if (shouldPurge) { |
| 150 | removePathIds.add(pathIntent.getId()); |
| 151 | Intent parentIntent = pathIntent.getParentIntent(); |
| 152 | if (parentIntent != null) { |
| 153 | // |
| 154 | // Remove the High-level Intent only if it was |
| 155 | // explicitly deleted by the user via the API. |
| 156 | // |
| 157 | String intentId = parentIntent.getId(); |
| 158 | if (removedApplicationIntentIds.contains(intentId)) { |
| 159 | removeIntentIds.add(intentId); |
| 160 | removedApplicationIntentIds.remove(intentId); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Purge the intents |
| 167 | if (!removeIntentIds.isEmpty()) { |
| 168 | highLevelIntents.purge(removeIntentIds); |
| 169 | } |
| 170 | if (!removePathIds.isEmpty()) { |
| 171 | pathIntents.purge(removePathIds); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 176 | private PathCalcRuntime runtime; |
| 177 | private IDatagridService datagridService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 178 | private ITopologyService topologyService; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 179 | private IntentMap highLevelIntents; |
| 180 | private PathIntentMap pathIntents; |
| 181 | private IControllerRegistryService controllerRegistry; |
| 182 | private PersistIntent persistIntent; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 183 | private IRestApiService restApi; |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 184 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 185 | private IEventChannel<Long, IntentOperationList> opEventChannel; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 186 | private final ReentrantLock lock = new ReentrantLock(true); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 187 | private static final String INTENT_OP_EVENT_CHANNEL_NAME = "onos.pathintent"; |
| 188 | private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state"; |
| 189 | private static final Logger log = LoggerFactory.getLogger(PathCalcRuntimeModule.class); |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 190 | |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 191 | private HashSet<LinkEvent> unmatchedLinkEvents = new HashSet<>(); |
| 192 | private ConcurrentMap<String, Set<Long>> intentInstalledMap = new ConcurrentHashMap<String, Set<Long>>(); |
| 193 | private ConcurrentMap<String, Intent> staleIntents = new ConcurrentHashMap<String, Intent>(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 194 | private DeleteIntentsTracker deleteIntentsTracker = new DeleteIntentsTracker(); |
| 195 | private Set<String> removedApplicationIntentIds = new HashSet<String>(); |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 196 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 197 | // ================================================================================ |
| 198 | // private methods |
| 199 | // ================================================================================ |
| 200 | |
| 201 | private void reroutePaths(Collection<Intent> oldPaths) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 202 | if (oldPaths == null || oldPaths.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 203 | return; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 204 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | |
| 206 | IntentOperationList reroutingOperation = new IntentOperationList(); |
| 207 | for (Intent intent : oldPaths) { |
| 208 | PathIntent pathIntent = (PathIntent) intent; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 209 | if (pathIntent.isPathFrozen()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 210 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 211 | } |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 212 | Intent parentIntent = pathIntent.getParentIntent(); |
| 213 | if (parentIntent == null) { |
| 214 | continue; |
| 215 | } |
| 216 | if (pathIntent.getState().equals(IntentState.INST_ACK)) { |
| 217 | if (!reroutingOperation.contains(parentIntent)) { |
| 218 | // reroute now |
| 219 | reroutingOperation.add(Operator.ADD, parentIntent); |
| 220 | } |
| 221 | } else if (pathIntent.getState().equals(IntentState.INST_REQ)) { |
| 222 | // reroute after the completion of the current execution |
| 223 | staleIntents.put(parentIntent.getId(), parentIntent); |
| 224 | log.debug("pending reroute execution for intent ID:{}", parentIntent.getId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | executeIntentOperations(reroutingOperation); |
| 228 | } |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 229 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 230 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 231 | // ================================================================================ |
| 232 | // IFloodlightModule implementations |
| 233 | // ================================================================================ |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 234 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 235 | @Override |
| 236 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 237 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(1); |
| 238 | l.add(IPathCalcRuntimeService.class); |
| 239 | return l; |
| 240 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 241 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 242 | @Override |
| 243 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 244 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(); |
| 245 | m.put(IPathCalcRuntimeService.class, this); |
| 246 | return m; |
| 247 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 248 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 249 | @Override |
| 250 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 251 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(2); |
| 252 | l.add(IDatagridService.class); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 253 | l.add(IRestApiService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 254 | l.add(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 255 | return l; |
| 256 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 257 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 258 | @Override |
| 259 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
| 260 | datagridService = context.getServiceImpl(IDatagridService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 261 | topologyService = context.getServiceImpl(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 262 | controllerRegistry = context.getServiceImpl(IControllerRegistryService.class); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 263 | restApi = context.getServiceImpl(IRestApiService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 264 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 265 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 266 | @Override |
| 267 | public void startUp(FloodlightModuleContext context) { |
| 268 | highLevelIntents = new IntentMap(); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 269 | runtime = new PathCalcRuntime(topologyService.getTopology()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 270 | pathIntents = new PathIntentMap(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 271 | pathIntents.addChangeListener(deleteIntentsTracker); |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 272 | opEventChannel = datagridService.createChannel( |
| 273 | INTENT_OP_EVENT_CHANNEL_NAME, Long.class, IntentOperationList.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 274 | datagridService.addListener(INTENT_STATE_EVENT_CHANNEL_NAME, this, Long.class, IntentStateList.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 275 | topologyService.registerTopologyListener(this); |
Pavlin Radoslavov | 0294e05 | 2014-04-10 13:36:45 -0700 | [diff] [blame] | 276 | persistIntent = new PersistIntent(controllerRegistry); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 277 | restApi.addRestletRoutable(new IntentWebRoutable()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 278 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 279 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 280 | // ====================================================================== |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 281 | // IPathCalcRuntimeService implementations |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 282 | // ====================================================================== |
| 283 | |
| 284 | /** |
| 285 | * Add Application Intents. |
| 286 | * |
| 287 | * @param appId the Application ID to use. |
| 288 | * @param appIntents the Application Intents to add. |
| 289 | * @return true on success, otherwise false. |
| 290 | */ |
| 291 | @Override |
| 292 | public boolean addApplicationIntents( |
| 293 | final String appId, |
| 294 | Collection<ApplicationIntent> appIntents) { |
| 295 | // |
| 296 | // Process all intents one-by-one |
| 297 | // |
| 298 | // TODO: The Intent Type should be enum instead of a string, |
| 299 | // and we should use a switch statement below to process the |
| 300 | // different type of intents. |
| 301 | // |
| 302 | IntentOperationList intentOperations = new IntentOperationList(); |
| 303 | for (ApplicationIntent appIntent : appIntents) { |
| 304 | String appIntentId = appId + ":" + appIntent.intentId(); |
| 305 | |
| 306 | IntentOperation.Operator operator = IntentOperation.Operator.ADD; |
| 307 | Dpid srcSwitchDpid = new Dpid(appIntent.srcSwitchDpid()); |
| 308 | Dpid dstSwitchDpid = new Dpid(appIntent.dstSwitchDpid()); |
| 309 | |
| 310 | if (appIntent.intentType().equals("SHORTEST_PATH")) { |
| 311 | // |
| 312 | // Process Shortest-Path Intent |
| 313 | // |
| 314 | ShortestPathIntent spi = |
| 315 | new ShortestPathIntent(appIntentId, |
| 316 | srcSwitchDpid.value(), |
| 317 | appIntent.srcSwitchPort(), |
| 318 | MACAddress.valueOf(appIntent.matchSrcMac()).toLong(), |
| 319 | dstSwitchDpid.value(), |
| 320 | appIntent.dstSwitchPort(), |
| 321 | MACAddress.valueOf(appIntent.matchDstMac()).toLong()); |
| 322 | spi.setPathFrozen(appIntent.isStaticPath()); |
| 323 | intentOperations.add(operator, spi); |
| 324 | } else if (appIntent.intentType().equals("CONSTRAINED_SHORTEST_PATH")) { |
| 325 | // |
| 326 | // Process Constrained Shortest-Path Intent |
| 327 | // |
| 328 | ConstrainedShortestPathIntent cspi = |
| 329 | new ConstrainedShortestPathIntent(appIntentId, |
| 330 | srcSwitchDpid.value(), |
| 331 | appIntent.srcSwitchPort(), |
| 332 | MACAddress.valueOf(appIntent.matchSrcMac()).toLong(), |
| 333 | dstSwitchDpid.value(), |
| 334 | appIntent.dstSwitchPort(), |
| 335 | MACAddress.valueOf(appIntent.matchDstMac()).toLong(), |
| 336 | appIntent.bandwidth()); |
| 337 | cspi.setPathFrozen(appIntent.isStaticPath()); |
| 338 | intentOperations.add(operator, cspi); |
| 339 | } else { |
| 340 | log.error("Unknown Application Intent Type: {}", |
| 341 | appIntent.intentType()); |
| 342 | return false; |
| 343 | } |
| 344 | removedApplicationIntentIds.remove(appIntentId); |
| 345 | } |
| 346 | // Apply the Intent Operations |
| 347 | executeIntentOperations(intentOperations); |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Remove Application Intents. |
| 353 | * |
| 354 | * @param appId the Application ID to use. |
| 355 | * @param intentIds the Application Intent IDs to remove. |
| 356 | * @return true on success, otherwise false. |
| 357 | */ |
| 358 | @Override |
| 359 | public boolean removeApplicationIntents(final String appId, |
| 360 | Collection<String> intentIds) { |
| 361 | IntentMap intentMap = getHighLevelIntents(); |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 362 | List<String> removeIntentIds = new LinkedList<String>(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 363 | |
| 364 | // |
| 365 | // Process all intents one-by-one |
| 366 | // |
| 367 | IntentOperationList operations = new IntentOperationList(); |
| 368 | for (String intentId : intentIds) { |
| 369 | String appIntentId = appId + ":" + intentId; |
| 370 | Intent intent = intentMap.getIntent(appIntentId); |
| 371 | if (intent != null) { |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 372 | if (intent.getState() == IntentState.INST_NACK) { |
| 373 | // TODO: A hack to remove intents stuck in INST_NACK state |
| 374 | removeIntentIds.add(intent.getId()); |
| 375 | continue; |
| 376 | } |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 377 | operations.add(IntentOperation.Operator.REMOVE, intent); |
| 378 | removedApplicationIntentIds.add(appIntentId); |
| 379 | } |
| 380 | } |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 381 | |
| 382 | // Purge intents |
| 383 | if (!removeIntentIds.isEmpty()) { |
| 384 | lock.lock(); // TODO optimize locking using smaller steps |
| 385 | try { |
| 386 | highLevelIntents.purge(removeIntentIds); |
| 387 | } finally { |
| 388 | lock.unlock(); |
| 389 | } |
| 390 | } |
| 391 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 392 | executeIntentOperations(operations); |
| 393 | |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Remove all Application Intents. |
| 399 | * |
| 400 | * @param appId the Application ID to use. |
| 401 | * @return true on success, otherwise false. |
| 402 | */ |
| 403 | @Override |
| 404 | public boolean removeAllApplicationIntents(final String appId) { |
| 405 | IntentMap intentMap = getHighLevelIntents(); |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 406 | List<String> removeIntentIds = new LinkedList<String>(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 407 | |
| 408 | // |
| 409 | // Remove all intents |
| 410 | // |
| 411 | IntentOperationList operations = new IntentOperationList(); |
| 412 | for (Intent intent : intentMap.getAllIntents()) { |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 413 | if (intent.getState() == IntentState.INST_NACK) { |
| 414 | // TODO: A hack to remove intents stuck in INST_NACK state |
| 415 | removeIntentIds.add(intent.getId()); |
| 416 | continue; |
| 417 | } |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 418 | operations.add(IntentOperation.Operator.REMOVE, intent); |
| 419 | removedApplicationIntentIds.add(intent.getId()); |
| 420 | } |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 421 | |
| 422 | // Purge intents |
| 423 | if (!removeIntentIds.isEmpty()) { |
| 424 | lock.lock(); // TODO optimize locking using smaller steps |
| 425 | try { |
| 426 | highLevelIntents.purge(removeIntentIds); |
| 427 | } finally { |
| 428 | lock.unlock(); |
| 429 | } |
| 430 | } |
| 431 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 432 | executeIntentOperations(operations); |
| 433 | |
| 434 | return true; |
| 435 | } |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 436 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 437 | @Override |
| 438 | public IntentOperationList executeIntentOperations(IntentOperationList list) { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 439 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 440 | if (list == null || list.size() == 0) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 441 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 442 | } |
Toshio Koide | 275d814 | 2014-02-24 16:41:52 -0800 | [diff] [blame] | 443 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 444 | lock.lock(); // TODO optimize locking using smaller steps |
| 445 | try { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 446 | log.trace("lock executeIntentOperations, lock obj is already locked? {}", lock.isLocked()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 447 | // update the map of high-level intents |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 448 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 449 | highLevelIntents.executeOperations(list); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 450 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 451 | // change states of high-level intents |
| 452 | IntentStateList states = new IntentStateList(); |
| 453 | for (IntentOperation op : list) { |
| 454 | switch (op.operator) { |
| 455 | case ADD: |
| 456 | switch (op.intent.getState()) { |
| 457 | case CREATED: |
| 458 | states.put(op.intent.getId(), IntentState.INST_REQ); |
| 459 | break; |
| 460 | case INST_ACK: |
| 461 | states.put(op.intent.getId(), IntentState.REROUTE_REQ); |
| 462 | break; |
| 463 | default: |
| 464 | break; |
| 465 | } |
| 466 | break; |
| 467 | case REMOVE: |
| 468 | switch (op.intent.getState()) { |
| 469 | case CREATED: |
| 470 | states.put(op.intent.getId(), IntentState.DEL_REQ); |
| 471 | break; |
| 472 | default: |
| 473 | break; |
| 474 | } |
| 475 | break; |
| 476 | default: |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | highLevelIntents.changeStates(states); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 481 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 482 | // calculate path-intents (low-level operations) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 483 | IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, highLevelIntents, pathIntents); |
Toshio Koide | 600ae5f | 2014-02-20 18:42:00 -0800 | [diff] [blame] | 484 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 485 | // persist calculated low-level operations into data store |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 486 | long key = persistIntent.getKey(); |
| 487 | persistIntent.persistIfLeader(key, pathIntentOperations); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 488 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 489 | // remove error-intents and reflect them to high-level intents |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 490 | states.clear(); |
| 491 | Iterator<IntentOperation> i = pathIntentOperations.iterator(); |
| 492 | while (i.hasNext()) { |
| 493 | IntentOperation op = i.next(); |
| 494 | if (op.operator.equals(Operator.ERROR)) { |
| 495 | states.put(op.intent.getId(), IntentState.INST_NACK); |
| 496 | i.remove(); |
| 497 | } |
| 498 | } |
| 499 | highLevelIntents.changeStates(states); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 500 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 501 | // update the map of path intents and publish the path operations |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 502 | pathIntents.executeOperations(pathIntentOperations); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 503 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 504 | // send notification |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 505 | // XXX: Send notifications using the same key every time |
| 506 | // and receive them by entryAdded() and entryUpdated() |
| 507 | opEventChannel.addEntry(0L, pathIntentOperations); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 508 | //opEventChannel.removeEntry(key); |
| 509 | return pathIntentOperations; |
| 510 | } finally { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 511 | lock.unlock(); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 512 | log.trace("unlock executeIntentOperations"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 515 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 516 | @Override |
| 517 | public IntentMap getHighLevelIntents() { |
| 518 | return highLevelIntents; |
| 519 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 520 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 521 | @Override |
| 522 | public IntentMap getPathIntents() { |
| 523 | return pathIntents; |
| 524 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 525 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 526 | @Override |
| 527 | public void purgeIntents() { |
| 528 | highLevelIntents.purge(); |
| 529 | pathIntents.purge(); |
| 530 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 531 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 532 | // ================================================================================ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 533 | // ITopologyListener implementations |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 534 | // ================================================================================ |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 535 | |
Ray Milkey | a5450cc | 2014-04-17 14:31:30 -0700 | [diff] [blame] | 536 | // CHECKSTYLE:OFF suppress warning about too many parameters |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 537 | @Override |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 538 | public void topologyEvents(Collection<SwitchEvent> addedSwitchEvents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 539 | Collection<SwitchEvent> removedSwitchEvents, |
| 540 | Collection<PortEvent> addedPortEvents, |
| 541 | Collection<PortEvent> removedPortEvents, |
| 542 | Collection<LinkEvent> addedLinkEvents, |
| 543 | Collection<LinkEvent> removedLinkEvents, |
| 544 | Collection<DeviceEvent> addedDeviceEvents, |
| 545 | Collection<DeviceEvent> removedDeviceEvents) { |
Ray Milkey | a5450cc | 2014-04-17 14:31:30 -0700 | [diff] [blame] | 546 | // CHECKSTYLE:ON |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 547 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 548 | PerfLogger p = new PerfLogger("networkGraphEvents"); |
| 549 | HashSet<Intent> affectedPaths = new HashSet<>(); |
Toshio Koide | 93797dc | 2014-02-27 23:54:26 -0800 | [diff] [blame] | 550 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 551 | boolean rerouteAll = false; |
| 552 | for (LinkEvent le : addedLinkEvents) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 553 | LinkEvent rev = new LinkEvent(le.getDst().getDpid(), |
| 554 | le.getDst().getNumber(), le.getSrc().getDpid(), |
| 555 | le.getSrc().getNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 556 | if (unmatchedLinkEvents.contains(rev)) { |
| 557 | rerouteAll = true; |
| 558 | unmatchedLinkEvents.remove(rev); |
| 559 | log.debug("Found matched LinkEvent: {} {}", rev, le); |
| 560 | } else { |
| 561 | unmatchedLinkEvents.add(le); |
| 562 | log.debug("Adding unmatched LinkEvent: {}", le); |
| 563 | } |
| 564 | } |
| 565 | for (LinkEvent le : removedLinkEvents) { |
| 566 | if (unmatchedLinkEvents.contains(le)) { |
| 567 | unmatchedLinkEvents.remove(le); |
| 568 | log.debug("Removing LinkEvent: {}", le); |
| 569 | } |
| 570 | } |
| 571 | if (unmatchedLinkEvents.size() > 0) { |
| 572 | log.debug("Unmatched link events: {} events", unmatchedLinkEvents.size()); |
| 573 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 574 | |
Ray Milkey | 7f1567c | 2014-04-08 13:53:32 -0700 | [diff] [blame] | 575 | if (rerouteAll) { //addedLinkEvents.size() > 0) { // || |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 576 | // addedPortEvents.size() > 0 || |
| 577 | // addedSwitchEvents.size() > 0) { |
| 578 | p.log("begin_getAllIntents"); |
| 579 | affectedPaths.addAll(getPathIntents().getAllIntents()); |
| 580 | p.log("end_getAllIntents"); |
| 581 | } else if (removedSwitchEvents.size() > 0 || |
| 582 | removedLinkEvents.size() > 0 || |
| 583 | removedPortEvents.size() > 0) { |
| 584 | p.log("begin_getIntentsByLink"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 585 | for (LinkEvent linkEvent : removedLinkEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 586 | affectedPaths.addAll(pathIntents.getIntentsByLink(linkEvent)); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 587 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 588 | p.log("end_getIntentsByLink"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 589 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 590 | p.log("begin_getIntentsByPort"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 591 | for (PortEvent portEvent : removedPortEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 592 | affectedPaths.addAll(pathIntents.getIntentsByPort(portEvent.getDpid(), portEvent.getNumber())); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 593 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 594 | p.log("end_getIntentsByPort"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 595 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 596 | p.log("begin_getIntentsByDpid"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 597 | for (SwitchEvent switchEvent : removedSwitchEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 598 | affectedPaths.addAll(pathIntents.getIntentsByDpid(switchEvent.getDpid())); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 599 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 600 | p.log("end_getIntentsByDpid"); |
| 601 | } |
| 602 | p.log("begin_reroutePaths"); |
| 603 | reroutePaths(affectedPaths); |
| 604 | p.log("end_reroutePaths"); |
| 605 | p.flushLog(); |
| 606 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 607 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 608 | // ================================================================================ |
| 609 | // IEventChannelListener implementations |
| 610 | // ================================================================================ |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 611 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 612 | @Override |
| 613 | public void entryAdded(IntentStateList value) { |
| 614 | entryUpdated(value); |
| 615 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 616 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 617 | @Override |
| 618 | public void entryRemoved(IntentStateList value) { |
| 619 | // do nothing |
| 620 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 621 | |
Ray Milkey | 149693c | 2014-05-20 14:58:53 -0700 | [diff] [blame] | 622 | @SuppressWarnings("fallthrough") |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 623 | @Override |
| 624 | public void entryUpdated(IntentStateList value) { |
| 625 | // TODO draw state transition diagram in multiple ONOS instances and update this method |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 626 | |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 627 | IntentOperationList opList = new IntentOperationList(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 628 | lock.lock(); // TODO optimize locking using smaller steps |
| 629 | try { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 630 | log.trace("lock entryUpdated, lock obj is already locked? {}", lock.isLocked()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 631 | // reflect state changes of path-level intent into application-level intents |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 632 | IntentStateList highLevelIntentStates = new IntentStateList(); |
| 633 | IntentStateList pathIntentStates = new IntentStateList(); |
| 634 | for (Entry<String, IntentState> entry : value.entrySet()) { |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 635 | String pathIntentId = entry.getKey(); |
| 636 | IntentState nextPathIntentState = entry.getValue(); |
| 637 | PathIntent pathIntent = (PathIntent) pathIntents.getIntent(pathIntentId); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 638 | if (pathIntent == null) { |
| 639 | continue; |
| 640 | } |
Toshio Koide | 8315d7d | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 641 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 642 | Intent parentIntent = pathIntent.getParentIntent(); |
| 643 | if (parentIntent == null || |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 644 | !(parentIntent instanceof ShortestPathIntent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 645 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 646 | } |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 647 | String parentIntentId = parentIntent.getId(); |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 648 | |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 649 | boolean isChildIntent = ((ShortestPathIntent) parentIntent).getPathIntentId().equals(pathIntentId); |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 650 | |
| 651 | // Check necessity for retrying the intent execution. |
| 652 | // When the PathIntent(=isChildIntent) transitioned to INST_{ACK/NACK} |
| 653 | // but was marked as stale (e.g., has been requested to reroute by Topology event), |
| 654 | // then immediately enqueue the re-computation of parent intent. |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 655 | if (isChildIntent && staleIntents.containsKey(parentIntentId) && ( |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 656 | nextPathIntentState.equals(IntentState.INST_ACK) || |
| 657 | nextPathIntentState.equals(IntentState.INST_NACK))) { |
| 658 | opList.add(Operator.ADD, parentIntent); |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 659 | staleIntents.remove(parentIntentId); |
| 660 | log.debug("retrying intent execution for intent ID:{}", parentIntentId); |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 663 | switch (nextPathIntentState) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 664 | case INST_ACK: |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 665 | Set<Long> installedDpids = calcInstalledDpids(pathIntent, value.domainSwitchDpids); |
| 666 | if (!isFlowInstalled(pathIntent, installedDpids)) { |
| 667 | break; |
| 668 | } |
| 669 | // FALLTHROUGH |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 670 | case INST_NACK: |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 671 | // FALLTHROUGH |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 672 | case DEL_PENDING: |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 673 | if (isChildIntent) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 674 | log.debug("put the state highLevelIntentStates ID {}, state {}", |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 675 | parentIntentId, nextPathIntentState); |
| 676 | highLevelIntentStates.put(parentIntentId, nextPathIntentState); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 677 | } |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 678 | log.debug("put the state pathIntentStates ID {}, state {}", |
| 679 | pathIntentId, nextPathIntentState); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 680 | pathIntentStates.put(pathIntentId, nextPathIntentState); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 681 | break; |
| 682 | case DEL_ACK: |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 683 | if (isChildIntent) { |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 684 | if (intentInstalledMap.containsKey(pathIntentId)) { |
| 685 | intentInstalledMap.remove(pathIntentId); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 686 | } |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 687 | log.debug("put the state highLevelIntentStates ID {}, state {}", |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 688 | parentIntentId, nextPathIntentState); |
| 689 | highLevelIntentStates.put(parentIntentId, nextPathIntentState); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 690 | } |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 691 | log.debug("put the state pathIntentStates ID {}, state {}", |
| 692 | pathIntentId, nextPathIntentState); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 693 | pathIntentStates.put(pathIntentId, nextPathIntentState); |
| 694 | break; |
| 695 | case CREATED: |
| 696 | break; |
| 697 | case DEL_REQ: |
| 698 | break; |
| 699 | case INST_REQ: |
| 700 | break; |
| 701 | case REROUTE_REQ: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 702 | break; |
| 703 | default: |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | highLevelIntents.changeStates(highLevelIntentStates); |
| 708 | pathIntents.changeStates(pathIntentStates); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 709 | } finally { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 710 | lock.unlock(); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 711 | log.trace("unlock entryUpdated"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 712 | } |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 713 | executeIntentOperations(opList); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 714 | } |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 715 | |
| 716 | /*** |
| 717 | * This function is to check whether the entire path's flow entries are installed or not. |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 718 | * |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 719 | * @param pathIntent : The pathIntent to be checked |
| 720 | * @param installedDpids : The dpids installed on one ONOS instance |
| 721 | * @return The result of whether a pathIntent has been installed or not. |
| 722 | */ |
| 723 | private boolean isFlowInstalled(PathIntent pathIntent, Set<Long> installedDpids) { |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 724 | String pathIntentId = pathIntent.getId(); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 725 | |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 726 | if (intentInstalledMap.containsKey(pathIntentId)) { |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 727 | if (!installedDpids.isEmpty()) { |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 728 | intentInstalledMap.get(pathIntentId).addAll(installedDpids); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 729 | } |
| 730 | } else { |
| 731 | // This is the creation of an entry. |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 732 | intentInstalledMap.put(pathIntentId, installedDpids); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | Set<Long> allSwitchesForPath = new HashSet<Long>(); |
| 736 | ShortestPathIntent spfIntent = (ShortestPathIntent) pathIntent.getParentIntent(); |
| 737 | |
| 738 | for (LinkEvent linkEvent : pathIntent.getPath()) { |
| 739 | long sw = linkEvent.getSrc().getDpid(); |
| 740 | allSwitchesForPath.add(sw); |
| 741 | } |
| 742 | allSwitchesForPath.add(spfIntent.getDstSwitchDpid()); |
| 743 | |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 744 | if (log.isDebugEnabled()) { |
| 745 | log.debug("checking flow installation. ID:{}, dpids:{}, installed:{}", |
| 746 | pathIntentId, |
| 747 | allSwitchesForPath, |
| 748 | intentInstalledMap.get(pathIntentId)); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame^] | 751 | if (allSwitchesForPath.equals(intentInstalledMap.get(pathIntentId))) { |
| 752 | intentInstalledMap.remove(pathIntentId); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 753 | return true; |
| 754 | } |
| 755 | |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | private Set<Long> calcInstalledDpids(PathIntent pathIntent, Set<Long> domainSwitchDpids) { |
| 760 | Set<Long> allSwitchesForPath = new HashSet<Long>(); |
| 761 | ShortestPathIntent spfIntent = (ShortestPathIntent) pathIntent.getParentIntent(); |
| 762 | |
| 763 | for (LinkEvent linkEvent : pathIntent.getPath()) { |
| 764 | long sw = linkEvent.getSrc().getDpid(); |
| 765 | |
| 766 | if (domainSwitchDpids.contains(sw)) { |
| 767 | allSwitchesForPath.add(sw); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (domainSwitchDpids.contains(spfIntent.getDstSwitchDpid())) { |
| 772 | allSwitchesForPath.add(spfIntent.getDstSwitchDpid()); |
| 773 | } |
| 774 | |
| 775 | if (log.isTraceEnabled()) { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 776 | log.trace("All switches for a path {}, domain switch dpids {}", allSwitchesForPath, domainSwitchDpids); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | return allSwitchesForPath; |
| 780 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 781 | } |