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