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