blob: 280df9ea419bb7e6bc2f8568881134469ec9f115 [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.topology.impl;
tomcfde0622014-09-09 11:02:42 -070017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.event.Event;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.DeviceId;
Ray Milkey7483e1b2018-02-07 15:43:01 -080021import org.onosproject.net.DisjointPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.Link;
23import org.onosproject.net.Path;
Ray Milkey7483e1b2018-02-07 15:43:01 -080024import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.provider.AbstractProviderService;
26import org.onosproject.net.topology.ClusterId;
27import org.onosproject.net.topology.GraphDescription;
Andrey Komarov2398d962016-09-26 15:11:23 +030028import org.onosproject.net.topology.LinkWeigher;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.topology.Topology;
30import org.onosproject.net.topology.TopologyCluster;
31import org.onosproject.net.topology.TopologyEvent;
32import org.onosproject.net.topology.TopologyGraph;
33import org.onosproject.net.topology.TopologyListener;
34import org.onosproject.net.topology.TopologyProvider;
35import org.onosproject.net.topology.TopologyProviderRegistry;
36import org.onosproject.net.topology.TopologyProviderService;
37import org.onosproject.net.topology.TopologyService;
38import org.onosproject.net.topology.TopologyStore;
39import org.onosproject.net.topology.TopologyStoreDelegate;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040import org.osgi.service.component.annotations.Activate;
41import org.osgi.service.component.annotations.Component;
42import org.osgi.service.component.annotations.Deactivate;
43import org.osgi.service.component.annotations.Reference;
44import org.osgi.service.component.annotations.ReferenceCardinality;
tomcfde0622014-09-09 11:02:42 -070045import org.slf4j.Logger;
46
47import java.util.List;
Ray Milkey7483e1b2018-02-07 15:43:01 -080048import java.util.Map;
tomcfde0622014-09-09 11:02:42 -070049import java.util.Set;
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -070050import java.util.stream.Stream;
tomcfde0622014-09-09 11:02:42 -070051
52import static com.google.common.base.Preconditions.checkNotNull;
Changhoon Yoon541ef712015-05-23 17:18:34 +090053import static org.onosproject.security.AppGuard.checkPermission;
Ray Milkey7483e1b2018-02-07 15:43:01 -080054import static org.onosproject.security.AppPermission.Type.TOPOLOGY_READ;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070055import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090056
tomcfde0622014-09-09 11:02:42 -070057
58/**
59 * Provides basic implementation of the topology SB & NB APIs.
60 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070061@Component(immediate = true, service = {TopologyService.class, TopologyProviderRegistry.class})
tom10262dd2014-09-19 10:51:19 -070062public class TopologyManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070063 extends AbstractListenerProviderRegistry<TopologyEvent, TopologyListener,
Thomas Vachuska48e64e42015-09-22 15:32:55 -070064 TopologyProvider, TopologyProviderService>
tomcfde0622014-09-09 11:02:42 -070065 implements TopologyService, TopologyProviderRegistry {
66
Simon Hunt11a29282016-03-31 09:16:47 -070067 private static final String TOPOLOGY_NULL = "Topology cannot be null";
tomcfde0622014-09-09 11:02:42 -070068 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
tom13cb4852014-09-11 12:44:17 -070069 private static final String CLUSTER_ID_NULL = "Cluster ID cannot be null";
70 private static final String CLUSTER_NULL = "Topology cluster cannot be null";
Simon Hunt11a29282016-03-31 09:16:47 -070071 private static final String CONNECTION_POINT_NULL = "Connection point cannot be null";
72 private static final String LINK_WEIGHT_NULL = "Link weight cannot be null";
tomcfde0622014-09-09 11:02:42 -070073
74 private final Logger log = getLogger(getClass());
75
tomc78acee2014-09-24 15:16:55 -070076 private TopologyStoreDelegate delegate = new InternalStoreDelegate();
77
Ray Milkeyd84f89b2018-08-17 14:54:17 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tom10262dd2014-09-19 10:51:19 -070079 protected TopologyStore store;
tom0efbb1d2014-09-09 11:54:28 -070080
tomcfde0622014-09-09 11:02:42 -070081 @Activate
82 public void activate() {
tomc78acee2014-09-24 15:16:55 -070083 store.setDelegate(delegate);
tomcfde0622014-09-09 11:02:42 -070084 eventDispatcher.addSink(TopologyEvent.class, listenerRegistry);
85 log.info("Started");
86 }
87
88 @Deactivate
89 public void deactivate() {
tomc78acee2014-09-24 15:16:55 -070090 store.unsetDelegate(delegate);
tomcfde0622014-09-09 11:02:42 -070091 eventDispatcher.removeSink(TopologyEvent.class);
92 log.info("Stopped");
93 }
94
95 @Override
tomdc361b62014-09-09 20:36:52 -070096 public Topology currentTopology() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090097 checkPermission(TOPOLOGY_READ);
tomdc361b62014-09-09 20:36:52 -070098 return store.currentTopology();
tomcfde0622014-09-09 11:02:42 -070099 }
100
101 @Override
tomdc361b62014-09-09 20:36:52 -0700102 public boolean isLatest(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900103 checkPermission(TOPOLOGY_READ);
tomdc361b62014-09-09 20:36:52 -0700104 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700105 return store.isLatest(topology);
tomcfde0622014-09-09 11:02:42 -0700106 }
107
108 @Override
109 public Set<TopologyCluster> getClusters(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900110 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700111 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700112 return store.getClusters(topology);
tomcfde0622014-09-09 11:02:42 -0700113 }
114
115 @Override
tom13cb4852014-09-11 12:44:17 -0700116 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900117 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700118 checkNotNull(topology, TOPOLOGY_NULL);
Sean Condon436c60a2021-01-01 14:23:29 +0000119 checkNotNull(clusterId, CLUSTER_ID_NULL);
tom10262dd2014-09-19 10:51:19 -0700120 return store.getCluster(topology, clusterId);
tom13cb4852014-09-11 12:44:17 -0700121 }
122
123 @Override
124 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900125 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700126 checkNotNull(topology, TOPOLOGY_NULL);
Sean Condon436c60a2021-01-01 14:23:29 +0000127 checkNotNull(cluster, CLUSTER_NULL);
tom10262dd2014-09-19 10:51:19 -0700128 return store.getClusterDevices(topology, cluster);
tom13cb4852014-09-11 12:44:17 -0700129 }
130
131 @Override
132 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900133 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700134 checkNotNull(topology, TOPOLOGY_NULL);
Sean Condon436c60a2021-01-01 14:23:29 +0000135 checkNotNull(cluster, CLUSTER_NULL);
tom10262dd2014-09-19 10:51:19 -0700136 return store.getClusterLinks(topology, cluster);
tom13cb4852014-09-11 12:44:17 -0700137 }
138
139 @Override
tom97937552014-09-11 10:48:42 -0700140 public TopologyGraph getGraph(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900141 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700142 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700143 return store.getGraph(topology);
tomcfde0622014-09-09 11:02:42 -0700144 }
145
146 @Override
147 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900148 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700149 checkNotNull(topology, TOPOLOGY_NULL);
150 checkNotNull(src, DEVICE_ID_NULL);
151 checkNotNull(dst, DEVICE_ID_NULL);
tom10262dd2014-09-19 10:51:19 -0700152 return store.getPaths(topology, src, dst);
tomcfde0622014-09-09 11:02:42 -0700153 }
154
155 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300156 public Set<Path> getPaths(Topology topology, DeviceId src,
Andrey Komarov2398d962016-09-26 15:11:23 +0300157 DeviceId dst, LinkWeigher weigher) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900158 checkPermission(TOPOLOGY_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900159
tomcfde0622014-09-09 11:02:42 -0700160 checkNotNull(topology, TOPOLOGY_NULL);
161 checkNotNull(src, DEVICE_ID_NULL);
162 checkNotNull(dst, DEVICE_ID_NULL);
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -0700163 checkNotNull(weigher, LINK_WEIGHT_NULL);
Andrey Komarov2398d962016-09-26 15:11:23 +0300164 return store.getPaths(topology, src, dst, weigher);
tomcfde0622014-09-09 11:02:42 -0700165 }
166
167 @Override
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -0700168 public Set<Path> getKShortestPaths(Topology topology, DeviceId src,
169 DeviceId dst, LinkWeigher weigher,
170 int maxPaths) {
171 checkPermission(TOPOLOGY_READ);
172
173 checkNotNull(topology, TOPOLOGY_NULL);
174 checkNotNull(src, DEVICE_ID_NULL);
175 checkNotNull(dst, DEVICE_ID_NULL);
176 checkNotNull(weigher, LINK_WEIGHT_NULL);
177 return store.getKShortestPaths(topology, src, dst, weigher, maxPaths);
178 }
179
180 @Override
181 public Stream<Path> getKShortestPaths(Topology topology,
182 DeviceId src,
183 DeviceId dst,
184 LinkWeigher weigher) {
185 checkPermission(TOPOLOGY_READ);
186
187 checkNotNull(topology, TOPOLOGY_NULL);
188 checkNotNull(src, DEVICE_ID_NULL);
189 checkNotNull(dst, DEVICE_ID_NULL);
190 checkNotNull(weigher, LINK_WEIGHT_NULL);
191 return store.getKShortestPaths(topology, src, dst, weigher);
192 }
193
194 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300195 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
196 DeviceId dst) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900197 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700198 checkNotNull(topology, TOPOLOGY_NULL);
199 checkNotNull(src, DEVICE_ID_NULL);
200 checkNotNull(dst, DEVICE_ID_NULL);
201 return store.getDisjointPaths(topology, src, dst);
202 }
203
204 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700205 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
Andrey Komarov2398d962016-09-26 15:11:23 +0300206 DeviceId dst,
Andrey Komarov2398d962016-09-26 15:11:23 +0300207 LinkWeigher weigher) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900208 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700209 checkNotNull(topology, TOPOLOGY_NULL);
210 checkNotNull(src, DEVICE_ID_NULL);
211 checkNotNull(dst, DEVICE_ID_NULL);
Andrey Komarov2398d962016-09-26 15:11:23 +0300212 checkNotNull(weigher, LINK_WEIGHT_NULL);
213 return store.getDisjointPaths(topology, src, dst, weigher);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700214 }
215
216 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300217 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
218 DeviceId dst,
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700219 Map<Link, Object> riskProfile) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900220 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700221 checkNotNull(topology, TOPOLOGY_NULL);
222 checkNotNull(src, DEVICE_ID_NULL);
223 checkNotNull(dst, DEVICE_ID_NULL);
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700224 return store.getDisjointPaths(topology, src, dst, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700225 }
226
227 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700228 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
Andrey Komarov2398d962016-09-26 15:11:23 +0300229 DeviceId dst,
230 LinkWeigher weigher,
231 Map<Link, Object> riskProfile) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900232 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700233 checkNotNull(topology, TOPOLOGY_NULL);
234 checkNotNull(src, DEVICE_ID_NULL);
235 checkNotNull(dst, DEVICE_ID_NULL);
Andrey Komarov2398d962016-09-26 15:11:23 +0300236 checkNotNull(weigher, LINK_WEIGHT_NULL);
237 return store.getDisjointPaths(topology, src, dst, weigher, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700238 }
239
240 @Override
tomcfde0622014-09-09 11:02:42 -0700241 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900242 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700243 checkNotNull(topology, TOPOLOGY_NULL);
244 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700245 return store.isInfrastructure(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700246 }
247
248 @Override
tom4d0dd3a2014-09-14 23:12:28 -0700249 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900250 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700251 checkNotNull(topology, TOPOLOGY_NULL);
252 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700253 return store.isBroadcastPoint(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700254 }
255
tomcfde0622014-09-09 11:02:42 -0700256 // Personalized host provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700257 @Override
258 protected TopologyProviderService createProviderService(TopologyProvider provider) {
259 return new InternalTopologyProviderService(provider);
260 }
261
tomcfde0622014-09-09 11:02:42 -0700262 private class InternalTopologyProviderService
263 extends AbstractProviderService<TopologyProvider>
264 implements TopologyProviderService {
265
266 InternalTopologyProviderService(TopologyProvider provider) {
267 super(provider);
268 }
269
270 @Override
tom97937552014-09-11 10:48:42 -0700271 public void topologyChanged(GraphDescription topoDescription,
tomcfde0622014-09-09 11:02:42 -0700272 List<Event> reasons) {
273 checkNotNull(topoDescription, "Topology description cannot be null");
tomcbff9392014-09-10 00:45:23 -0700274
tome52ce702014-09-11 00:12:54 -0700275 TopologyEvent event = store.updateTopology(provider().id(),
276 topoDescription, reasons);
tomdc361b62014-09-09 20:36:52 -0700277 if (event != null) {
tom782a7cf2014-09-11 23:58:38 -0700278 log.info("Topology {} changed", event.subject());
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700279 post(event);
tomdc361b62014-09-09 20:36:52 -0700280 }
tomcfde0622014-09-09 11:02:42 -0700281 }
282 }
283
tomc78acee2014-09-24 15:16:55 -0700284 // Store delegate to re-post events emitted from the store.
285 private class InternalStoreDelegate implements TopologyStoreDelegate {
286 @Override
287 public void notify(TopologyEvent event) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700288 post(event);
tomc78acee2014-09-24 15:16:55 -0700289 }
290 }
tomcfde0622014-09-09 11:02:42 -0700291}