blob: 78bc18652742d51cf869f1eadc57d91362f4a5fb [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.topology.impl;
alshabib339a3d92014-09-26 17:54:32 -070017
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.event.Event;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.Link;
33import org.onosproject.net.Path;
34import org.onosproject.net.provider.ProviderId;
35import org.onosproject.net.topology.ClusterId;
36import org.onosproject.net.topology.DefaultGraphDescription;
37import org.onosproject.net.topology.GraphDescription;
38import org.onosproject.net.topology.LinkWeight;
39import org.onosproject.net.topology.Topology;
40import org.onosproject.net.topology.TopologyCluster;
41import org.onosproject.net.topology.TopologyEvent;
42import org.onosproject.net.topology.TopologyGraph;
43import org.onosproject.net.topology.TopologyStore;
44import org.onosproject.net.topology.TopologyStoreDelegate;
45import org.onosproject.store.AbstractStore;
alshabib339a3d92014-09-26 17:54:32 -070046import org.slf4j.Logger;
47
48/**
49 * Manages inventory of topology snapshots using trivial in-memory
50 * structures implementation.
Brian O'Connor44008532014-12-04 16:41:36 -080051 *
52 * Note: This component is not distributed per-se. It runs on every
53 * instance and feeds off of other distributed stores.
alshabib339a3d92014-09-26 17:54:32 -070054 */
alshabib339a3d92014-09-26 17:54:32 -070055@Component(immediate = true)
56@Service
57public class DistributedTopologyStore
58extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
59implements TopologyStore {
60
61 private final Logger log = getLogger(getClass());
62
Jonathan Hart29858af2014-10-29 15:33:18 -070063 private volatile DefaultTopology current =
64 new DefaultTopology(ProviderId.NONE,
65 new DefaultGraphDescription(0L,
66 Collections.<Device>emptyList(),
67 Collections.<Link>emptyList()));
alshabib339a3d92014-09-26 17:54:32 -070068
69 @Activate
70 public void activate() {
71 log.info("Started");
72 }
73
74 @Deactivate
75 public void deactivate() {
76 log.info("Stopped");
77 }
78 @Override
79 public Topology currentTopology() {
80 return current;
81 }
82
83 @Override
84 public boolean isLatest(Topology topology) {
85 // Topology is current only if it is the same as our current topology
86 return topology == current;
87 }
88
89 @Override
90 public TopologyGraph getGraph(Topology topology) {
91 return defaultTopology(topology).getGraph();
92 }
93
94 @Override
95 public Set<TopologyCluster> getClusters(Topology topology) {
96 return defaultTopology(topology).getClusters();
97 }
98
99 @Override
100 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
101 return defaultTopology(topology).getCluster(clusterId);
102 }
103
104 @Override
105 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
106 return defaultTopology(topology).getClusterDevices(cluster);
107 }
108
109 @Override
110 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
111 return defaultTopology(topology).getClusterLinks(cluster);
112 }
113
114 @Override
115 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
116 return defaultTopology(topology).getPaths(src, dst);
117 }
118
119 @Override
120 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
121 LinkWeight weight) {
122 return defaultTopology(topology).getPaths(src, dst, weight);
123 }
124
125 @Override
126 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
127 return defaultTopology(topology).isInfrastructure(connectPoint);
128 }
129
130 @Override
131 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
132 return defaultTopology(topology).isBroadcastPoint(connectPoint);
133 }
134
135 @Override
136 public TopologyEvent updateTopology(ProviderId providerId,
137 GraphDescription graphDescription,
138 List<Event> reasons) {
139 // First off, make sure that what we're given is indeed newer than
140 // what we already have.
141 if (current != null && graphDescription.timestamp() < current.time()) {
142 return null;
143 }
144
145 // Have the default topology construct self from the description data.
146 DefaultTopology newTopology =
147 new DefaultTopology(providerId, graphDescription);
148
149 // Promote the new topology to current and return a ready-to-send event.
150 synchronized (this) {
151 current = newTopology;
tom95329eb2014-10-06 08:40:06 -0700152 return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED,
153 current, reasons);
alshabib339a3d92014-09-26 17:54:32 -0700154 }
155 }
156
157 // Validates the specified topology and returns it as a default
158 private DefaultTopology defaultTopology(Topology topology) {
159 if (topology instanceof DefaultTopology) {
160 return (DefaultTopology) topology;
161 }
162 throw new IllegalArgumentException("Topology class " + topology.getClass() +
163 " not supported");
164 }
165
166}