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