blob: 3878c79b1df0fa8a1660350fa6788292197813ca [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
20import java.util.List;
21import java.util.Set;
22
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.onos.event.Event;
28import org.onlab.onos.net.ConnectPoint;
29import org.onlab.onos.net.DeviceId;
30import org.onlab.onos.net.Link;
31import org.onlab.onos.net.Path;
32import org.onlab.onos.net.provider.ProviderId;
33import org.onlab.onos.net.topology.ClusterId;
34import org.onlab.onos.net.topology.GraphDescription;
35import org.onlab.onos.net.topology.LinkWeight;
36import org.onlab.onos.net.topology.Topology;
37import org.onlab.onos.net.topology.TopologyCluster;
38import org.onlab.onos.net.topology.TopologyEvent;
39import org.onlab.onos.net.topology.TopologyGraph;
40import org.onlab.onos.net.topology.TopologyStore;
41import org.onlab.onos.net.topology.TopologyStoreDelegate;
42import org.onlab.onos.store.AbstractStore;
43import org.slf4j.Logger;
44
45/**
46 * Manages inventory of topology snapshots using trivial in-memory
47 * structures implementation.
48 */
49//FIXME: I LIE I AM NOT DISTRIBUTED
50@Component(immediate = true)
51@Service
52public class DistributedTopologyStore
53extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
54implements TopologyStore {
55
56 private final Logger log = getLogger(getClass());
57
58 private volatile DefaultTopology current;
59
60 @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() {
71 return current;
72 }
73
74 @Override
75 public boolean isLatest(Topology topology) {
76 // Topology is current only if it is the same as our current topology
77 return topology == current;
78 }
79
80 @Override
81 public TopologyGraph getGraph(Topology topology) {
82 return defaultTopology(topology).getGraph();
83 }
84
85 @Override
86 public Set<TopologyCluster> getClusters(Topology topology) {
87 return defaultTopology(topology).getClusters();
88 }
89
90 @Override
91 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
92 return defaultTopology(topology).getCluster(clusterId);
93 }
94
95 @Override
96 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
97 return defaultTopology(topology).getClusterDevices(cluster);
98 }
99
100 @Override
101 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
102 return defaultTopology(topology).getClusterLinks(cluster);
103 }
104
105 @Override
106 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
107 return defaultTopology(topology).getPaths(src, dst);
108 }
109
110 @Override
111 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
112 LinkWeight weight) {
113 return defaultTopology(topology).getPaths(src, dst, weight);
114 }
115
116 @Override
117 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
118 return defaultTopology(topology).isInfrastructure(connectPoint);
119 }
120
121 @Override
122 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
123 return defaultTopology(topology).isBroadcastPoint(connectPoint);
124 }
125
126 @Override
127 public TopologyEvent updateTopology(ProviderId providerId,
128 GraphDescription graphDescription,
129 List<Event> reasons) {
130 // First off, make sure that what we're given is indeed newer than
131 // what we already have.
132 if (current != null && graphDescription.timestamp() < current.time()) {
133 return null;
134 }
135
136 // Have the default topology construct self from the description data.
137 DefaultTopology newTopology =
138 new DefaultTopology(providerId, graphDescription);
139
140 // Promote the new topology to current and return a ready-to-send event.
141 synchronized (this) {
142 current = newTopology;
tom95329eb2014-10-06 08:40:06 -0700143 return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED,
144 current, reasons);
alshabib339a3d92014-09-26 17:54:32 -0700145 }
146 }
147
148 // 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
157}