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