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.link.impl; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 17 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | |
tom | db0d03f | 2014-08-27 16:34:15 -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; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | db0d03f | 2014-08-27 16:34:15 -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.event.AbstractListenerRegistry; |
| 30 | import org.onosproject.event.EventDeliveryService; |
| 31 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | f269540 | 2014-12-08 16:24:24 -0800 | [diff] [blame^] | 32 | import org.onosproject.net.Device; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.Link; |
| 35 | import org.onosproject.net.Link.State; |
| 36 | import org.onosproject.net.MastershipRole; |
| 37 | import org.onosproject.net.device.DeviceEvent; |
| 38 | import org.onosproject.net.device.DeviceListener; |
| 39 | import org.onosproject.net.device.DeviceService; |
| 40 | import org.onosproject.net.link.LinkAdminService; |
| 41 | import org.onosproject.net.link.LinkDescription; |
| 42 | import org.onosproject.net.link.LinkEvent; |
| 43 | import org.onosproject.net.link.LinkListener; |
| 44 | import org.onosproject.net.link.LinkProvider; |
| 45 | import org.onosproject.net.link.LinkProviderRegistry; |
| 46 | import org.onosproject.net.link.LinkProviderService; |
| 47 | import org.onosproject.net.link.LinkService; |
| 48 | import org.onosproject.net.link.LinkStore; |
| 49 | import org.onosproject.net.link.LinkStoreDelegate; |
| 50 | import org.onosproject.net.provider.AbstractProviderRegistry; |
| 51 | import org.onosproject.net.provider.AbstractProviderService; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 52 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 53 | |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 54 | import com.google.common.base.Predicate; |
| 55 | import com.google.common.collect.FluentIterable; |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 56 | import com.google.common.collect.Sets; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Provides basic implementation of the link SB & NB APIs. |
| 60 | */ |
| 61 | @Component(immediate = true) |
| 62 | @Service |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 63 | public class LinkManager |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 64 | extends AbstractProviderRegistry<LinkProvider, LinkProviderService> |
| 65 | implements LinkService, LinkAdminService, LinkProviderRegistry { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 66 | |
| 67 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 68 | private static final String LINK_DESC_NULL = "Link description cannot be null"; |
| 69 | private static final String CONNECT_POINT_NULL = "Connection point cannot be null"; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 70 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 71 | private final Logger log = getLogger(getClass()); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 72 | |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 73 | protected final AbstractListenerRegistry<LinkEvent, LinkListener> |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 74 | listenerRegistry = new AbstractListenerRegistry<>(); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 75 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 76 | private final LinkStoreDelegate delegate = new InternalStoreDelegate(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 77 | |
| 78 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 81 | protected LinkStore store; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 84 | protected DeviceService deviceService; |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 85 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 87 | protected EventDeliveryService eventDispatcher; |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 88 | |
| 89 | @Activate |
| 90 | public void activate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 91 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 92 | eventDispatcher.addSink(LinkEvent.class, listenerRegistry); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 93 | deviceService.addListener(deviceListener); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 94 | log.info("Started"); |
| 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 99 | store.unsetDelegate(delegate); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 100 | eventDispatcher.removeSink(LinkEvent.class); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 101 | deviceService.removeListener(deviceListener); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 102 | log.info("Stopped"); |
| 103 | } |
| 104 | |
| 105 | @Override |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 106 | public int getLinkCount() { |
| 107 | return store.getLinkCount(); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Iterable<Link> getLinks() { |
| 112 | return store.getLinks(); |
| 113 | } |
| 114 | |
| 115 | @Override |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 116 | public Iterable<Link> getActiveLinks() { |
| 117 | return FluentIterable.from(getLinks()) |
| 118 | .filter(new Predicate<Link>() { |
| 119 | |
| 120 | @Override |
| 121 | public boolean apply(Link input) { |
| 122 | return input.state() == State.ACTIVE; |
| 123 | } |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | @Override |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 128 | public Set<Link> getDeviceLinks(DeviceId deviceId) { |
| 129 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 130 | return Sets.union(store.getDeviceEgressLinks(deviceId), |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 131 | store.getDeviceIngressLinks(deviceId)); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public Set<Link> getDeviceEgressLinks(DeviceId deviceId) { |
| 136 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 137 | return store.getDeviceEgressLinks(deviceId); |
| 138 | } |
| 139 | |
| 140 | @Override |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 141 | public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 142 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 143 | return store.getDeviceIngressLinks(deviceId); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Set<Link> getLinks(ConnectPoint connectPoint) { |
| 148 | checkNotNull(connectPoint, CONNECT_POINT_NULL); |
| 149 | return Sets.union(store.getEgressLinks(connectPoint), |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 150 | store.getIngressLinks(connectPoint)); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Set<Link> getEgressLinks(ConnectPoint connectPoint) { |
| 155 | checkNotNull(connectPoint, CONNECT_POINT_NULL); |
| 156 | return store.getEgressLinks(connectPoint); |
| 157 | } |
| 158 | |
| 159 | @Override |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 160 | public Set<Link> getIngressLinks(ConnectPoint connectPoint) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 161 | checkNotNull(connectPoint, CONNECT_POINT_NULL); |
| 162 | return store.getIngressLinks(connectPoint); |
| 163 | } |
| 164 | |
| 165 | @Override |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 166 | public Link getLink(ConnectPoint src, ConnectPoint dst) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 167 | checkNotNull(src, CONNECT_POINT_NULL); |
| 168 | checkNotNull(dst, CONNECT_POINT_NULL); |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 169 | return store.getLink(src, dst); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void removeLinks(ConnectPoint connectPoint) { |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 174 | if (deviceService.getRole(connectPoint.deviceId()) != MastershipRole.MASTER) { |
| 175 | return; |
| 176 | } |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 177 | removeLinks(getLinks(connectPoint), false); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void removeLinks(DeviceId deviceId) { |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 182 | if (deviceService.getRole(deviceId) != MastershipRole.MASTER) { |
| 183 | return; |
| 184 | } |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 185 | removeLinks(getDeviceLinks(deviceId), false); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void addListener(LinkListener listener) { |
| 190 | listenerRegistry.addListener(listener); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void removeListener(LinkListener listener) { |
| 195 | listenerRegistry.removeListener(listener); |
| 196 | } |
| 197 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 198 | // Auxiliary interceptor for device remove events to prune links that |
| 199 | // are associated with the removed device or its port. |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 200 | private class InternalDeviceListener implements DeviceListener { |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 201 | @Override |
| 202 | public void event(DeviceEvent event) { |
| 203 | if (event.type() == DeviceEvent.Type.DEVICE_REMOVED) { |
| 204 | removeLinks(event.subject().id()); |
| 205 | } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) { |
| 206 | removeLinks(new ConnectPoint(event.subject().id(), |
| 207 | event.port().number())); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 212 | @Override |
| 213 | protected LinkProviderService createProviderService(LinkProvider provider) { |
| 214 | return new InternalLinkProviderService(provider); |
| 215 | } |
| 216 | |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 217 | // Personalized link provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 218 | private class InternalLinkProviderService |
| 219 | extends AbstractProviderService<LinkProvider> |
| 220 | implements LinkProviderService { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 221 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 222 | InternalLinkProviderService(LinkProvider provider) { |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 223 | super(provider); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void linkDetected(LinkDescription linkDescription) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 228 | checkNotNull(linkDescription, LINK_DESC_NULL); |
| 229 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 230 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 231 | LinkEvent event = store.createOrUpdateLink(provider().id(), |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 232 | linkDescription); |
| 233 | if (event != null) { |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 234 | log.info("Link {} detected", linkDescription); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 235 | post(event); |
| 236 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void linkVanished(LinkDescription linkDescription) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 241 | checkNotNull(linkDescription, LINK_DESC_NULL); |
| 242 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 243 | |
| 244 | ConnectPoint src = linkDescription.src(); |
| 245 | ConnectPoint dst = linkDescription.dst(); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 246 | |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 247 | LinkEvent event = store.removeOrDownLink(src, dst); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 248 | if (event != null) { |
| 249 | log.info("Link {} vanished", linkDescription); |
| 250 | post(event); |
| 251 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void linksVanished(ConnectPoint connectPoint) { |
| 256 | checkNotNull(connectPoint, "Connect point cannot be null"); |
| 257 | checkValidity(); |
alshabib | 12288c8 | 2014-10-23 10:24:23 -0700 | [diff] [blame] | 258 | |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 259 | log.info("Links for connection point {} vanished", connectPoint); |
Yuta HIGUCHI | e794cbe | 2014-10-17 13:21:23 -0700 | [diff] [blame] | 260 | // FIXME: This will remove links registered by other providers |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 261 | removeLinks(getLinks(connectPoint), true); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void linksVanished(DeviceId deviceId) { |
| 266 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 267 | checkValidity(); |
alshabib | 12288c8 | 2014-10-23 10:24:23 -0700 | [diff] [blame] | 268 | |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 269 | log.info("Links for device {} vanished", deviceId); |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 270 | removeLinks(getDeviceLinks(deviceId), true); |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 273 | |
| 274 | // Removes all links in the specified set and emits appropriate events. |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 275 | private void removeLinks(Set<Link> links, boolean isSoftRemove) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 276 | for (Link link : links) { |
Ray Milkey | f269540 | 2014-12-08 16:24:24 -0800 | [diff] [blame^] | 277 | final Device srcDevice = deviceService.getDevice(link.src().deviceId()); |
| 278 | final Device dstDevice = deviceService.getDevice(link.dst().deviceId()); |
| 279 | if (srcDevice != null && |
| 280 | dstDevice != null && |
| 281 | !srcDevice.type().equals(dstDevice.type())) { |
| 282 | //TODO this is awful. need to be fixed so that we don't down |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 283 | // configured links. perhaps add a mechanism to figure out the |
| 284 | // state of this link |
| 285 | log.info("Ignoring removal of link as device types are " + |
| 286 | "different {} {} ", |
| 287 | link.src() , |
| 288 | link.dst()); |
| 289 | continue; |
| 290 | } |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 291 | LinkEvent event = isSoftRemove ? |
| 292 | store.removeOrDownLink(link.src(), link.dst()) : |
| 293 | store.removeLink(link.src(), link.dst()); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 294 | post(event); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Posts the specified event to the local event dispatcher. |
| 299 | private void post(LinkEvent event) { |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 300 | if (event != null) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 301 | eventDispatcher.post(event); |
| 302 | } |
| 303 | } |
| 304 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 305 | // Store delegate to re-post events emitted from the store. |
| 306 | private class InternalStoreDelegate implements LinkStoreDelegate { |
| 307 | @Override |
| 308 | public void notify(LinkEvent event) { |
| 309 | post(event); |
| 310 | } |
| 311 | } |
tom | db0d03f | 2014-08-27 16:34:15 -0700 | [diff] [blame] | 312 | } |