blob: b167dbc3f04e46cec06b83b978db01414b3420d1 [file] [log] [blame]
tom8bf2e6b2014-09-10 20:53:54 -07001package org.onlab.onos.net.trivial.topology.impl;
tom0efbb1d2014-09-09 11:54:28 -07002
tomdc361b62014-09-09 20:36:52 -07003import org.onlab.onos.event.Event;
4import org.onlab.onos.net.ConnectPoint;
5import org.onlab.onos.net.DeviceId;
6import org.onlab.onos.net.Path;
tome52ce702014-09-11 00:12:54 -07007import org.onlab.onos.net.provider.ProviderId;
tom97937552014-09-11 10:48:42 -07008import org.onlab.onos.net.topology.GraphDescription;
tomdc361b62014-09-09 20:36:52 -07009import org.onlab.onos.net.topology.LinkWeight;
tomdc361b62014-09-09 20:36:52 -070010import org.onlab.onos.net.topology.Topology;
11import org.onlab.onos.net.topology.TopologyCluster;
tomdc361b62014-09-09 20:36:52 -070012import org.onlab.onos.net.topology.TopologyEvent;
tom97937552014-09-11 10:48:42 -070013import org.onlab.onos.net.topology.TopologyGraph;
tomdc361b62014-09-09 20:36:52 -070014
15import java.util.List;
16import java.util.Set;
17
tom0efbb1d2014-09-09 11:54:28 -070018/**
19 * Manages inventory of topology snapshots using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070020 * structures implementation.
tom0efbb1d2014-09-09 11:54:28 -070021 */
tomcbff9392014-09-10 00:45:23 -070022class SimpleTopologyStore {
tomdc361b62014-09-09 20:36:52 -070023
24 private volatile DefaultTopology current;
25
26 /**
27 * Returns the current topology snapshot.
28 *
29 * @return current topology descriptor
30 */
31 Topology currentTopology() {
32 return current;
33 }
34
35 /**
36 * Indicates whether the topology is the latest.
tomcbff9392014-09-10 00:45:23 -070037 *
tomdc361b62014-09-09 20:36:52 -070038 * @param topology topology descriptor
39 * @return true if topology is the most recent one
40 */
41 boolean isLatest(Topology topology) {
tomcbff9392014-09-10 00:45:23 -070042 // Topology is current only if it is the same as our current topology
tomdc361b62014-09-09 20:36:52 -070043 return topology == current;
44 }
45
46 /**
47 * Returns the set of topology SCC clusters.
48 *
49 * @param topology topology descriptor
50 * @return set of clusters
51 */
tome52ce702014-09-11 00:12:54 -070052 Set<TopologyCluster> getClusters(DefaultTopology topology) {
53 return topology.getClusters();
tomdc361b62014-09-09 20:36:52 -070054 }
55
56 /**
57 * Returns the immutable graph view of the current topology.
58 *
59 * @param topology topology descriptor
60 * @return graph view
61 */
tom97937552014-09-11 10:48:42 -070062 TopologyGraph getGraph(DefaultTopology topology) {
tome52ce702014-09-11 00:12:54 -070063 return topology.getGraph();
tomdc361b62014-09-09 20:36:52 -070064 }
65
66 /**
67 * Returns the set of pre-computed shortest paths between src and dest.
68 *
69 * @param topology topology descriptor
70 * @param src source device
71 * @param dst destination device
72 * @return set of shortest paths
73 */
tome52ce702014-09-11 00:12:54 -070074 Set<Path> getPaths(DefaultTopology topology, DeviceId src, DeviceId dst) {
75 return topology.getPaths(src, dst);
tomdc361b62014-09-09 20:36:52 -070076 }
77
78 /**
79 * Computes and returns the set of shortest paths between src and dest.
80 *
81 * @param topology topology descriptor
82 * @param src source device
83 * @param dst destination device
84 * @param weight link weight function
85 * @return set of shortest paths
86 */
tome52ce702014-09-11 00:12:54 -070087 Set<Path> getPaths(DefaultTopology topology, DeviceId src, DeviceId dst,
tomdc361b62014-09-09 20:36:52 -070088 LinkWeight weight) {
tom97937552014-09-11 10:48:42 -070089 return topology.getPaths(src, dst, weight);
tomdc361b62014-09-09 20:36:52 -070090 }
91
92 /**
93 * Indicates whether the given connect point is part of the network fabric.
94 *
95 * @param topology topology descriptor
96 * @param connectPoint connection point
97 * @return true if infrastructure; false otherwise
98 */
tome52ce702014-09-11 00:12:54 -070099 boolean isInfrastructure(DefaultTopology topology, ConnectPoint connectPoint) {
100 return topology.isInfrastructure(connectPoint);
tomdc361b62014-09-09 20:36:52 -0700101 }
102
103 /**
104 * Indicates whether the given connect point is part of the broadcast tree.
105 *
106 * @param topology topology descriptor
107 * @param connectPoint connection point
108 * @return true if in broadcast tree; false otherwise
109 */
tome52ce702014-09-11 00:12:54 -0700110 boolean isInBroadcastTree(DefaultTopology topology, ConnectPoint connectPoint) {
111 return topology.isInBroadcastTree(connectPoint);
tomdc361b62014-09-09 20:36:52 -0700112 }
113
114 /**
115 * Generates a new topology snapshot from the specified description.
116 *
tom97937552014-09-11 10:48:42 -0700117 * @param providerId provider identification
118 * @param graphDescription topology graph description
119 * @param reasons list of events that triggered the update
tomdc361b62014-09-09 20:36:52 -0700120 * @return topology update event or null if the description is old
121 */
tome52ce702014-09-11 00:12:54 -0700122 TopologyEvent updateTopology(ProviderId providerId,
tom97937552014-09-11 10:48:42 -0700123 GraphDescription graphDescription,
tomcbff9392014-09-10 00:45:23 -0700124 List<Event> reasons) {
tome52ce702014-09-11 00:12:54 -0700125 // First off, make sure that what we're given is indeed newer than
126 // what we already have.
tom97937552014-09-11 10:48:42 -0700127 if (current != null && graphDescription.timestamp() < current.time()) {
tome52ce702014-09-11 00:12:54 -0700128 return null;
129 }
130
131 // Have the default topology construct self from the description data.
132 DefaultTopology newTopology =
tom97937552014-09-11 10:48:42 -0700133 new DefaultTopology(providerId, graphDescription);
tome52ce702014-09-11 00:12:54 -0700134
135 // Promote the new topology to current and return a ready-to-send event.
tom97937552014-09-11 10:48:42 -0700136 synchronized (this) {
137 current = newTopology;
138 return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, current);
139 }
tomdc361b62014-09-09 20:36:52 -0700140 }
141
tom0efbb1d2014-09-09 11:54:28 -0700142}