blob: cfed82679b2e656609b1c2e7bfe88acf1859f4f1 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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;
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -070020
tomdb0d03f2014-08-27 16:34:15 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
tom5f38b3a2014-08-27 23:50:54 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
tomdb0d03f2014-08-27 16:34:15 -070026import org.apache.felix.scr.annotations.Service;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070027import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -070028import org.onosproject.net.provider.ProviderId;
Ray Milkeya4122362015-08-18 15:19:08 -070029import org.onosproject.net.config.NetworkConfigEvent;
30import org.onosproject.net.config.NetworkConfigListener;
31import org.onosproject.net.config.NetworkConfigService;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070032import org.onosproject.net.config.basics.BasicLinkConfig;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.Link;
36import org.onosproject.net.Link.State;
Sahil Lele3a0cdd52015-07-21 14:16:31 -070037import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.MastershipRole;
39import org.onosproject.net.device.DeviceEvent;
40import org.onosproject.net.device.DeviceListener;
41import org.onosproject.net.device.DeviceService;
42import org.onosproject.net.link.LinkAdminService;
43import org.onosproject.net.link.LinkDescription;
44import org.onosproject.net.link.LinkEvent;
45import org.onosproject.net.link.LinkListener;
46import org.onosproject.net.link.LinkProvider;
47import org.onosproject.net.link.LinkProviderRegistry;
48import org.onosproject.net.link.LinkProviderService;
49import org.onosproject.net.link.LinkService;
50import org.onosproject.net.link.LinkStore;
51import org.onosproject.net.link.LinkStoreDelegate;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.provider.AbstractProviderService;
tomdb0d03f2014-08-27 16:34:15 -070053import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070054
Yuta HIGUCHI31207782017-02-17 14:43:40 -080055import java.util.Optional;
Simon Huntff663742015-05-14 13:33:05 -070056import java.util.Set;
tomdb0d03f2014-08-27 16:34:15 -070057
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080058import static com.google.common.base.Preconditions.checkNotNull;
Sahil Lele3a0cdd52015-07-21 14:16:31 -070059import static org.onosproject.net.LinkKey.linkKey;
Changhoon Yoon541ef712015-05-23 17:18:34 +090060import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070061import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090062import static org.onosproject.security.AppPermission.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090063
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080064
tomdb0d03f2014-08-27 16:34:15 -070065/**
66 * Provides basic implementation of the link SB & NB APIs.
67 */
68@Component(immediate = true)
69@Service
tom35c0dc32014-09-19 10:00:58 -070070public class LinkManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070071 extends AbstractListenerProviderRegistry<LinkEvent, LinkListener, LinkProvider, LinkProviderService>
tomdc361b62014-09-09 20:36:52 -070072 implements LinkService, LinkAdminService, LinkProviderRegistry {
tomeadbb462014-09-07 16:10:19 -070073
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";
tomdb0d03f2014-08-27 16:34:15 -070077
tom5f38b3a2014-08-27 23:50:54 -070078 private final Logger log = getLogger(getClass());
tomdb0d03f2014-08-27 16:34:15 -070079
alshabibb5522ff2014-09-29 19:20:00 -070080 private final LinkStoreDelegate delegate = new InternalStoreDelegate();
tomc78acee2014-09-24 15:16:55 -070081
82 private final DeviceListener deviceListener = new InternalDeviceListener();
tom89b63c52014-09-16 09:19:51 -070083
Sahil Lele3a0cdd52015-07-21 14:16:31 -070084 private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener();
85
tom89b63c52014-09-16 09:19:51 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom35c0dc32014-09-19 10:00:58 -070087 protected LinkStore store;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom89b63c52014-09-16 09:19:51 -070090 protected DeviceService deviceService;
tom4c6606f2014-09-07 11:11:21 -070091
tom5f38b3a2014-08-27 23:50:54 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sahil Lele3a0cdd52015-07-21 14:16:31 -070093 protected NetworkConfigService networkConfigService;
94
tomdb0d03f2014-08-27 16:34:15 -070095 @Activate
96 public void activate() {
tomc78acee2014-09-24 15:16:55 -070097 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -070098 eventDispatcher.addSink(LinkEvent.class, listenerRegistry);
tom89b63c52014-09-16 09:19:51 -070099 deviceService.addListener(deviceListener);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700100 networkConfigService.addListener(networkConfigListener);
tomdb0d03f2014-08-27 16:34:15 -0700101 log.info("Started");
102 }
103
104 @Deactivate
105 public void deactivate() {
tomc78acee2014-09-24 15:16:55 -0700106 store.unsetDelegate(delegate);
tom5f38b3a2014-08-27 23:50:54 -0700107 eventDispatcher.removeSink(LinkEvent.class);
tom89b63c52014-09-16 09:19:51 -0700108 deviceService.removeListener(deviceListener);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700109 networkConfigService.removeListener(networkConfigListener);
tomdb0d03f2014-08-27 16:34:15 -0700110 log.info("Stopped");
111 }
112
113 @Override
tomeadbb462014-09-07 16:10:19 -0700114 public int getLinkCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900115 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700116 return store.getLinkCount();
117 }
118
119 @Override
120 public Iterable<Link> getLinks() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900121 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700122 return store.getLinks();
123 }
124
125 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800126 public Iterable<Link> getActiveLinks() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900127 checkPermission(LINK_READ);
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800128 return FluentIterable.from(getLinks())
Sho SHIMIZU74626412015-09-11 11:46:27 -0700129 .filter(input -> input.state() == State.ACTIVE);
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800130 }
131
132 @Override
tomeadbb462014-09-07 16:10:19 -0700133 public Set<Link> getDeviceLinks(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900134 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700135 checkNotNull(deviceId, DEVICE_ID_NULL);
136 return Sets.union(store.getDeviceEgressLinks(deviceId),
tomdc361b62014-09-09 20:36:52 -0700137 store.getDeviceIngressLinks(deviceId));
tomeadbb462014-09-07 16:10:19 -0700138 }
139
140 @Override
141 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900142 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700143 checkNotNull(deviceId, DEVICE_ID_NULL);
144 return store.getDeviceEgressLinks(deviceId);
145 }
146
147 @Override
tomd176fc42014-09-08 00:12:30 -0700148 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900149 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700150 checkNotNull(deviceId, DEVICE_ID_NULL);
151 return store.getDeviceIngressLinks(deviceId);
152 }
153
154 @Override
155 public Set<Link> getLinks(ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900156 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700157 checkNotNull(connectPoint, CONNECT_POINT_NULL);
158 return Sets.union(store.getEgressLinks(connectPoint),
tomdc361b62014-09-09 20:36:52 -0700159 store.getIngressLinks(connectPoint));
tomeadbb462014-09-07 16:10:19 -0700160 }
161
162 @Override
163 public Set<Link> getEgressLinks(ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900164 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700165 checkNotNull(connectPoint, CONNECT_POINT_NULL);
166 return store.getEgressLinks(connectPoint);
167 }
168
169 @Override
tomd176fc42014-09-08 00:12:30 -0700170 public Set<Link> getIngressLinks(ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900171 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700172 checkNotNull(connectPoint, CONNECT_POINT_NULL);
173 return store.getIngressLinks(connectPoint);
174 }
175
176 @Override
tomd176fc42014-09-08 00:12:30 -0700177 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900178 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700179 checkNotNull(src, CONNECT_POINT_NULL);
180 checkNotNull(dst, CONNECT_POINT_NULL);
tomd176fc42014-09-08 00:12:30 -0700181 return store.getLink(src, dst);
tomeadbb462014-09-07 16:10:19 -0700182 }
183
184 @Override
185 public void removeLinks(ConnectPoint connectPoint) {
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700186 if (deviceService.getRole(connectPoint.deviceId()) != MastershipRole.MASTER) {
187 return;
188 }
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800189 removeLinks(getLinks(connectPoint), false);
tomeadbb462014-09-07 16:10:19 -0700190 }
191
192 @Override
193 public void removeLinks(DeviceId deviceId) {
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700194 if (deviceService.getRole(deviceId) != MastershipRole.MASTER) {
195 return;
196 }
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800197 removeLinks(getDeviceLinks(deviceId), false);
tomeadbb462014-09-07 16:10:19 -0700198 }
199
Ayaka Koshibe5373e762015-08-06 12:31:44 -0700200 @Override
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700201 public void removeLink(ConnectPoint src, ConnectPoint dst) {
202 post(store.removeLink(src, dst));
203 }
204
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700205 private boolean isAllowed(BasicLinkConfig cfg) {
206 return (cfg == null || cfg.isAllowed());
207 }
208
tom89b63c52014-09-16 09:19:51 -0700209 // Auxiliary interceptor for device remove events to prune links that
210 // are associated with the removed device or its port.
tomc78acee2014-09-24 15:16:55 -0700211 private class InternalDeviceListener implements DeviceListener {
tom89b63c52014-09-16 09:19:51 -0700212 @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 Lele3a0cdd52015-07-21 14:16:31 -0700218 event.port().number()));
tom89b63c52014-09-16 09:19:51 -0700219 }
220 }
221 }
222
tom7869ad92014-09-09 14:32:08 -0700223 @Override
224 protected LinkProviderService createProviderService(LinkProvider provider) {
225 return new InternalLinkProviderService(provider);
226 }
227
tomdb0d03f2014-08-27 16:34:15 -0700228 // Personalized link provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700229 private class InternalLinkProviderService
230 extends AbstractProviderService<LinkProvider>
231 implements LinkProviderService {
tomdb0d03f2014-08-27 16:34:15 -0700232
tomcfde0622014-09-09 11:02:42 -0700233 InternalLinkProviderService(LinkProvider provider) {
tomdb0d03f2014-08-27 16:34:15 -0700234 super(provider);
235 }
236
237 @Override
238 public void linkDetected(LinkDescription linkDescription) {
tomeadbb462014-09-07 16:10:19 -0700239 checkNotNull(linkDescription, LINK_DESC_NULL);
240 checkValidity();
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700241 linkDescription = validateLink(linkDescription);
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700242 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 }
tomdc361b62014-09-09 20:36:52 -0700248 }
tomdb0d03f2014-08-27 16:34:15 -0700249 }
250
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700251 // returns a LinkDescription made from the union of the BasicLinkConfig
252 // annotations if it exists
253 private LinkDescription validateLink(LinkDescription linkDescription) {
254 // TODO Investigate whether this can be made more efficient
255 BasicLinkConfig cfg = networkConfigService.getConfig(linkKey(linkDescription.src(),
256 linkDescription.dst()),
257 BasicLinkConfig.class);
258 BasicLinkConfig cfgTwo = networkConfigService.getConfig(linkKey(linkDescription.dst(),
259 linkDescription.src()),
260 BasicLinkConfig.class);
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700261 if (isAllowed(cfg) && isAllowed(cfgTwo)) {
262 return BasicLinkOperator.combine(cfg, linkDescription);
263 } else {
Yuta HIGUCHI59bde762017-02-17 09:45:57 -0800264 log.trace("Link {} is not allowed", linkDescription);
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700265 return null;
266 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700267 }
268
tomdb0d03f2014-08-27 16:34:15 -0700269 @Override
270 public void linkVanished(LinkDescription linkDescription) {
tomeadbb462014-09-07 16:10:19 -0700271 checkNotNull(linkDescription, LINK_DESC_NULL);
272 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700273
274 ConnectPoint src = linkDescription.src();
275 ConnectPoint dst = linkDescription.dst();
alshabibdfc7afb2014-10-21 20:13:27 -0700276
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800277 LinkEvent event = store.removeOrDownLink(src, dst);
tomdc361b62014-09-09 20:36:52 -0700278 if (event != null) {
279 log.info("Link {} vanished", linkDescription);
280 post(event);
281 }
tomeadbb462014-09-07 16:10:19 -0700282 }
283
284 @Override
285 public void linksVanished(ConnectPoint connectPoint) {
286 checkNotNull(connectPoint, "Connect point cannot be null");
287 checkValidity();
alshabib12288c82014-10-23 10:24:23 -0700288
Yuta HIGUCHIddb77722014-12-11 19:39:04 -0800289 log.debug("Links for connection point {} vanished", connectPoint);
Yuta HIGUCHIe794cbe2014-10-17 13:21:23 -0700290 // FIXME: This will remove links registered by other providers
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800291 removeLinks(getLinks(connectPoint), true);
tomeadbb462014-09-07 16:10:19 -0700292 }
293
294 @Override
295 public void linksVanished(DeviceId deviceId) {
296 checkNotNull(deviceId, DEVICE_ID_NULL);
297 checkValidity();
alshabib12288c82014-10-23 10:24:23 -0700298
Yuta HIGUCHIddb77722014-12-11 19:39:04 -0800299 log.debug("Links for device {} vanished", deviceId);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800300 removeLinks(getDeviceLinks(deviceId), true);
tomdb0d03f2014-08-27 16:34:15 -0700301 }
302 }
tomeadbb462014-09-07 16:10:19 -0700303
304 // Removes all links in the specified set and emits appropriate events.
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700305 private void removeLinks(Set<Link> links, boolean isSoftRemove) {
tomeadbb462014-09-07 16:10:19 -0700306 for (Link link : links) {
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800307 LinkEvent event = isSoftRemove ?
308 store.removeOrDownLink(link.src(), link.dst()) :
309 store.removeLink(link.src(), link.dst());
Yuta HIGUCHIddb77722014-12-11 19:39:04 -0800310 if (event != null) {
311 log.info("Link {} removed/vanished", event.subject());
312 post(event);
313 }
tomeadbb462014-09-07 16:10:19 -0700314 }
315 }
316
tomc78acee2014-09-24 15:16:55 -0700317 // Store delegate to re-post events emitted from the store.
318 private class InternalStoreDelegate implements LinkStoreDelegate {
319 @Override
320 public void notify(LinkEvent event) {
321 post(event);
322 }
323 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700324
325 // listens for NetworkConfigEvents of type BasicLinkConfig and removes
326 // links that the config does not allow
327 private class InternalNetworkConfigListener implements NetworkConfigListener {
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700328
329 @Override
330 public boolean isRelevant(NetworkConfigEvent event) {
331 return event.configClass().equals(BasicLinkConfig.class)
332 && (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
333 || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED);
334 }
335
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700336 @Override
337 public void event(NetworkConfigEvent event) {
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700338 LinkKey lk = (LinkKey) event.subject();
339 BasicLinkConfig cfg = networkConfigService.getConfig(lk, BasicLinkConfig.class);
340
Thomas Vachuska138de8b2016-01-11 21:31:38 -0800341 log.debug("Detected link network config event {}", event.type());
342
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700343 if (!isAllowed(cfg)) {
344 log.info("Kicking out links between {} and {}", lk.src(), lk.dst());
345 removeLink(lk.src(), lk.dst());
346 removeLink(lk.dst(), lk.src());
347 return;
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700348 }
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700349 Link link = getLink(lk.src(), lk.dst());
350 LinkDescription fldesc;
351 LinkDescription rldesc;
352 if (link == null) {
353 fldesc = BasicLinkOperator.descriptionOf(lk.src(), lk.dst(), cfg);
354 rldesc = BasicLinkOperator.descriptionOf(lk.dst(), lk.src(), cfg);
355 } else {
356 fldesc = BasicLinkOperator.combine(cfg,
357 BasicLinkOperator.descriptionOf(lk.src(), lk.dst(), link));
358 rldesc = BasicLinkOperator.combine(cfg,
359 BasicLinkOperator.descriptionOf(lk.dst(), lk.src(), link));
360 }
Yuta HIGUCHI31207782017-02-17 14:43:40 -0800361
362 ProviderId pid = Optional.ofNullable(link)
363 .map(Link::providerId)
364 .orElse(ProviderId.NONE);
365 store.createOrUpdateLink(pid, fldesc);
366 store.createOrUpdateLink(pid, rldesc);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700367 }
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700368
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700369 }
tomdb0d03f2014-08-27 16:34:15 -0700370}