blob: 7302b3c36250860e802f2a3d2c1331aa40770902 [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,
65 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";
73
74 private final Logger log = getLogger(getClass());
75
tomc78acee2014-09-24 15:16:55 -070076 private TopologyStoreDelegate delegate = new InternalStoreDelegate();
77
tom10262dd2014-09-19 10:51:19 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 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);
119 checkNotNull(topology, 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);
127 checkNotNull(topology, 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);
135 checkNotNull(topology, 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
156 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900157 checkPermission(TOPOLOGY_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900158
tomcfde0622014-09-09 11:02:42 -0700159 checkNotNull(topology, TOPOLOGY_NULL);
160 checkNotNull(src, DEVICE_ID_NULL);
161 checkNotNull(dst, DEVICE_ID_NULL);
162 checkNotNull(weight, "Link weight cannot be null");
tom10262dd2014-09-19 10:51:19 -0700163 return store.getPaths(topology, src, dst, weight);
tomcfde0622014-09-09 11:02:42 -0700164 }
165
166 @Override
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700167 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) {
168 checkNotNull(topology, TOPOLOGY_NULL);
169 checkNotNull(src, DEVICE_ID_NULL);
170 checkNotNull(dst, DEVICE_ID_NULL);
171 return store.getDisjointPaths(topology, src, dst);
172 }
173
174 @Override
175 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
176 checkNotNull(topology, TOPOLOGY_NULL);
177 checkNotNull(src, DEVICE_ID_NULL);
178 checkNotNull(dst, DEVICE_ID_NULL);
179 checkNotNull(weight, "Link weight cannot be null");
180 return store.getDisjointPaths(topology, src, dst, weight);
181 }
182
183 @Override
184 public Set<DisjointPath> getSRLGDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
185 Map<Link, Object> riskProfile) {
186 checkNotNull(topology, TOPOLOGY_NULL);
187 checkNotNull(src, DEVICE_ID_NULL);
188 checkNotNull(dst, DEVICE_ID_NULL);
189 return store.getSRLGDisjointPaths(topology, src, dst, riskProfile);
190 }
191
192 @Override
193 public Set<DisjointPath> getSRLGDisjointPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight,
194 Map<Link, Object> riskProfile) {
195 checkNotNull(topology, TOPOLOGY_NULL);
196 checkNotNull(src, DEVICE_ID_NULL);
197 checkNotNull(dst, DEVICE_ID_NULL);
198 checkNotNull(weight, "Link weight cannot be null");
199 return store.getSRLGDisjointPaths(topology, src, dst, weight, riskProfile);
200 }
201
202 @Override
tomcfde0622014-09-09 11:02:42 -0700203 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900204 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700205 checkNotNull(topology, TOPOLOGY_NULL);
206 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700207 return store.isInfrastructure(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700208 }
209
210 @Override
tom4d0dd3a2014-09-14 23:12:28 -0700211 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900212 checkPermission(TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700213 checkNotNull(topology, TOPOLOGY_NULL);
214 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700215 return store.isBroadcastPoint(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700216 }
217
tomcfde0622014-09-09 11:02:42 -0700218 // Personalized host provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700219 @Override
220 protected TopologyProviderService createProviderService(TopologyProvider provider) {
221 return new InternalTopologyProviderService(provider);
222 }
223
tomcfde0622014-09-09 11:02:42 -0700224 private class InternalTopologyProviderService
225 extends AbstractProviderService<TopologyProvider>
226 implements TopologyProviderService {
227
228 InternalTopologyProviderService(TopologyProvider provider) {
229 super(provider);
230 }
231
232 @Override
tom97937552014-09-11 10:48:42 -0700233 public void topologyChanged(GraphDescription topoDescription,
tomcfde0622014-09-09 11:02:42 -0700234 List<Event> reasons) {
235 checkNotNull(topoDescription, "Topology description cannot be null");
tomcbff9392014-09-10 00:45:23 -0700236
tome52ce702014-09-11 00:12:54 -0700237 TopologyEvent event = store.updateTopology(provider().id(),
238 topoDescription, reasons);
tomdc361b62014-09-09 20:36:52 -0700239 if (event != null) {
tom782a7cf2014-09-11 23:58:38 -0700240 log.info("Topology {} changed", event.subject());
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700241 post(event);
tomdc361b62014-09-09 20:36:52 -0700242 }
tomcfde0622014-09-09 11:02:42 -0700243 }
244 }
245
tomc78acee2014-09-24 15:16:55 -0700246 // Store delegate to re-post events emitted from the store.
247 private class InternalStoreDelegate implements TopologyStoreDelegate {
248 @Override
249 public void notify(TopologyEvent event) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700250 post(event);
tomc78acee2014-09-24 15:16:55 -0700251 }
252 }
tomcfde0622014-09-09 11:02:42 -0700253}