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; |
Brian O'Connor | a9c18b9 | 2015-09-28 16:03:43 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
| 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; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceEvent; |
| 39 | import org.onosproject.net.device.DeviceListener; |
| 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.net.host.HostEvent; |
| 42 | import org.onosproject.net.host.HostListener; |
| 43 | import org.onosproject.net.host.HostService; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 44 | import org.onosproject.net.intent.Intent; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 45 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 47 | import org.onosproject.net.intent.Key; |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 48 | import org.onosproject.net.intent.IntentPartitionEvent; |
| 49 | import org.onosproject.net.intent.IntentPartitionEventListener; |
| 50 | import org.onosproject.net.intent.IntentPartitionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.link.LinkEvent; |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 52 | import org.onosproject.net.newresource.ResourceEvent; |
| 53 | import org.onosproject.net.newresource.ResourceListener; |
| 54 | import org.onosproject.net.newresource.ResourceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 55 | import org.onosproject.net.topology.TopologyEvent; |
| 56 | import org.onosproject.net.topology.TopologyListener; |
| 57 | import org.onosproject.net.topology.TopologyService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 58 | import org.slf4j.Logger; |
| 59 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 60 | import java.util.Collection; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 61 | import java.util.Collections; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 62 | import java.util.HashSet; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 63 | import java.util.List; |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 64 | import java.util.Optional; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 65 | import java.util.Set; |
| 66 | import java.util.concurrent.ExecutorService; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 67 | import java.util.concurrent.Executors; |
| 68 | import java.util.concurrent.ScheduledExecutorService; |
| 69 | import java.util.concurrent.TimeUnit; |
| 70 | import java.util.concurrent.atomic.AtomicBoolean; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 71 | |
| 72 | import static com.google.common.base.Preconditions.checkArgument; |
| 73 | import static com.google.common.base.Preconditions.checkNotNull; |
| 74 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
| 75 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 76 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 77 | import static org.onlab.util.Tools.isNullOrEmpty; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 78 | import static org.onosproject.net.LinkKey.linkKey; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 79 | import static org.onosproject.net.intent.IntentState.INSTALLED; |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 80 | import static org.onosproject.net.intent.IntentState.INSTALLING; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 81 | import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED; |
| 82 | import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 83 | import static org.slf4j.LoggerFactory.getLogger; |
| 84 | |
| 85 | /** |
| 86 | * Entity responsible for tracking installed flows and for monitoring topology |
| 87 | * events to determine what flows are affected by topology changes. |
| 88 | */ |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 89 | @Component(immediate = true) |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 90 | @Service |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 91 | public class ObjectiveTracker implements ObjectiveTrackerService { |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 92 | |
| 93 | private final Logger log = getLogger(getClass()); |
| 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) |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 106 | protected ResourceService resourceService; |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 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) |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 119 | protected IntentPartitionService partitionService; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 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(); |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 127 | private ResourceListener resourceListener = new InternalResourceListener(); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 128 | private DeviceListener deviceListener = new InternalDeviceListener(); |
| 129 | private HostListener hostListener = new InternalHostListener(); |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 130 | private IntentPartitionEventListener partitionListener = new InternalPartitionListener(); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 131 | private TopologyChangeDelegate delegate; |
| 132 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 133 | protected final AtomicBoolean updateScheduled = new AtomicBoolean(false); |
| 134 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 135 | @Activate |
| 136 | public void activate() { |
| 137 | topologyService.addListener(listener); |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 138 | resourceService.addListener(resourceListener); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 139 | deviceService.addListener(deviceListener); |
| 140 | hostService.addListener(hostListener); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 141 | partitionService.addListener(partitionListener); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 142 | log.info("Started"); |
| 143 | } |
| 144 | |
| 145 | @Deactivate |
| 146 | public void deactivate() { |
| 147 | topologyService.removeListener(listener); |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 148 | resourceService.removeListener(resourceListener); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 149 | deviceService.removeListener(deviceListener); |
| 150 | hostService.removeListener(hostListener); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 151 | partitionService.removeListener(partitionListener); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 152 | log.info("Stopped"); |
| 153 | } |
| 154 | |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 155 | protected void bindIntentService(IntentService service) { |
| 156 | if (intentService == null) { |
| 157 | intentService = service; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | protected void unbindIntentService(IntentService service) { |
| 162 | if (intentService == service) { |
| 163 | intentService = null; |
| 164 | } |
| 165 | } |
| 166 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 167 | @Override |
| 168 | public void setDelegate(TopologyChangeDelegate delegate) { |
| 169 | checkNotNull(delegate, "Delegate cannot be null"); |
| 170 | checkArgument(this.delegate == null || this.delegate == delegate, |
| 171 | "Another delegate already set"); |
| 172 | this.delegate = delegate; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void unsetDelegate(TopologyChangeDelegate delegate) { |
| 177 | checkArgument(this.delegate == delegate, "Not the current delegate"); |
| 178 | this.delegate = null; |
| 179 | } |
| 180 | |
| 181 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 182 | public void addTrackedResources(Key intentKey, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 183 | Collection<NetworkResource> resources) { |
| 184 | for (NetworkResource resource : resources) { |
| 185 | if (resource instanceof Link) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 186 | intentsByLink.put(linkKey((Link) resource), intentKey); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 187 | } else if (resource instanceof ElementId) { |
| 188 | intentsByDevice.put((ElementId) resource, intentKey); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 189 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 194 | public void removeTrackedResources(Key intentKey, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 195 | Collection<NetworkResource> resources) { |
| 196 | for (NetworkResource resource : resources) { |
| 197 | if (resource instanceof Link) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 198 | intentsByLink.remove(linkKey((Link) resource), intentKey); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 199 | } else if (resource instanceof ElementId) { |
Sho SHIMIZU | d82a4e6 | 2015-09-09 14:53:46 -0700 | [diff] [blame] | 200 | intentsByDevice.remove(resource, intentKey); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 201 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 205 | @Override |
| 206 | public void trackIntent(IntentData intentData) { |
| 207 | |
| 208 | //NOTE: This will be called for intents that are being added to the store |
| 209 | // locally (i.e. every intent update) |
| 210 | |
| 211 | Key key = intentData.key(); |
| 212 | Intent intent = intentData.intent(); |
| 213 | boolean isLocal = intentService.isLocal(key); |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 214 | boolean isInstalled = intentData.state() == INSTALLING || |
| 215 | intentData.state() == INSTALLED; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 216 | List<Intent> installables = intentData.installables(); |
| 217 | |
| 218 | if (log.isTraceEnabled()) { |
| 219 | log.trace("intent {}, old: {}, new: {}, installableCount: {}, resourceCount: {}", |
| 220 | key, |
| 221 | intentsByDevice.values().contains(key), |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 222 | isLocal && isInstalled, |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 223 | installables.size(), |
| 224 | intent.resources().size() + |
| 225 | installables.stream() |
| 226 | .mapToLong(i -> i.resources().size()).sum()); |
| 227 | } |
| 228 | |
| 229 | if (isNullOrEmpty(installables) && intentData.state() == INSTALLED) { |
| 230 | log.warn("Intent {} is INSTALLED with no installables", key); |
| 231 | } |
| 232 | |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 233 | // FIXME Intents will be added 3 times (once directly using addTracked, |
| 234 | // then when installing and when installed) |
| 235 | if (isLocal && isInstalled) { |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 236 | addTrackedResources(key, intent.resources()); |
| 237 | for (Intent installable : installables) { |
| 238 | addTrackedResources(key, installable.resources()); |
| 239 | } |
| 240 | // FIXME check all resources against current topo service(s); recompile if necessary |
| 241 | } else { |
| 242 | removeTrackedResources(key, intent.resources()); |
| 243 | for (Intent installable : installables) { |
| 244 | removeTrackedResources(key, installable.resources()); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 249 | // Internal re-actor to topology change events. |
| 250 | private class InternalTopologyListener implements TopologyListener { |
| 251 | @Override |
| 252 | public void event(TopologyEvent event) { |
| 253 | executorService.execute(new TopologyChangeHandler(event)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Re-dispatcher of topology change events. |
| 258 | private class TopologyChangeHandler implements Runnable { |
| 259 | |
| 260 | private final TopologyEvent event; |
| 261 | |
| 262 | TopologyChangeHandler(TopologyEvent event) { |
| 263 | this.event = event; |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void run() { |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 268 | // If there is no delegate, why bother? Just bail. |
| 269 | if (delegate == null) { |
| 270 | return; |
| 271 | } |
| 272 | |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 273 | if (event.reasons() == null || event.reasons().isEmpty()) { |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 274 | delegate.triggerCompile(Collections.emptySet(), true); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 275 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 276 | } else { |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 277 | Set<Key> intentsToRecompile = new HashSet<>(); |
| 278 | boolean dontRecompileAllFailedIntents = true; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 279 | |
| 280 | // Scan through the list of reasons and keep accruing all |
| 281 | // intents that need to be recompiled. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 282 | for (Event reason : event.reasons()) { |
| 283 | if (reason instanceof LinkEvent) { |
| 284 | LinkEvent linkEvent = (LinkEvent) reason; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 285 | final LinkKey linkKey = linkKey(linkEvent.subject()); |
| 286 | synchronized (intentsByLink) { |
| 287 | Set<Key> intentKeys = intentsByLink.get(linkKey); |
| 288 | log.debug("recompile triggered by LinkEvent {} ({}) for {}", |
| 289 | linkKey, linkEvent.type(), intentKeys); |
| 290 | intentsToRecompile.addAll(intentKeys); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 291 | } |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 292 | dontRecompileAllFailedIntents = dontRecompileAllFailedIntents && |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 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 | } |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 298 | delegate.triggerCompile(intentsToRecompile, !dontRecompileAllFailedIntents); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 303 | private class InternalResourceListener implements ResourceListener { |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 304 | @Override |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 305 | public void event(ResourceEvent event) { |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 306 | Optional<Class<?>> deviceEvent = event.subject().components().stream() |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 307 | .map(Object::getClass) |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 308 | .filter(x -> x == PortNumber.class) |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 309 | .findFirst(); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 310 | if (deviceEvent.isPresent()) { |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 311 | executorService.execute(() -> { |
| 312 | if (delegate == null) { |
| 313 | return; |
| 314 | } |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 315 | |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 316 | delegate.triggerCompile(Collections.emptySet(), true); |
| 317 | }); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 318 | } |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 322 | //TODO consider adding flow rule event tracking |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 323 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 324 | private void updateTrackedResources(ApplicationId appId, boolean track) { |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 325 | if (intentService == null) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 326 | log.warn("Intent service is not bound yet"); |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 327 | return; |
| 328 | } |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 329 | intentService.getIntents().forEach(intent -> { |
| 330 | if (intent.appId().equals(appId)) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 331 | Key key = intent.key(); |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 332 | Collection<NetworkResource> resources = Lists.newArrayList(); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 333 | intentService.getInstallableIntents(key).stream() |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 334 | .map(installable -> installable.resources()) |
| 335 | .forEach(resources::addAll); |
| 336 | if (track) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 337 | addTrackedResources(key, resources); |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 338 | } else { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 339 | removeTrackedResources(key, resources); |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | }); |
| 343 | } |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 344 | |
| 345 | /* |
| 346 | * Re-dispatcher of device and host events. |
| 347 | */ |
| 348 | private class DeviceAvailabilityHandler implements Runnable { |
| 349 | |
| 350 | private final ElementId id; |
| 351 | private final boolean available; |
| 352 | |
| 353 | DeviceAvailabilityHandler(ElementId id, boolean available) { |
| 354 | this.id = checkNotNull(id); |
| 355 | this.available = available; |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public void run() { |
| 360 | // If there is no delegate, why bother? Just bail. |
| 361 | if (delegate == null) { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | // TODO should we recompile on available==true? |
HIGUCHI Yuta | 99b7b34 | 2015-09-29 16:54:21 -0700 | [diff] [blame] | 366 | |
| 367 | final ImmutableSet<Key> snapshot; |
| 368 | synchronized (intentsByDevice) { |
| 369 | snapshot = ImmutableSet.copyOf(intentsByDevice.get(id)); |
| 370 | } |
| 371 | delegate.triggerCompile(snapshot, available); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | |
| 376 | private class InternalDeviceListener implements DeviceListener { |
| 377 | @Override |
| 378 | public void event(DeviceEvent event) { |
| 379 | DeviceEvent.Type type = event.type(); |
Jonathan Hart | 94470fe | 2015-07-31 11:41:10 -0700 | [diff] [blame] | 380 | switch (type) { |
| 381 | case DEVICE_ADDED: |
| 382 | case DEVICE_AVAILABILITY_CHANGED: |
| 383 | case DEVICE_REMOVED: |
| 384 | case DEVICE_SUSPENDED: |
| 385 | case DEVICE_UPDATED: |
| 386 | DeviceId id = event.subject().id(); |
| 387 | // TODO we need to check whether AVAILABILITY_CHANGED means up or down |
| 388 | boolean available = (type == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 389 | type == DeviceEvent.Type.DEVICE_ADDED || |
| 390 | type == DeviceEvent.Type.DEVICE_UPDATED); |
| 391 | executorService.execute(new DeviceAvailabilityHandler(id, available)); |
| 392 | break; |
| 393 | case PORT_ADDED: |
| 394 | case PORT_REMOVED: |
| 395 | case PORT_UPDATED: |
| 396 | case PORT_STATS_UPDATED: |
| 397 | default: |
| 398 | // Don't handle port events for now |
| 399 | break; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 400 | } |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | private class InternalHostListener implements HostListener { |
| 405 | @Override |
| 406 | public void event(HostEvent event) { |
| 407 | HostId id = event.subject().id(); |
Brian O'Connor | a9c18b9 | 2015-09-28 16:03:43 -0700 | [diff] [blame] | 408 | switch (event.type()) { |
| 409 | case HOST_ADDED: |
| 410 | case HOST_MOVED: |
| 411 | case HOST_REMOVED: |
| 412 | executorService.execute(new DeviceAvailabilityHandler(id, false)); |
| 413 | break; |
| 414 | case HOST_UPDATED: |
| 415 | default: |
| 416 | // DO NOTHING |
| 417 | break; |
| 418 | } |
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 | |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 448 | private final class InternalPartitionListener implements IntentPartitionEventListener { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 449 | @Override |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 450 | public void event(IntentPartitionEvent 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 | } |