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