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