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