Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 32 | import org.onosproject.net.topology.LinkWeight; |
| 33 | import org.onosproject.net.topology.Topology; |
| 34 | import org.onosproject.net.topology.TopologyCluster; |
| 35 | import org.onosproject.net.topology.TopologyEvent; |
| 36 | import org.onosproject.net.topology.TopologyGraph; |
| 37 | import org.onosproject.net.topology.TopologyStore; |
| 38 | import org.onosproject.net.topology.TopologyStoreDelegate; |
| 39 | import org.onosproject.store.AbstractStore; |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 41 | |
| 42 | import java.util.List; |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 43 | import java.util.Map; |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 44 | import java.util.Set; |
| 45 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Manages inventory of topology snapshots using trivial in-memory |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 50 | * structures implementation. |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 51 | */ |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 52 | @Component(immediate = true) |
| 53 | @Service |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 54 | public class SimpleTopologyStore |
| 55 | extends AbstractStore<TopologyEvent, TopologyStoreDelegate> |
| 56 | implements TopologyStore { |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 59 | |
| 60 | private volatile DefaultTopology current; |
| 61 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 62 | @Activate |
| 63 | public void activate() { |
| 64 | log.info("Started"); |
| 65 | } |
| 66 | |
| 67 | @Deactivate |
| 68 | public void deactivate() { |
| 69 | log.info("Stopped"); |
| 70 | } |
| 71 | @Override |
| 72 | public Topology currentTopology() { |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 73 | return current; |
| 74 | } |
| 75 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 76 | @Override |
| 77 | public boolean isLatest(Topology topology) { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 78 | // Topology is current only if it is the same as our current topology |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 79 | return topology == current; |
| 80 | } |
| 81 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 82 | @Override |
| 83 | public TopologyGraph getGraph(Topology topology) { |
| 84 | return defaultTopology(topology).getGraph(); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 85 | } |
| 86 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public Set<TopologyCluster> getClusters(Topology topology) { |
| 89 | return defaultTopology(topology).getClusters(); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 90 | } |
| 91 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 92 | @Override |
| 93 | public TopologyCluster getCluster(Topology topology, ClusterId clusterId) { |
| 94 | return defaultTopology(topology).getCluster(clusterId); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 95 | } |
| 96 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 97 | @Override |
| 98 | public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) { |
| 99 | return defaultTopology(topology).getClusterDevices(cluster); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 100 | } |
| 101 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 102 | @Override |
| 103 | public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) { |
| 104 | return defaultTopology(topology).getClusterLinks(cluster); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 105 | } |
| 106 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 109 | return defaultTopology(topology).getPaths(src, dst); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 110 | } |
| 111 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 112 | @Override |
| 113 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, |
| 114 | LinkWeight weight) { |
| 115 | return defaultTopology(topology).getPaths(src, dst, weight); |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 116 | } |
| 117 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 118 | @Override |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 119 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 120 | return defaultTopology(topology).getDisjointPaths(src, dst); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, |
| 125 | LinkWeight weight) { |
| 126 | return defaultTopology(topology).getDisjointPaths(src, dst, weight); |
| 127 | } |
| 128 | |
| 129 | @Override |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 130 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 131 | Map<Link, Object> riskProfile) { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 132 | return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 136 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 137 | LinkWeight weight, Map<Link, Object> riskProfile) { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 138 | return defaultTopology(topology).getDisjointPaths(src, dst, weight, 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 | } |