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