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(); |
| 362 | |
| 363 | // |
| 364 | // Process all intents one-by-one |
| 365 | // |
| 366 | IntentOperationList operations = new IntentOperationList(); |
| 367 | for (String intentId : intentIds) { |
| 368 | String appIntentId = appId + ":" + intentId; |
| 369 | Intent intent = intentMap.getIntent(appIntentId); |
| 370 | if (intent != null) { |
| 371 | operations.add(IntentOperation.Operator.REMOVE, intent); |
| 372 | removedApplicationIntentIds.add(appIntentId); |
| 373 | } |
| 374 | } |
| 375 | executeIntentOperations(operations); |
| 376 | |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Remove all Application Intents. |
| 382 | * |
| 383 | * @param appId the Application ID to use. |
| 384 | * @return true on success, otherwise false. |
| 385 | */ |
| 386 | @Override |
| 387 | public boolean removeAllApplicationIntents(final String appId) { |
| 388 | IntentMap intentMap = getHighLevelIntents(); |
| 389 | |
| 390 | // |
| 391 | // Remove all intents |
| 392 | // |
| 393 | IntentOperationList operations = new IntentOperationList(); |
| 394 | for (Intent intent : intentMap.getAllIntents()) { |
| 395 | operations.add(IntentOperation.Operator.REMOVE, intent); |
| 396 | removedApplicationIntentIds.add(intent.getId()); |
| 397 | } |
| 398 | executeIntentOperations(operations); |
| 399 | |
| 400 | return true; |
| 401 | } |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 402 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 403 | @Override |
| 404 | public IntentOperationList executeIntentOperations(IntentOperationList list) { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 405 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 406 | if (list == null || list.size() == 0) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 407 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 408 | } |
Toshio Koide | 275d814 | 2014-02-24 16:41:52 -0800 | [diff] [blame] | 409 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 410 | lock.lock(); // TODO optimize locking using smaller steps |
| 411 | try { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 412 | log.trace("lock executeIntentOperations, lock obj is already locked? {}", lock.isLocked()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 413 | // update the map of high-level intents |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 414 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 415 | highLevelIntents.executeOperations(list); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 416 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 417 | // change states of high-level intents |
| 418 | IntentStateList states = new IntentStateList(); |
| 419 | for (IntentOperation op : list) { |
| 420 | switch (op.operator) { |
| 421 | case ADD: |
| 422 | switch (op.intent.getState()) { |
| 423 | case CREATED: |
| 424 | states.put(op.intent.getId(), IntentState.INST_REQ); |
| 425 | break; |
| 426 | case INST_ACK: |
| 427 | states.put(op.intent.getId(), IntentState.REROUTE_REQ); |
| 428 | break; |
| 429 | default: |
| 430 | break; |
| 431 | } |
| 432 | break; |
| 433 | case REMOVE: |
| 434 | switch (op.intent.getState()) { |
| 435 | case CREATED: |
| 436 | states.put(op.intent.getId(), IntentState.DEL_REQ); |
| 437 | break; |
| 438 | default: |
| 439 | break; |
| 440 | } |
| 441 | break; |
| 442 | default: |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | highLevelIntents.changeStates(states); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 447 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 448 | // calculate path-intents (low-level operations) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 449 | IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, highLevelIntents, pathIntents); |
Toshio Koide | 600ae5f | 2014-02-20 18:42:00 -0800 | [diff] [blame] | 450 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 451 | // persist calculated low-level operations into data store |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 452 | long key = persistIntent.getKey(); |
| 453 | persistIntent.persistIfLeader(key, pathIntentOperations); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 454 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 455 | // remove error-intents and reflect them to high-level intents |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 456 | states.clear(); |
| 457 | Iterator<IntentOperation> i = pathIntentOperations.iterator(); |
| 458 | while (i.hasNext()) { |
| 459 | IntentOperation op = i.next(); |
| 460 | if (op.operator.equals(Operator.ERROR)) { |
| 461 | states.put(op.intent.getId(), IntentState.INST_NACK); |
| 462 | i.remove(); |
| 463 | } |
| 464 | } |
| 465 | highLevelIntents.changeStates(states); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 466 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 467 | // update the map of path intents and publish the path operations |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 468 | pathIntents.executeOperations(pathIntentOperations); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 469 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 470 | // send notification |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 471 | // XXX: Send notifications using the same key every time |
| 472 | // and receive them by entryAdded() and entryUpdated() |
| 473 | opEventChannel.addEntry(0L, pathIntentOperations); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 474 | //opEventChannel.removeEntry(key); |
| 475 | return pathIntentOperations; |
| 476 | } finally { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 477 | lock.unlock(); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 478 | log.trace("unlock executeIntentOperations"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 481 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 482 | @Override |
| 483 | public IntentMap getHighLevelIntents() { |
| 484 | return highLevelIntents; |
| 485 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 486 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 487 | @Override |
| 488 | public IntentMap getPathIntents() { |
| 489 | return pathIntents; |
| 490 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 491 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 492 | @Override |
| 493 | public void purgeIntents() { |
| 494 | highLevelIntents.purge(); |
| 495 | pathIntents.purge(); |
| 496 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 497 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 498 | // ================================================================================ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 499 | // ITopologyListener implementations |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 500 | // ================================================================================ |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 501 | |
Ray Milkey | a5450cc | 2014-04-17 14:31:30 -0700 | [diff] [blame] | 502 | // CHECKSTYLE:OFF suppress warning about too many parameters |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 503 | @Override |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 504 | public void topologyEvents(Collection<SwitchEvent> addedSwitchEvents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 505 | Collection<SwitchEvent> removedSwitchEvents, |
| 506 | Collection<PortEvent> addedPortEvents, |
| 507 | Collection<PortEvent> removedPortEvents, |
| 508 | Collection<LinkEvent> addedLinkEvents, |
| 509 | Collection<LinkEvent> removedLinkEvents, |
| 510 | Collection<DeviceEvent> addedDeviceEvents, |
| 511 | Collection<DeviceEvent> removedDeviceEvents) { |
Ray Milkey | a5450cc | 2014-04-17 14:31:30 -0700 | [diff] [blame] | 512 | // CHECKSTYLE:ON |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 513 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 514 | PerfLogger p = new PerfLogger("networkGraphEvents"); |
| 515 | HashSet<Intent> affectedPaths = new HashSet<>(); |
Toshio Koide | 93797dc | 2014-02-27 23:54:26 -0800 | [diff] [blame] | 516 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 517 | boolean rerouteAll = false; |
| 518 | for (LinkEvent le : addedLinkEvents) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 519 | LinkEvent rev = new LinkEvent(le.getDst().getDpid(), |
| 520 | le.getDst().getNumber(), le.getSrc().getDpid(), |
| 521 | le.getSrc().getNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 522 | if (unmatchedLinkEvents.contains(rev)) { |
| 523 | rerouteAll = true; |
| 524 | unmatchedLinkEvents.remove(rev); |
| 525 | log.debug("Found matched LinkEvent: {} {}", rev, le); |
| 526 | } else { |
| 527 | unmatchedLinkEvents.add(le); |
| 528 | log.debug("Adding unmatched LinkEvent: {}", le); |
| 529 | } |
| 530 | } |
| 531 | for (LinkEvent le : removedLinkEvents) { |
| 532 | if (unmatchedLinkEvents.contains(le)) { |
| 533 | unmatchedLinkEvents.remove(le); |
| 534 | log.debug("Removing LinkEvent: {}", le); |
| 535 | } |
| 536 | } |
| 537 | if (unmatchedLinkEvents.size() > 0) { |
| 538 | log.debug("Unmatched link events: {} events", unmatchedLinkEvents.size()); |
| 539 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 540 | |
Ray Milkey | 7f1567c | 2014-04-08 13:53:32 -0700 | [diff] [blame] | 541 | if (rerouteAll) { //addedLinkEvents.size() > 0) { // || |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 542 | // addedPortEvents.size() > 0 || |
| 543 | // addedSwitchEvents.size() > 0) { |
| 544 | p.log("begin_getAllIntents"); |
| 545 | affectedPaths.addAll(getPathIntents().getAllIntents()); |
| 546 | p.log("end_getAllIntents"); |
| 547 | } else if (removedSwitchEvents.size() > 0 || |
| 548 | removedLinkEvents.size() > 0 || |
| 549 | removedPortEvents.size() > 0) { |
| 550 | p.log("begin_getIntentsByLink"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 551 | for (LinkEvent linkEvent : removedLinkEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 552 | affectedPaths.addAll(pathIntents.getIntentsByLink(linkEvent)); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 553 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 554 | p.log("end_getIntentsByLink"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 555 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 556 | p.log("begin_getIntentsByPort"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 557 | for (PortEvent portEvent : removedPortEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 558 | affectedPaths.addAll(pathIntents.getIntentsByPort(portEvent.getDpid(), portEvent.getNumber())); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 559 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 560 | p.log("end_getIntentsByPort"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 561 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 562 | p.log("begin_getIntentsByDpid"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 563 | for (SwitchEvent switchEvent : removedSwitchEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 564 | affectedPaths.addAll(pathIntents.getIntentsByDpid(switchEvent.getDpid())); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 565 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 566 | p.log("end_getIntentsByDpid"); |
| 567 | } |
| 568 | p.log("begin_reroutePaths"); |
| 569 | reroutePaths(affectedPaths); |
| 570 | p.log("end_reroutePaths"); |
| 571 | p.flushLog(); |
| 572 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 573 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 574 | // ================================================================================ |
| 575 | // IEventChannelListener implementations |
| 576 | // ================================================================================ |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 577 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 578 | @Override |
| 579 | public void entryAdded(IntentStateList value) { |
| 580 | entryUpdated(value); |
| 581 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 582 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 583 | @Override |
| 584 | public void entryRemoved(IntentStateList value) { |
| 585 | // do nothing |
| 586 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 587 | |
Ray Milkey | 149693c | 2014-05-20 14:58:53 -0700 | [diff] [blame] | 588 | @SuppressWarnings("fallthrough") |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 589 | @Override |
| 590 | public void entryUpdated(IntentStateList value) { |
| 591 | // TODO draw state transition diagram in multiple ONOS instances and update this method |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 592 | |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 593 | IntentOperationList opList = new IntentOperationList(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 594 | lock.lock(); // TODO optimize locking using smaller steps |
| 595 | try { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 596 | log.trace("lock entryUpdated, lock obj is already locked? {}", lock.isLocked()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 597 | // reflect state changes of path-level intent into application-level intents |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 598 | IntentStateList highLevelIntentStates = new IntentStateList(); |
| 599 | IntentStateList pathIntentStates = new IntentStateList(); |
| 600 | for (Entry<String, IntentState> entry : value.entrySet()) { |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 601 | String pathIntentId = entry.getKey(); |
| 602 | IntentState nextPathIntentState = entry.getValue(); |
| 603 | PathIntent pathIntent = (PathIntent) pathIntents.getIntent(pathIntentId); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 604 | if (pathIntent == null) { |
| 605 | continue; |
| 606 | } |
Toshio Koide | 8315d7d | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 607 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 608 | Intent parentIntent = pathIntent.getParentIntent(); |
| 609 | if (parentIntent == null || |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 610 | !(parentIntent instanceof ShortestPathIntent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 611 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 612 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 613 | |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 614 | boolean isChildIntent = ((ShortestPathIntent) parentIntent).getPathIntentId().equals(pathIntentId); |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 615 | |
| 616 | // Check necessity for retrying the intent execution. |
| 617 | // When the PathIntent(=isChildIntent) transitioned to INST_{ACK/NACK} |
| 618 | // but was marked as stale (e.g., has been requested to reroute by Topology event), |
| 619 | // then immediately enqueue the re-computation of parent intent. |
| 620 | if (isChildIntent && staleIntents.containsKey(parentIntent.getId()) && ( |
| 621 | nextPathIntentState.equals(IntentState.INST_ACK) || |
| 622 | nextPathIntentState.equals(IntentState.INST_NACK))) { |
| 623 | opList.add(Operator.ADD, parentIntent); |
| 624 | staleIntents.remove(parentIntent.getId()); |
| 625 | log.debug("retrying intent execution for intent ID:{}", parentIntent.getId()); |
| 626 | } |
| 627 | |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 628 | switch (nextPathIntentState) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 629 | case INST_ACK: |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 630 | Set<Long> installedDpids = calcInstalledDpids(pathIntent, value.domainSwitchDpids); |
| 631 | if (!isFlowInstalled(pathIntent, installedDpids)) { |
| 632 | break; |
| 633 | } |
| 634 | // FALLTHROUGH |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 635 | case INST_NACK: |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 636 | // FALLTHROUGH |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 637 | case DEL_PENDING: |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 638 | if (isChildIntent) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 639 | log.debug("put the state highLevelIntentStates ID {}, state {}", |
| 640 | parentIntent.getId(), nextPathIntentState); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 641 | highLevelIntentStates.put(parentIntent.getId(), nextPathIntentState); |
| 642 | } |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 643 | log.debug("put the state pathIntentStates ID {}, state {}", |
| 644 | pathIntentId, nextPathIntentState); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 645 | pathIntentStates.put(pathIntentId, nextPathIntentState); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 646 | break; |
| 647 | case DEL_ACK: |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 648 | if (isChildIntent) { |
| 649 | if (intentInstalledMap.containsKey(parentIntent.getId())) { |
| 650 | intentInstalledMap.remove(parentIntent.getId()); |
| 651 | } |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 652 | log.debug("put the state highLevelIntentStates ID {}, state {}", |
| 653 | parentIntent.getId(), nextPathIntentState); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 654 | highLevelIntentStates.put(parentIntent.getId(), nextPathIntentState); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 655 | } |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 656 | log.debug("put the state pathIntentStates ID {}, state {}", |
| 657 | pathIntentId, nextPathIntentState); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 658 | pathIntentStates.put(pathIntentId, nextPathIntentState); |
| 659 | break; |
| 660 | case CREATED: |
| 661 | break; |
| 662 | case DEL_REQ: |
| 663 | break; |
| 664 | case INST_REQ: |
| 665 | break; |
| 666 | case REROUTE_REQ: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 667 | break; |
| 668 | default: |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | highLevelIntents.changeStates(highLevelIntentStates); |
| 673 | pathIntents.changeStates(pathIntentStates); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 674 | } finally { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 675 | lock.unlock(); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 676 | log.trace("unlock entryUpdated"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 677 | } |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 678 | executeIntentOperations(opList); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 679 | } |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 680 | |
| 681 | /*** |
| 682 | * 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] | 683 | * |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 684 | * @param pathIntent : The pathIntent to be checked |
| 685 | * @param installedDpids : The dpids installed on one ONOS instance |
| 686 | * @return The result of whether a pathIntent has been installed or not. |
| 687 | */ |
| 688 | private boolean isFlowInstalled(PathIntent pathIntent, Set<Long> installedDpids) { |
| 689 | String parentIntentId = pathIntent.getParentIntent().getId(); |
| 690 | log.debug("parentIntentId {}", parentIntentId); |
| 691 | |
| 692 | if (intentInstalledMap.containsKey(parentIntentId)) { |
| 693 | if (!installedDpids.isEmpty()) { |
| 694 | intentInstalledMap.get(parentIntentId).addAll(installedDpids); |
| 695 | } |
| 696 | } else { |
| 697 | // This is the creation of an entry. |
| 698 | intentInstalledMap.put(parentIntentId, installedDpids); |
| 699 | } |
| 700 | |
| 701 | Set<Long> allSwitchesForPath = new HashSet<Long>(); |
| 702 | ShortestPathIntent spfIntent = (ShortestPathIntent) pathIntent.getParentIntent(); |
| 703 | |
| 704 | for (LinkEvent linkEvent : pathIntent.getPath()) { |
| 705 | long sw = linkEvent.getSrc().getDpid(); |
| 706 | allSwitchesForPath.add(sw); |
| 707 | } |
| 708 | allSwitchesForPath.add(spfIntent.getDstSwitchDpid()); |
| 709 | |
| 710 | if (log.isTraceEnabled()) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 711 | log.trace("All switches {}, installed installedDpids {}", |
| 712 | allSwitchesForPath, intentInstalledMap.get(parentIntentId)); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | if (allSwitchesForPath.equals(intentInstalledMap.get(parentIntentId))) { |
| 716 | intentInstalledMap.remove(parentIntentId); |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | private Set<Long> calcInstalledDpids(PathIntent pathIntent, Set<Long> domainSwitchDpids) { |
| 724 | Set<Long> allSwitchesForPath = new HashSet<Long>(); |
| 725 | ShortestPathIntent spfIntent = (ShortestPathIntent) pathIntent.getParentIntent(); |
| 726 | |
| 727 | for (LinkEvent linkEvent : pathIntent.getPath()) { |
| 728 | long sw = linkEvent.getSrc().getDpid(); |
| 729 | |
| 730 | if (domainSwitchDpids.contains(sw)) { |
| 731 | allSwitchesForPath.add(sw); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | if (domainSwitchDpids.contains(spfIntent.getDstSwitchDpid())) { |
| 736 | allSwitchesForPath.add(spfIntent.getDstSwitchDpid()); |
| 737 | } |
| 738 | |
| 739 | if (log.isTraceEnabled()) { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 740 | log.trace("All switches for a path {}, domain switch dpids {}", allSwitchesForPath, domainSwitchDpids); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | return allSwitchesForPath; |
| 744 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 745 | } |