Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.SetMultimap; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.event.Event; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.LinkKey; |
| 31 | import org.onosproject.net.NetworkResource; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.link.LinkEvent; |
| 35 | import org.onosproject.net.resource.LinkResourceEvent; |
| 36 | import org.onosproject.net.resource.LinkResourceListener; |
| 37 | import org.onosproject.net.resource.LinkResourceService; |
| 38 | import org.onosproject.net.topology.TopologyEvent; |
| 39 | import org.onosproject.net.topology.TopologyListener; |
| 40 | import org.onosproject.net.topology.TopologyService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 43 | import java.util.Collection; |
| 44 | import java.util.HashSet; |
| 45 | import java.util.Set; |
| 46 | import java.util.concurrent.ExecutorService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 47 | |
| 48 | import static com.google.common.base.Preconditions.checkArgument; |
| 49 | import static com.google.common.base.Preconditions.checkNotNull; |
| 50 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
| 51 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 52 | import static org.onlab.util.Tools.groupedThreads; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import static org.onosproject.net.LinkKey.linkKey; |
| 54 | import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED; |
| 55 | import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 56 | import static org.slf4j.LoggerFactory.getLogger; |
| 57 | |
| 58 | /** |
| 59 | * Entity responsible for tracking installed flows and for monitoring topology |
| 60 | * events to determine what flows are affected by topology changes. |
| 61 | */ |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 62 | @Component(immediate = true) |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 63 | @Service |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 64 | public class ObjectiveTracker implements ObjectiveTrackerService { |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 65 | |
| 66 | private final Logger log = getLogger(getClass()); |
| 67 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 68 | private final SetMultimap<LinkKey, Key> intentsByLink = |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 69 | //TODO this could be slow as a point of synchronization |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 70 | synchronizedSetMultimap(HashMultimap.<LinkKey, Key>create()); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected TopologyService topologyService; |
| 74 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected LinkResourceService resourceManager; |
| 77 | |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY) |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 79 | protected IntentService intentService; |
| 80 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 81 | private ExecutorService executorService = |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 82 | newSingleThreadExecutor(groupedThreads("onos/intent", "flowtracker")); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 83 | |
| 84 | private TopologyListener listener = new InternalTopologyListener(); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 85 | private LinkResourceListener linkResourceListener = |
| 86 | new InternalLinkResourceListener(); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 87 | private TopologyChangeDelegate delegate; |
| 88 | |
| 89 | @Activate |
| 90 | public void activate() { |
| 91 | topologyService.addListener(listener); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 92 | resourceManager.addListener(linkResourceListener); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 93 | log.info("Started"); |
| 94 | } |
| 95 | |
| 96 | @Deactivate |
| 97 | public void deactivate() { |
| 98 | topologyService.removeListener(listener); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 99 | resourceManager.removeListener(linkResourceListener); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 100 | log.info("Stopped"); |
| 101 | } |
| 102 | |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 103 | protected void bindIntentService(IntentService service) { |
| 104 | if (intentService == null) { |
| 105 | intentService = service; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | protected void unbindIntentService(IntentService service) { |
| 110 | if (intentService == service) { |
| 111 | intentService = null; |
| 112 | } |
| 113 | } |
| 114 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 115 | @Override |
| 116 | public void setDelegate(TopologyChangeDelegate delegate) { |
| 117 | checkNotNull(delegate, "Delegate cannot be null"); |
| 118 | checkArgument(this.delegate == null || this.delegate == delegate, |
| 119 | "Another delegate already set"); |
| 120 | this.delegate = delegate; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void unsetDelegate(TopologyChangeDelegate delegate) { |
| 125 | checkArgument(this.delegate == delegate, "Not the current delegate"); |
| 126 | this.delegate = null; |
| 127 | } |
| 128 | |
| 129 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 130 | public void addTrackedResources(Key intentKey, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 131 | Collection<NetworkResource> resources) { |
| 132 | for (NetworkResource resource : resources) { |
| 133 | if (resource instanceof Link) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 134 | intentsByLink.put(linkKey((Link) resource), intentKey); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 135 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 140 | public void removeTrackedResources(Key intentKey, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 141 | Collection<NetworkResource> resources) { |
| 142 | for (NetworkResource resource : resources) { |
| 143 | if (resource instanceof Link) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 144 | intentsByLink.remove(linkKey((Link) resource), intentKey); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 145 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | // Internal re-actor to topology change events. |
| 150 | private class InternalTopologyListener implements TopologyListener { |
| 151 | @Override |
| 152 | public void event(TopologyEvent event) { |
| 153 | executorService.execute(new TopologyChangeHandler(event)); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Re-dispatcher of topology change events. |
| 158 | private class TopologyChangeHandler implements Runnable { |
| 159 | |
| 160 | private final TopologyEvent event; |
| 161 | |
| 162 | TopologyChangeHandler(TopologyEvent event) { |
| 163 | this.event = event; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void run() { |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 168 | // If there is no delegate, why bother? Just bail. |
| 169 | if (delegate == null) { |
| 170 | return; |
| 171 | } |
| 172 | |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 173 | if (event.reasons() == null || event.reasons().isEmpty()) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 174 | delegate.triggerCompile(new HashSet<Key>(), true); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 175 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 176 | } else { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 177 | Set<Key> toBeRecompiled = new HashSet<>(); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 178 | boolean recompileOnly = true; |
| 179 | |
| 180 | // Scan through the list of reasons and keep accruing all |
| 181 | // intents that need to be recompiled. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 182 | for (Event reason : event.reasons()) { |
| 183 | if (reason instanceof LinkEvent) { |
| 184 | LinkEvent linkEvent = (LinkEvent) reason; |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 185 | if (linkEvent.type() == LINK_REMOVED |
| 186 | || (linkEvent.type() == LINK_UPDATED && |
| 187 | linkEvent.subject().isDurable())) { |
Yuta HIGUCHI | 56df70f | 2014-10-28 22:26:15 -0700 | [diff] [blame] | 188 | final LinkKey linkKey = linkKey(linkEvent.subject()); |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 189 | synchronized (intentsByLink) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 190 | Set<Key> intentKeys = intentsByLink.get(linkKey); |
| 191 | log.debug("recompile triggered by LinkDown {} {}", linkKey, intentKeys); |
| 192 | toBeRecompiled.addAll(intentKeys); |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 193 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 194 | } |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 195 | recompileOnly = recompileOnly && |
| 196 | (linkEvent.type() == LINK_REMOVED || |
| 197 | (linkEvent.type() == LINK_UPDATED && |
| 198 | linkEvent.subject().isDurable())); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 201 | delegate.triggerCompile(toBeRecompiled, !recompileOnly); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 206 | /** |
| 207 | * Internal re-actor to resource available events. |
| 208 | */ |
| 209 | private class InternalLinkResourceListener implements LinkResourceListener { |
| 210 | @Override |
| 211 | public void event(LinkResourceEvent event) { |
| 212 | executorService.execute(new ResourceAvailableHandler(event)); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Re-dispatcher of resource available events. |
| 218 | */ |
| 219 | private class ResourceAvailableHandler implements Runnable { |
| 220 | |
| 221 | private final LinkResourceEvent event; |
| 222 | |
| 223 | ResourceAvailableHandler(LinkResourceEvent event) { |
| 224 | this.event = event; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void run() { |
| 229 | // If there is no delegate, why bother? Just bail. |
| 230 | if (delegate == null) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | delegate.triggerCompile(new HashSet<>(), true); |
| 235 | } |
| 236 | } |
| 237 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 238 | //TODO consider adding flow rule event tracking |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 239 | |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 240 | private void updateTrackedResources(ApplicationId appId, boolean track) { |
Brian O'Connor | 5d55ed4 | 2014-12-01 18:27:47 -0800 | [diff] [blame] | 241 | if (intentService == null) { |
| 242 | log.debug("Intent service is not bound yet"); |
| 243 | return; |
| 244 | } |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 245 | intentService.getIntents().forEach(intent -> { |
| 246 | if (intent.appId().equals(appId)) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 247 | Key key = intent.key(); |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 248 | Collection<NetworkResource> resources = Lists.newArrayList(); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 249 | intentService.getInstallableIntents(key).stream() |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 250 | .map(installable -> installable.resources()) |
| 251 | .forEach(resources::addAll); |
| 252 | if (track) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 253 | addTrackedResources(key, resources); |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 254 | } else { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 255 | removeTrackedResources(key, resources); |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | }); |
| 259 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 260 | } |