Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.topology.impl; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 24 | import org.onosproject.core.Permission; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 25 | import org.onosproject.event.ListenerRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.event.Event; |
| 27 | import org.onosproject.event.EventDeliveryService; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.Path; |
| 32 | import org.onosproject.net.provider.AbstractProviderRegistry; |
| 33 | import org.onosproject.net.provider.AbstractProviderService; |
| 34 | import org.onosproject.net.topology.ClusterId; |
| 35 | import org.onosproject.net.topology.GraphDescription; |
| 36 | import org.onosproject.net.topology.LinkWeight; |
| 37 | import org.onosproject.net.topology.Topology; |
| 38 | import org.onosproject.net.topology.TopologyCluster; |
| 39 | import org.onosproject.net.topology.TopologyEvent; |
| 40 | import org.onosproject.net.topology.TopologyGraph; |
| 41 | import org.onosproject.net.topology.TopologyListener; |
| 42 | import org.onosproject.net.topology.TopologyProvider; |
| 43 | import org.onosproject.net.topology.TopologyProviderRegistry; |
| 44 | import org.onosproject.net.topology.TopologyProviderService; |
| 45 | import org.onosproject.net.topology.TopologyService; |
| 46 | import org.onosproject.net.topology.TopologyStore; |
| 47 | import org.onosproject.net.topology.TopologyStoreDelegate; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | |
| 50 | import java.util.List; |
| 51 | import java.util.Set; |
| 52 | |
| 53 | import static com.google.common.base.Preconditions.checkNotNull; |
| 54 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 55 | import static org.onosproject.security.AppGuard.checkPermission; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Provides basic implementation of the topology SB & NB APIs. |
| 59 | */ |
| 60 | @Component(immediate = true) |
| 61 | @Service |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 62 | public class TopologyManager |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 63 | extends AbstractProviderRegistry<TopologyProvider, TopologyProviderService> |
| 64 | implements TopologyService, TopologyProviderRegistry { |
| 65 | |
| 66 | public static final String TOPOLOGY_NULL = "Topology cannot be null"; |
| 67 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 68 | private static final String CLUSTER_ID_NULL = "Cluster ID cannot be null"; |
| 69 | private static final String CLUSTER_NULL = "Topology cluster cannot be null"; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 70 | public static final String CONNECTION_POINT_NULL = "Connection point cannot be null"; |
| 71 | |
| 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 74 | private final ListenerRegistry<TopologyEvent, TopologyListener> |
| 75 | listenerRegistry = new ListenerRegistry<>(); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 76 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 77 | private TopologyStoreDelegate delegate = new InternalStoreDelegate(); |
| 78 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected TopologyStore store; |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 81 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 83 | protected EventDeliveryService eventDispatcher; |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 84 | |
| 85 | |
| 86 | @Activate |
| 87 | public void activate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 88 | store.setDelegate(delegate); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 89 | eventDispatcher.addSink(TopologyEvent.class, listenerRegistry); |
| 90 | log.info("Started"); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | public void deactivate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 95 | store.unsetDelegate(delegate); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 96 | eventDispatcher.removeSink(TopologyEvent.class); |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
| 100 | @Override |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 101 | public Topology currentTopology() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 102 | checkPermission(Permission.TOPOLOGY_READ); |
| 103 | |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 104 | return store.currentTopology(); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 108 | public boolean isLatest(Topology topology) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 109 | checkPermission(Permission.TOPOLOGY_READ); |
| 110 | |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 111 | checkNotNull(topology, TOPOLOGY_NULL); |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 112 | return store.isLatest(topology); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Set<TopologyCluster> getClusters(Topology topology) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 117 | checkPermission(Permission.TOPOLOGY_READ); |
| 118 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 119 | checkNotNull(topology, TOPOLOGY_NULL); |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 120 | return store.getClusters(topology); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 124 | public TopologyCluster getCluster(Topology topology, ClusterId clusterId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 125 | checkPermission(Permission.TOPOLOGY_READ); |
| 126 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 127 | checkNotNull(topology, TOPOLOGY_NULL); |
| 128 | checkNotNull(topology, CLUSTER_ID_NULL); |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 129 | return store.getCluster(topology, clusterId); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 134 | checkPermission(Permission.TOPOLOGY_READ); |
| 135 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 136 | checkNotNull(topology, TOPOLOGY_NULL); |
| 137 | checkNotNull(topology, CLUSTER_NULL); |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 138 | return store.getClusterDevices(topology, cluster); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 143 | checkPermission(Permission.TOPOLOGY_READ); |
| 144 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 145 | checkNotNull(topology, TOPOLOGY_NULL); |
| 146 | checkNotNull(topology, CLUSTER_NULL); |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 147 | return store.getClusterLinks(topology, cluster); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 151 | public TopologyGraph getGraph(Topology topology) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 152 | checkPermission(Permission.TOPOLOGY_READ); |
| 153 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 154 | checkNotNull(topology, TOPOLOGY_NULL); |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 155 | return store.getGraph(topology); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 160 | checkPermission(Permission.TOPOLOGY_READ); |
| 161 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 162 | checkNotNull(topology, TOPOLOGY_NULL); |
| 163 | checkNotNull(src, DEVICE_ID_NULL); |
| 164 | checkNotNull(dst, DEVICE_ID_NULL); |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 165 | return store.getPaths(topology, src, dst); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 170 | checkPermission(Permission.TOPOLOGY_READ); |
| 171 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 172 | checkNotNull(topology, TOPOLOGY_NULL); |
| 173 | checkNotNull(src, DEVICE_ID_NULL); |
| 174 | checkNotNull(dst, DEVICE_ID_NULL); |
| 175 | checkNotNull(weight, "Link weight cannot be null"); |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 176 | return store.getPaths(topology, src, dst, weight); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 181 | checkPermission(Permission.TOPOLOGY_READ); |
| 182 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 183 | checkNotNull(topology, TOPOLOGY_NULL); |
| 184 | checkNotNull(connectPoint, CONNECTION_POINT_NULL); |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 185 | return store.isInfrastructure(topology, connectPoint); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 189 | public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 190 | checkPermission(Permission.TOPOLOGY_READ); |
| 191 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 192 | checkNotNull(topology, TOPOLOGY_NULL); |
| 193 | checkNotNull(connectPoint, CONNECTION_POINT_NULL); |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 194 | return store.isBroadcastPoint(topology, connectPoint); |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void addListener(TopologyListener listener) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 199 | checkPermission(Permission.TOPOLOGY_EVENT); |
| 200 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 201 | listenerRegistry.addListener(listener); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void removeListener(TopologyListener listener) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame^] | 206 | checkPermission(Permission.TOPOLOGY_EVENT); |
| 207 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 208 | listenerRegistry.removeListener(listener); |
| 209 | } |
| 210 | |
| 211 | // Personalized host provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 212 | @Override |
| 213 | protected TopologyProviderService createProviderService(TopologyProvider provider) { |
| 214 | return new InternalTopologyProviderService(provider); |
| 215 | } |
| 216 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 217 | private class InternalTopologyProviderService |
| 218 | extends AbstractProviderService<TopologyProvider> |
| 219 | implements TopologyProviderService { |
| 220 | |
| 221 | InternalTopologyProviderService(TopologyProvider provider) { |
| 222 | super(provider); |
| 223 | } |
| 224 | |
| 225 | @Override |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 226 | public void topologyChanged(GraphDescription topoDescription, |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 227 | List<Event> reasons) { |
| 228 | checkNotNull(topoDescription, "Topology description cannot be null"); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 229 | |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 230 | TopologyEvent event = store.updateTopology(provider().id(), |
| 231 | topoDescription, reasons); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 232 | if (event != null) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 233 | log.info("Topology {} changed", event.subject()); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 234 | eventDispatcher.post(event); |
| 235 | } |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 239 | // Store delegate to re-post events emitted from the store. |
| 240 | private class InternalStoreDelegate implements TopologyStoreDelegate { |
| 241 | @Override |
| 242 | public void notify(TopologyEvent event) { |
| 243 | eventDispatcher.post(event); |
| 244 | } |
| 245 | } |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 246 | } |