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