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