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