Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 17 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 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.Service; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 22 | import org.onosproject.common.DefaultTopology; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.event.Event; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DeviceId; |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DisjointPath; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.Path; |
| 29 | import org.onosproject.net.provider.ProviderId; |
| 30 | import org.onosproject.net.topology.ClusterId; |
| 31 | import org.onosproject.net.topology.GraphDescription; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 32 | import org.onosproject.net.topology.LinkWeigher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.topology.LinkWeight; |
| 34 | import org.onosproject.net.topology.Topology; |
| 35 | import org.onosproject.net.topology.TopologyCluster; |
| 36 | import org.onosproject.net.topology.TopologyEvent; |
| 37 | import org.onosproject.net.topology.TopologyGraph; |
| 38 | import org.onosproject.net.topology.TopologyStore; |
| 39 | import org.onosproject.net.topology.TopologyStoreDelegate; |
| 40 | import org.onosproject.store.AbstractStore; |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 41 | import org.slf4j.Logger; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 42 | |
| 43 | import java.util.List; |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 44 | import java.util.Map; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 45 | import java.util.Set; |
| 46 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 47 | import static org.onosproject.net.topology.AdapterLinkWeigher.adapt; |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Manages inventory of topology snapshots using trivial in-memory |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 52 | * structures implementation. |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 53 | */ |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 54 | @Component(immediate = true) |
| 55 | @Service |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 56 | public class SimpleTopologyStore |
| 57 | extends AbstractStore<TopologyEvent, TopologyStoreDelegate> |
| 58 | implements TopologyStore { |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 59 | |
| 60 | private final Logger log = getLogger(getClass()); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 61 | |
| 62 | private volatile DefaultTopology current; |
| 63 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 64 | @Activate |
| 65 | public void activate() { |
| 66 | log.info("Started"); |
| 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
| 71 | log.info("Stopped"); |
| 72 | } |
| 73 | @Override |
| 74 | public Topology currentTopology() { |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 75 | return current; |
| 76 | } |
| 77 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 78 | @Override |
| 79 | public boolean isLatest(Topology topology) { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 80 | // Topology is current only if it is the same as our current topology |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 81 | return topology == current; |
| 82 | } |
| 83 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 84 | @Override |
| 85 | public TopologyGraph getGraph(Topology topology) { |
| 86 | return defaultTopology(topology).getGraph(); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 87 | } |
| 88 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public Set<TopologyCluster> getClusters(Topology topology) { |
| 91 | return defaultTopology(topology).getClusters(); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 92 | } |
| 93 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public TopologyCluster getCluster(Topology topology, ClusterId clusterId) { |
| 96 | return defaultTopology(topology).getCluster(clusterId); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 97 | } |
| 98 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 99 | @Override |
| 100 | public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) { |
| 101 | return defaultTopology(topology).getClusterDevices(cluster); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 102 | } |
| 103 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 104 | @Override |
| 105 | public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) { |
| 106 | return defaultTopology(topology).getClusterLinks(cluster); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 107 | } |
| 108 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 109 | @Override |
| 110 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 111 | return defaultTopology(topology).getPaths(src, dst); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 112 | } |
| 113 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, |
| 116 | LinkWeight weight) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 117 | return getPaths(topology, src, dst, adapt(weight)); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Set<Path> getPaths(Topology topology, DeviceId src, |
| 122 | DeviceId dst, LinkWeigher weigher) { |
| 123 | return defaultTopology(topology).getPaths(src, dst, weigher); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 124 | } |
| 125 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 126 | @Override |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 127 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 128 | return defaultTopology(topology).getDisjointPaths(src, dst); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, |
| 133 | LinkWeight weight) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 134 | return getDisjointPaths(topology, src, dst, adapt(weight)); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, |
| 139 | DeviceId dst, LinkWeigher weigher) { |
| 140 | return defaultTopology(topology).getDisjointPaths(src, dst, weigher); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 144 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 145 | Map<Link, Object> riskProfile) { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 146 | return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 150 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 151 | LinkWeight weight, Map<Link, Object> riskProfile) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 152 | return getDisjointPaths(topology, src, dst, adapt(weight), riskProfile); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, |
| 157 | DeviceId dst, |
| 158 | LinkWeigher weigher, |
| 159 | Map<Link, Object> riskProfile) { |
| 160 | return defaultTopology(topology).getDisjointPaths(src, dst, weigher, riskProfile); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 164 | public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { |
| 165 | return defaultTopology(topology).isInfrastructure(connectPoint); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 166 | } |
| 167 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) { |
| 170 | return defaultTopology(topology).isBroadcastPoint(connectPoint); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 171 | } |
| 172 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 173 | @Override |
| 174 | public TopologyEvent updateTopology(ProviderId providerId, |
| 175 | GraphDescription graphDescription, |
| 176 | List<Event> reasons) { |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 177 | // First off, make sure that what we're given is indeed newer than |
| 178 | // what we already have. |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 179 | if (current != null && graphDescription.timestamp() < current.time()) { |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 180 | return null; |
| 181 | } |
| 182 | |
| 183 | // Have the default topology construct self from the description data. |
| 184 | DefaultTopology newTopology = |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 185 | new DefaultTopology(providerId, graphDescription); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 186 | |
| 187 | // Promote the new topology to current and return a ready-to-send event. |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 188 | synchronized (this) { |
| 189 | current = newTopology; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 190 | return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, |
| 191 | current, reasons); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 192 | } |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 193 | } |
| 194 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 195 | // Validates the specified topology and returns it as a default |
| 196 | private DefaultTopology defaultTopology(Topology topology) { |
| 197 | if (topology instanceof DefaultTopology) { |
| 198 | return (DefaultTopology) topology; |
| 199 | } |
| 200 | throw new IllegalArgumentException("Topology class " + topology.getClass() + |
| 201 | " not supported"); |
| 202 | } |
| 203 | |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 204 | } |