alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.topology.impl; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.List; |
| 6 | import java.util.Set; |
| 7 | |
| 8 | import org.apache.felix.scr.annotations.Activate; |
| 9 | import org.apache.felix.scr.annotations.Component; |
| 10 | import org.apache.felix.scr.annotations.Deactivate; |
| 11 | import org.apache.felix.scr.annotations.Service; |
| 12 | import org.onlab.onos.event.Event; |
| 13 | import org.onlab.onos.net.ConnectPoint; |
| 14 | import org.onlab.onos.net.DeviceId; |
| 15 | import org.onlab.onos.net.Link; |
| 16 | import org.onlab.onos.net.Path; |
| 17 | import org.onlab.onos.net.provider.ProviderId; |
| 18 | import org.onlab.onos.net.topology.ClusterId; |
| 19 | import org.onlab.onos.net.topology.GraphDescription; |
| 20 | import org.onlab.onos.net.topology.LinkWeight; |
| 21 | import org.onlab.onos.net.topology.Topology; |
| 22 | import org.onlab.onos.net.topology.TopologyCluster; |
| 23 | import org.onlab.onos.net.topology.TopologyEvent; |
| 24 | import org.onlab.onos.net.topology.TopologyGraph; |
| 25 | import org.onlab.onos.net.topology.TopologyStore; |
| 26 | import org.onlab.onos.net.topology.TopologyStoreDelegate; |
| 27 | import org.onlab.onos.store.AbstractStore; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | /** |
| 31 | * Manages inventory of topology snapshots using trivial in-memory |
| 32 | * structures implementation. |
| 33 | */ |
| 34 | //FIXME: I LIE I AM NOT DISTRIBUTED |
| 35 | @Component(immediate = true) |
| 36 | @Service |
| 37 | public class DistributedTopologyStore |
| 38 | extends AbstractStore<TopologyEvent, TopologyStoreDelegate> |
| 39 | implements TopologyStore { |
| 40 | |
| 41 | private final Logger log = getLogger(getClass()); |
| 42 | |
| 43 | private volatile DefaultTopology current; |
| 44 | |
| 45 | @Activate |
| 46 | public void activate() { |
| 47 | log.info("Started"); |
| 48 | } |
| 49 | |
| 50 | @Deactivate |
| 51 | public void deactivate() { |
| 52 | log.info("Stopped"); |
| 53 | } |
| 54 | @Override |
| 55 | public Topology currentTopology() { |
| 56 | return current; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public boolean isLatest(Topology topology) { |
| 61 | // Topology is current only if it is the same as our current topology |
| 62 | return topology == current; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public TopologyGraph getGraph(Topology topology) { |
| 67 | return defaultTopology(topology).getGraph(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Set<TopologyCluster> getClusters(Topology topology) { |
| 72 | return defaultTopology(topology).getClusters(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public TopologyCluster getCluster(Topology topology, ClusterId clusterId) { |
| 77 | return defaultTopology(topology).getCluster(clusterId); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) { |
| 82 | return defaultTopology(topology).getClusterDevices(cluster); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) { |
| 87 | return defaultTopology(topology).getClusterLinks(cluster); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 92 | return defaultTopology(topology).getPaths(src, dst); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, |
| 97 | LinkWeight weight) { |
| 98 | return defaultTopology(topology).getPaths(src, dst, weight); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { |
| 103 | return defaultTopology(topology).isInfrastructure(connectPoint); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) { |
| 108 | return defaultTopology(topology).isBroadcastPoint(connectPoint); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public TopologyEvent updateTopology(ProviderId providerId, |
| 113 | GraphDescription graphDescription, |
| 114 | List<Event> reasons) { |
| 115 | // First off, make sure that what we're given is indeed newer than |
| 116 | // what we already have. |
| 117 | if (current != null && graphDescription.timestamp() < current.time()) { |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | // Have the default topology construct self from the description data. |
| 122 | DefaultTopology newTopology = |
| 123 | new DefaultTopology(providerId, graphDescription); |
| 124 | |
| 125 | // Promote the new topology to current and return a ready-to-send event. |
| 126 | synchronized (this) { |
| 127 | current = newTopology; |
| 128 | return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, current); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Validates the specified topology and returns it as a default |
| 133 | private DefaultTopology defaultTopology(Topology topology) { |
| 134 | if (topology instanceof DefaultTopology) { |
| 135 | return (DefaultTopology) topology; |
| 136 | } |
| 137 | throw new IllegalArgumentException("Topology class " + topology.getClass() + |
| 138 | " not supported"); |
| 139 | } |
| 140 | |
| 141 | } |