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 | */ |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame^] | 18 | import java.util.Collection; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.Set; |
| 21 | import java.util.concurrent.ExecutorService; |
| 22 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
| 29 | import org.onlab.onos.event.Event; |
| 30 | import org.onlab.onos.net.Link; |
| 31 | import org.onlab.onos.net.LinkKey; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.NetworkResource; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.intent.IntentId; |
| 34 | import org.onlab.onos.net.link.LinkEvent; |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame^] | 35 | import org.onlab.onos.net.resource.LinkResourceEvent; |
| 36 | import org.onlab.onos.net.resource.LinkResourceListener; |
| 37 | import org.onlab.onos.net.resource.LinkResourceService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.topology.TopologyEvent; |
| 39 | import org.onlab.onos.net.topology.TopologyListener; |
| 40 | import org.onlab.onos.net.topology.TopologyService; |
| 41 | import org.slf4j.Logger; |
| 42 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame^] | 43 | import com.google.common.collect.HashMultimap; |
| 44 | import com.google.common.collect.SetMultimap; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 45 | |
| 46 | import static com.google.common.base.Preconditions.checkArgument; |
| 47 | import static com.google.common.base.Preconditions.checkNotNull; |
| 48 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
| 49 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 50 | import static org.onlab.onos.net.LinkKey.linkKey; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 51 | import static org.onlab.onos.net.link.LinkEvent.Type.LINK_REMOVED; |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 52 | import static org.onlab.onos.net.link.LinkEvent.Type.LINK_UPDATED; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 53 | import static org.onlab.util.Tools.namedThreads; |
| 54 | import static org.slf4j.LoggerFactory.getLogger; |
| 55 | |
| 56 | /** |
| 57 | * Entity responsible for tracking installed flows and for monitoring topology |
| 58 | * events to determine what flows are affected by topology changes. |
| 59 | */ |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame^] | 60 | @Component(immediate = true) |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 61 | @Service |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 62 | public class ObjectiveTracker implements ObjectiveTrackerService { |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 63 | |
| 64 | private final Logger log = getLogger(getClass()); |
| 65 | |
| 66 | private final SetMultimap<LinkKey, IntentId> intentsByLink = |
| 67 | synchronizedSetMultimap(HashMultimap.<LinkKey, IntentId>create()); |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected TopologyService topologyService; |
| 71 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame^] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected LinkResourceService resourceManager; |
| 74 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 75 | private ExecutorService executorService = |
| 76 | newSingleThreadExecutor(namedThreads("onos-flowtracker")); |
| 77 | |
| 78 | private TopologyListener listener = new InternalTopologyListener(); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame^] | 79 | private LinkResourceListener linkResourceListener = |
| 80 | new InternalLinkResourceListener(); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 81 | private TopologyChangeDelegate delegate; |
| 82 | |
| 83 | @Activate |
| 84 | public void activate() { |
| 85 | topologyService.addListener(listener); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame^] | 86 | resourceManager.addListener(linkResourceListener); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 87 | log.info("Started"); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | public void deactivate() { |
| 92 | topologyService.removeListener(listener); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame^] | 93 | resourceManager.removeListener(linkResourceListener); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 94 | log.info("Stopped"); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void setDelegate(TopologyChangeDelegate delegate) { |
| 99 | checkNotNull(delegate, "Delegate cannot be null"); |
| 100 | checkArgument(this.delegate == null || this.delegate == delegate, |
| 101 | "Another delegate already set"); |
| 102 | this.delegate = delegate; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void unsetDelegate(TopologyChangeDelegate delegate) { |
| 107 | checkArgument(this.delegate == delegate, "Not the current delegate"); |
| 108 | this.delegate = null; |
| 109 | } |
| 110 | |
| 111 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 112 | public void addTrackedResources(IntentId intentId, |
| 113 | Collection<NetworkResource> resources) { |
| 114 | for (NetworkResource resource : resources) { |
| 115 | if (resource instanceof Link) { |
| 116 | intentsByLink.put(linkKey((Link) resource), intentId); |
| 117 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 122 | public void removeTrackedResources(IntentId intentId, |
| 123 | Collection<NetworkResource> resources) { |
| 124 | for (NetworkResource resource : resources) { |
| 125 | if (resource instanceof Link) { |
| 126 | intentsByLink.remove(linkKey((Link) resource), intentId); |
| 127 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | // Internal re-actor to topology change events. |
| 132 | private class InternalTopologyListener implements TopologyListener { |
| 133 | @Override |
| 134 | public void event(TopologyEvent event) { |
| 135 | executorService.execute(new TopologyChangeHandler(event)); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Re-dispatcher of topology change events. |
| 140 | private class TopologyChangeHandler implements Runnable { |
| 141 | |
| 142 | private final TopologyEvent event; |
| 143 | |
| 144 | TopologyChangeHandler(TopologyEvent event) { |
| 145 | this.event = event; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void run() { |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 150 | // If there is no delegate, why bother? Just bail. |
| 151 | if (delegate == null) { |
| 152 | return; |
| 153 | } |
| 154 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 155 | if (event.reasons() == null) { |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame] | 156 | delegate.triggerCompile(new HashSet<IntentId>(), true); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 157 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 158 | } else { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 159 | Set<IntentId> toBeRecompiled = new HashSet<>(); |
| 160 | boolean recompileOnly = true; |
| 161 | |
| 162 | // Scan through the list of reasons and keep accruing all |
| 163 | // intents that need to be recompiled. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 164 | for (Event reason : event.reasons()) { |
| 165 | if (reason instanceof LinkEvent) { |
| 166 | LinkEvent linkEvent = (LinkEvent) reason; |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 167 | if (linkEvent.type() == LINK_REMOVED |
| 168 | || (linkEvent.type() == LINK_UPDATED && |
| 169 | linkEvent.subject().isDurable())) { |
Yuta HIGUCHI | 56df70f | 2014-10-28 22:26:15 -0700 | [diff] [blame] | 170 | final LinkKey linkKey = linkKey(linkEvent.subject()); |
| 171 | Set<IntentId> intentIds = intentsByLink.get(linkKey); |
| 172 | log.debug("recompile triggered by LinkDown {} {}", linkKey, intentIds); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 173 | toBeRecompiled.addAll(intentIds); |
| 174 | } |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 175 | recompileOnly = recompileOnly && |
| 176 | (linkEvent.type() == LINK_REMOVED || |
| 177 | (linkEvent.type() == LINK_UPDATED && |
| 178 | linkEvent.subject().isDurable())); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 181 | |
| 182 | delegate.triggerCompile(toBeRecompiled, !recompileOnly); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame^] | 187 | /** |
| 188 | * Internal re-actor to resource available events. |
| 189 | */ |
| 190 | private class InternalLinkResourceListener implements LinkResourceListener { |
| 191 | @Override |
| 192 | public void event(LinkResourceEvent event) { |
| 193 | executorService.execute(new ResourceAvailableHandler(event)); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Re-dispatcher of resource available events. |
| 199 | */ |
| 200 | private class ResourceAvailableHandler implements Runnable { |
| 201 | |
| 202 | private final LinkResourceEvent event; |
| 203 | |
| 204 | ResourceAvailableHandler(LinkResourceEvent event) { |
| 205 | this.event = event; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void run() { |
| 210 | // If there is no delegate, why bother? Just bail. |
| 211 | if (delegate == null) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | delegate.triggerCompile(new HashSet<>(), true); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 220 | } |