blob: c5d1738d6d64e8c9b2a11425ccb4d446216f4796 [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 */
tomea961ff2014-10-01 12:45:15 -070016package org.onlab.onos.store.trivial.impl;
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;
tomdc361b62014-09-09 20:36:52 -070022import org.onlab.onos.event.Event;
23import org.onlab.onos.net.ConnectPoint;
24import org.onlab.onos.net.DeviceId;
tom13cb4852014-09-11 12:44:17 -070025import org.onlab.onos.net.Link;
tomdc361b62014-09-09 20:36:52 -070026import org.onlab.onos.net.Path;
tome52ce702014-09-11 00:12:54 -070027import org.onlab.onos.net.provider.ProviderId;
tom13cb4852014-09-11 12:44:17 -070028import org.onlab.onos.net.topology.ClusterId;
tom97937552014-09-11 10:48:42 -070029import org.onlab.onos.net.topology.GraphDescription;
tomdc361b62014-09-09 20:36:52 -070030import org.onlab.onos.net.topology.LinkWeight;
tomdc361b62014-09-09 20:36:52 -070031import org.onlab.onos.net.topology.Topology;
32import org.onlab.onos.net.topology.TopologyCluster;
tomdc361b62014-09-09 20:36:52 -070033import org.onlab.onos.net.topology.TopologyEvent;
tom97937552014-09-11 10:48:42 -070034import org.onlab.onos.net.topology.TopologyGraph;
tom10262dd2014-09-19 10:51:19 -070035import org.onlab.onos.net.topology.TopologyStore;
tomc78acee2014-09-24 15:16:55 -070036import org.onlab.onos.net.topology.TopologyStoreDelegate;
37import org.onlab.onos.store.AbstractStore;
tom10262dd2014-09-19 10:51:19 -070038import org.slf4j.Logger;
tomdc361b62014-09-09 20:36:52 -070039
40import java.util.List;
41import java.util.Set;
42
tom10262dd2014-09-19 10:51:19 -070043import static org.slf4j.LoggerFactory.getLogger;
44
tom0efbb1d2014-09-09 11:54:28 -070045/**
46 * Manages inventory of topology snapshots using trivial in-memory
tomcbff9392014-09-10 00:45:23 -070047 * structures implementation.
tom0efbb1d2014-09-09 11:54:28 -070048 */
tom10262dd2014-09-19 10:51:19 -070049@Component(immediate = true)
50@Service
tomc78acee2014-09-24 15:16:55 -070051public class SimpleTopologyStore
52 extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
53 implements TopologyStore {
tom10262dd2014-09-19 10:51:19 -070054
55 private final Logger log = getLogger(getClass());
tomdc361b62014-09-09 20:36:52 -070056
57 private volatile DefaultTopology current;
58
tom10262dd2014-09-19 10:51:19 -070059 @Activate
60 public void activate() {
61 log.info("Started");
62 }
63
64 @Deactivate
65 public void deactivate() {
66 log.info("Stopped");
67 }
68 @Override
69 public Topology currentTopology() {
tomdc361b62014-09-09 20:36:52 -070070 return current;
71 }
72
tom10262dd2014-09-19 10:51:19 -070073 @Override
74 public boolean isLatest(Topology topology) {
tomcbff9392014-09-10 00:45:23 -070075 // Topology is current only if it is the same as our current topology
tomdc361b62014-09-09 20:36:52 -070076 return topology == current;
77 }
78
tom10262dd2014-09-19 10:51:19 -070079 @Override
80 public TopologyGraph getGraph(Topology topology) {
81 return defaultTopology(topology).getGraph();
tom13cb4852014-09-11 12:44:17 -070082 }
83
tom10262dd2014-09-19 10:51:19 -070084 @Override
85 public Set<TopologyCluster> getClusters(Topology topology) {
86 return defaultTopology(topology).getClusters();
tomdc361b62014-09-09 20:36:52 -070087 }
88
tom10262dd2014-09-19 10:51:19 -070089 @Override
90 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
91 return defaultTopology(topology).getCluster(clusterId);
tom13cb4852014-09-11 12:44:17 -070092 }
93
tom10262dd2014-09-19 10:51:19 -070094 @Override
95 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
96 return defaultTopology(topology).getClusterDevices(cluster);
tom13cb4852014-09-11 12:44:17 -070097 }
98
tom10262dd2014-09-19 10:51:19 -070099 @Override
100 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
101 return defaultTopology(topology).getClusterLinks(cluster);
tomdc361b62014-09-09 20:36:52 -0700102 }
103
tom10262dd2014-09-19 10:51:19 -0700104 @Override
105 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
106 return defaultTopology(topology).getPaths(src, dst);
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 LinkWeight weight) {
112 return defaultTopology(topology).getPaths(src, dst, weight);
tomdc361b62014-09-09 20:36:52 -0700113 }
114
tom10262dd2014-09-19 10:51:19 -0700115 @Override
116 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
117 return defaultTopology(topology).isInfrastructure(connectPoint);
tomdc361b62014-09-09 20:36:52 -0700118 }
119
tom10262dd2014-09-19 10:51:19 -0700120 @Override
121 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
122 return defaultTopology(topology).isBroadcastPoint(connectPoint);
tomdc361b62014-09-09 20:36:52 -0700123 }
124
tom10262dd2014-09-19 10:51:19 -0700125 @Override
126 public TopologyEvent updateTopology(ProviderId providerId,
127 GraphDescription graphDescription,
128 List<Event> reasons) {
tome52ce702014-09-11 00:12:54 -0700129 // First off, make sure that what we're given is indeed newer than
130 // what we already have.
tom97937552014-09-11 10:48:42 -0700131 if (current != null && graphDescription.timestamp() < current.time()) {
tome52ce702014-09-11 00:12:54 -0700132 return null;
133 }
134
135 // Have the default topology construct self from the description data.
136 DefaultTopology newTopology =
tom97937552014-09-11 10:48:42 -0700137 new DefaultTopology(providerId, graphDescription);
tome52ce702014-09-11 00:12:54 -0700138
139 // Promote the new topology to current and return a ready-to-send event.
tom97937552014-09-11 10:48:42 -0700140 synchronized (this) {
141 current = newTopology;
tom95329eb2014-10-06 08:40:06 -0700142 return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED,
143 current, reasons);
tom97937552014-09-11 10:48:42 -0700144 }
tomdc361b62014-09-09 20:36:52 -0700145 }
146
tom10262dd2014-09-19 10:51:19 -0700147 // Validates the specified topology and returns it as a default
148 private DefaultTopology defaultTopology(Topology topology) {
149 if (topology instanceof DefaultTopology) {
150 return (DefaultTopology) topology;
151 }
152 throw new IllegalArgumentException("Topology class " + topology.getClass() +
153 " not supported");
154 }
155
tom0efbb1d2014-09-09 11:54:28 -0700156}