blob: ef09ee7000a993053eb989fcd263bd5dd253e307 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.link.impl;
tomdb0d03f2014-08-27 16:34:15 -070017
Simon Huntff663742015-05-14 13:33:05 -070018import com.google.common.collect.FluentIterable;
19import com.google.common.collect.Sets;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.Link;
23import org.onosproject.net.Link.State;
Sahil Lele3a0cdd52015-07-21 14:16:31 -070024import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.MastershipRole;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070026import org.onosproject.net.config.NetworkConfigEvent;
27import org.onosproject.net.config.NetworkConfigListener;
28import org.onosproject.net.config.NetworkConfigService;
29import org.onosproject.net.config.basics.BasicLinkConfig;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.device.DeviceEvent;
31import org.onosproject.net.device.DeviceListener;
32import org.onosproject.net.device.DeviceService;
33import org.onosproject.net.link.LinkAdminService;
34import org.onosproject.net.link.LinkDescription;
35import org.onosproject.net.link.LinkEvent;
36import org.onosproject.net.link.LinkListener;
37import org.onosproject.net.link.LinkProvider;
38import org.onosproject.net.link.LinkProviderRegistry;
39import org.onosproject.net.link.LinkProviderService;
40import org.onosproject.net.link.LinkService;
41import org.onosproject.net.link.LinkStore;
42import org.onosproject.net.link.LinkStoreDelegate;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070043import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.provider.AbstractProviderService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070045import org.onosproject.net.provider.ProviderId;
46import org.osgi.service.component.annotations.Activate;
47import org.osgi.service.component.annotations.Component;
48import org.osgi.service.component.annotations.Deactivate;
49import org.osgi.service.component.annotations.Reference;
50import org.osgi.service.component.annotations.ReferenceCardinality;
tomdb0d03f2014-08-27 16:34:15 -070051import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070052
Yuta HIGUCHI31207782017-02-17 14:43:40 -080053import java.util.Optional;
Simon Huntff663742015-05-14 13:33:05 -070054import java.util.Set;
tomdb0d03f2014-08-27 16:34:15 -070055
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080056import static com.google.common.base.Preconditions.checkNotNull;
Sahil Lele3a0cdd52015-07-21 14:16:31 -070057import static org.onosproject.net.LinkKey.linkKey;
Changhoon Yoon541ef712015-05-23 17:18:34 +090058import static org.onosproject.security.AppGuard.checkPermission;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059import static org.onosproject.security.AppPermission.Type.LINK_READ;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070060import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoon541ef712015-05-23 17:18:34 +090061
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080062
tomdb0d03f2014-08-27 16:34:15 -070063/**
64 * Provides basic implementation of the link SB & NB APIs.
65 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066@Component(immediate = true, service = { LinkService.class, LinkAdminService.class, LinkProviderRegistry.class })
tom35c0dc32014-09-19 10:00:58 -070067public class LinkManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070068 extends AbstractListenerProviderRegistry<LinkEvent, LinkListener, LinkProvider, LinkProviderService>
tomdc361b62014-09-09 20:36:52 -070069 implements LinkService, LinkAdminService, LinkProviderRegistry {
tomeadbb462014-09-07 16:10:19 -070070
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";
tomdb0d03f2014-08-27 16:34:15 -070074
tom5f38b3a2014-08-27 23:50:54 -070075 private final Logger log = getLogger(getClass());
tomdb0d03f2014-08-27 16:34:15 -070076
alshabibb5522ff2014-09-29 19:20:00 -070077 private final LinkStoreDelegate delegate = new InternalStoreDelegate();
tomc78acee2014-09-24 15:16:55 -070078
79 private final DeviceListener deviceListener = new InternalDeviceListener();
tom89b63c52014-09-16 09:19:51 -070080
Sahil Lele3a0cdd52015-07-21 14:16:31 -070081 private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener();
82
Ray Milkeyd84f89b2018-08-17 14:54:17 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tom35c0dc32014-09-19 10:00:58 -070084 protected LinkStore store;
85
Ray Milkeyd84f89b2018-08-17 14:54:17 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tom89b63c52014-09-16 09:19:51 -070087 protected DeviceService deviceService;
tom4c6606f2014-09-07 11:11:21 -070088
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sahil Lele3a0cdd52015-07-21 14:16:31 -070090 protected NetworkConfigService networkConfigService;
91
tomdb0d03f2014-08-27 16:34:15 -070092 @Activate
93 public void activate() {
tomc78acee2014-09-24 15:16:55 -070094 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -070095 eventDispatcher.addSink(LinkEvent.class, listenerRegistry);
tom89b63c52014-09-16 09:19:51 -070096 deviceService.addListener(deviceListener);
Sahil Lele3a0cdd52015-07-21 14:16:31 -070097 networkConfigService.addListener(networkConfigListener);
tomdb0d03f2014-08-27 16:34:15 -070098 log.info("Started");
99 }
100
101 @Deactivate
102 public void deactivate() {
tomc78acee2014-09-24 15:16:55 -0700103 store.unsetDelegate(delegate);
tom5f38b3a2014-08-27 23:50:54 -0700104 eventDispatcher.removeSink(LinkEvent.class);
tom89b63c52014-09-16 09:19:51 -0700105 deviceService.removeListener(deviceListener);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700106 networkConfigService.removeListener(networkConfigListener);
tomdb0d03f2014-08-27 16:34:15 -0700107 log.info("Stopped");
108 }
109
110 @Override
tomeadbb462014-09-07 16:10:19 -0700111 public int getLinkCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900112 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700113 return store.getLinkCount();
114 }
115
116 @Override
117 public Iterable<Link> getLinks() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900118 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700119 return store.getLinks();
120 }
121
122 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800123 public Iterable<Link> getActiveLinks() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900124 checkPermission(LINK_READ);
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800125 return FluentIterable.from(getLinks())
Sho SHIMIZU74626412015-09-11 11:46:27 -0700126 .filter(input -> input.state() == State.ACTIVE);
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800127 }
128
129 @Override
tomeadbb462014-09-07 16:10:19 -0700130 public Set<Link> getDeviceLinks(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900131 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700132 checkNotNull(deviceId, DEVICE_ID_NULL);
133 return Sets.union(store.getDeviceEgressLinks(deviceId),
tomdc361b62014-09-09 20:36:52 -0700134 store.getDeviceIngressLinks(deviceId));
tomeadbb462014-09-07 16:10:19 -0700135 }
136
137 @Override
138 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900139 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700140 checkNotNull(deviceId, DEVICE_ID_NULL);
141 return store.getDeviceEgressLinks(deviceId);
142 }
143
144 @Override
tomd176fc42014-09-08 00:12:30 -0700145 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900146 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700147 checkNotNull(deviceId, DEVICE_ID_NULL);
148 return store.getDeviceIngressLinks(deviceId);
149 }
150
151 @Override
152 public Set<Link> getLinks(ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900153 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700154 checkNotNull(connectPoint, CONNECT_POINT_NULL);
155 return Sets.union(store.getEgressLinks(connectPoint),
tomdc361b62014-09-09 20:36:52 -0700156 store.getIngressLinks(connectPoint));
tomeadbb462014-09-07 16:10:19 -0700157 }
158
159 @Override
160 public Set<Link> getEgressLinks(ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900161 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700162 checkNotNull(connectPoint, CONNECT_POINT_NULL);
163 return store.getEgressLinks(connectPoint);
164 }
165
166 @Override
tomd176fc42014-09-08 00:12:30 -0700167 public Set<Link> getIngressLinks(ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900168 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700169 checkNotNull(connectPoint, CONNECT_POINT_NULL);
170 return store.getIngressLinks(connectPoint);
171 }
172
173 @Override
tomd176fc42014-09-08 00:12:30 -0700174 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900175 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700176 checkNotNull(src, CONNECT_POINT_NULL);
177 checkNotNull(dst, CONNECT_POINT_NULL);
tomd176fc42014-09-08 00:12:30 -0700178 return store.getLink(src, dst);
tomeadbb462014-09-07 16:10:19 -0700179 }
180
181 @Override
182 public void removeLinks(ConnectPoint connectPoint) {
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700183 if (deviceService.getRole(connectPoint.deviceId()) != MastershipRole.MASTER) {
184 return;
185 }
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800186 removeLinks(getLinks(connectPoint), false);
tomeadbb462014-09-07 16:10:19 -0700187 }
188
189 @Override
190 public void removeLinks(DeviceId deviceId) {
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700191 if (deviceService.getRole(deviceId) != MastershipRole.MASTER) {
192 return;
193 }
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800194 removeLinks(getDeviceLinks(deviceId), false);
tomeadbb462014-09-07 16:10:19 -0700195 }
196
Ayaka Koshibe5373e762015-08-06 12:31:44 -0700197 @Override
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700198 public void removeLink(ConnectPoint src, ConnectPoint dst) {
199 post(store.removeLink(src, dst));
200 }
201
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700202 private boolean isAllowed(BasicLinkConfig cfg) {
203 return (cfg == null || cfg.isAllowed());
204 }
205
tom89b63c52014-09-16 09:19:51 -0700206 // Auxiliary interceptor for device remove events to prune links that
207 // are associated with the removed device or its port.
tomc78acee2014-09-24 15:16:55 -0700208 private class InternalDeviceListener implements DeviceListener {
tom89b63c52014-09-16 09:19:51 -0700209 @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 Lele3a0cdd52015-07-21 14:16:31 -0700215 event.port().number()));
tom89b63c52014-09-16 09:19:51 -0700216 }
217 }
218 }
219
tom7869ad92014-09-09 14:32:08 -0700220 @Override
221 protected LinkProviderService createProviderService(LinkProvider provider) {
222 return new InternalLinkProviderService(provider);
223 }
224
tomdb0d03f2014-08-27 16:34:15 -0700225 // Personalized link provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700226 private class InternalLinkProviderService
227 extends AbstractProviderService<LinkProvider>
228 implements LinkProviderService {
tomdb0d03f2014-08-27 16:34:15 -0700229
tomcfde0622014-09-09 11:02:42 -0700230 InternalLinkProviderService(LinkProvider provider) {
tomdb0d03f2014-08-27 16:34:15 -0700231 super(provider);
232 }
233
234 @Override
235 public void linkDetected(LinkDescription linkDescription) {
tomeadbb462014-09-07 16:10:19 -0700236 checkNotNull(linkDescription, LINK_DESC_NULL);
237 checkValidity();
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700238 linkDescription = validateLink(linkDescription);
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700239 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 }
tomdc361b62014-09-09 20:36:52 -0700245 }
tomdb0d03f2014-08-27 16:34:15 -0700246 }
247
Yuta HIGUCHI2ad387d2017-06-07 10:47:20 -0700248 /**
249 * Validates configuration against link configuration.
250 *
251 * @param linkDescription input
252 * @return description combined with configuration or null if disallowed
253 */
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700254 private LinkDescription validateLink(LinkDescription linkDescription) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700255 BasicLinkConfig cfg = networkConfigService.getConfig(linkKey(linkDescription.src(),
256 linkDescription.dst()),
257 BasicLinkConfig.class);
Yuta HIGUCHI2ad387d2017-06-07 10:47:20 -0700258 if (!isAllowed(cfg)) {
Yuta HIGUCHI59bde762017-02-17 09:45:57 -0800259 log.trace("Link {} is not allowed", linkDescription);
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700260 return null;
261 }
Yuta HIGUCHI2ad387d2017-06-07 10:47:20 -0700262
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 Lele3a0cdd52015-07-21 14:16:31 -0700278 }
279
tomdb0d03f2014-08-27 16:34:15 -0700280 @Override
281 public void linkVanished(LinkDescription linkDescription) {
tomeadbb462014-09-07 16:10:19 -0700282 checkNotNull(linkDescription, LINK_DESC_NULL);
283 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700284
285 ConnectPoint src = linkDescription.src();
286 ConnectPoint dst = linkDescription.dst();
alshabibdfc7afb2014-10-21 20:13:27 -0700287
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800288 LinkEvent event = store.removeOrDownLink(src, dst);
tomdc361b62014-09-09 20:36:52 -0700289 if (event != null) {
290 log.info("Link {} vanished", linkDescription);
291 post(event);
292 }
tomeadbb462014-09-07 16:10:19 -0700293 }
294
295 @Override
296 public void linksVanished(ConnectPoint connectPoint) {
297 checkNotNull(connectPoint, "Connect point cannot be null");
298 checkValidity();
alshabib12288c82014-10-23 10:24:23 -0700299
Yuta HIGUCHIddb77722014-12-11 19:39:04 -0800300 log.debug("Links for connection point {} vanished", connectPoint);
Yuta HIGUCHIe794cbe2014-10-17 13:21:23 -0700301 // FIXME: This will remove links registered by other providers
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800302 removeLinks(getLinks(connectPoint), true);
tomeadbb462014-09-07 16:10:19 -0700303 }
304
305 @Override
306 public void linksVanished(DeviceId deviceId) {
307 checkNotNull(deviceId, DEVICE_ID_NULL);
308 checkValidity();
alshabib12288c82014-10-23 10:24:23 -0700309
Yuta HIGUCHIddb77722014-12-11 19:39:04 -0800310 log.debug("Links for device {} vanished", deviceId);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800311 removeLinks(getDeviceLinks(deviceId), true);
tomdb0d03f2014-08-27 16:34:15 -0700312 }
313 }
tomeadbb462014-09-07 16:10:19 -0700314
315 // Removes all links in the specified set and emits appropriate events.
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700316 private void removeLinks(Set<Link> links, boolean isSoftRemove) {
tomeadbb462014-09-07 16:10:19 -0700317 for (Link link : links) {
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800318 LinkEvent event = isSoftRemove ?
319 store.removeOrDownLink(link.src(), link.dst()) :
320 store.removeLink(link.src(), link.dst());
Yuta HIGUCHIddb77722014-12-11 19:39:04 -0800321 if (event != null) {
322 log.info("Link {} removed/vanished", event.subject());
323 post(event);
324 }
tomeadbb462014-09-07 16:10:19 -0700325 }
326 }
327
tomc78acee2014-09-24 15:16:55 -0700328 // 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 Lele3a0cdd52015-07-21 14:16:31 -0700335
336 // listens for NetworkConfigEvents of type BasicLinkConfig and removes
337 // links that the config does not allow
338 private class InternalNetworkConfigListener implements NetworkConfigListener {
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700339
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 Lele3a0cdd52015-07-21 14:16:31 -0700347 @Override
348 public void event(NetworkConfigEvent event) {
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700349 LinkKey lk = (LinkKey) event.subject();
Yuta HIGUCHI80918772017-05-04 14:51:25 -0700350 BasicLinkConfig cfg = (BasicLinkConfig) event.config().get();
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700351
Thomas Vachuska138de8b2016-01-11 21:31:38 -0800352 log.debug("Detected link network config event {}", event.type());
353
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700354 if (!isAllowed(cfg)) {
355 log.info("Kicking out links between {} and {}", lk.src(), lk.dst());
356 removeLink(lk.src(), lk.dst());
Yuta HIGUCHI2ad387d2017-06-07 10:47:20 -0700357 if (cfg.isBidirectional()) {
358 removeLink(lk.dst(), lk.src());
359 }
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700360 return;
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700361 }
Marc De Leenheer4effa062017-03-15 11:40:34 -0700362
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
David Glantz77e18552021-09-21 11:21:36 -0500373 if (link != null) {
Marc De Leenheer4effa062017-03-15 11:40:34 -0700374 desc = BasicLinkOperator.combine(cfg,
375 BasicLinkOperator.descriptionOf(src, dst, link));
David Glantz77e18552021-09-21 11:21:36 -0500376 ProviderId pid = Optional.ofNullable(link)
377 .map(Link::providerId)
378 .orElse(ProviderId.NONE);
379 store.createOrUpdateLink(pid, desc);
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700380 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700381 }
382 }
tomdb0d03f2014-08-27 16:34:15 -0700383}