blob: 157288a4360b1a9caf18589a274549e942acd2a5 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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'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.base.Predicate;
19import com.google.common.collect.FluentIterable;
20import com.google.common.collect.Sets;
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;
Ray Milkeya4122362015-08-18 15:19:08 -070028import org.onosproject.net.config.NetworkConfigEvent;
29import org.onosproject.net.config.NetworkConfigListener;
30import org.onosproject.net.config.NetworkConfigService;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070031import org.onosproject.net.config.basics.BasicLinkConfig;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.Link;
35import org.onosproject.net.Link.State;
Sahil Lele3a0cdd52015-07-21 14:16:31 -070036import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.MastershipRole;
38import org.onosproject.net.device.DeviceEvent;
39import org.onosproject.net.device.DeviceListener;
40import org.onosproject.net.device.DeviceService;
41import org.onosproject.net.link.LinkAdminService;
42import org.onosproject.net.link.LinkDescription;
43import org.onosproject.net.link.LinkEvent;
44import org.onosproject.net.link.LinkListener;
45import org.onosproject.net.link.LinkProvider;
46import org.onosproject.net.link.LinkProviderRegistry;
47import org.onosproject.net.link.LinkProviderService;
48import org.onosproject.net.link.LinkService;
49import org.onosproject.net.link.LinkStore;
50import org.onosproject.net.link.LinkStoreDelegate;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.provider.AbstractProviderService;
tomdb0d03f2014-08-27 16:34:15 -070052import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070053
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 com.google.common.base.Preconditions.checkState;
58import static org.onosproject.net.LinkKey.linkKey;
Changhoon Yoon541ef712015-05-23 17:18:34 +090059import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070060import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090061import static org.onosproject.security.AppPermission.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090062
Ray Milkey7bbeb3f2014-12-11 14:59:26 -080063
tomdb0d03f2014-08-27 16:34:15 -070064/**
65 * Provides basic implementation of the link SB & NB APIs.
66 */
67@Component(immediate = true)
68@Service
tom35c0dc32014-09-19 10:00:58 -070069public class LinkManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070070 extends AbstractListenerProviderRegistry<LinkEvent, LinkListener, LinkProvider, LinkProviderService>
tomdc361b62014-09-09 20:36:52 -070071 implements LinkService, LinkAdminService, LinkProviderRegistry {
tomeadbb462014-09-07 16:10:19 -070072
73 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
74 private static final String LINK_DESC_NULL = "Link description cannot be null";
75 private static final String CONNECT_POINT_NULL = "Connection point cannot be null";
tomdb0d03f2014-08-27 16:34:15 -070076
tom5f38b3a2014-08-27 23:50:54 -070077 private final Logger log = getLogger(getClass());
tomdb0d03f2014-08-27 16:34:15 -070078
alshabibb5522ff2014-09-29 19:20:00 -070079 private final LinkStoreDelegate delegate = new InternalStoreDelegate();
tomc78acee2014-09-24 15:16:55 -070080
81 private final DeviceListener deviceListener = new InternalDeviceListener();
tom89b63c52014-09-16 09:19:51 -070082
Sahil Lele3a0cdd52015-07-21 14:16:31 -070083 private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener();
84
tom89b63c52014-09-16 09:19:51 -070085 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom35c0dc32014-09-19 10:00:58 -070086 protected LinkStore store;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom89b63c52014-09-16 09:19:51 -070089 protected DeviceService deviceService;
tom4c6606f2014-09-07 11:11:21 -070090
tom5f38b3a2014-08-27 23:50:54 -070091 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sahil Lele3a0cdd52015-07-21 14:16:31 -070092 protected NetworkConfigService networkConfigService;
93
tomdb0d03f2014-08-27 16:34:15 -070094 @Activate
95 public void activate() {
tomc78acee2014-09-24 15:16:55 -070096 store.setDelegate(delegate);
tom96dfcab2014-08-28 09:26:03 -070097 eventDispatcher.addSink(LinkEvent.class, listenerRegistry);
tom89b63c52014-09-16 09:19:51 -070098 deviceService.addListener(deviceListener);
Sahil Lele3a0cdd52015-07-21 14:16:31 -070099 networkConfigService.addListener(networkConfigListener);
tomdb0d03f2014-08-27 16:34:15 -0700100 log.info("Started");
101 }
102
103 @Deactivate
104 public void deactivate() {
tomc78acee2014-09-24 15:16:55 -0700105 store.unsetDelegate(delegate);
tom5f38b3a2014-08-27 23:50:54 -0700106 eventDispatcher.removeSink(LinkEvent.class);
tom89b63c52014-09-16 09:19:51 -0700107 deviceService.removeListener(deviceListener);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700108 networkConfigService.removeListener(networkConfigListener);
tomdb0d03f2014-08-27 16:34:15 -0700109 log.info("Stopped");
110 }
111
112 @Override
tomeadbb462014-09-07 16:10:19 -0700113 public int getLinkCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900114 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700115 return store.getLinkCount();
116 }
117
118 @Override
119 public Iterable<Link> getLinks() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900120 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700121 return store.getLinks();
122 }
123
124 @Override
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800125 public Iterable<Link> getActiveLinks() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900126 checkPermission(LINK_READ);
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800127 return FluentIterable.from(getLinks())
128 .filter(new Predicate<Link>() {
129
130 @Override
131 public boolean apply(Link input) {
132 return input.state() == State.ACTIVE;
133 }
134 });
135 }
136
137 @Override
tomeadbb462014-09-07 16:10:19 -0700138 public Set<Link> getDeviceLinks(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 Sets.union(store.getDeviceEgressLinks(deviceId),
tomdc361b62014-09-09 20:36:52 -0700142 store.getDeviceIngressLinks(deviceId));
tomeadbb462014-09-07 16:10:19 -0700143 }
144
145 @Override
146 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900147 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700148 checkNotNull(deviceId, DEVICE_ID_NULL);
149 return store.getDeviceEgressLinks(deviceId);
150 }
151
152 @Override
tomd176fc42014-09-08 00:12:30 -0700153 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900154 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700155 checkNotNull(deviceId, DEVICE_ID_NULL);
156 return store.getDeviceIngressLinks(deviceId);
157 }
158
159 @Override
160 public Set<Link> getLinks(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 Sets.union(store.getEgressLinks(connectPoint),
tomdc361b62014-09-09 20:36:52 -0700164 store.getIngressLinks(connectPoint));
tomeadbb462014-09-07 16:10:19 -0700165 }
166
167 @Override
168 public Set<Link> getEgressLinks(ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900169 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700170 checkNotNull(connectPoint, CONNECT_POINT_NULL);
171 return store.getEgressLinks(connectPoint);
172 }
173
174 @Override
tomd176fc42014-09-08 00:12:30 -0700175 public Set<Link> getIngressLinks(ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900176 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700177 checkNotNull(connectPoint, CONNECT_POINT_NULL);
178 return store.getIngressLinks(connectPoint);
179 }
180
181 @Override
tomd176fc42014-09-08 00:12:30 -0700182 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900183 checkPermission(LINK_READ);
tomeadbb462014-09-07 16:10:19 -0700184 checkNotNull(src, CONNECT_POINT_NULL);
185 checkNotNull(dst, CONNECT_POINT_NULL);
tomd176fc42014-09-08 00:12:30 -0700186 return store.getLink(src, dst);
tomeadbb462014-09-07 16:10:19 -0700187 }
188
189 @Override
190 public void removeLinks(ConnectPoint connectPoint) {
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700191 if (deviceService.getRole(connectPoint.deviceId()) != MastershipRole.MASTER) {
192 return;
193 }
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800194 removeLinks(getLinks(connectPoint), false);
tomeadbb462014-09-07 16:10:19 -0700195 }
196
197 @Override
198 public void removeLinks(DeviceId deviceId) {
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700199 if (deviceService.getRole(deviceId) != MastershipRole.MASTER) {
200 return;
201 }
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800202 removeLinks(getDeviceLinks(deviceId), false);
tomeadbb462014-09-07 16:10:19 -0700203 }
204
Ayaka Koshibe5373e762015-08-06 12:31:44 -0700205 @Override
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700206 public void removeLink(ConnectPoint src, ConnectPoint dst) {
207 post(store.removeLink(src, dst));
208 }
209
tom89b63c52014-09-16 09:19:51 -0700210 // Auxiliary interceptor for device remove events to prune links that
211 // are associated with the removed device or its port.
tomc78acee2014-09-24 15:16:55 -0700212 private class InternalDeviceListener implements DeviceListener {
tom89b63c52014-09-16 09:19:51 -0700213 @Override
214 public void event(DeviceEvent event) {
215 if (event.type() == DeviceEvent.Type.DEVICE_REMOVED) {
216 removeLinks(event.subject().id());
217 } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) {
218 removeLinks(new ConnectPoint(event.subject().id(),
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700219 event.port().number()));
tom89b63c52014-09-16 09:19:51 -0700220 }
221 }
222 }
223
tom7869ad92014-09-09 14:32:08 -0700224 @Override
225 protected LinkProviderService createProviderService(LinkProvider provider) {
226 return new InternalLinkProviderService(provider);
227 }
228
tomdb0d03f2014-08-27 16:34:15 -0700229 // Personalized link provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700230 private class InternalLinkProviderService
231 extends AbstractProviderService<LinkProvider>
232 implements LinkProviderService {
tomdb0d03f2014-08-27 16:34:15 -0700233
tomcfde0622014-09-09 11:02:42 -0700234 InternalLinkProviderService(LinkProvider provider) {
tomdb0d03f2014-08-27 16:34:15 -0700235 super(provider);
236 }
237
238 @Override
239 public void linkDetected(LinkDescription linkDescription) {
tomeadbb462014-09-07 16:10:19 -0700240 checkNotNull(linkDescription, LINK_DESC_NULL);
241 checkValidity();
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700242 linkDescription = validateLink(linkDescription);
tomeadbb462014-09-07 16:10:19 -0700243 LinkEvent event = store.createOrUpdateLink(provider().id(),
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700244 linkDescription);
tomdc361b62014-09-09 20:36:52 -0700245 if (event != null) {
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700246 log.info("Link {} detected", linkDescription);
tomdc361b62014-09-09 20:36:52 -0700247 post(event);
248 }
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);
261
262 checkState(cfg == null || cfg.isAllowed(), "Link " + linkDescription.toString() + " is not allowed");
263 checkState(cfgTwo == null || cfgTwo.isAllowed(), "Link " + linkDescription.toString() + " is not allowed");
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700264
Ayaka Koshibe5373e762015-08-06 12:31:44 -0700265 return BasicLinkOperator.combine(cfg, linkDescription);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700266 }
267
tomdb0d03f2014-08-27 16:34:15 -0700268 @Override
269 public void linkVanished(LinkDescription linkDescription) {
tomeadbb462014-09-07 16:10:19 -0700270 checkNotNull(linkDescription, LINK_DESC_NULL);
271 checkValidity();
Ayaka Koshibeb5c63a02014-10-18 18:42:27 -0700272
273 ConnectPoint src = linkDescription.src();
274 ConnectPoint dst = linkDescription.dst();
alshabibdfc7afb2014-10-21 20:13:27 -0700275
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800276 LinkEvent event = store.removeOrDownLink(src, dst);
tomdc361b62014-09-09 20:36:52 -0700277 if (event != null) {
278 log.info("Link {} vanished", linkDescription);
279 post(event);
280 }
tomeadbb462014-09-07 16:10:19 -0700281 }
282
283 @Override
284 public void linksVanished(ConnectPoint connectPoint) {
285 checkNotNull(connectPoint, "Connect point cannot be null");
286 checkValidity();
alshabib12288c82014-10-23 10:24:23 -0700287
Yuta HIGUCHIddb77722014-12-11 19:39:04 -0800288 log.debug("Links for connection point {} vanished", connectPoint);
Yuta HIGUCHIe794cbe2014-10-17 13:21:23 -0700289 // FIXME: This will remove links registered by other providers
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800290 removeLinks(getLinks(connectPoint), true);
tomeadbb462014-09-07 16:10:19 -0700291 }
292
293 @Override
294 public void linksVanished(DeviceId deviceId) {
295 checkNotNull(deviceId, DEVICE_ID_NULL);
296 checkValidity();
alshabib12288c82014-10-23 10:24:23 -0700297
Yuta HIGUCHIddb77722014-12-11 19:39:04 -0800298 log.debug("Links for device {} vanished", deviceId);
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800299 removeLinks(getDeviceLinks(deviceId), true);
tomdb0d03f2014-08-27 16:34:15 -0700300 }
301 }
tomeadbb462014-09-07 16:10:19 -0700302
303 // Removes all links in the specified set and emits appropriate events.
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700304 private void removeLinks(Set<Link> links, boolean isSoftRemove) {
tomeadbb462014-09-07 16:10:19 -0700305 for (Link link : links) {
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800306 LinkEvent event = isSoftRemove ?
307 store.removeOrDownLink(link.src(), link.dst()) :
308 store.removeLink(link.src(), link.dst());
Yuta HIGUCHIddb77722014-12-11 19:39:04 -0800309 if (event != null) {
310 log.info("Link {} removed/vanished", event.subject());
311 post(event);
312 }
tomeadbb462014-09-07 16:10:19 -0700313 }
314 }
315
tomc78acee2014-09-24 15:16:55 -0700316 // Store delegate to re-post events emitted from the store.
317 private class InternalStoreDelegate implements LinkStoreDelegate {
318 @Override
319 public void notify(LinkEvent event) {
320 post(event);
321 }
322 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700323
324 // listens for NetworkConfigEvents of type BasicLinkConfig and removes
325 // links that the config does not allow
326 private class InternalNetworkConfigListener implements NetworkConfigListener {
327 @Override
328 public void event(NetworkConfigEvent event) {
329 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
330 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
331 event.configClass().equals(BasicLinkConfig.class)) {
332 log.info("Detected Link network config event {}", event.type());
333 LinkKey lk = (LinkKey) event.subject();
334 BasicLinkConfig cfg = networkConfigService.getConfig(lk, BasicLinkConfig.class);
335 if (cfg != null && !cfg.isAllowed()) {
336 log.info("Kicking out links between {} and {}", lk.src(), lk.dst());
337 removeLink(lk.src(), lk.dst());
338 removeLink(lk.dst(), lk.src());
339 }
340 }
341 }
342 }
tomdb0d03f2014-08-27 16:34:15 -0700343}