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