blob: 0a0db62ff83d4037eb81d7882fe0ed3dab171b9e [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;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070024import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Changhoon Yoon541ef712015-05-23 17:18:34 +090025import org.onosproject.core.Permission;
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;
50
51import static com.google.common.base.Preconditions.checkNotNull;
Changhoon Yoon541ef712015-05-23 17:18:34 +090052import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070053import static org.slf4j.LoggerFactory.getLogger;
tomcfde0622014-09-09 11:02:42 -070054
55/**
56 * Provides basic implementation of the topology SB & NB APIs.
57 */
58@Component(immediate = true)
59@Service
tom10262dd2014-09-19 10:51:19 -070060public class TopologyManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070061 extends AbstractListenerProviderRegistry<TopologyEvent, TopologyListener,
62 TopologyProvider, TopologyProviderService>
tomcfde0622014-09-09 11:02:42 -070063 implements TopologyService, TopologyProviderRegistry {
64
65 public static final String TOPOLOGY_NULL = "Topology cannot be null";
66 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
tom13cb4852014-09-11 12:44:17 -070067 private static final String CLUSTER_ID_NULL = "Cluster ID cannot be null";
68 private static final String CLUSTER_NULL = "Topology cluster cannot be null";
tomcfde0622014-09-09 11:02:42 -070069 public static final String CONNECTION_POINT_NULL = "Connection point cannot be null";
70
71 private final Logger log = getLogger(getClass());
72
tomc78acee2014-09-24 15:16:55 -070073 private TopologyStoreDelegate delegate = new InternalStoreDelegate();
74
tom10262dd2014-09-19 10:51:19 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected TopologyStore store;
tom0efbb1d2014-09-09 11:54:28 -070077
tomcfde0622014-09-09 11:02:42 -070078 @Activate
79 public void activate() {
tomc78acee2014-09-24 15:16:55 -070080 store.setDelegate(delegate);
tomcfde0622014-09-09 11:02:42 -070081 eventDispatcher.addSink(TopologyEvent.class, listenerRegistry);
82 log.info("Started");
83 }
84
85 @Deactivate
86 public void deactivate() {
tomc78acee2014-09-24 15:16:55 -070087 store.unsetDelegate(delegate);
tomcfde0622014-09-09 11:02:42 -070088 eventDispatcher.removeSink(TopologyEvent.class);
89 log.info("Stopped");
90 }
91
92 @Override
tomdc361b62014-09-09 20:36:52 -070093 public Topology currentTopology() {
Changhoon Yoon541ef712015-05-23 17:18:34 +090094 checkPermission(Permission.TOPOLOGY_READ);
tomdc361b62014-09-09 20:36:52 -070095 return store.currentTopology();
tomcfde0622014-09-09 11:02:42 -070096 }
97
98 @Override
tomdc361b62014-09-09 20:36:52 -070099 public boolean isLatest(Topology topology) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900100 checkPermission(Permission.TOPOLOGY_READ);
tomdc361b62014-09-09 20:36:52 -0700101 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700102 return store.isLatest(topology);
tomcfde0622014-09-09 11:02:42 -0700103 }
104
105 @Override
106 public Set<TopologyCluster> getClusters(Topology topology) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900107 checkPermission(Permission.TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700108 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700109 return store.getClusters(topology);
tomcfde0622014-09-09 11:02:42 -0700110 }
111
112 @Override
tom13cb4852014-09-11 12:44:17 -0700113 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900114 checkPermission(Permission.TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700115 checkNotNull(topology, TOPOLOGY_NULL);
116 checkNotNull(topology, CLUSTER_ID_NULL);
tom10262dd2014-09-19 10:51:19 -0700117 return store.getCluster(topology, clusterId);
tom13cb4852014-09-11 12:44:17 -0700118 }
119
120 @Override
121 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900122 checkPermission(Permission.TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700123 checkNotNull(topology, TOPOLOGY_NULL);
124 checkNotNull(topology, CLUSTER_NULL);
tom10262dd2014-09-19 10:51:19 -0700125 return store.getClusterDevices(topology, cluster);
tom13cb4852014-09-11 12:44:17 -0700126 }
127
128 @Override
129 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900130 checkPermission(Permission.TOPOLOGY_READ);
tom13cb4852014-09-11 12:44:17 -0700131 checkNotNull(topology, TOPOLOGY_NULL);
132 checkNotNull(topology, CLUSTER_NULL);
tom10262dd2014-09-19 10:51:19 -0700133 return store.getClusterLinks(topology, cluster);
tom13cb4852014-09-11 12:44:17 -0700134 }
135
136 @Override
tom97937552014-09-11 10:48:42 -0700137 public TopologyGraph getGraph(Topology topology) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900138 checkPermission(Permission.TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700139 checkNotNull(topology, TOPOLOGY_NULL);
tom10262dd2014-09-19 10:51:19 -0700140 return store.getGraph(topology);
tomcfde0622014-09-09 11:02:42 -0700141 }
142
143 @Override
144 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900145 checkPermission(Permission.TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700146 checkNotNull(topology, TOPOLOGY_NULL);
147 checkNotNull(src, DEVICE_ID_NULL);
148 checkNotNull(dst, DEVICE_ID_NULL);
tom10262dd2014-09-19 10:51:19 -0700149 return store.getPaths(topology, src, dst);
tomcfde0622014-09-09 11:02:42 -0700150 }
151
152 @Override
153 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900154 checkPermission(Permission.TOPOLOGY_READ);
155
tomcfde0622014-09-09 11:02:42 -0700156 checkNotNull(topology, TOPOLOGY_NULL);
157 checkNotNull(src, DEVICE_ID_NULL);
158 checkNotNull(dst, DEVICE_ID_NULL);
159 checkNotNull(weight, "Link weight cannot be null");
tom10262dd2014-09-19 10:51:19 -0700160 return store.getPaths(topology, src, dst, weight);
tomcfde0622014-09-09 11:02:42 -0700161 }
162
163 @Override
164 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900165 checkPermission(Permission.TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700166 checkNotNull(topology, TOPOLOGY_NULL);
167 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700168 return store.isInfrastructure(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700169 }
170
171 @Override
tom4d0dd3a2014-09-14 23:12:28 -0700172 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900173 checkPermission(Permission.TOPOLOGY_READ);
tomcfde0622014-09-09 11:02:42 -0700174 checkNotNull(topology, TOPOLOGY_NULL);
175 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
tom10262dd2014-09-19 10:51:19 -0700176 return store.isBroadcastPoint(topology, connectPoint);
tomcfde0622014-09-09 11:02:42 -0700177 }
178
tomcfde0622014-09-09 11:02:42 -0700179 // Personalized host provider service issued to the supplied provider.
tomdc361b62014-09-09 20:36:52 -0700180 @Override
181 protected TopologyProviderService createProviderService(TopologyProvider provider) {
182 return new InternalTopologyProviderService(provider);
183 }
184
tomcfde0622014-09-09 11:02:42 -0700185 private class InternalTopologyProviderService
186 extends AbstractProviderService<TopologyProvider>
187 implements TopologyProviderService {
188
189 InternalTopologyProviderService(TopologyProvider provider) {
190 super(provider);
191 }
192
193 @Override
tom97937552014-09-11 10:48:42 -0700194 public void topologyChanged(GraphDescription topoDescription,
tomcfde0622014-09-09 11:02:42 -0700195 List<Event> reasons) {
196 checkNotNull(topoDescription, "Topology description cannot be null");
tomcbff9392014-09-10 00:45:23 -0700197
tome52ce702014-09-11 00:12:54 -0700198 TopologyEvent event = store.updateTopology(provider().id(),
199 topoDescription, reasons);
tomdc361b62014-09-09 20:36:52 -0700200 if (event != null) {
tom782a7cf2014-09-11 23:58:38 -0700201 log.info("Topology {} changed", event.subject());
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700202 post(event);
tomdc361b62014-09-09 20:36:52 -0700203 }
tomcfde0622014-09-09 11:02:42 -0700204 }
205 }
206
tomc78acee2014-09-24 15:16:55 -0700207 // Store delegate to re-post events emitted from the store.
208 private class InternalStoreDelegate implements TopologyStoreDelegate {
209 @Override
210 public void notify(TopologyEvent event) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700211 post(event);
tomc78acee2014-09-24 15:16:55 -0700212 }
213 }
tomcfde0622014-09-09 11:02:42 -0700214}