blob: 8030869c24dcf8c96d565016c054416d09dd7f9b [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
tom0efbb1d2014-09-09 11:54:28 -070017
tom10262dd2014-09-19 10:51:19 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Service;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070022import org.onosproject.common.DefaultTopology;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.event.Event;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DeviceId;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070026import org.onosproject.net.DisjointPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Link;
28import org.onosproject.net.Path;
29import org.onosproject.net.provider.ProviderId;
30import org.onosproject.net.topology.ClusterId;
31import org.onosproject.net.topology.GraphDescription;
Andrey Komarov2398d962016-09-26 15:11:23 +030032import org.onosproject.net.topology.LinkWeigher;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.topology.LinkWeight;
34import org.onosproject.net.topology.Topology;
35import org.onosproject.net.topology.TopologyCluster;
36import org.onosproject.net.topology.TopologyEvent;
37import org.onosproject.net.topology.TopologyGraph;
38import org.onosproject.net.topology.TopologyStore;
39import org.onosproject.net.topology.TopologyStoreDelegate;
40import org.onosproject.store.AbstractStore;
tom10262dd2014-09-19 10:51:19 -070041import org.slf4j.Logger;
tomdc361b62014-09-09 20:36:52 -070042
43import java.util.List;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070044import java.util.Map;
tomdc361b62014-09-09 20:36:52 -070045import java.util.Set;
46
Andrey Komarov2398d962016-09-26 15:11:23 +030047import static org.onosproject.net.topology.AdapterLinkWeigher.adapt;
tom10262dd2014-09-19 10:51:19 -070048import static org.slf4j.LoggerFactory.getLogger;
49
tom0efbb1d2014-09-09 11:54:28 -070050/**
51 * Manages inventory of topology snapshots using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070052 * structures implementation.
tom0efbb1d2014-09-09 11:54:28 -070053 */
tom10262dd2014-09-19 10:51:19 -070054@Component(immediate = true)
55@Service
tomc78acee2014-09-24 15:16:55 -070056public class SimpleTopologyStore
57 extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
58 implements TopologyStore {
tom10262dd2014-09-19 10:51:19 -070059
60 private final Logger log = getLogger(getClass());
tomdc361b62014-09-09 20:36:52 -070061
62 private volatile DefaultTopology current;
63
tom10262dd2014-09-19 10:51:19 -070064 @Activate
65 public void activate() {
66 log.info("Started");
67 }
68
69 @Deactivate
70 public void deactivate() {
71 log.info("Stopped");
72 }
73 @Override
74 public Topology currentTopology() {
tomdc361b62014-09-09 20:36:52 -070075 return current;
76 }
77
tom10262dd2014-09-19 10:51:19 -070078 @Override
79 public boolean isLatest(Topology topology) {
tomcbff9392014-09-10 00:45:23 -070080 // Topology is current only if it is the same as our current topology
tomdc361b62014-09-09 20:36:52 -070081 return topology == current;
82 }
83
tom10262dd2014-09-19 10:51:19 -070084 @Override
85 public TopologyGraph getGraph(Topology topology) {
86 return defaultTopology(topology).getGraph();
tom13cb4852014-09-11 12:44:17 -070087 }
88
tom10262dd2014-09-19 10:51:19 -070089 @Override
90 public Set<TopologyCluster> getClusters(Topology topology) {
91 return defaultTopology(topology).getClusters();
tomdc361b62014-09-09 20:36:52 -070092 }
93
tom10262dd2014-09-19 10:51:19 -070094 @Override
95 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
96 return defaultTopology(topology).getCluster(clusterId);
tom13cb4852014-09-11 12:44:17 -070097 }
98
tom10262dd2014-09-19 10:51:19 -070099 @Override
100 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
101 return defaultTopology(topology).getClusterDevices(cluster);
tom13cb4852014-09-11 12:44:17 -0700102 }
103
tom10262dd2014-09-19 10:51:19 -0700104 @Override
105 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
106 return defaultTopology(topology).getClusterLinks(cluster);
tomdc361b62014-09-09 20:36:52 -0700107 }
108
tom10262dd2014-09-19 10:51:19 -0700109 @Override
110 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
111 return defaultTopology(topology).getPaths(src, dst);
tomdc361b62014-09-09 20:36:52 -0700112 }
113
tom10262dd2014-09-19 10:51:19 -0700114 @Override
115 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
116 LinkWeight weight) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300117 return getPaths(topology, src, dst, adapt(weight));
118 }
119
120 @Override
121 public Set<Path> getPaths(Topology topology, DeviceId src,
122 DeviceId dst, LinkWeigher weigher) {
123 return defaultTopology(topology).getPaths(src, dst, weigher);
tomdc361b62014-09-09 20:36:52 -0700124 }
125
tom10262dd2014-09-19 10:51:19 -0700126 @Override
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700127 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) {
128 return defaultTopology(topology).getDisjointPaths(src, dst);
129 }
130
131 @Override
132 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
133 LinkWeight weight) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300134 return getDisjointPaths(topology, src, dst, adapt(weight));
135 }
136
137 @Override
138 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
139 DeviceId dst, LinkWeigher weigher) {
140 return defaultTopology(topology).getDisjointPaths(src, dst, weigher);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700141 }
142
143 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700144 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700145 Map<Link, Object> riskProfile) {
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700146 return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700147 }
148
149 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700150 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700151 LinkWeight weight, Map<Link, Object> riskProfile) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300152 return getDisjointPaths(topology, src, dst, adapt(weight), riskProfile);
153 }
154
155 @Override
156 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
157 DeviceId dst,
158 LinkWeigher weigher,
159 Map<Link, Object> riskProfile) {
160 return defaultTopology(topology).getDisjointPaths(src, dst, weigher, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700161 }
162
163 @Override
tom10262dd2014-09-19 10:51:19 -0700164 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
165 return defaultTopology(topology).isInfrastructure(connectPoint);
tomdc361b62014-09-09 20:36:52 -0700166 }
167
tom10262dd2014-09-19 10:51:19 -0700168 @Override
169 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
170 return defaultTopology(topology).isBroadcastPoint(connectPoint);
tomdc361b62014-09-09 20:36:52 -0700171 }
172
tom10262dd2014-09-19 10:51:19 -0700173 @Override
174 public TopologyEvent updateTopology(ProviderId providerId,
175 GraphDescription graphDescription,
176 List<Event> reasons) {
tome52ce702014-09-11 00:12:54 -0700177 // First off, make sure that what we're given is indeed newer than
178 // what we already have.
tom97937552014-09-11 10:48:42 -0700179 if (current != null && graphDescription.timestamp() < current.time()) {
tome52ce702014-09-11 00:12:54 -0700180 return null;
181 }
182
183 // Have the default topology construct self from the description data.
184 DefaultTopology newTopology =
tom97937552014-09-11 10:48:42 -0700185 new DefaultTopology(providerId, graphDescription);
tome52ce702014-09-11 00:12:54 -0700186
187 // Promote the new topology to current and return a ready-to-send event.
tom97937552014-09-11 10:48:42 -0700188 synchronized (this) {
189 current = newTopology;
tom95329eb2014-10-06 08:40:06 -0700190 return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED,
191 current, reasons);
tom97937552014-09-11 10:48:42 -0700192 }
tomdc361b62014-09-09 20:36:52 -0700193 }
194
tom10262dd2014-09-19 10:51:19 -0700195 // Validates the specified topology and returns it as a default
196 private DefaultTopology defaultTopology(Topology topology) {
197 if (topology instanceof DefaultTopology) {
198 return (DefaultTopology) topology;
199 }
200 throw new IllegalArgumentException("Topology class " + topology.getClass() +
201 " not supported");
202 }
203
tom0efbb1d2014-09-09 11:54:28 -0700204}