Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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.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.event.Event; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.ElementId; |
| 31 | import org.onosproject.net.HostId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.Link; |
| 33 | import org.onosproject.net.LinkKey; |
| 34 | import org.onosproject.net.NetworkResource; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 35 | import org.onosproject.net.PortNumber; |
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; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 43 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.Key; |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 46 | import org.onosproject.net.intent.WorkPartitionEvent; |
| 47 | import org.onosproject.net.intent.WorkPartitionEventListener; |
| 48 | import org.onosproject.net.intent.WorkPartitionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.link.LinkEvent; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 50 | import org.onosproject.net.resource.ResourceEvent; |
| 51 | import org.onosproject.net.resource.ResourceListener; |
| 52 | import org.onosproject.net.resource.ResourceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import org.onosproject.net.topology.TopologyEvent; |
| 54 | import org.onosproject.net.topology.TopologyListener; |
| 55 | import org.onosproject.net.topology.TopologyService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 56 | import org.slf4j.Logger; |
| 57 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 58 | import java.util.Collection; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 59 | import java.util.Collections; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 60 | import java.util.HashSet; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 61 | import java.util.List; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 62 | import java.util.Set; |
| 63 | import java.util.concurrent.ExecutorService; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 64 | import java.util.concurrent.ScheduledExecutorService; |
| 65 | import java.util.concurrent.TimeUnit; |
| 66 | import java.util.concurrent.atomic.AtomicBoolean; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 67 | |
| 68 | import static com.google.common.base.Preconditions.checkArgument; |
| 69 | import static com.google.common.base.Preconditions.checkNotNull; |
| 70 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 71 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 72 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 73 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 74 | import static org.onlab.util.Tools.isNullOrEmpty; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 75 | import static org.onosproject.net.LinkKey.linkKey; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 76 | import static org.onosproject.net.intent.IntentState.INSTALLED; |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 77 | import static org.onosproject.net.intent.IntentState.INSTALLING; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 78 | import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED; |
| 79 | import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 80 | import static org.slf4j.LoggerFactory.getLogger; |
| 81 | |
| 82 | /** |
| 83 | * Entity responsible for tracking installed flows and for monitoring topology |
| 84 | * events to determine what flows are affected by topology changes. |
| 85 | */ |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 86 | @Component(immediate = true) |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 87 | @Service |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 88 | public class ObjectiveTracker implements ObjectiveTrackerService { |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 89 | |
| 90 | private final Logger log = getLogger(getClass()); |
| 91 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 92 | private final SetMultimap<LinkKey, Key> intentsByLink = |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 93 | //TODO this could be slow as a point of synchronization |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 94 | synchronizedSetMultimap(HashMultimap.<LinkKey, Key>create()); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 95 | |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 96 | private final SetMultimap<ElementId, Key> intentsByDevice = |
| 97 | synchronizedSetMultimap(HashMultimap.<ElementId, Key>create()); |
| 98 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected TopologyService topologyService; |
| 101 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 103 | protected ResourceService resourceService; |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 104 | |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected DeviceService deviceService; |
| 107 | |
| 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected HostService hostService; |
| 110 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 111 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, |
| 112 | policy = ReferencePolicy.DYNAMIC) |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 113 | protected IntentService intentService; |
| 114 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 116 | protected WorkPartitionService partitionService; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 117 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 118 | private ExecutorService executorService = |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 119 | newSingleThreadExecutor(groupedThreads("onos/intent", "objectivetracker", log)); |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 120 | private ScheduledExecutorService executor = |
| 121 | newScheduledThreadPool(1, groupedThreads("onos/intent", "scheduledIntentUpdate", log)); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 122 | |
| 123 | private TopologyListener listener = new InternalTopologyListener(); |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 124 | private ResourceListener resourceListener = new InternalResourceListener(); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 125 | private DeviceListener deviceListener = new InternalDeviceListener(); |
| 126 | private HostListener hostListener = new InternalHostListener(); |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 127 | private WorkPartitionEventListener partitionListener = new InternalPartitionListener(); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 128 | private TopologyChangeDelegate delegate; |
| 129 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 130 | protected final AtomicBoolean updateScheduled = new AtomicBoolean(false); |
| 131 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 132 | @Activate |
| 133 | public void activate() { |
| 134 | topologyService.addListener(listener); |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 135 | resourceService.addListener(resourceListener); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 136 | deviceService.addListener(deviceListener); |
| 137 | hostService.addListener(hostListener); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 138 | partitionService.addListener(partitionListener); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 139 | log.info("Started"); |
| 140 | } |
| 141 | |
| 142 | @Deactivate |
| 143 | public void deactivate() { |
| 144 | topologyService.removeListener(listener); |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 145 | resourceService.removeListener(resourceListener); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 146 | deviceService.removeListener(deviceListener); |
| 147 | hostService.removeListener(hostListener); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 148 | partitionService.removeListener(partitionListener); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 149 | log.info("Stopped"); |
| 150 | } |
| 151 | |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 152 | protected void bindIntentService(IntentService service) { |
| 153 | if (intentService == null) { |
| 154 | intentService = service; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | protected void unbindIntentService(IntentService service) { |
| 159 | if (intentService == service) { |
| 160 | intentService = null; |
| 161 | } |
| 162 | } |
| 163 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 164 | @Override |
| 165 | public void setDelegate(TopologyChangeDelegate delegate) { |
| 166 | checkNotNull(delegate, "Delegate cannot be null"); |
| 167 | checkArgument(this.delegate == null || this.delegate == delegate, |
| 168 | "Another delegate already set"); |
| 169 | this.delegate = delegate; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void unsetDelegate(TopologyChangeDelegate delegate) { |
| 174 | checkArgument(this.delegate == delegate, "Not the current delegate"); |
| 175 | this.delegate = null; |
| 176 | } |
| 177 | |
| 178 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 179 | public void addTrackedResources(Key intentKey, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 180 | Collection<NetworkResource> resources) { |
| 181 | for (NetworkResource resource : resources) { |
| 182 | if (resource instanceof Link) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 183 | intentsByLink.put(linkKey((Link) resource), intentKey); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 184 | } else if (resource instanceof ElementId) { |
| 185 | intentsByDevice.put((ElementId) resource, intentKey); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 186 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 191 | public void removeTrackedResources(Key intentKey, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 192 | Collection<NetworkResource> resources) { |
| 193 | for (NetworkResource resource : resources) { |
| 194 | if (resource instanceof Link) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 195 | intentsByLink.remove(linkKey((Link) resource), intentKey); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 196 | } else if (resource instanceof ElementId) { |
Sho SHIMIZU | d82a4e6 | 2015-09-09 14:53:46 -0700 | [diff] [blame] | 197 | intentsByDevice.remove(resource, intentKey); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 198 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 202 | @Override |
| 203 | public void trackIntent(IntentData intentData) { |
| 204 | |
| 205 | //NOTE: This will be called for intents that are being added to the store |
| 206 | // locally (i.e. every intent update) |
| 207 | |
| 208 | Key key = intentData.key(); |
| 209 | Intent intent = intentData.intent(); |
| 210 | boolean isLocal = intentService.isLocal(key); |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 211 | boolean isInstalled = intentData.state() == INSTALLING || |
| 212 | intentData.state() == INSTALLED; |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 213 | List<Intent> installables = intentData.installables(); |
| 214 | |
| 215 | if (log.isTraceEnabled()) { |
| 216 | log.trace("intent {}, old: {}, new: {}, installableCount: {}, resourceCount: {}", |
| 217 | key, |
| 218 | intentsByDevice.values().contains(key), |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 219 | isLocal && isInstalled, |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 220 | installables.size(), |
| 221 | intent.resources().size() + |
| 222 | installables.stream() |
| 223 | .mapToLong(i -> i.resources().size()).sum()); |
| 224 | } |
| 225 | |
| 226 | if (isNullOrEmpty(installables) && intentData.state() == INSTALLED) { |
| 227 | log.warn("Intent {} is INSTALLED with no installables", key); |
| 228 | } |
| 229 | |
Brian O'Connor | 690fd1c | 2015-06-04 19:50:33 -0700 | [diff] [blame] | 230 | // FIXME Intents will be added 3 times (once directly using addTracked, |
| 231 | // then when installing and when installed) |
| 232 | if (isLocal && isInstalled) { |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 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 { |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 274 | Set<Key> intentsToRecompile = new HashSet<>(); |
| 275 | boolean dontRecompileAllFailedIntents = true; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 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; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 282 | final LinkKey linkKey = linkKey(linkEvent.subject()); |
| 283 | synchronized (intentsByLink) { |
| 284 | Set<Key> intentKeys = intentsByLink.get(linkKey); |
| 285 | log.debug("recompile triggered by LinkEvent {} ({}) for {}", |
| 286 | linkKey, linkEvent.type(), intentKeys); |
| 287 | intentsToRecompile.addAll(intentKeys); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 288 | } |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 289 | dontRecompileAllFailedIntents = dontRecompileAllFailedIntents && |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 290 | (linkEvent.type() == LINK_REMOVED || |
| 291 | (linkEvent.type() == LINK_UPDATED && |
| 292 | linkEvent.subject().isDurable())); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 295 | delegate.triggerCompile(intentsToRecompile, !dontRecompileAllFailedIntents); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 300 | private class InternalResourceListener implements ResourceListener { |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 301 | @Override |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 302 | public void event(ResourceEvent event) { |
Sho SHIMIZU | b08d586 | 2016-02-11 12:37:28 -0800 | [diff] [blame] | 303 | if (event.subject().isSubTypeOf(PortNumber.class)) { |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 304 | executorService.execute(() -> { |
| 305 | if (delegate == null) { |
| 306 | return; |
| 307 | } |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 308 | |
Sho SHIMIZU | 0b9c468 | 2015-11-02 18:30:34 -0800 | [diff] [blame] | 309 | delegate.triggerCompile(Collections.emptySet(), true); |
| 310 | }); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 311 | } |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 315 | //TODO consider adding flow rule event tracking |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 316 | |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 317 | /* |
| 318 | * Re-dispatcher of device and host events. |
| 319 | */ |
| 320 | private class DeviceAvailabilityHandler implements Runnable { |
| 321 | |
| 322 | private final ElementId id; |
| 323 | private final boolean available; |
| 324 | |
| 325 | DeviceAvailabilityHandler(ElementId id, boolean available) { |
| 326 | this.id = checkNotNull(id); |
| 327 | this.available = available; |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public void run() { |
| 332 | // If there is no delegate, why bother? Just bail. |
| 333 | if (delegate == null) { |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | // TODO should we recompile on available==true? |
HIGUCHI Yuta | 99b7b34 | 2015-09-29 16:54:21 -0700 | [diff] [blame] | 338 | |
| 339 | final ImmutableSet<Key> snapshot; |
| 340 | synchronized (intentsByDevice) { |
| 341 | snapshot = ImmutableSet.copyOf(intentsByDevice.get(id)); |
| 342 | } |
| 343 | delegate.triggerCompile(snapshot, available); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | |
| 348 | private class InternalDeviceListener implements DeviceListener { |
| 349 | @Override |
| 350 | public void event(DeviceEvent event) { |
| 351 | DeviceEvent.Type type = event.type(); |
Jonathan Hart | 94470fe | 2015-07-31 11:41:10 -0700 | [diff] [blame] | 352 | switch (type) { |
| 353 | case DEVICE_ADDED: |
| 354 | case DEVICE_AVAILABILITY_CHANGED: |
| 355 | case DEVICE_REMOVED: |
| 356 | case DEVICE_SUSPENDED: |
| 357 | case DEVICE_UPDATED: |
| 358 | DeviceId id = event.subject().id(); |
| 359 | // TODO we need to check whether AVAILABILITY_CHANGED means up or down |
| 360 | boolean available = (type == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 361 | type == DeviceEvent.Type.DEVICE_ADDED || |
| 362 | type == DeviceEvent.Type.DEVICE_UPDATED); |
| 363 | executorService.execute(new DeviceAvailabilityHandler(id, available)); |
| 364 | break; |
| 365 | case PORT_ADDED: |
| 366 | case PORT_REMOVED: |
| 367 | case PORT_UPDATED: |
| 368 | case PORT_STATS_UPDATED: |
| 369 | default: |
| 370 | // Don't handle port events for now |
| 371 | break; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 372 | } |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | private class InternalHostListener implements HostListener { |
| 377 | @Override |
| 378 | public void event(HostEvent event) { |
| 379 | HostId id = event.subject().id(); |
Brian O'Connor | a9c18b9 | 2015-09-28 16:03:43 -0700 | [diff] [blame] | 380 | switch (event.type()) { |
| 381 | case HOST_ADDED: |
| 382 | case HOST_MOVED: |
| 383 | case HOST_REMOVED: |
| 384 | executorService.execute(new DeviceAvailabilityHandler(id, false)); |
| 385 | break; |
| 386 | case HOST_UPDATED: |
| 387 | default: |
| 388 | // DO NOTHING |
| 389 | break; |
| 390 | } |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 393 | |
Sho SHIMIZU | 97a93dd | 2016-03-30 18:07:51 -0700 | [diff] [blame] | 394 | private void doIntentUpdate() { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 395 | updateScheduled.set(false); |
| 396 | if (intentService == null) { |
| 397 | log.warn("Intent service is not bound yet"); |
| 398 | return; |
| 399 | } |
| 400 | try { |
| 401 | //FIXME very inefficient |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 402 | for (IntentData intentData : intentService.getIntentData()) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 403 | try { |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 404 | trackIntent(intentData); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 405 | } catch (NullPointerException npe) { |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 406 | log.warn("intent error {}", intentData.key(), npe); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } catch (Exception e) { |
| 410 | log.warn("Exception caught during update task", e); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | private void scheduleIntentUpdate(int afterDelaySec) { |
| 415 | if (updateScheduled.compareAndSet(false, true)) { |
| 416 | executor.schedule(this::doIntentUpdate, afterDelaySec, TimeUnit.SECONDS); |
| 417 | } |
| 418 | } |
| 419 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 420 | private final class InternalPartitionListener implements WorkPartitionEventListener { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 421 | @Override |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 422 | public void event(WorkPartitionEvent event) { |
Brian O'Connor | 3057f21 | 2015-05-29 18:22:18 -0700 | [diff] [blame] | 423 | log.debug("got message {}", event.subject()); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 424 | scheduleIntentUpdate(1); |
| 425 | } |
| 426 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 427 | } |