Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.Lists; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 21 | import com.google.common.collect.SetMultimap; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.ReferencePolicy; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.event.Event; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.ElementId; |
| 33 | import org.onosproject.net.HostId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.Link; |
| 35 | import org.onosproject.net.LinkKey; |
| 36 | import org.onosproject.net.NetworkResource; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceEvent; |
| 38 | import org.onosproject.net.device.DeviceListener; |
| 39 | import org.onosproject.net.device.DeviceService; |
| 40 | import org.onosproject.net.host.HostEvent; |
| 41 | import org.onosproject.net.host.HostListener; |
| 42 | import org.onosproject.net.host.HostService; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 43 | import org.onosproject.net.intent.Intent; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 44 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.Key; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 47 | import org.onosproject.net.intent.PartitionEvent; |
| 48 | import org.onosproject.net.intent.PartitionEventListener; |
| 49 | import org.onosproject.net.intent.PartitionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.link.LinkEvent; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 51 | import org.onosproject.net.resource.link.LinkResourceEvent; |
| 52 | import org.onosproject.net.resource.link.LinkResourceListener; |
| 53 | import org.onosproject.net.resource.link.LinkResourceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.topology.TopologyEvent; |
| 55 | import org.onosproject.net.topology.TopologyListener; |
| 56 | import org.onosproject.net.topology.TopologyService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 59 | import java.util.Collection; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 60 | import java.util.Collections; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 61 | import java.util.HashSet; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 62 | import java.util.List; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 63 | import java.util.Set; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 64 | import java.util.concurrent.ConcurrentMap; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 65 | import java.util.concurrent.ExecutorService; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 66 | import java.util.concurrent.Executors; |
| 67 | import java.util.concurrent.ScheduledExecutorService; |
| 68 | import java.util.concurrent.TimeUnit; |
| 69 | import java.util.concurrent.atomic.AtomicBoolean; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 70 | |
| 71 | import static com.google.common.base.Preconditions.checkArgument; |
| 72 | import static com.google.common.base.Preconditions.checkNotNull; |
| 73 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
| 74 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 75 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 76 | import static org.onlab.util.Tools.isNullOrEmpty; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 77 | import static org.onosproject.net.LinkKey.linkKey; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 78 | import static org.onosproject.net.intent.IntentState.INSTALLED; |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 79 | import static org.onosproject.net.intent.IntentState.INSTALLING; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 80 | import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED; |
| 81 | import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 82 | import static org.slf4j.LoggerFactory.getLogger; |
| 83 | |
| 84 | /** |
| 85 | * Entity responsible for tracking installed flows and for monitoring topology |
| 86 | * events to determine what flows are affected by topology changes. |
| 87 | */ |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 88 | @Component(immediate = true) |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 89 | @Service |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 90 | public class ObjectiveTracker implements ObjectiveTrackerService { |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 91 | |
| 92 | private final Logger log = getLogger(getClass()); |
| 93 | |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 94 | private final ConcurrentMap<Key, Intent> intents = Maps.newConcurrentMap(); |
| 95 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 96 | private final SetMultimap<LinkKey, Key> intentsByLink = |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 97 | //TODO this could be slow as a point of synchronization |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 98 | synchronizedSetMultimap(HashMultimap.<LinkKey, Key>create()); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 99 | |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 100 | private final SetMultimap<ElementId, Key> intentsByDevice = |
| 101 | synchronizedSetMultimap(HashMultimap.<ElementId, Key>create()); |
| 102 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected TopologyService topologyService; |
| 105 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected LinkResourceService resourceManager; |
| 108 | |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected DeviceService deviceService; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected HostService hostService; |
| 114 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 115 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, |
| 116 | policy = ReferencePolicy.DYNAMIC) |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 117 | protected IntentService intentService; |
| 118 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 120 | protected PartitionService partitionService; |
| 121 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 122 | private ExecutorService executorService = |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 123 | newSingleThreadExecutor(groupedThreads("onos/intent", "objectivetracker")); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 124 | private ScheduledExecutorService executor = Executors |
| 125 | .newScheduledThreadPool(1); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 126 | |
| 127 | private TopologyListener listener = new InternalTopologyListener(); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 128 | private LinkResourceListener linkResourceListener = |
| 129 | new InternalLinkResourceListener(); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 130 | private DeviceListener deviceListener = new InternalDeviceListener(); |
| 131 | private HostListener hostListener = new InternalHostListener(); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 132 | private PartitionEventListener partitionListener = new InternalPartitionListener(); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 133 | private TopologyChangeDelegate delegate; |
| 134 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 135 | protected final AtomicBoolean updateScheduled = new AtomicBoolean(false); |
| 136 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 137 | @Activate |
| 138 | public void activate() { |
| 139 | topologyService.addListener(listener); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 140 | resourceManager.addListener(linkResourceListener); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 141 | deviceService.addListener(deviceListener); |
| 142 | hostService.addListener(hostListener); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 143 | partitionService.addListener(partitionListener); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 144 | log.info("Started"); |
| 145 | } |
| 146 | |
| 147 | @Deactivate |
| 148 | public void deactivate() { |
| 149 | topologyService.removeListener(listener); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 150 | resourceManager.removeListener(linkResourceListener); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 151 | deviceService.removeListener(deviceListener); |
| 152 | hostService.removeListener(hostListener); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 153 | partitionService.removeListener(partitionListener); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 154 | log.info("Stopped"); |
| 155 | } |
| 156 | |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 157 | protected void bindIntentService(IntentService service) { |
| 158 | if (intentService == null) { |
| 159 | intentService = service; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | protected void unbindIntentService(IntentService service) { |
| 164 | if (intentService == service) { |
| 165 | intentService = null; |
| 166 | } |
| 167 | } |
| 168 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 169 | @Override |
| 170 | public void setDelegate(TopologyChangeDelegate delegate) { |
| 171 | checkNotNull(delegate, "Delegate cannot be null"); |
| 172 | checkArgument(this.delegate == null || this.delegate == delegate, |
| 173 | "Another delegate already set"); |
| 174 | this.delegate = delegate; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void unsetDelegate(TopologyChangeDelegate delegate) { |
| 179 | checkArgument(this.delegate == delegate, "Not the current delegate"); |
| 180 | this.delegate = null; |
| 181 | } |
| 182 | |
| 183 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 184 | public void addTrackedResources(Key intentKey, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 185 | Collection<NetworkResource> resources) { |
| 186 | for (NetworkResource resource : resources) { |
| 187 | if (resource instanceof Link) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 188 | intentsByLink.put(linkKey((Link) resource), intentKey); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 189 | } else if (resource instanceof ElementId) { |
| 190 | intentsByDevice.put((ElementId) resource, intentKey); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 191 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 196 | public void removeTrackedResources(Key intentKey, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 197 | Collection<NetworkResource> resources) { |
| 198 | for (NetworkResource resource : resources) { |
| 199 | if (resource instanceof Link) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 200 | intentsByLink.remove(linkKey((Link) resource), intentKey); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 201 | } else if (resource instanceof ElementId) { |
Sho SHIMIZU | d82a4e6 | 2015-09-09 14:53:46 -0700 | [diff] [blame] | 202 | intentsByDevice.remove(resource, intentKey); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 203 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 207 | @Override |
| 208 | public void trackIntent(IntentData intentData) { |
| 209 | |
| 210 | //NOTE: This will be called for intents that are being added to the store |
| 211 | // locally (i.e. every intent update) |
| 212 | |
| 213 | Key key = intentData.key(); |
| 214 | Intent intent = intentData.intent(); |
| 215 | boolean isLocal = intentService.isLocal(key); |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 216 | boolean isInstalled = intentData.state() == INSTALLING || |
| 217 | intentData.state() == INSTALLED; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 218 | List<Intent> installables = intentData.installables(); |
| 219 | |
| 220 | if (log.isTraceEnabled()) { |
| 221 | log.trace("intent {}, old: {}, new: {}, installableCount: {}, resourceCount: {}", |
| 222 | key, |
| 223 | intentsByDevice.values().contains(key), |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 224 | isLocal && isInstalled, |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 225 | installables.size(), |
| 226 | intent.resources().size() + |
| 227 | installables.stream() |
| 228 | .mapToLong(i -> i.resources().size()).sum()); |
| 229 | } |
| 230 | |
| 231 | if (isNullOrEmpty(installables) && intentData.state() == INSTALLED) { |
| 232 | log.warn("Intent {} is INSTALLED with no installables", key); |
| 233 | } |
| 234 | |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 235 | // FIXME Intents will be added 3 times (once directly using addTracked, |
| 236 | // then when installing and when installed) |
| 237 | if (isLocal && isInstalled) { |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 238 | addTrackedResources(key, intent.resources()); |
| 239 | for (Intent installable : installables) { |
| 240 | addTrackedResources(key, installable.resources()); |
| 241 | } |
| 242 | // FIXME check all resources against current topo service(s); recompile if necessary |
| 243 | } else { |
| 244 | removeTrackedResources(key, intent.resources()); |
| 245 | for (Intent installable : installables) { |
| 246 | removeTrackedResources(key, installable.resources()); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 251 | // Internal re-actor to topology change events. |
| 252 | private class InternalTopologyListener implements TopologyListener { |
| 253 | @Override |
| 254 | public void event(TopologyEvent event) { |
| 255 | executorService.execute(new TopologyChangeHandler(event)); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // Re-dispatcher of topology change events. |
| 260 | private class TopologyChangeHandler implements Runnable { |
| 261 | |
| 262 | private final TopologyEvent event; |
| 263 | |
| 264 | TopologyChangeHandler(TopologyEvent event) { |
| 265 | this.event = event; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void run() { |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 270 | // If there is no delegate, why bother? Just bail. |
| 271 | if (delegate == null) { |
| 272 | return; |
| 273 | } |
| 274 | |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 275 | if (event.reasons() == null || event.reasons().isEmpty()) { |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 276 | delegate.triggerCompile(Collections.emptySet(), true); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 277 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 278 | } else { |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 279 | Set<Key> intentsToRecompile = new HashSet<>(); |
| 280 | boolean dontRecompileAllFailedIntents = true; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 281 | |
| 282 | // Scan through the list of reasons and keep accruing all |
| 283 | // intents that need to be recompiled. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 284 | for (Event reason : event.reasons()) { |
| 285 | if (reason instanceof LinkEvent) { |
| 286 | LinkEvent linkEvent = (LinkEvent) reason; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 287 | final LinkKey linkKey = linkKey(linkEvent.subject()); |
| 288 | synchronized (intentsByLink) { |
| 289 | Set<Key> intentKeys = intentsByLink.get(linkKey); |
| 290 | log.debug("recompile triggered by LinkEvent {} ({}) for {}", |
| 291 | linkKey, linkEvent.type(), intentKeys); |
| 292 | intentsToRecompile.addAll(intentKeys); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 293 | } |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 294 | dontRecompileAllFailedIntents = dontRecompileAllFailedIntents && |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 295 | (linkEvent.type() == LINK_REMOVED || |
| 296 | (linkEvent.type() == LINK_UPDATED && |
| 297 | linkEvent.subject().isDurable())); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 300 | delegate.triggerCompile(intentsToRecompile, !dontRecompileAllFailedIntents); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 305 | /** |
| 306 | * Internal re-actor to resource available events. |
| 307 | */ |
| 308 | private class InternalLinkResourceListener implements LinkResourceListener { |
| 309 | @Override |
| 310 | public void event(LinkResourceEvent event) { |
| 311 | executorService.execute(new ResourceAvailableHandler(event)); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Re-dispatcher of resource available events. |
| 317 | */ |
| 318 | private class ResourceAvailableHandler implements Runnable { |
| 319 | |
| 320 | private final LinkResourceEvent event; |
| 321 | |
| 322 | ResourceAvailableHandler(LinkResourceEvent event) { |
| 323 | this.event = event; |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void run() { |
| 328 | // If there is no delegate, why bother? Just bail. |
| 329 | if (delegate == null) { |
| 330 | return; |
| 331 | } |
| 332 | |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 333 | delegate.triggerCompile(Collections.emptySet(), true); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 337 | //TODO consider adding flow rule event tracking |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 338 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 339 | private void updateTrackedResources(ApplicationId appId, boolean track) { |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 340 | if (intentService == null) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 341 | log.warn("Intent service is not bound yet"); |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 342 | return; |
| 343 | } |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 344 | intentService.getIntents().forEach(intent -> { |
| 345 | if (intent.appId().equals(appId)) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 346 | Key key = intent.key(); |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 347 | Collection<NetworkResource> resources = Lists.newArrayList(); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 348 | intentService.getInstallableIntents(key).stream() |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 349 | .map(installable -> installable.resources()) |
| 350 | .forEach(resources::addAll); |
| 351 | if (track) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 352 | addTrackedResources(key, resources); |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 353 | } else { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 354 | removeTrackedResources(key, resources); |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | }); |
| 358 | } |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * Re-dispatcher of device and host events. |
| 362 | */ |
| 363 | private class DeviceAvailabilityHandler implements Runnable { |
| 364 | |
| 365 | private final ElementId id; |
| 366 | private final boolean available; |
| 367 | |
| 368 | DeviceAvailabilityHandler(ElementId id, boolean available) { |
| 369 | this.id = checkNotNull(id); |
| 370 | this.available = available; |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public void run() { |
| 375 | // If there is no delegate, why bother? Just bail. |
| 376 | if (delegate == null) { |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | // TODO should we recompile on available==true? |
| 381 | delegate.triggerCompile(intentsByDevice.get(id), available); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | |
| 386 | private class InternalDeviceListener implements DeviceListener { |
| 387 | @Override |
| 388 | public void event(DeviceEvent event) { |
| 389 | DeviceEvent.Type type = event.type(); |
Jonathan Hart | 94470fe | 2015-07-31 11:41:10 -0700 | [diff] [blame] | 390 | switch (type) { |
| 391 | case DEVICE_ADDED: |
| 392 | case DEVICE_AVAILABILITY_CHANGED: |
| 393 | case DEVICE_REMOVED: |
| 394 | case DEVICE_SUSPENDED: |
| 395 | case DEVICE_UPDATED: |
| 396 | DeviceId id = event.subject().id(); |
| 397 | // TODO we need to check whether AVAILABILITY_CHANGED means up or down |
| 398 | boolean available = (type == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 399 | type == DeviceEvent.Type.DEVICE_ADDED || |
| 400 | type == DeviceEvent.Type.DEVICE_UPDATED); |
| 401 | executorService.execute(new DeviceAvailabilityHandler(id, available)); |
| 402 | break; |
| 403 | case PORT_ADDED: |
| 404 | case PORT_REMOVED: |
| 405 | case PORT_UPDATED: |
| 406 | case PORT_STATS_UPDATED: |
| 407 | default: |
| 408 | // Don't handle port events for now |
| 409 | break; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 410 | } |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | private class InternalHostListener implements HostListener { |
| 415 | @Override |
| 416 | public void event(HostEvent event) { |
| 417 | HostId id = event.subject().id(); |
Jonathan Hart | b0e1dd0 | 2015-09-23 17:54:11 -0700 | [diff] [blame^] | 418 | executorService.execute(new DeviceAvailabilityHandler(id, false)); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 421 | |
| 422 | protected void doIntentUpdate() { |
| 423 | updateScheduled.set(false); |
| 424 | if (intentService == null) { |
| 425 | log.warn("Intent service is not bound yet"); |
| 426 | return; |
| 427 | } |
| 428 | try { |
| 429 | //FIXME very inefficient |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 430 | for (IntentData intentData : intentService.getIntentData()) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 431 | try { |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 432 | trackIntent(intentData); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 433 | } catch (NullPointerException npe) { |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 434 | log.warn("intent error {}", intentData.key(), npe); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | } catch (Exception e) { |
| 438 | log.warn("Exception caught during update task", e); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | private void scheduleIntentUpdate(int afterDelaySec) { |
| 443 | if (updateScheduled.compareAndSet(false, true)) { |
| 444 | executor.schedule(this::doIntentUpdate, afterDelaySec, TimeUnit.SECONDS); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | private final class InternalPartitionListener implements PartitionEventListener { |
| 449 | @Override |
| 450 | public void event(PartitionEvent event) { |
Brian O'Connor | 3057f21 | 2015-05-29 18:22:18 -0700 | [diff] [blame] | 451 | log.debug("got message {}", event.subject()); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 452 | scheduleIntentUpdate(1); |
| 453 | } |
| 454 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 455 | } |