blob: 3a0f1859fc89103e47311cdd1602dc36b7c8e487 [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
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070018import static com.google.common.base.Preconditions.checkArgument;
19import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
alshabib339a3d92014-09-26 17:54:32 -070020import static org.slf4j.LoggerFactory.getLogger;
21
Jonathan Hart29858af2014-10-29 15:33:18 -070022import java.util.Collections;
alshabib339a3d92014-09-26 17:54:32 -070023import java.util.List;
24import java.util.Set;
25
26import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Service;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070030import org.onosproject.common.DefaultTopology;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.event.Event;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.Device;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.Link;
36import org.onosproject.net.Path;
37import org.onosproject.net.provider.ProviderId;
38import org.onosproject.net.topology.ClusterId;
39import org.onosproject.net.topology.DefaultGraphDescription;
40import org.onosproject.net.topology.GraphDescription;
41import org.onosproject.net.topology.LinkWeight;
42import org.onosproject.net.topology.Topology;
43import org.onosproject.net.topology.TopologyCluster;
44import org.onosproject.net.topology.TopologyEvent;
45import org.onosproject.net.topology.TopologyGraph;
46import org.onosproject.net.topology.TopologyStore;
47import org.onosproject.net.topology.TopologyStoreDelegate;
48import org.onosproject.store.AbstractStore;
alshabib339a3d92014-09-26 17:54:32 -070049import org.slf4j.Logger;
50
51/**
52 * Manages inventory of topology snapshots using trivial in-memory
53 * structures implementation.
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070054 * <p>
Brian O'Connor44008532014-12-04 16:41:36 -080055 * Note: This component is not distributed per-se. It runs on every
56 * instance and feeds off of other distributed stores.
alshabib339a3d92014-09-26 17:54:32 -070057 */
alshabib339a3d92014-09-26 17:54:32 -070058@Component(immediate = true)
59@Service
60public class DistributedTopologyStore
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070061 extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
62 implements TopologyStore {
alshabib339a3d92014-09-26 17:54:32 -070063
64 private final Logger log = getLogger(getClass());
65
Jonathan Hart29858af2014-10-29 15:33:18 -070066 private volatile DefaultTopology current =
67 new DefaultTopology(ProviderId.NONE,
Thomas Vachuska320c58f2015-08-05 10:42:32 -070068 new DefaultGraphDescription(0L, System.currentTimeMillis(),
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070069 Collections.<Device>emptyList(),
70 Collections.<Link>emptyList()));
alshabib339a3d92014-09-26 17:54:32 -070071
72 @Activate
73 public void activate() {
74 log.info("Started");
75 }
76
77 @Deactivate
78 public void deactivate() {
79 log.info("Stopped");
80 }
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070081
alshabib339a3d92014-09-26 17:54:32 -070082 @Override
83 public Topology currentTopology() {
84 return current;
85 }
86
87 @Override
88 public boolean isLatest(Topology topology) {
89 // Topology is current only if it is the same as our current topology
90 return topology == current;
91 }
92
93 @Override
94 public TopologyGraph getGraph(Topology topology) {
95 return defaultTopology(topology).getGraph();
96 }
97
98 @Override
99 public Set<TopologyCluster> getClusters(Topology topology) {
100 return defaultTopology(topology).getClusters();
101 }
102
103 @Override
104 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
105 return defaultTopology(topology).getCluster(clusterId);
106 }
107
108 @Override
109 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
110 return defaultTopology(topology).getClusterDevices(cluster);
111 }
112
113 @Override
114 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
115 return defaultTopology(topology).getClusterLinks(cluster);
116 }
117
118 @Override
119 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
120 return defaultTopology(topology).getPaths(src, dst);
121 }
122
123 @Override
124 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700125 LinkWeight weight) {
alshabib339a3d92014-09-26 17:54:32 -0700126 return defaultTopology(topology).getPaths(src, dst, weight);
127 }
128
129 @Override
130 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
131 return defaultTopology(topology).isInfrastructure(connectPoint);
132 }
133
134 @Override
135 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
136 return defaultTopology(topology).isBroadcastPoint(connectPoint);
137 }
138
139 @Override
140 public TopologyEvent updateTopology(ProviderId providerId,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700141 GraphDescription graphDescription,
142 List<Event> reasons) {
alshabib339a3d92014-09-26 17:54:32 -0700143 // First off, make sure that what we're given is indeed newer than
144 // what we already have.
145 if (current != null && graphDescription.timestamp() < current.time()) {
146 return null;
147 }
148
149 // Have the default topology construct self from the description data.
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700150 DefaultTopology newTopology = new DefaultTopology(providerId, graphDescription);
alshabib339a3d92014-09-26 17:54:32 -0700151
152 // Promote the new topology to current and return a ready-to-send event.
153 synchronized (this) {
154 current = newTopology;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700155 return new TopologyEvent(TOPOLOGY_CHANGED, current, reasons);
alshabib339a3d92014-09-26 17:54:32 -0700156 }
157 }
158
159 // Validates the specified topology and returns it as a default
160 private DefaultTopology defaultTopology(Topology topology) {
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700161 checkArgument(topology instanceof DefaultTopology,
162 "Topology class %s not supported", topology.getClass());
163 return (DefaultTopology) topology;
alshabib339a3d92014-09-26 17:54:32 -0700164 }
165
166}