blob: 5d9c8decff2058b6a504f6ad040aee883c7e48c1 [file] [log] [blame]
tom202175a2014-09-19 19:00:11 -07001package org.onlab.onos.net.trivial.impl;
tom0efbb1d2014-09-09 11:54:28 -07002
tom10262dd2014-09-19 10:51:19 -07003import org.apache.felix.scr.annotations.Activate;
4import org.apache.felix.scr.annotations.Component;
5import org.apache.felix.scr.annotations.Deactivate;
6import org.apache.felix.scr.annotations.Service;
tomdc361b62014-09-09 20:36:52 -07007import org.onlab.onos.event.Event;
8import org.onlab.onos.net.ConnectPoint;
9import org.onlab.onos.net.DeviceId;
tom13cb4852014-09-11 12:44:17 -070010import org.onlab.onos.net.Link;
tomdc361b62014-09-09 20:36:52 -070011import org.onlab.onos.net.Path;
tome52ce702014-09-11 00:12:54 -070012import org.onlab.onos.net.provider.ProviderId;
tom13cb4852014-09-11 12:44:17 -070013import org.onlab.onos.net.topology.ClusterId;
tom97937552014-09-11 10:48:42 -070014import org.onlab.onos.net.topology.GraphDescription;
tomdc361b62014-09-09 20:36:52 -070015import org.onlab.onos.net.topology.LinkWeight;
tomdc361b62014-09-09 20:36:52 -070016import org.onlab.onos.net.topology.Topology;
17import org.onlab.onos.net.topology.TopologyCluster;
tomdc361b62014-09-09 20:36:52 -070018import org.onlab.onos.net.topology.TopologyEvent;
tom97937552014-09-11 10:48:42 -070019import org.onlab.onos.net.topology.TopologyGraph;
tom10262dd2014-09-19 10:51:19 -070020import org.onlab.onos.net.topology.TopologyStore;
21import org.slf4j.Logger;
tomdc361b62014-09-09 20:36:52 -070022
23import java.util.List;
24import java.util.Set;
25
tom10262dd2014-09-19 10:51:19 -070026import static org.slf4j.LoggerFactory.getLogger;
27
tom0efbb1d2014-09-09 11:54:28 -070028/**
29 * Manages inventory of topology snapshots using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070030 * structures implementation.
tom0efbb1d2014-09-09 11:54:28 -070031 */
tom10262dd2014-09-19 10:51:19 -070032@Component(immediate = true)
33@Service
34public class SimpleTopologyStore implements TopologyStore {
35
36 private final Logger log = getLogger(getClass());
tomdc361b62014-09-09 20:36:52 -070037
38 private volatile DefaultTopology current;
39
tom10262dd2014-09-19 10:51:19 -070040 @Activate
41 public void activate() {
42 log.info("Started");
43 }
44
45 @Deactivate
46 public void deactivate() {
47 log.info("Stopped");
48 }
49 @Override
50 public Topology currentTopology() {
tomdc361b62014-09-09 20:36:52 -070051 return current;
52 }
53
tom10262dd2014-09-19 10:51:19 -070054 @Override
55 public boolean isLatest(Topology topology) {
tomcbff9392014-09-10 00:45:23 -070056 // Topology is current only if it is the same as our current topology
tomdc361b62014-09-09 20:36:52 -070057 return topology == current;
58 }
59
tom10262dd2014-09-19 10:51:19 -070060 @Override
61 public TopologyGraph getGraph(Topology topology) {
62 return defaultTopology(topology).getGraph();
tom13cb4852014-09-11 12:44:17 -070063 }
64
tom10262dd2014-09-19 10:51:19 -070065 @Override
66 public Set<TopologyCluster> getClusters(Topology topology) {
67 return defaultTopology(topology).getClusters();
tomdc361b62014-09-09 20:36:52 -070068 }
69
tom10262dd2014-09-19 10:51:19 -070070 @Override
71 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
72 return defaultTopology(topology).getCluster(clusterId);
tom13cb4852014-09-11 12:44:17 -070073 }
74
tom10262dd2014-09-19 10:51:19 -070075 @Override
76 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
77 return defaultTopology(topology).getClusterDevices(cluster);
tom13cb4852014-09-11 12:44:17 -070078 }
79
tom10262dd2014-09-19 10:51:19 -070080 @Override
81 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
82 return defaultTopology(topology).getClusterLinks(cluster);
tomdc361b62014-09-09 20:36:52 -070083 }
84
tom10262dd2014-09-19 10:51:19 -070085 @Override
86 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
87 return defaultTopology(topology).getPaths(src, dst);
tomdc361b62014-09-09 20:36:52 -070088 }
89
tom10262dd2014-09-19 10:51:19 -070090 @Override
91 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
92 LinkWeight weight) {
93 return defaultTopology(topology).getPaths(src, dst, weight);
tomdc361b62014-09-09 20:36:52 -070094 }
95
tom10262dd2014-09-19 10:51:19 -070096 @Override
97 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
98 return defaultTopology(topology).isInfrastructure(connectPoint);
tomdc361b62014-09-09 20:36:52 -070099 }
100
tom10262dd2014-09-19 10:51:19 -0700101 @Override
102 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
103 return defaultTopology(topology).isBroadcastPoint(connectPoint);
tomdc361b62014-09-09 20:36:52 -0700104 }
105
tom10262dd2014-09-19 10:51:19 -0700106 @Override
107 public TopologyEvent updateTopology(ProviderId providerId,
108 GraphDescription graphDescription,
109 List<Event> reasons) {
tome52ce702014-09-11 00:12:54 -0700110 // First off, make sure that what we're given is indeed newer than
111 // what we already have.
tom97937552014-09-11 10:48:42 -0700112 if (current != null && graphDescription.timestamp() < current.time()) {
tome52ce702014-09-11 00:12:54 -0700113 return null;
114 }
115
116 // Have the default topology construct self from the description data.
117 DefaultTopology newTopology =
tom97937552014-09-11 10:48:42 -0700118 new DefaultTopology(providerId, graphDescription);
tome52ce702014-09-11 00:12:54 -0700119
120 // Promote the new topology to current and return a ready-to-send event.
tom97937552014-09-11 10:48:42 -0700121 synchronized (this) {
122 current = newTopology;
123 return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, current);
124 }
tomdc361b62014-09-09 20:36:52 -0700125 }
126
tom10262dd2014-09-19 10:51:19 -0700127 // Validates the specified topology and returns it as a default
128 private DefaultTopology defaultTopology(Topology topology) {
129 if (topology instanceof DefaultTopology) {
130 return (DefaultTopology) topology;
131 }
132 throw new IllegalArgumentException("Topology class " + topology.getClass() +
133 " not supported");
134 }
135
tom0efbb1d2014-09-09 11:54:28 -0700136}