blob: 8c21730a804715dc6b12bcc27f743ecef21ee66e [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.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;
34import org.onosproject.net.topology.LinkWeight;
35import org.onosproject.net.topology.Topology;
36import org.onosproject.net.topology.TopologyCluster;
37import org.onosproject.net.topology.TopologyEvent;
38import org.onosproject.net.topology.TopologyGraph;
39import org.onosproject.net.topology.TopologyListener;
40import org.onosproject.net.topology.TopologyProvider;
41import org.onosproject.net.topology.TopologyProviderRegistry;
42import org.onosproject.net.topology.TopologyProviderService;
43import org.onosproject.net.topology.TopologyService;
44import org.onosproject.net.topology.TopologyStore;
45import org.onosproject.net.topology.TopologyStoreDelegate;
tomcfde0622014-09-09 11:02:42 -070046import org.slf4j.Logger;
47
48import java.util.List;
49import java.util.Set;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070050import java.util.Map;
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;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070054import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090055import static org.onosproject.security.AppPermission.Type.*;
56
tomcfde0622014-09-09 11:02:42 -070057
58/**
59 * Provides basic implementation of the topology SB & NB APIs.
60 */
61@Component(immediate = true)
62@Service
tom10262dd2014-09-19 10:51:19 -070063public class TopologyManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070064 extends AbstractListenerProviderRegistry<TopologyEvent, TopologyListener,
Thomas Vachuska48e64e42015-09-22 15:32:55 -070065 TopologyProvider, TopologyProviderService>
tomcfde0622014-09-09 11:02:42 -070066 implements TopologyService, TopologyProviderRegistry {
67
68 public static final String TOPOLOGY_NULL = "Topology cannot be null";
69 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
tom13cb4852014-09-11 12:44:17 -070070 private static final String CLUSTER_ID_NULL = "Cluster ID cannot be null";
71 private static final String CLUSTER_NULL = "Topology cluster cannot be null";
tomcfde0622014-09-09 11:02:42 -070072 public static final String CONNECTION_POINT_NULL = "Connection point cannot be null";
Thomas Vachuska48e64e42015-09-22 15:32:55 -070073 public static final String LINK_WEIGHT_NULL = "Link weight cannot be null";
tomcfde0622014-09-09 11:02:42 -070074
75 private final Logger log = getLogger(getClass());
76
tomc78acee2014-09-24 15:16:55 -070077 private TopologyStoreDelegate delegate = new InternalStoreDelegate();
78
tom10262dd2014-09-19 10:51:19 -070079 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected TopologyStore store;
tom0efbb1d2014-09-09 11:54:28 -070081
tomcfde0622014-09-09 11:02:42 -070082 @Activate
83 public void activate() {
tomc78acee2014-09-24 15:16:55 -070084 store.setDelegate(delegate);
tomcfde0622014-09-09 11:02:42 -070085 eventDispatcher.addSink(TopologyEvent.class, listenerRegistry);
86 log.info("Started");
87 }
88
89 @Deactivate
90 public void deactivate() {
tomc78acee2014-09-24 15:16:55 -070091 store.unsetDelegate(delegate);
tomcfde0622014-09-09 11:02:42 -070092 eventDispatcher.removeSink(TopologyEvent.class);
93 log.info("Stopped");
94 }
95
96 @Override
tomdc361b62014-09-09 20:36:52 -070097 public Topology currentTopology() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090098 checkPermission(TOPOLOGY_READ);
tomdc361b62014-09-09 20:36:52 -070099 return store.currentTopology();
tomcfde0622014-09-09 11:02:42 -0700100 }
101
102 @Override
tomdc361b62014-09-09 20:36:52 -0700103 public boolean isLatest(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900104 checkPermission(TOPOLOGY_READ);
tomdc361b62014-09-09 20:36:52 -0700105 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700106 return store.isLatest(topology);
tomcfde0622014-09-09 11:02:42 -0700107 }
108
109 @Override
110 public Set<TopologyCluster> getClusters(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900111 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700112 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700113 return store.getClusters(topology);
tomcfde0622014-09-09 11:02:42 -0700114 }
115
116 @Override
tom13cb4852014-09-11 12:44:17 -0700117 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900118 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700119 checkNotNull(topology, TOPOLOGY_NULL);
120 checkNotNull(topology, CLUSTER_ID_NULL);
tom10262dd2014-09-19 10:51:19 -0700121 return store.getCluster(topology, clusterId);
tom13cb4852014-09-11 12:44:17 -0700122 }
123
124 @Override
125 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900126 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700127 checkNotNull(topology, TOPOLOGY_NULL);
128 checkNotNull(topology, CLUSTER_NULL);
tom10262dd2014-09-19 10:51:19 -0700129 return store.getClusterDevices(topology, cluster);
tom13cb4852014-09-11 12:44:17 -0700130 }
131
132 @Override
133 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900134 checkPermission(TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700135 checkNotNull(topology, TOPOLOGY_NULL);
136 checkNotNull(topology, CLUSTER_NULL);
tom10262dd2014-09-19 10:51:19 -0700137 return store.getClusterLinks(topology, cluster);
tom13cb4852014-09-11 12:44:17 -0700138 }
139
140 @Override
tom97937552014-09-11 10:48:42 -0700141 public TopologyGraph getGraph(Topology topology) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900142 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700143 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700144 return store.getGraph(topology);
tomcfde0622014-09-09 11:02:42 -0700145 }
146
147 @Override
148 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900149 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700150 checkNotNull(topology, TOPOLOGY_NULL);
151 checkNotNull(src, DEVICE_ID_NULL);
152 checkNotNull(dst, DEVICE_ID_NULL);
tom10262dd2014-09-19 10:51:19 -0700153 return store.getPaths(topology, src, dst);
tomcfde0622014-09-09 11:02:42 -0700154 }
155
156 @Override
157 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
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);
163 checkNotNull(weight, "Link weight cannot be null");
tom10262dd2014-09-19 10:51:19 -0700164 return store.getPaths(topology, src, dst, weight);
tomcfde0622014-09-09 11:02:42 -0700165 }
166
167 @Override
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700168 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900169 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700170 checkNotNull(topology, TOPOLOGY_NULL);
171 checkNotNull(src, DEVICE_ID_NULL);
172 checkNotNull(dst, DEVICE_ID_NULL);
173 return store.getDisjointPaths(topology, src, dst);
174 }
175
176 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700177 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
178 DeviceId dst, LinkWeight weight) {
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);
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700183 checkNotNull(weight, LINK_WEIGHT_NULL);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700184 return store.getDisjointPaths(topology, src, dst, weight);
185 }
186
187 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700188 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
189 Map<Link, Object> riskProfile) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900190 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700191 checkNotNull(topology, TOPOLOGY_NULL);
192 checkNotNull(src, DEVICE_ID_NULL);
193 checkNotNull(dst, DEVICE_ID_NULL);
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700194 return store.getDisjointPaths(topology, src, dst, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700195 }
196
197 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700198 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
199 DeviceId dst, LinkWeight weight,
200 Map<Link, Object> riskProfile) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900201 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700202 checkNotNull(topology, TOPOLOGY_NULL);
203 checkNotNull(src, DEVICE_ID_NULL);
204 checkNotNull(dst, DEVICE_ID_NULL);
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700205 checkNotNull(weight, LINK_WEIGHT_NULL);
206 return store.getDisjointPaths(topology, src, dst, weight, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700207 }
208
209 @Override
tomcfde0622014-09-09 11:02:42 -0700210 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900211 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700212 checkNotNull(topology, TOPOLOGY_NULL);
213 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700214 return store.isInfrastructure(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700215 }
216
217 @Override
tom4d0dd3a2014-09-14 23:12:28 -0700218 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900219 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700220 checkNotNull(topology, TOPOLOGY_NULL);
221 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700222 return store.isBroadcastPoint(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700223 }
224
tomcfde0622014-09-09 11:02:42 -0700225 // Personalized host provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700226 @Override
227 protected TopologyProviderService createProviderService(TopologyProvider provider) {
228 return new InternalTopologyProviderService(provider);
229 }
230
tomcfde0622014-09-09 11:02:42 -0700231 private class InternalTopologyProviderService
232 extends AbstractProviderService<TopologyProvider>
233 implements TopologyProviderService {
234
235 InternalTopologyProviderService(TopologyProvider provider) {
236 super(provider);
237 }
238
239 @Override
tom97937552014-09-11 10:48:42 -0700240 public void topologyChanged(GraphDescription topoDescription,
tomcfde0622014-09-09 11:02:42 -0700241 List<Event> reasons) {
242 checkNotNull(topoDescription, "Topology description cannot be null");
tomcbff9392014-09-10 00:45:23 -0700243
tome52ce702014-09-11 00:12:54 -0700244 TopologyEvent event = store.updateTopology(provider().id(),
245 topoDescription, reasons);
tomdc361b62014-09-09 20:36:52 -0700246 if (event != null) {
tom782a7cf2014-09-11 23:58:38 -0700247 log.info("Topology {} changed", event.subject());
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700248 post(event);
tomdc361b62014-09-09 20:36:52 -0700249 }
tomcfde0622014-09-09 11:02:42 -0700250 }
251 }
252
tomc78acee2014-09-24 15:16:55 -0700253 // Store delegate to re-post events emitted from the store.
254 private class InternalStoreDelegate implements TopologyStoreDelegate {
255 @Override
256 public void notify(TopologyEvent event) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700257 post(event);
tomc78acee2014-09-24 15:16:55 -0700258 }
259 }
tomcfde0622014-09-09 11:02:42 -0700260}