blob: ecf7e25cab0d2b36c9c4e42d3d528f4af28530f4 [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;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070051import java.util.Map;
tomcfde0622014-09-09 11:02:42 -070052
53import static com.google.common.base.Preconditions.checkNotNull;
Andrey Komarov2398d962016-09-26 15:11:23 +030054import static org.onosproject.net.topology.AdapterLinkWeigher.adapt;
Changhoon Yoon541ef712015-05-23 17:18:34 +090055import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070056import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090057import static org.onosproject.security.AppPermission.Type.*;
58
tomcfde0622014-09-09 11:02:42 -070059
60/**
61 * Provides basic implementation of the topology SB & NB APIs.
62 */
63@Component(immediate = true)
64@Service
tom10262dd2014-09-19 10:51:19 -070065public class TopologyManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070066 extends AbstractListenerProviderRegistry<TopologyEvent, TopologyListener,
Thomas Vachuska48e64e42015-09-22 15:32:55 -070067 TopologyProvider, TopologyProviderService>
tomcfde0622014-09-09 11:02:42 -070068 implements TopologyService, TopologyProviderRegistry {
69
Simon Hunt11a29282016-03-31 09:16:47 -070070 private static final String TOPOLOGY_NULL = "Topology cannot be null";
tomcfde0622014-09-09 11:02:42 -070071 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
tom13cb4852014-09-11 12:44:17 -070072 private static final String CLUSTER_ID_NULL = "Cluster ID cannot be null";
73 private static final String CLUSTER_NULL = "Topology cluster cannot be null";
Simon Hunt11a29282016-03-31 09:16:47 -070074 private static final String CONNECTION_POINT_NULL = "Connection point cannot be null";
75 private static final String LINK_WEIGHT_NULL = "Link weight cannot be null";
tomcfde0622014-09-09 11:02:42 -070076
77 private final Logger log = getLogger(getClass());
78
tomc78acee2014-09-24 15:16:55 -070079 private TopologyStoreDelegate delegate = new InternalStoreDelegate();
80
tom10262dd2014-09-19 10:51:19 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected TopologyStore store;
tom0efbb1d2014-09-09 11:54:28 -070083
tomcfde0622014-09-09 11:02:42 -070084 @Activate
85 public void activate() {
tomc78acee2014-09-24 15:16:55 -070086 store.setDelegate(delegate);
tomcfde0622014-09-09 11:02:42 -070087 eventDispatcher.addSink(TopologyEvent.class, listenerRegistry);
88 log.info("Started");
89 }
90
91 @Deactivate
92 public void deactivate() {
tomc78acee2014-09-24 15:16:55 -070093 store.unsetDelegate(delegate);
tomcfde0622014-09-09 11:02:42 -070094 eventDispatcher.removeSink(TopologyEvent.class);
95 log.info("Stopped");
96 }
97
98 @Override
tomdc361b62014-09-09 20:36:52 -070099 public Topology currentTopology() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900100 checkPermission(TOPOLOGY_READ);
tomdc361b62014-09-09 20:36:52 -0700101 return store.currentTopology();
tomcfde0622014-09-09 11:02:42 -0700102 }
103
104 @Override
tomdc361b62014-09-09 20:36:52 -0700105 public boolean isLatest(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900106 checkPermission(TOPOLOGY_READ);
tomdc361b62014-09-09 20:36:52 -0700107 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700108 return store.isLatest(topology);
tomcfde0622014-09-09 11:02:42 -0700109 }
110
111 @Override
112 public Set<TopologyCluster> getClusters(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900113 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700114 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700115 return store.getClusters(topology);
tomcfde0622014-09-09 11:02:42 -0700116 }
117
118 @Override
tom13cb4852014-09-11 12:44:17 -0700119 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900120 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700121 checkNotNull(topology, TOPOLOGY_NULL);
122 checkNotNull(topology, CLUSTER_ID_NULL);
tom10262dd2014-09-19 10:51:19 -0700123 return store.getCluster(topology, clusterId);
tom13cb4852014-09-11 12:44:17 -0700124 }
125
126 @Override
127 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900128 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700129 checkNotNull(topology, TOPOLOGY_NULL);
130 checkNotNull(topology, CLUSTER_NULL);
tom10262dd2014-09-19 10:51:19 -0700131 return store.getClusterDevices(topology, cluster);
tom13cb4852014-09-11 12:44:17 -0700132 }
133
134 @Override
135 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900136 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700137 checkNotNull(topology, TOPOLOGY_NULL);
138 checkNotNull(topology, CLUSTER_NULL);
tom10262dd2014-09-19 10:51:19 -0700139 return store.getClusterLinks(topology, cluster);
tom13cb4852014-09-11 12:44:17 -0700140 }
141
142 @Override
tom97937552014-09-11 10:48:42 -0700143 public TopologyGraph getGraph(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900144 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700145 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700146 return store.getGraph(topology);
tomcfde0622014-09-09 11:02:42 -0700147 }
148
149 @Override
150 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900151 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700152 checkNotNull(topology, TOPOLOGY_NULL);
153 checkNotNull(src, DEVICE_ID_NULL);
154 checkNotNull(dst, DEVICE_ID_NULL);
tom10262dd2014-09-19 10:51:19 -0700155 return store.getPaths(topology, src, dst);
tomcfde0622014-09-09 11:02:42 -0700156 }
157
158 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300159 public Set<Path> getPaths(Topology topology, DeviceId src,
160 DeviceId dst, LinkWeight weight) {
161 return getPaths(topology, src, dst, adapt(weight));
162 }
163
164 @Override
165 public Set<Path> getPaths(Topology topology, DeviceId src,
166 DeviceId dst, LinkWeigher weigher) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900167 checkPermission(TOPOLOGY_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900168
tomcfde0622014-09-09 11:02:42 -0700169 checkNotNull(topology, TOPOLOGY_NULL);
170 checkNotNull(src, DEVICE_ID_NULL);
171 checkNotNull(dst, DEVICE_ID_NULL);
Andrey Komarov2398d962016-09-26 15:11:23 +0300172 checkNotNull(weigher, "Link weight cannot be null");
173 return store.getPaths(topology, src, dst, weigher);
tomcfde0622014-09-09 11:02:42 -0700174 }
175
176 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300177 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
178 DeviceId dst) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900179 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700180 checkNotNull(topology, TOPOLOGY_NULL);
181 checkNotNull(src, DEVICE_ID_NULL);
182 checkNotNull(dst, DEVICE_ID_NULL);
183 return store.getDisjointPaths(topology, src, dst);
184 }
185
186 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700187 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
Andrey Komarov2398d962016-09-26 15:11:23 +0300188 DeviceId dst,
189 LinkWeight weight) {
190 return getDisjointPaths(topology, src, dst, adapt(weight));
191 }
192
193 @Override
194 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
195 DeviceId dst,
196 LinkWeigher weigher) {
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);
Andrey Komarov2398d962016-09-26 15:11:23 +0300201 checkNotNull(weigher, LINK_WEIGHT_NULL);
202 return store.getDisjointPaths(topology, src, dst, weigher);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700203 }
204
205 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300206 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
207 DeviceId dst,
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700208 Map<Link, Object> riskProfile) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900209 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700210 checkNotNull(topology, TOPOLOGY_NULL);
211 checkNotNull(src, DEVICE_ID_NULL);
212 checkNotNull(dst, DEVICE_ID_NULL);
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700213 return store.getDisjointPaths(topology, src, dst, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700214 }
215
216 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700217 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
218 DeviceId dst, LinkWeight weight,
219 Map<Link, Object> riskProfile) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300220 return getDisjointPaths(topology, src, dst, adapt(weight), riskProfile);
221 }
222
223 @Override
224 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
225 DeviceId dst,
226 LinkWeigher weigher,
227 Map<Link, Object> riskProfile) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900228 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700229 checkNotNull(topology, TOPOLOGY_NULL);
230 checkNotNull(src, DEVICE_ID_NULL);
231 checkNotNull(dst, DEVICE_ID_NULL);
Andrey Komarov2398d962016-09-26 15:11:23 +0300232 checkNotNull(weigher, LINK_WEIGHT_NULL);
233 return store.getDisjointPaths(topology, src, dst, weigher, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700234 }
235
236 @Override
tomcfde0622014-09-09 11:02:42 -0700237 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900238 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700239 checkNotNull(topology, TOPOLOGY_NULL);
240 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700241 return store.isInfrastructure(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700242 }
243
244 @Override
tom4d0dd3a2014-09-14 23:12:28 -0700245 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900246 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700247 checkNotNull(topology, TOPOLOGY_NULL);
248 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700249 return store.isBroadcastPoint(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700250 }
251
tomcfde0622014-09-09 11:02:42 -0700252 // Personalized host provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700253 @Override
254 protected TopologyProviderService createProviderService(TopologyProvider provider) {
255 return new InternalTopologyProviderService(provider);
256 }
257
tomcfde0622014-09-09 11:02:42 -0700258 private class InternalTopologyProviderService
259 extends AbstractProviderService<TopologyProvider>
260 implements TopologyProviderService {
261
262 InternalTopologyProviderService(TopologyProvider provider) {
263 super(provider);
264 }
265
266 @Override
tom97937552014-09-11 10:48:42 -0700267 public void topologyChanged(GraphDescription topoDescription,
tomcfde0622014-09-09 11:02:42 -0700268 List<Event> reasons) {
269 checkNotNull(topoDescription, "Topology description cannot be null");
tomcbff9392014-09-10 00:45:23 -0700270
tome52ce702014-09-11 00:12:54 -0700271 TopologyEvent event = store.updateTopology(provider().id(),
272 topoDescription, reasons);
tomdc361b62014-09-09 20:36:52 -0700273 if (event != null) {
tom782a7cf2014-09-11 23:58:38 -0700274 log.info("Topology {} changed", event.subject());
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700275 post(event);
tomdc361b62014-09-09 20:36:52 -0700276 }
tomcfde0622014-09-09 11:02:42 -0700277 }
278 }
279
tomc78acee2014-09-24 15:16:55 -0700280 // Store delegate to re-post events emitted from the store.
281 private class InternalStoreDelegate implements TopologyStoreDelegate {
282 @Override
283 public void notify(TopologyEvent event) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700284 post(event);
tomc78acee2014-09-24 15:16:55 -0700285 }
286 }
tomcfde0622014-09-09 11:02:42 -0700287}