blob: cc42c88a304bf388f250b34d72976f06252f36ef [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 */
alshabib339a3d92014-09-26 17:54:32 -070016package org.onlab.onos.store.topology.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
Jonathan Hart29858af2014-10-29 15:33:18 -070020import java.util.Collections;
alshabib339a3d92014-09-26 17:54:32 -070021import java.util.List;
22import java.util.Set;
23
24import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Service;
28import org.onlab.onos.event.Event;
29import org.onlab.onos.net.ConnectPoint;
Jonathan Hart29858af2014-10-29 15:33:18 -070030import org.onlab.onos.net.Device;
alshabib339a3d92014-09-26 17:54:32 -070031import org.onlab.onos.net.DeviceId;
32import org.onlab.onos.net.Link;
33import org.onlab.onos.net.Path;
34import org.onlab.onos.net.provider.ProviderId;
35import org.onlab.onos.net.topology.ClusterId;
Jonathan Hart29858af2014-10-29 15:33:18 -070036import org.onlab.onos.net.topology.DefaultGraphDescription;
alshabib339a3d92014-09-26 17:54:32 -070037import org.onlab.onos.net.topology.GraphDescription;
38import org.onlab.onos.net.topology.LinkWeight;
39import org.onlab.onos.net.topology.Topology;
40import org.onlab.onos.net.topology.TopologyCluster;
41import org.onlab.onos.net.topology.TopologyEvent;
42import org.onlab.onos.net.topology.TopologyGraph;
43import org.onlab.onos.net.topology.TopologyStore;
44import org.onlab.onos.net.topology.TopologyStoreDelegate;
45import org.onlab.onos.store.AbstractStore;
46import org.slf4j.Logger;
47
48/**
49 * Manages inventory of topology snapshots using trivial in-memory
50 * structures implementation.
51 */
52//FIXME: I LIE I AM NOT DISTRIBUTED
53@Component(immediate = true)
54@Service
55public class DistributedTopologyStore
56extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
57implements TopologyStore {
58
59 private final Logger log = getLogger(getClass());
60
Jonathan Hart29858af2014-10-29 15:33:18 -070061 private volatile DefaultTopology current =
62 new DefaultTopology(ProviderId.NONE,
63 new DefaultGraphDescription(0L,
64 Collections.<Device>emptyList(),
65 Collections.<Link>emptyList()));
alshabib339a3d92014-09-26 17:54:32 -070066
67 @Activate
68 public void activate() {
69 log.info("Started");
70 }
71
72 @Deactivate
73 public void deactivate() {
74 log.info("Stopped");
75 }
76 @Override
77 public Topology currentTopology() {
78 return current;
79 }
80
81 @Override
82 public boolean isLatest(Topology topology) {
83 // Topology is current only if it is the same as our current topology
84 return topology == current;
85 }
86
87 @Override
88 public TopologyGraph getGraph(Topology topology) {
89 return defaultTopology(topology).getGraph();
90 }
91
92 @Override
93 public Set<TopologyCluster> getClusters(Topology topology) {
94 return defaultTopology(topology).getClusters();
95 }
96
97 @Override
98 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
99 return defaultTopology(topology).getCluster(clusterId);
100 }
101
102 @Override
103 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
104 return defaultTopology(topology).getClusterDevices(cluster);
105 }
106
107 @Override
108 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
109 return defaultTopology(topology).getClusterLinks(cluster);
110 }
111
112 @Override
113 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
114 return defaultTopology(topology).getPaths(src, dst);
115 }
116
117 @Override
118 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
119 LinkWeight weight) {
120 return defaultTopology(topology).getPaths(src, dst, weight);
121 }
122
123 @Override
124 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
125 return defaultTopology(topology).isInfrastructure(connectPoint);
126 }
127
128 @Override
129 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
130 return defaultTopology(topology).isBroadcastPoint(connectPoint);
131 }
132
133 @Override
134 public TopologyEvent updateTopology(ProviderId providerId,
135 GraphDescription graphDescription,
136 List<Event> reasons) {
137 // First off, make sure that what we're given is indeed newer than
138 // what we already have.
139 if (current != null && graphDescription.timestamp() < current.time()) {
140 return null;
141 }
142
143 // Have the default topology construct self from the description data.
144 DefaultTopology newTopology =
145 new DefaultTopology(providerId, graphDescription);
146
147 // Promote the new topology to current and return a ready-to-send event.
148 synchronized (this) {
149 current = newTopology;
tom95329eb2014-10-06 08:40:06 -0700150 return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED,
151 current, reasons);
alshabib339a3d92014-09-26 17:54:32 -0700152 }
153 }
154
155 // Validates the specified topology and returns it as a default
156 private DefaultTopology defaultTopology(Topology topology) {
157 if (topology instanceof DefaultTopology) {
158 return (DefaultTopology) topology;
159 }
160 throw new IllegalArgumentException("Topology class " + topology.getClass() +
161 " not supported");
162 }
163
164}