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