blob: 6a89c0190295222e7cb3a6a62c5062fa1504397f [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 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;
26import org.onosproject.net.Link;
27import org.onosproject.net.Path;
28import org.onosproject.net.provider.ProviderId;
29import org.onosproject.net.topology.ClusterId;
30import org.onosproject.net.topology.GraphDescription;
31import org.onosproject.net.topology.LinkWeight;
32import org.onosproject.net.topology.Topology;
33import org.onosproject.net.topology.TopologyCluster;
34import org.onosproject.net.topology.TopologyEvent;
35import org.onosproject.net.topology.TopologyGraph;
36import org.onosproject.net.topology.TopologyStore;
37import org.onosproject.net.topology.TopologyStoreDelegate;
38import org.onosproject.store.AbstractStore;
tom10262dd2014-09-19 10:51:19 -070039import org.slf4j.Logger;
tomdc361b62014-09-09 20:36:52 -070040
41import java.util.List;
42import java.util.Set;
43
tom10262dd2014-09-19 10:51:19 -070044import static org.slf4j.LoggerFactory.getLogger;
45
tom0efbb1d2014-09-09 11:54:28 -070046/**
47 * Manages inventory of topology snapshots using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070048 * structures implementation.
tom0efbb1d2014-09-09 11:54:28 -070049 */
tom10262dd2014-09-19 10:51:19 -070050@Component(immediate = true)
51@Service
tomc78acee2014-09-24 15:16:55 -070052public class SimpleTopologyStore
53 extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
54 implements TopologyStore {
tom10262dd2014-09-19 10:51:19 -070055
56 private final Logger log = getLogger(getClass());
tomdc361b62014-09-09 20:36:52 -070057
58 private volatile DefaultTopology current;
59
tom10262dd2014-09-19 10:51:19 -070060 @Activate
61 public void activate() {
62 log.info("Started");
63 }
64
65 @Deactivate
66 public void deactivate() {
67 log.info("Stopped");
68 }
69 @Override
70 public Topology currentTopology() {
tomdc361b62014-09-09 20:36:52 -070071 return current;
72 }
73
tom10262dd2014-09-19 10:51:19 -070074 @Override
75 public boolean isLatest(Topology topology) {
tomcbff9392014-09-10 00:45:23 -070076 // Topology is current only if it is the same as our current topology
tomdc361b62014-09-09 20:36:52 -070077 return topology == current;
78 }
79
tom10262dd2014-09-19 10:51:19 -070080 @Override
81 public TopologyGraph getGraph(Topology topology) {
82 return defaultTopology(topology).getGraph();
tom13cb4852014-09-11 12:44:17 -070083 }
84
tom10262dd2014-09-19 10:51:19 -070085 @Override
86 public Set<TopologyCluster> getClusters(Topology topology) {
87 return defaultTopology(topology).getClusters();
tomdc361b62014-09-09 20:36:52 -070088 }
89
tom10262dd2014-09-19 10:51:19 -070090 @Override
91 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
92 return defaultTopology(topology).getCluster(clusterId);
tom13cb4852014-09-11 12:44:17 -070093 }
94
tom10262dd2014-09-19 10:51:19 -070095 @Override
96 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
97 return defaultTopology(topology).getClusterDevices(cluster);
tom13cb4852014-09-11 12:44:17 -070098 }
99
tom10262dd2014-09-19 10:51:19 -0700100 @Override
101 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
102 return defaultTopology(topology).getClusterLinks(cluster);
tomdc361b62014-09-09 20:36:52 -0700103 }
104
tom10262dd2014-09-19 10:51:19 -0700105 @Override
106 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
107 return defaultTopology(topology).getPaths(src, dst);
tomdc361b62014-09-09 20:36:52 -0700108 }
109
tom10262dd2014-09-19 10:51:19 -0700110 @Override
111 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
112 LinkWeight weight) {
113 return defaultTopology(topology).getPaths(src, dst, weight);
tomdc361b62014-09-09 20:36:52 -0700114 }
115
tom10262dd2014-09-19 10:51:19 -0700116 @Override
117 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
118 return defaultTopology(topology).isInfrastructure(connectPoint);
tomdc361b62014-09-09 20:36:52 -0700119 }
120
tom10262dd2014-09-19 10:51:19 -0700121 @Override
122 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
123 return defaultTopology(topology).isBroadcastPoint(connectPoint);
tomdc361b62014-09-09 20:36:52 -0700124 }
125
tom10262dd2014-09-19 10:51:19 -0700126 @Override
127 public TopologyEvent updateTopology(ProviderId providerId,
128 GraphDescription graphDescription,
129 List<Event> reasons) {
tome52ce702014-09-11 00:12:54 -0700130 // First off, make sure that what we're given is indeed newer than
131 // what we already have.
tom97937552014-09-11 10:48:42 -0700132 if (current != null && graphDescription.timestamp() < current.time()) {
tome52ce702014-09-11 00:12:54 -0700133 return null;
134 }
135
136 // Have the default topology construct self from the description data.
137 DefaultTopology newTopology =
tom97937552014-09-11 10:48:42 -0700138 new DefaultTopology(providerId, graphDescription);
tome52ce702014-09-11 00:12:54 -0700139
140 // Promote the new topology to current and return a ready-to-send event.
tom97937552014-09-11 10:48:42 -0700141 synchronized (this) {
142 current = newTopology;
tom95329eb2014-10-06 08:40:06 -0700143 return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED,
144 current, reasons);
tom97937552014-09-11 10:48:42 -0700145 }
tomdc361b62014-09-09 20:36:52 -0700146 }
147
tom10262dd2014-09-19 10:51:19 -0700148 // Validates the specified topology and returns it as a default
149 private DefaultTopology defaultTopology(Topology topology) {
150 if (topology instanceof DefaultTopology) {
151 return (DefaultTopology) topology;
152 }
153 throw new IllegalArgumentException("Topology class " + topology.getClass() +
154 " not supported");
155 }
156
tom0efbb1d2014-09-09 11:54:28 -0700157}