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; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.metrics.OnosMetrics; |
| 41 | import net.onrc.onos.core.metrics.OnosMetrics.MetricsComponent; |
| 42 | import net.onrc.onos.core.metrics.OnosMetrics.MetricsFeature; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 43 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 44 | import net.onrc.onos.core.topology.ITopologyListener; |
| 45 | import net.onrc.onos.core.topology.ITopologyService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 46 | import net.onrc.onos.core.topology.LinkEvent; |
| 47 | import net.onrc.onos.core.topology.PortEvent; |
| 48 | import net.onrc.onos.core.topology.SwitchEvent; |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 49 | import net.onrc.onos.core.topology.TopologyEvents; |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 50 | import net.onrc.onos.core.util.Dpid; |
Yuta HIGUCHI | b809364 | 2014-07-16 13:17:07 -0700 | [diff] [blame] | 51 | import net.onrc.onos.core.util.LinkTuple; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 52 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
| 55 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 56 | import com.codahale.metrics.Gauge; |
| 57 | import com.codahale.metrics.Meter; |
| 58 | |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 59 | /** |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 60 | * The PathCalcRuntimeModule contains the PathCalcRuntime and PersistIntent. |
| 61 | * <p> |
| 62 | * It is responsible for converting operations for application level intents |
| 63 | * into operations for path level intents and send the converted operations |
| 64 | * to PlanCalcRuntimeModule in order to calculate flow entries and install them. |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 65 | */ |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 66 | public class PathCalcRuntimeModule implements IFloodlightModule, |
| 67 | IPathCalcRuntimeService, |
| 68 | ITopologyListener, |
| 69 | IEventChannelListener<Long, IntentStateList> { |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Logging object for performance measurement. |
| 73 | * TODO: merge this into measurement framework |
| 74 | */ |
Pavlin Radoslavov | fee8098 | 2014-04-10 12:12:04 -0700 | [diff] [blame] | 75 | static class PerfLog { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | private String step; |
| 77 | private long time; |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 78 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | public PerfLog(String step) { |
| 80 | this.step = step; |
| 81 | this.time = System.nanoTime(); |
| 82 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 83 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | public void logThis() { |
Pavlin Radoslavov | 964f8ae | 2014-04-18 16:44:14 -0700 | [diff] [blame] | 85 | log.debug("Time:{}, Step:{}", time, step); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 88 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Formatted logger for performance measurement. |
| 91 | * TODO: merge this into measurement framework |
| 92 | */ |
Pavlin Radoslavov | 53ad5e3 | 2014-04-10 14:24:26 -0700 | [diff] [blame] | 93 | static class PerfLogger { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | private LinkedList<PerfLog> logData = new LinkedList<>(); |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 95 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | public PerfLogger(String logPhase) { |
| 97 | log("start_" + logPhase); |
| 98 | } |
Toshio Koide | bf87566 | 2014-02-24 12:19:15 -0800 | [diff] [blame] | 99 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | public void log(String step) { |
| 101 | logData.add(new PerfLog(step)); |
| 102 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 103 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | public void flushLog() { |
| 105 | log("finish"); |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 106 | for (PerfLog perfLog : logData) { |
| 107 | perfLog.logThis(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | } |
| 109 | logData.clear(); |
| 110 | } |
| 111 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 112 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 113 | /** |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 114 | * A class to track the status of high-level intents. |
| 115 | * Currently, it is used for monitoring and measurement purposes. |
| 116 | */ |
| 117 | private class HighLevelIntentsTracker implements ChangedListener { |
| 118 | @Override |
| 119 | public void intentsChange(LinkedList<ChangedEvent> events) { |
| 120 | // |
| 121 | // Process the events one-by-one and collect measurements. |
| 122 | // |
| 123 | for (ChangedEvent event : events) { |
| 124 | log.debug("HighLevelIntentsTracker: Intent ID {}, eventType {}, intentState {}", |
| 125 | event.intent.getId(), event.eventType, |
| 126 | event.intent.getState()); |
| 127 | |
| 128 | // |
| 129 | // Update the metrics |
| 130 | // |
| 131 | switch (event.eventType) { |
| 132 | case ADDED: |
| 133 | break; |
| 134 | case REMOVED: |
| 135 | break; |
| 136 | case STATE_CHANGED: |
| 137 | IntentState state = event.intent.getState(); |
| 138 | switch (state) { |
| 139 | case INST_REQ: |
| 140 | break; |
| 141 | case INST_ACK: |
| 142 | intentAddProcessingRate.mark(1); |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 143 | intentAddEndTimestampEpochMs = System.currentTimeMillis(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 144 | break; |
| 145 | case INST_NACK: |
| 146 | break; |
| 147 | case DEL_REQ: |
| 148 | break; |
| 149 | case DEL_ACK: |
| 150 | intentRemoveProcessingRate.mark(1); |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 151 | intentRemoveEndTimestampEpochMs = System.currentTimeMillis(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 152 | break; |
| 153 | case DEL_PENDING: |
| 154 | break; |
| 155 | case REROUTE_REQ: |
| 156 | break; |
| 157 | default: |
| 158 | break; |
| 159 | } |
| 160 | break; |
| 161 | default: |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 169 | * A class to track the deletion of intents and purge them as appropriate. |
| 170 | */ |
| 171 | private class DeleteIntentsTracker implements ChangedListener { |
| 172 | @Override |
| 173 | public void intentsChange(LinkedList<ChangedEvent> events) { |
| 174 | List<String> removeIntentIds = new LinkedList<String>(); |
| 175 | List<String> removePathIds = new LinkedList<String>(); |
| 176 | |
| 177 | // |
| 178 | // Process the events one-by-one and collect the Intent IDs of |
| 179 | // those intents that should be purged. |
| 180 | // |
| 181 | for (ChangedEvent event : events) { |
| 182 | log.debug("DeleteIntentsTracker: Intent ID {}, eventType {}", |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 183 | event.intent.getId(), event.eventType); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 184 | PathIntent pathIntent = (PathIntent) pathIntents.getIntent(event.intent.getId()); |
| 185 | if (pathIntent == null) { |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | // |
| 190 | // Test whether the new Intent state allows the Intent |
| 191 | // to be purged. |
| 192 | // |
| 193 | boolean shouldPurge = false; |
| 194 | switch (event.eventType) { |
| 195 | case ADDED: |
| 196 | break; |
| 197 | case REMOVED: |
| 198 | break; |
| 199 | case STATE_CHANGED: |
| 200 | IntentState state = pathIntent.getState(); |
| 201 | switch (state) { |
| 202 | case INST_REQ: |
| 203 | break; |
| 204 | case INST_ACK: |
| 205 | break; |
| 206 | case INST_NACK: |
| 207 | shouldPurge = true; |
| 208 | break; |
| 209 | case DEL_REQ: |
| 210 | break; |
| 211 | case DEL_ACK: |
| 212 | shouldPurge = true; |
| 213 | break; |
| 214 | case DEL_PENDING: |
| 215 | break; |
Pavlin Radoslavov | 954e082 | 2014-06-24 12:59:44 -0700 | [diff] [blame] | 216 | case REROUTE_REQ: |
| 217 | break; |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 218 | default: |
| 219 | break; |
| 220 | } |
| 221 | break; |
| 222 | default: |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | if (shouldPurge) { |
| 227 | removePathIds.add(pathIntent.getId()); |
| 228 | Intent parentIntent = pathIntent.getParentIntent(); |
| 229 | if (parentIntent != null) { |
| 230 | // |
| 231 | // Remove the High-level Intent only if it was |
| 232 | // explicitly deleted by the user via the API. |
| 233 | // |
| 234 | String intentId = parentIntent.getId(); |
| 235 | if (removedApplicationIntentIds.contains(intentId)) { |
| 236 | removeIntentIds.add(intentId); |
| 237 | removedApplicationIntentIds.remove(intentId); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // Purge the intents |
| 244 | if (!removeIntentIds.isEmpty()) { |
| 245 | highLevelIntents.purge(removeIntentIds); |
| 246 | } |
| 247 | if (!removePathIds.isEmpty()) { |
| 248 | pathIntents.purge(removePathIds); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 253 | private PathCalcRuntime runtime; |
| 254 | private IDatagridService datagridService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 255 | private ITopologyService topologyService; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 256 | private IntentMap highLevelIntents; |
| 257 | private PathIntentMap pathIntents; |
| 258 | private IControllerRegistryService controllerRegistry; |
| 259 | private PersistIntent persistIntent; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 260 | private IRestApiService restApi; |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 261 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 262 | private IEventChannel<Long, IntentOperationList> opEventChannel; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 263 | private final ReentrantLock lock = new ReentrantLock(true); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 264 | private static final String INTENT_OP_EVENT_CHANNEL_NAME = "onos.pathintent"; |
| 265 | private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state"; |
| 266 | private static final Logger log = LoggerFactory.getLogger(PathCalcRuntimeModule.class); |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 267 | |
Yuta HIGUCHI | b809364 | 2014-07-16 13:17:07 -0700 | [diff] [blame] | 268 | private HashSet<LinkTuple> unmatchedLinkEvents = new HashSet<>(); |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 269 | private ConcurrentMap<String, Set<Long>> intentInstalledMap = new ConcurrentHashMap<String, Set<Long>>(); |
| 270 | private ConcurrentMap<String, Intent> staleIntents = new ConcurrentHashMap<String, Intent>(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 271 | private DeleteIntentsTracker deleteIntentsTracker = new DeleteIntentsTracker(); |
| 272 | private Set<String> removedApplicationIntentIds = new HashSet<String>(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 273 | private HighLevelIntentsTracker highLevelIntentsTracker = new HighLevelIntentsTracker(); |
| 274 | |
| 275 | // |
| 276 | // Metrics |
| 277 | // |
| 278 | private static final MetricsComponent METRICS_COMPONENT = |
| 279 | OnosMetrics.registerComponent("Intents"); |
| 280 | private static final MetricsFeature METRICS_FEATURE_ADD_OPERATION = |
| 281 | METRICS_COMPONENT.registerFeature("AddOperation"); |
| 282 | private static final MetricsFeature METRICS_FEATURE_REMOVE_OPERATION = |
| 283 | METRICS_COMPONENT.registerFeature("RemoveOperation"); |
| 284 | // |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 285 | // Timestamp of the incoming Add Intent API operation (ms from the Epoch) |
| 286 | private volatile long intentAddBeginTimestampEpochMs = 0; |
| 287 | private final Gauge<Long> gaugeIntentAddBeginTimestampEpochMs = |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 288 | OnosMetrics.registerMetric(METRICS_COMPONENT, |
| 289 | METRICS_FEATURE_ADD_OPERATION, |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 290 | "BeginOperationTimestamp.EpochMs", |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 291 | new Gauge<Long>() { |
| 292 | @Override |
| 293 | public Long getValue() { |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 294 | return intentAddBeginTimestampEpochMs; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 295 | } |
| 296 | }); |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 297 | // Timestamp of the Add Intent operation completion (ms from the Epoch) |
| 298 | private volatile long intentAddEndTimestampEpochMs = 0; |
| 299 | private final Gauge<Long> gaugeIntentAddEndTimestampEpochMs = |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 300 | OnosMetrics.registerMetric(METRICS_COMPONENT, |
| 301 | METRICS_FEATURE_ADD_OPERATION, |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 302 | "EndOperationTimestamp.EpochMs", |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 303 | new Gauge<Long>() { |
| 304 | @Override |
| 305 | public Long getValue() { |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 306 | return intentAddEndTimestampEpochMs; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 307 | } |
| 308 | }); |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 309 | // Timestamp of the incoming Remove Intent API operation (ms from the Epoch) |
| 310 | private volatile long intentRemoveBeginTimestampEpochMs = 0; |
| 311 | private final Gauge<Long> gaugeIntentRemoveBeginTimestampEpochMs = |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 312 | OnosMetrics.registerMetric(METRICS_COMPONENT, |
| 313 | METRICS_FEATURE_REMOVE_OPERATION, |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 314 | "BeginOperationTimestamp.EpochMs", |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 315 | new Gauge<Long>() { |
| 316 | @Override |
| 317 | public Long getValue() { |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 318 | return intentRemoveBeginTimestampEpochMs; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 319 | } |
| 320 | }); |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 321 | // Timestamp of the Remove Intent operation completion (ms from the Epoch) |
| 322 | private volatile long intentRemoveEndTimestampEpochMs = 0; |
| 323 | private final Gauge<Long> gaugeIntentRemoveEndTimestampEpochMs = |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 324 | OnosMetrics.registerMetric(METRICS_COMPONENT, |
| 325 | METRICS_FEATURE_REMOVE_OPERATION, |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 326 | "EndOperationTimestamp.EpochMs", |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 327 | new Gauge<Long>() { |
| 328 | @Override |
| 329 | public Long getValue() { |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 330 | return intentRemoveEndTimestampEpochMs; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 331 | } |
| 332 | }); |
| 333 | // |
| 334 | // Rate of the incoming Add Intent API operations |
| 335 | private final Meter intentAddIncomingRate = |
| 336 | OnosMetrics.createMeter(METRICS_COMPONENT, |
| 337 | METRICS_FEATURE_ADD_OPERATION, |
| 338 | "IncomingRate"); |
| 339 | // Rate of processing the Add Intent operations |
| 340 | private final Meter intentAddProcessingRate = |
| 341 | OnosMetrics.createMeter(METRICS_COMPONENT, |
| 342 | METRICS_FEATURE_ADD_OPERATION, |
| 343 | "ProcessingRate"); |
| 344 | // Rate of the incoming Remove Intent API operations |
| 345 | private final Meter intentRemoveIncomingRate = |
| 346 | OnosMetrics.createMeter(METRICS_COMPONENT, |
| 347 | METRICS_FEATURE_REMOVE_OPERATION, |
| 348 | "IncomingRate"); |
| 349 | // Rate of processing the Remove Intent operations |
| 350 | private final Meter intentRemoveProcessingRate = |
| 351 | OnosMetrics.createMeter(METRICS_COMPONENT, |
| 352 | METRICS_FEATURE_REMOVE_OPERATION, |
| 353 | "ProcessingRate"); |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 354 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 355 | // ================================================================================ |
| 356 | // private methods |
| 357 | // ================================================================================ |
| 358 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 359 | /** |
| 360 | * Creates operations (IntentOperationList) for Application-level |
| 361 | * intents that should be rerouted because of topology change, |
| 362 | * and execute the created operations. |
| 363 | * |
| 364 | * @param oldPaths a list of invalid path intents (which should be rerouted) |
| 365 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 366 | private void reroutePaths(Collection<Intent> oldPaths) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 367 | if (oldPaths == null || oldPaths.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 368 | return; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 369 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 370 | |
| 371 | IntentOperationList reroutingOperation = new IntentOperationList(); |
| 372 | for (Intent intent : oldPaths) { |
| 373 | PathIntent pathIntent = (PathIntent) intent; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 374 | if (pathIntent.isPathFrozen()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 375 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 376 | } |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 377 | Intent parentIntent = pathIntent.getParentIntent(); |
| 378 | if (parentIntent == null) { |
| 379 | continue; |
| 380 | } |
| 381 | if (pathIntent.getState().equals(IntentState.INST_ACK)) { |
| 382 | if (!reroutingOperation.contains(parentIntent)) { |
| 383 | // reroute now |
| 384 | reroutingOperation.add(Operator.ADD, parentIntent); |
| 385 | } |
| 386 | } else if (pathIntent.getState().equals(IntentState.INST_REQ)) { |
| 387 | // reroute after the completion of the current execution |
| 388 | staleIntents.put(parentIntent.getId(), parentIntent); |
| 389 | log.debug("pending reroute execution for intent ID:{}", parentIntent.getId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | executeIntentOperations(reroutingOperation); |
| 393 | } |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 394 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 395 | /** |
| 396 | * Checks whether the entire path's flow entries are installed or not. |
| 397 | * |
| 398 | * @param pathIntent : The pathIntent to be checked |
| 399 | * @param installedDpids : The dpids installed on one ONOS instance |
| 400 | * @return The result of whether a pathIntent has been installed or not. |
| 401 | */ |
| 402 | private boolean isFlowInstalled(PathIntent pathIntent, Set<Long> installedDpids) { |
| 403 | String pathIntentId = pathIntent.getId(); |
| 404 | |
| 405 | if (intentInstalledMap.containsKey(pathIntentId)) { |
| 406 | if (!installedDpids.isEmpty()) { |
| 407 | intentInstalledMap.get(pathIntentId).addAll(installedDpids); |
| 408 | } |
| 409 | } else { |
| 410 | // This is the creation of an entry. |
| 411 | intentInstalledMap.put(pathIntentId, installedDpids); |
| 412 | } |
| 413 | |
| 414 | Set<Long> allSwitchesForPath = new HashSet<Long>(); |
| 415 | ShortestPathIntent spfIntent = (ShortestPathIntent) pathIntent.getParentIntent(); |
| 416 | |
| 417 | for (LinkEvent linkEvent : pathIntent.getPath()) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 418 | long sw = linkEvent.getSrc().getDpid().value(); |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 419 | allSwitchesForPath.add(sw); |
| 420 | } |
| 421 | allSwitchesForPath.add(spfIntent.getDstSwitchDpid()); |
| 422 | |
| 423 | if (log.isDebugEnabled()) { |
| 424 | log.debug("checking flow installation. ID:{}, dpids:{}, installed:{}", |
| 425 | pathIntentId, |
| 426 | allSwitchesForPath, |
| 427 | intentInstalledMap.get(pathIntentId)); |
| 428 | } |
| 429 | |
| 430 | if (allSwitchesForPath.equals(intentInstalledMap.get(pathIntentId))) { |
| 431 | intentInstalledMap.remove(pathIntentId); |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Enumerates switch dpids along the specified path and inside the specified domain. |
| 440 | * |
| 441 | * @param pathIntent the path for enumeration |
| 442 | * @param domainSwitchDpids a set of the domain switch dpids |
| 443 | * @return a set of switch dpids along the specified path and inside the specified domain |
| 444 | */ |
| 445 | private Set<Long> calcInstalledDpids(PathIntent pathIntent, Set<Long> domainSwitchDpids) { |
| 446 | Set<Long> allSwitchesForPath = new HashSet<Long>(); |
| 447 | ShortestPathIntent spfIntent = (ShortestPathIntent) pathIntent.getParentIntent(); |
| 448 | |
| 449 | for (LinkEvent linkEvent : pathIntent.getPath()) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 450 | long sw = linkEvent.getSrc().getDpid().value(); |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 451 | |
| 452 | if (domainSwitchDpids.contains(sw)) { |
| 453 | allSwitchesForPath.add(sw); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (domainSwitchDpids.contains(spfIntent.getDstSwitchDpid())) { |
| 458 | allSwitchesForPath.add(spfIntent.getDstSwitchDpid()); |
| 459 | } |
| 460 | |
| 461 | if (log.isTraceEnabled()) { |
| 462 | log.trace("All switches for a path {}, domain switch dpids {}", allSwitchesForPath, domainSwitchDpids); |
| 463 | } |
| 464 | |
| 465 | return allSwitchesForPath; |
| 466 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 467 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 468 | // ================================================================================ |
| 469 | // IFloodlightModule implementations |
| 470 | // ================================================================================ |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 471 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 472 | /** |
| 473 | * {@inheritDoc} |
| 474 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 475 | @Override |
| 476 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 477 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(1); |
| 478 | l.add(IPathCalcRuntimeService.class); |
| 479 | return l; |
| 480 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 481 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 482 | /** |
| 483 | * {@inheritDoc} |
| 484 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 485 | @Override |
| 486 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 487 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(); |
| 488 | m.put(IPathCalcRuntimeService.class, this); |
| 489 | return m; |
| 490 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 491 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 492 | /** |
| 493 | * {@inheritDoc} |
| 494 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 495 | @Override |
| 496 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 497 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(2); |
| 498 | l.add(IDatagridService.class); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 499 | l.add(IRestApiService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 500 | l.add(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 501 | return l; |
| 502 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 503 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 504 | /** |
| 505 | * {@inheritDoc} |
| 506 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 507 | @Override |
| 508 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
| 509 | datagridService = context.getServiceImpl(IDatagridService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 510 | topologyService = context.getServiceImpl(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 511 | controllerRegistry = context.getServiceImpl(IControllerRegistryService.class); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 512 | restApi = context.getServiceImpl(IRestApiService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 513 | } |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 514 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 515 | /** |
| 516 | * {@inheritDoc} |
| 517 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 518 | @Override |
| 519 | public void startUp(FloodlightModuleContext context) { |
| 520 | highLevelIntents = new IntentMap(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 521 | highLevelIntents.addChangeListener(highLevelIntentsTracker); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 522 | runtime = new PathCalcRuntime(topologyService.getTopology()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 523 | pathIntents = new PathIntentMap(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 524 | pathIntents.addChangeListener(deleteIntentsTracker); |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 525 | opEventChannel = datagridService.createChannel( |
| 526 | INTENT_OP_EVENT_CHANNEL_NAME, Long.class, IntentOperationList.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 527 | datagridService.addListener(INTENT_STATE_EVENT_CHANNEL_NAME, this, Long.class, IntentStateList.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 528 | topologyService.registerTopologyListener(this); |
Pavlin Radoslavov | 0294e05 | 2014-04-10 13:36:45 -0700 | [diff] [blame] | 529 | persistIntent = new PersistIntent(controllerRegistry); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 530 | restApi.addRestletRoutable(new IntentWebRoutable()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 531 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 532 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 533 | // ====================================================================== |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 534 | // IPathCalcRuntimeService implementations |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 535 | // ====================================================================== |
| 536 | |
| 537 | /** |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 538 | * {@inheritDoc} |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 539 | */ |
| 540 | @Override |
| 541 | public boolean addApplicationIntents( |
| 542 | final String appId, |
| 543 | Collection<ApplicationIntent> appIntents) { |
| 544 | // |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 545 | // Update the metrics |
| 546 | // |
| 547 | if (!appIntents.isEmpty()) { |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 548 | this.intentAddBeginTimestampEpochMs = System.currentTimeMillis(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 549 | this.intentAddIncomingRate.mark(appIntents.size()); |
| 550 | } |
| 551 | |
| 552 | // |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 553 | // Process all intents one-by-one |
| 554 | // |
| 555 | // TODO: The Intent Type should be enum instead of a string, |
| 556 | // and we should use a switch statement below to process the |
| 557 | // different type of intents. |
| 558 | // |
| 559 | IntentOperationList intentOperations = new IntentOperationList(); |
| 560 | for (ApplicationIntent appIntent : appIntents) { |
Pavlin Radoslavov | 954e082 | 2014-06-24 12:59:44 -0700 | [diff] [blame] | 561 | String appIntentId = appId + ":" + appIntent.getIntentId(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 562 | |
| 563 | IntentOperation.Operator operator = IntentOperation.Operator.ADD; |
Pavlin Radoslavov | 954e082 | 2014-06-24 12:59:44 -0700 | [diff] [blame] | 564 | Dpid srcSwitchDpid = new Dpid(appIntent.getSrcSwitchDpid()); |
| 565 | Dpid dstSwitchDpid = new Dpid(appIntent.getDstSwitchDpid()); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 566 | |
Pavlin Radoslavov | 954e082 | 2014-06-24 12:59:44 -0700 | [diff] [blame] | 567 | if (appIntent.getIntentType().equals("SHORTEST_PATH")) { |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 568 | // |
| 569 | // Process Shortest-Path Intent |
| 570 | // |
| 571 | ShortestPathIntent spi = |
| 572 | new ShortestPathIntent(appIntentId, |
| 573 | srcSwitchDpid.value(), |
Pavlin Radoslavov | 954e082 | 2014-06-24 12:59:44 -0700 | [diff] [blame] | 574 | appIntent.getSrcSwitchPort(), |
| 575 | MACAddress.valueOf(appIntent.getMatchSrcMac()).toLong(), |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 576 | dstSwitchDpid.value(), |
Pavlin Radoslavov | 954e082 | 2014-06-24 12:59:44 -0700 | [diff] [blame] | 577 | appIntent.getDstSwitchPort(), |
| 578 | MACAddress.valueOf(appIntent.getMatchDstMac()).toLong()); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 579 | spi.setPathFrozen(appIntent.isStaticPath()); |
| 580 | intentOperations.add(operator, spi); |
Pavlin Radoslavov | 954e082 | 2014-06-24 12:59:44 -0700 | [diff] [blame] | 581 | } else if (appIntent.getIntentType().equals("CONSTRAINED_SHORTEST_PATH")) { |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 582 | // |
| 583 | // Process Constrained Shortest-Path Intent |
| 584 | // |
| 585 | ConstrainedShortestPathIntent cspi = |
| 586 | new ConstrainedShortestPathIntent(appIntentId, |
| 587 | srcSwitchDpid.value(), |
Pavlin Radoslavov | 954e082 | 2014-06-24 12:59:44 -0700 | [diff] [blame] | 588 | appIntent.getSrcSwitchPort(), |
| 589 | MACAddress.valueOf(appIntent.getMatchSrcMac()).toLong(), |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 590 | dstSwitchDpid.value(), |
Pavlin Radoslavov | 954e082 | 2014-06-24 12:59:44 -0700 | [diff] [blame] | 591 | appIntent.getDstSwitchPort(), |
| 592 | MACAddress.valueOf(appIntent.getMatchDstMac()).toLong(), |
| 593 | appIntent.getBandwidth()); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 594 | cspi.setPathFrozen(appIntent.isStaticPath()); |
| 595 | intentOperations.add(operator, cspi); |
| 596 | } else { |
| 597 | log.error("Unknown Application Intent Type: {}", |
Pavlin Radoslavov | 954e082 | 2014-06-24 12:59:44 -0700 | [diff] [blame] | 598 | appIntent.getIntentType()); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 599 | return false; |
| 600 | } |
| 601 | removedApplicationIntentIds.remove(appIntentId); |
| 602 | } |
| 603 | // Apply the Intent Operations |
| 604 | executeIntentOperations(intentOperations); |
| 605 | return true; |
| 606 | } |
| 607 | |
| 608 | /** |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 609 | * {@inheritDoc} |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 610 | */ |
| 611 | @Override |
| 612 | public boolean removeApplicationIntents(final String appId, |
| 613 | Collection<String> intentIds) { |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 614 | // |
| 615 | // Prepare the timestamp for metrics |
| 616 | // |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 617 | long timestampEpochMs = System.currentTimeMillis(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 618 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 619 | IntentMap intentMap = getHighLevelIntents(); |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 620 | List<String> removeIntentIds = new LinkedList<String>(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 621 | |
| 622 | // |
| 623 | // Process all intents one-by-one |
| 624 | // |
| 625 | IntentOperationList operations = new IntentOperationList(); |
| 626 | for (String intentId : intentIds) { |
| 627 | String appIntentId = appId + ":" + intentId; |
| 628 | Intent intent = intentMap.getIntent(appIntentId); |
| 629 | if (intent != null) { |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 630 | if (intent.getState() == IntentState.INST_NACK) { |
| 631 | // TODO: A hack to remove intents stuck in INST_NACK state |
| 632 | removeIntentIds.add(intent.getId()); |
| 633 | continue; |
| 634 | } |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 635 | operations.add(IntentOperation.Operator.REMOVE, intent); |
| 636 | removedApplicationIntentIds.add(appIntentId); |
| 637 | } |
| 638 | } |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 639 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 640 | // |
| 641 | // Update the metrics |
| 642 | // |
| 643 | if (!operations.isEmpty()) { |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 644 | this.intentRemoveBeginTimestampEpochMs = timestampEpochMs; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 645 | this.intentRemoveIncomingRate.mark(operations.size()); |
| 646 | } |
| 647 | |
| 648 | // |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 649 | // Purge intents |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 650 | // |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 651 | if (!removeIntentIds.isEmpty()) { |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 652 | |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 653 | lock.lock(); // TODO optimize locking using smaller steps |
| 654 | try { |
| 655 | highLevelIntents.purge(removeIntentIds); |
| 656 | } finally { |
| 657 | lock.unlock(); |
| 658 | } |
| 659 | } |
| 660 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 661 | executeIntentOperations(operations); |
| 662 | |
| 663 | return true; |
| 664 | } |
| 665 | |
| 666 | /** |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 667 | * {@inheritDoc} |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 668 | */ |
| 669 | @Override |
| 670 | public boolean removeAllApplicationIntents(final String appId) { |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 671 | // |
| 672 | // Prepare the timestamp for metrics |
| 673 | // |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 674 | long timestampEpochMs = System.currentTimeMillis(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 675 | |
| 676 | Collection<Intent> allHighLevelIntents = |
| 677 | getHighLevelIntents().getAllIntents(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 678 | |
| 679 | // |
| 680 | // Remove all intents |
| 681 | // |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 682 | List<String> removeIntentIds = new LinkedList<String>(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 683 | IntentOperationList operations = new IntentOperationList(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 684 | for (Intent intent : allHighLevelIntents) { |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 685 | if (intent.getState() == IntentState.INST_NACK) { |
| 686 | // TODO: A hack to remove intents stuck in INST_NACK state |
| 687 | removeIntentIds.add(intent.getId()); |
| 688 | continue; |
| 689 | } |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 690 | operations.add(IntentOperation.Operator.REMOVE, intent); |
| 691 | removedApplicationIntentIds.add(intent.getId()); |
| 692 | } |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 693 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 694 | // |
| 695 | // Update the metrics |
| 696 | // |
| 697 | if (!operations.isEmpty()) { |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 698 | this.intentRemoveBeginTimestampEpochMs = timestampEpochMs; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 699 | this.intentRemoveIncomingRate.mark(operations.size()); |
| 700 | } |
| 701 | |
| 702 | // |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 703 | // Purge intents |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 704 | // |
Pavlin Radoslavov | da14784 | 2014-06-13 20:08:09 -0700 | [diff] [blame] | 705 | if (!removeIntentIds.isEmpty()) { |
| 706 | lock.lock(); // TODO optimize locking using smaller steps |
| 707 | try { |
| 708 | highLevelIntents.purge(removeIntentIds); |
| 709 | } finally { |
| 710 | lock.unlock(); |
| 711 | } |
| 712 | } |
| 713 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 714 | executeIntentOperations(operations); |
| 715 | |
| 716 | return true; |
| 717 | } |
Toshio Koide | 798bc1b | 2014-02-20 14:02:40 -0800 | [diff] [blame] | 718 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 719 | /** |
| 720 | * {@inheritDoc} |
| 721 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 722 | @Override |
| 723 | public IntentOperationList executeIntentOperations(IntentOperationList list) { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 724 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 725 | if (list == null || list.size() == 0) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 726 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 727 | } |
Toshio Koide | 275d814 | 2014-02-24 16:41:52 -0800 | [diff] [blame] | 728 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 729 | lock.lock(); // TODO optimize locking using smaller steps |
| 730 | try { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 731 | log.trace("lock executeIntentOperations, lock obj is already locked? {}", lock.isLocked()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 732 | // update the map of high-level intents |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 733 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 734 | highLevelIntents.executeOperations(list); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 735 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 736 | // change states of high-level intents |
| 737 | IntentStateList states = new IntentStateList(); |
| 738 | for (IntentOperation op : list) { |
| 739 | switch (op.operator) { |
| 740 | case ADD: |
| 741 | switch (op.intent.getState()) { |
| 742 | case CREATED: |
| 743 | states.put(op.intent.getId(), IntentState.INST_REQ); |
| 744 | break; |
| 745 | case INST_ACK: |
| 746 | states.put(op.intent.getId(), IntentState.REROUTE_REQ); |
| 747 | break; |
| 748 | default: |
| 749 | break; |
| 750 | } |
| 751 | break; |
| 752 | case REMOVE: |
| 753 | switch (op.intent.getState()) { |
| 754 | case CREATED: |
| 755 | states.put(op.intent.getId(), IntentState.DEL_REQ); |
| 756 | break; |
| 757 | default: |
| 758 | break; |
| 759 | } |
| 760 | break; |
| 761 | default: |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | highLevelIntents.changeStates(states); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 766 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 767 | // calculate path-intents (low-level operations) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 768 | IntentOperationList pathIntentOperations = runtime.calcPathIntents(list, highLevelIntents, pathIntents); |
Toshio Koide | 600ae5f | 2014-02-20 18:42:00 -0800 | [diff] [blame] | 769 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 770 | // persist calculated low-level operations into data store |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 771 | long key = persistIntent.getKey(); |
| 772 | persistIntent.persistIfLeader(key, pathIntentOperations); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 773 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 774 | // remove error-intents and reflect them to high-level intents |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 775 | states.clear(); |
| 776 | Iterator<IntentOperation> i = pathIntentOperations.iterator(); |
| 777 | while (i.hasNext()) { |
| 778 | IntentOperation op = i.next(); |
| 779 | if (op.operator.equals(Operator.ERROR)) { |
| 780 | states.put(op.intent.getId(), IntentState.INST_NACK); |
| 781 | i.remove(); |
| 782 | } |
| 783 | } |
| 784 | highLevelIntents.changeStates(states); |
Toshio Koide | a94060f | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 785 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 786 | // update the map of path intents and publish the path operations |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 787 | pathIntents.executeOperations(pathIntentOperations); |
Toshio Koide | 93be5d6 | 2014-02-23 19:30:57 -0800 | [diff] [blame] | 788 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 789 | // send notification |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 790 | // XXX: Send notifications using the same key every time |
| 791 | // and receive them by entryAdded() and entryUpdated() |
| 792 | opEventChannel.addEntry(0L, pathIntentOperations); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 793 | //opEventChannel.removeEntry(key); |
| 794 | return pathIntentOperations; |
| 795 | } finally { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 796 | lock.unlock(); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 797 | log.trace("unlock executeIntentOperations"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 800 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 801 | /** |
| 802 | * {@inheritDoc} |
| 803 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 804 | @Override |
| 805 | public IntentMap getHighLevelIntents() { |
| 806 | return highLevelIntents; |
| 807 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 808 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 809 | /** |
| 810 | * {@inheritDoc} |
| 811 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 812 | @Override |
| 813 | public IntentMap getPathIntents() { |
| 814 | return pathIntents; |
| 815 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 816 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 817 | /** |
| 818 | * {@inheritDoc} |
| 819 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 820 | @Override |
| 821 | public void purgeIntents() { |
| 822 | highLevelIntents.purge(); |
| 823 | pathIntents.purge(); |
| 824 | } |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 825 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 826 | // ================================================================================ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 827 | // ITopologyListener implementations |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 828 | // ================================================================================ |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 829 | /** |
| 830 | * {@inheritDoc} |
| 831 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 832 | @Override |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 833 | public void topologyEvents(TopologyEvents topologyEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 834 | PerfLogger p = new PerfLogger("networkGraphEvents"); |
| 835 | HashSet<Intent> affectedPaths = new HashSet<>(); |
Toshio Koide | 93797dc | 2014-02-27 23:54:26 -0800 | [diff] [blame] | 836 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 837 | boolean rerouteAll = false; |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 838 | for (LinkEvent le : topologyEvents.getAddedLinkEvents()) { |
Yuta HIGUCHI | b809364 | 2014-07-16 13:17:07 -0700 | [diff] [blame] | 839 | final LinkTuple rev = new LinkTuple(le.getDst(), le.getSrc()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 840 | if (unmatchedLinkEvents.contains(rev)) { |
| 841 | rerouteAll = true; |
| 842 | unmatchedLinkEvents.remove(rev); |
| 843 | log.debug("Found matched LinkEvent: {} {}", rev, le); |
| 844 | } else { |
Yuta HIGUCHI | b809364 | 2014-07-16 13:17:07 -0700 | [diff] [blame] | 845 | unmatchedLinkEvents.add(le.getLinkTuple()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 846 | log.debug("Adding unmatched LinkEvent: {}", le); |
| 847 | } |
| 848 | } |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 849 | for (LinkEvent le : topologyEvents.getRemovedLinkEvents()) { |
Yuta HIGUCHI | b809364 | 2014-07-16 13:17:07 -0700 | [diff] [blame] | 850 | if (unmatchedLinkEvents.contains(le.getLinkTuple())) { |
| 851 | unmatchedLinkEvents.remove(le.getLinkTuple()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 852 | log.debug("Removing LinkEvent: {}", le); |
| 853 | } |
| 854 | } |
| 855 | if (unmatchedLinkEvents.size() > 0) { |
| 856 | log.debug("Unmatched link events: {} events", unmatchedLinkEvents.size()); |
| 857 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 858 | |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 859 | if (rerouteAll) { |
| 860 | // |
| 861 | // (topologyEvents.getAddedLinkEvents().size() > 0) || |
| 862 | // (topologyEvents.getAddedPortEvents().size() > 0) || |
| 863 | // (topologyEvents.getAddedSwitchEvents.size() > 0) |
| 864 | // |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 865 | p.log("begin_getAllIntents"); |
| 866 | affectedPaths.addAll(getPathIntents().getAllIntents()); |
| 867 | p.log("end_getAllIntents"); |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 868 | } else if (topologyEvents.getRemovedSwitchEvents().size() > 0 || |
| 869 | topologyEvents.getRemovedLinkEvents().size() > 0 || |
| 870 | topologyEvents.getRemovedPortEvents().size() > 0) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 871 | p.log("begin_getIntentsByLink"); |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 872 | for (LinkEvent linkEvent : topologyEvents.getRemovedLinkEvents()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 873 | affectedPaths.addAll(pathIntents.getIntentsByLink(linkEvent)); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 874 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 875 | p.log("end_getIntentsByLink"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 876 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 877 | p.log("begin_getIntentsByPort"); |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 878 | for (PortEvent portEvent : topologyEvents.getRemovedPortEvents()) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 879 | affectedPaths.addAll(pathIntents.getIntentsByPort( |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 880 | portEvent.getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 881 | portEvent.getPortNumber())); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 882 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 883 | p.log("end_getIntentsByPort"); |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 884 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 885 | p.log("begin_getIntentsByDpid"); |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 886 | for (SwitchEvent switchEvent : topologyEvents.getRemovedSwitchEvents()) { |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 887 | affectedPaths.addAll(pathIntents.getIntentsByDpid(switchEvent.getDpid())); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 888 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 889 | p.log("end_getIntentsByDpid"); |
| 890 | } |
| 891 | p.log("begin_reroutePaths"); |
| 892 | reroutePaths(affectedPaths); |
| 893 | p.log("end_reroutePaths"); |
| 894 | p.flushLog(); |
| 895 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 896 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 897 | // ================================================================================ |
| 898 | // IEventChannelListener implementations |
| 899 | // ================================================================================ |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 900 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 901 | /** |
| 902 | * {@inheritDoc} |
| 903 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 904 | @Override |
| 905 | public void entryAdded(IntentStateList value) { |
| 906 | entryUpdated(value); |
| 907 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 908 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 909 | /** |
| 910 | * {@inheritDoc} |
| 911 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 912 | @Override |
| 913 | public void entryRemoved(IntentStateList value) { |
| 914 | // do nothing |
| 915 | } |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 916 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 917 | /** |
| 918 | * {@inheritDoc} |
| 919 | */ |
Ray Milkey | 149693c | 2014-05-20 14:58:53 -0700 | [diff] [blame] | 920 | @SuppressWarnings("fallthrough") |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 921 | @Override |
| 922 | public void entryUpdated(IntentStateList value) { |
| 923 | // TODO draw state transition diagram in multiple ONOS instances and update this method |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 924 | |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 925 | IntentOperationList opList = new IntentOperationList(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 926 | lock.lock(); // TODO optimize locking using smaller steps |
| 927 | try { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 928 | log.trace("lock entryUpdated, lock obj is already locked? {}", lock.isLocked()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 929 | // reflect state changes of path-level intent into application-level intents |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 930 | IntentStateList highLevelIntentStates = new IntentStateList(); |
| 931 | IntentStateList pathIntentStates = new IntentStateList(); |
| 932 | for (Entry<String, IntentState> entry : value.entrySet()) { |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 933 | String pathIntentId = entry.getKey(); |
| 934 | IntentState nextPathIntentState = entry.getValue(); |
| 935 | PathIntent pathIntent = (PathIntent) pathIntents.getIntent(pathIntentId); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 936 | if (pathIntent == null) { |
| 937 | continue; |
| 938 | } |
Toshio Koide | 8315d7d | 2014-02-21 22:58:32 -0800 | [diff] [blame] | 939 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 940 | Intent parentIntent = pathIntent.getParentIntent(); |
| 941 | if (parentIntent == null || |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 942 | !(parentIntent instanceof ShortestPathIntent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 943 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 944 | } |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame] | 945 | String parentIntentId = parentIntent.getId(); |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 946 | |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 947 | boolean isChildIntent = ((ShortestPathIntent) parentIntent).getPathIntentId().equals(pathIntentId); |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 948 | |
| 949 | // Check necessity for retrying the intent execution. |
| 950 | // When the PathIntent(=isChildIntent) transitioned to INST_{ACK/NACK} |
| 951 | // but was marked as stale (e.g., has been requested to reroute by Topology event), |
| 952 | // then immediately enqueue the re-computation of parent intent. |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame] | 953 | if (isChildIntent && staleIntents.containsKey(parentIntentId) && ( |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 954 | nextPathIntentState.equals(IntentState.INST_ACK) || |
| 955 | nextPathIntentState.equals(IntentState.INST_NACK))) { |
| 956 | opList.add(Operator.ADD, parentIntent); |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame] | 957 | staleIntents.remove(parentIntentId); |
| 958 | log.debug("retrying intent execution for intent ID:{}", parentIntentId); |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 961 | switch (nextPathIntentState) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 962 | case INST_ACK: |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 963 | Set<Long> installedDpids = calcInstalledDpids(pathIntent, value.domainSwitchDpids); |
| 964 | if (!isFlowInstalled(pathIntent, installedDpids)) { |
| 965 | break; |
| 966 | } |
| 967 | // FALLTHROUGH |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 968 | case INST_NACK: |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 969 | // FALLTHROUGH |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 970 | case DEL_PENDING: |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 971 | if (isChildIntent) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 972 | log.debug("put the state highLevelIntentStates ID {}, state {}", |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame] | 973 | parentIntentId, nextPathIntentState); |
| 974 | highLevelIntentStates.put(parentIntentId, nextPathIntentState); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 975 | } |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 976 | log.debug("put the state pathIntentStates ID {}, state {}", |
| 977 | pathIntentId, nextPathIntentState); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 978 | pathIntentStates.put(pathIntentId, nextPathIntentState); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 979 | break; |
| 980 | case DEL_ACK: |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 981 | if (isChildIntent) { |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame] | 982 | if (intentInstalledMap.containsKey(pathIntentId)) { |
| 983 | intentInstalledMap.remove(pathIntentId); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 984 | } |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 985 | log.debug("put the state highLevelIntentStates ID {}, state {}", |
Toshio Koide | 4a58f0a | 2014-06-16 10:34:19 -0700 | [diff] [blame] | 986 | parentIntentId, nextPathIntentState); |
| 987 | highLevelIntentStates.put(parentIntentId, nextPathIntentState); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 988 | } |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 989 | log.debug("put the state pathIntentStates ID {}, state {}", |
| 990 | pathIntentId, nextPathIntentState); |
Toshio Koide | 14dba90 | 2014-06-05 18:39:21 -0700 | [diff] [blame] | 991 | pathIntentStates.put(pathIntentId, nextPathIntentState); |
| 992 | break; |
| 993 | case CREATED: |
| 994 | break; |
| 995 | case DEL_REQ: |
| 996 | break; |
| 997 | case INST_REQ: |
| 998 | break; |
| 999 | case REROUTE_REQ: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1000 | break; |
| 1001 | default: |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | highLevelIntents.changeStates(highLevelIntentStates); |
| 1006 | pathIntents.changeStates(pathIntentStates); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1007 | } finally { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1008 | lock.unlock(); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 1009 | log.trace("unlock entryUpdated"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1010 | } |
Toshio Koide | 353a9e1 | 2014-06-09 21:03:40 -0700 | [diff] [blame] | 1011 | executeIntentOperations(opList); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1012 | } |
Toshio Koide | a9078af | 2014-02-21 16:57:04 -0800 | [diff] [blame] | 1013 | } |