blob: 48a8d6526f7687d07c874b142b1bba32b30511fe [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.topology.impl;
tomcfde0622014-09-09 11:02:42 -070017
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070024import org.onosproject.net.DisjointPath;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070025import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.event.Event;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Link;
30import org.onosproject.net.Path;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.provider.AbstractProviderService;
32import org.onosproject.net.topology.ClusterId;
33import org.onosproject.net.topology.GraphDescription;
Andrey Komarov2398d962016-09-26 15:11:23 +030034import org.onosproject.net.topology.LinkWeigher;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.topology.LinkWeight;
36import org.onosproject.net.topology.Topology;
37import org.onosproject.net.topology.TopologyCluster;
38import org.onosproject.net.topology.TopologyEvent;
39import org.onosproject.net.topology.TopologyGraph;
40import org.onosproject.net.topology.TopologyListener;
41import org.onosproject.net.topology.TopologyProvider;
42import org.onosproject.net.topology.TopologyProviderRegistry;
43import org.onosproject.net.topology.TopologyProviderService;
44import org.onosproject.net.topology.TopologyService;
45import org.onosproject.net.topology.TopologyStore;
46import org.onosproject.net.topology.TopologyStoreDelegate;
tomcfde0622014-09-09 11:02:42 -070047import org.slf4j.Logger;
48
49import java.util.List;
50import java.util.Set;
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -070051import java.util.stream.Stream;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070052import java.util.Map;
tomcfde0622014-09-09 11:02:42 -070053
54import static com.google.common.base.Preconditions.checkNotNull;
Andrey Komarov2398d962016-09-26 15:11:23 +030055import static org.onosproject.net.topology.AdapterLinkWeigher.adapt;
Changhoon Yoon541ef712015-05-23 17:18:34 +090056import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070057import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090058import static org.onosproject.security.AppPermission.Type.*;
59
tomcfde0622014-09-09 11:02:42 -070060
61/**
62 * Provides basic implementation of the topology SB & NB APIs.
63 */
64@Component(immediate = true)
65@Service
tom10262dd2014-09-19 10:51:19 -070066public class TopologyManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070067 extends AbstractListenerProviderRegistry<TopologyEvent, TopologyListener,
Thomas Vachuska48e64e42015-09-22 15:32:55 -070068 TopologyProvider, TopologyProviderService>
tomcfde0622014-09-09 11:02:42 -070069 implements TopologyService, TopologyProviderRegistry {
70
Simon Hunt11a29282016-03-31 09:16:47 -070071 private static final String TOPOLOGY_NULL = "Topology cannot be null";
tomcfde0622014-09-09 11:02:42 -070072 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
tom13cb4852014-09-11 12:44:17 -070073 private static final String CLUSTER_ID_NULL = "Cluster ID cannot be null";
74 private static final String CLUSTER_NULL = "Topology cluster cannot be null";
Simon Hunt11a29282016-03-31 09:16:47 -070075 private static final String CONNECTION_POINT_NULL = "Connection point cannot be null";
76 private static final String LINK_WEIGHT_NULL = "Link weight cannot be null";
tomcfde0622014-09-09 11:02:42 -070077
78 private final Logger log = getLogger(getClass());
79
tomc78acee2014-09-24 15:16:55 -070080 private TopologyStoreDelegate delegate = new InternalStoreDelegate();
81
tom10262dd2014-09-19 10:51:19 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected TopologyStore store;
tom0efbb1d2014-09-09 11:54:28 -070084
tomcfde0622014-09-09 11:02:42 -070085 @Activate
86 public void activate() {
tomc78acee2014-09-24 15:16:55 -070087 store.setDelegate(delegate);
tomcfde0622014-09-09 11:02:42 -070088 eventDispatcher.addSink(TopologyEvent.class, listenerRegistry);
89 log.info("Started");
90 }
91
92 @Deactivate
93 public void deactivate() {
tomc78acee2014-09-24 15:16:55 -070094 store.unsetDelegate(delegate);
tomcfde0622014-09-09 11:02:42 -070095 eventDispatcher.removeSink(TopologyEvent.class);
96 log.info("Stopped");
97 }
98
99 @Override
tomdc361b62014-09-09 20:36:52 -0700100 public Topology currentTopology() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900101 checkPermission(TOPOLOGY_READ);
tomdc361b62014-09-09 20:36:52 -0700102 return store.currentTopology();
tomcfde0622014-09-09 11:02:42 -0700103 }
104
105 @Override
tomdc361b62014-09-09 20:36:52 -0700106 public boolean isLatest(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900107 checkPermission(TOPOLOGY_READ);
tomdc361b62014-09-09 20:36:52 -0700108 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700109 return store.isLatest(topology);
tomcfde0622014-09-09 11:02:42 -0700110 }
111
112 @Override
113 public Set<TopologyCluster> getClusters(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900114 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700115 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700116 return store.getClusters(topology);
tomcfde0622014-09-09 11:02:42 -0700117 }
118
119 @Override
tom13cb4852014-09-11 12:44:17 -0700120 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900121 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700122 checkNotNull(topology, TOPOLOGY_NULL);
123 checkNotNull(topology, CLUSTER_ID_NULL);
tom10262dd2014-09-19 10:51:19 -0700124 return store.getCluster(topology, clusterId);
tom13cb4852014-09-11 12:44:17 -0700125 }
126
127 @Override
128 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900129 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700130 checkNotNull(topology, TOPOLOGY_NULL);
131 checkNotNull(topology, CLUSTER_NULL);
tom10262dd2014-09-19 10:51:19 -0700132 return store.getClusterDevices(topology, cluster);
tom13cb4852014-09-11 12:44:17 -0700133 }
134
135 @Override
136 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900137 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700138 checkNotNull(topology, TOPOLOGY_NULL);
139 checkNotNull(topology, CLUSTER_NULL);
tom10262dd2014-09-19 10:51:19 -0700140 return store.getClusterLinks(topology, cluster);
tom13cb4852014-09-11 12:44:17 -0700141 }
142
143 @Override
tom97937552014-09-11 10:48:42 -0700144 public TopologyGraph getGraph(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900145 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700146 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700147 return store.getGraph(topology);
tomcfde0622014-09-09 11:02:42 -0700148 }
149
150 @Override
151 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900152 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700153 checkNotNull(topology, TOPOLOGY_NULL);
154 checkNotNull(src, DEVICE_ID_NULL);
155 checkNotNull(dst, DEVICE_ID_NULL);
tom10262dd2014-09-19 10:51:19 -0700156 return store.getPaths(topology, src, dst);
tomcfde0622014-09-09 11:02:42 -0700157 }
158
159 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300160 public Set<Path> getPaths(Topology topology, DeviceId src,
161 DeviceId dst, LinkWeight weight) {
162 return getPaths(topology, src, dst, adapt(weight));
163 }
164
165 @Override
166 public Set<Path> getPaths(Topology topology, DeviceId src,
167 DeviceId dst, LinkWeigher weigher) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900168 checkPermission(TOPOLOGY_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900169
tomcfde0622014-09-09 11:02:42 -0700170 checkNotNull(topology, TOPOLOGY_NULL);
171 checkNotNull(src, DEVICE_ID_NULL);
172 checkNotNull(dst, DEVICE_ID_NULL);
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -0700173 checkNotNull(weigher, LINK_WEIGHT_NULL);
Andrey Komarov2398d962016-09-26 15:11:23 +0300174 return store.getPaths(topology, src, dst, weigher);
tomcfde0622014-09-09 11:02:42 -0700175 }
176
177 @Override
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -0700178 public Set<Path> getKShortestPaths(Topology topology, DeviceId src,
179 DeviceId dst, LinkWeigher weigher,
180 int maxPaths) {
181 checkPermission(TOPOLOGY_READ);
182
183 checkNotNull(topology, TOPOLOGY_NULL);
184 checkNotNull(src, DEVICE_ID_NULL);
185 checkNotNull(dst, DEVICE_ID_NULL);
186 checkNotNull(weigher, LINK_WEIGHT_NULL);
187 return store.getKShortestPaths(topology, src, dst, weigher, maxPaths);
188 }
189
190 @Override
191 public Stream<Path> getKShortestPaths(Topology topology,
192 DeviceId src,
193 DeviceId dst,
194 LinkWeigher weigher) {
195 checkPermission(TOPOLOGY_READ);
196
197 checkNotNull(topology, TOPOLOGY_NULL);
198 checkNotNull(src, DEVICE_ID_NULL);
199 checkNotNull(dst, DEVICE_ID_NULL);
200 checkNotNull(weigher, LINK_WEIGHT_NULL);
201 return store.getKShortestPaths(topology, src, dst, weigher);
202 }
203
204 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300205 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
206 DeviceId dst) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900207 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700208 checkNotNull(topology, TOPOLOGY_NULL);
209 checkNotNull(src, DEVICE_ID_NULL);
210 checkNotNull(dst, DEVICE_ID_NULL);
211 return store.getDisjointPaths(topology, src, dst);
212 }
213
214 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700215 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
Andrey Komarov2398d962016-09-26 15:11:23 +0300216 DeviceId dst,
217 LinkWeight weight) {
218 return getDisjointPaths(topology, src, dst, adapt(weight));
219 }
220
221 @Override
222 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
223 DeviceId dst,
224 LinkWeigher weigher) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900225 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700226 checkNotNull(topology, TOPOLOGY_NULL);
227 checkNotNull(src, DEVICE_ID_NULL);
228 checkNotNull(dst, DEVICE_ID_NULL);
Andrey Komarov2398d962016-09-26 15:11:23 +0300229 checkNotNull(weigher, LINK_WEIGHT_NULL);
230 return store.getDisjointPaths(topology, src, dst, weigher);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700231 }
232
233 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300234 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
235 DeviceId dst,
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700236 Map<Link, Object> riskProfile) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900237 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700238 checkNotNull(topology, TOPOLOGY_NULL);
239 checkNotNull(src, DEVICE_ID_NULL);
240 checkNotNull(dst, DEVICE_ID_NULL);
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700241 return store.getDisjointPaths(topology, src, dst, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700242 }
243
244 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700245 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
246 DeviceId dst, LinkWeight weight,
247 Map<Link, Object> riskProfile) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300248 return getDisjointPaths(topology, src, dst, adapt(weight), riskProfile);
249 }
250
251 @Override
252 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
253 DeviceId dst,
254 LinkWeigher weigher,
255 Map<Link, Object> riskProfile) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900256 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700257 checkNotNull(topology, TOPOLOGY_NULL);
258 checkNotNull(src, DEVICE_ID_NULL);
259 checkNotNull(dst, DEVICE_ID_NULL);
Andrey Komarov2398d962016-09-26 15:11:23 +0300260 checkNotNull(weigher, LINK_WEIGHT_NULL);
261 return store.getDisjointPaths(topology, src, dst, weigher, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700262 }
263
264 @Override
tomcfde0622014-09-09 11:02:42 -0700265 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900266 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700267 checkNotNull(topology, TOPOLOGY_NULL);
268 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700269 return store.isInfrastructure(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700270 }
271
272 @Override
tom4d0dd3a2014-09-14 23:12:28 -0700273 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900274 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700275 checkNotNull(topology, TOPOLOGY_NULL);
276 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700277 return store.isBroadcastPoint(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700278 }
279
tomcfde0622014-09-09 11:02:42 -0700280 // Personalized host provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700281 @Override
282 protected TopologyProviderService createProviderService(TopologyProvider provider) {
283 return new InternalTopologyProviderService(provider);
284 }
285
tomcfde0622014-09-09 11:02:42 -0700286 private class InternalTopologyProviderService
287 extends AbstractProviderService<TopologyProvider>
288 implements TopologyProviderService {
289
290 InternalTopologyProviderService(TopologyProvider provider) {
291 super(provider);
292 }
293
294 @Override
tom97937552014-09-11 10:48:42 -0700295 public void topologyChanged(GraphDescription topoDescription,
tomcfde0622014-09-09 11:02:42 -0700296 List<Event> reasons) {
297 checkNotNull(topoDescription, "Topology description cannot be null");
tomcbff9392014-09-10 00:45:23 -0700298
tome52ce702014-09-11 00:12:54 -0700299 TopologyEvent event = store.updateTopology(provider().id(),
300 topoDescription, reasons);
tomdc361b62014-09-09 20:36:52 -0700301 if (event != null) {
tom782a7cf2014-09-11 23:58:38 -0700302 log.info("Topology {} changed", event.subject());
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700303 post(event);
tomdc361b62014-09-09 20:36:52 -0700304 }
tomcfde0622014-09-09 11:02:42 -0700305 }
306 }
307
tomc78acee2014-09-24 15:16:55 -0700308 // Store delegate to re-post events emitted from the store.
309 private class InternalStoreDelegate implements TopologyStoreDelegate {
310 @Override
311 public void notify(TopologyEvent event) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700312 post(event);
tomc78acee2014-09-24 15:16:55 -0700313 }
314 }
tomcfde0622014-09-09 11:02:42 -0700315}