Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.topology.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.List; |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.onos.event.Event; |
| 28 | import org.onlab.onos.net.ConnectPoint; |
| 29 | import org.onlab.onos.net.DeviceId; |
| 30 | import org.onlab.onos.net.Link; |
| 31 | import org.onlab.onos.net.Path; |
| 32 | import org.onlab.onos.net.provider.ProviderId; |
| 33 | import org.onlab.onos.net.topology.ClusterId; |
| 34 | import org.onlab.onos.net.topology.GraphDescription; |
| 35 | import org.onlab.onos.net.topology.LinkWeight; |
| 36 | import org.onlab.onos.net.topology.Topology; |
| 37 | import org.onlab.onos.net.topology.TopologyCluster; |
| 38 | import org.onlab.onos.net.topology.TopologyEvent; |
| 39 | import org.onlab.onos.net.topology.TopologyGraph; |
| 40 | import org.onlab.onos.net.topology.TopologyStore; |
| 41 | import org.onlab.onos.net.topology.TopologyStoreDelegate; |
| 42 | import org.onlab.onos.store.AbstractStore; |
| 43 | import 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 |
| 52 | public class DistributedTopologyStore |
| 53 | extends AbstractStore<TopologyEvent, TopologyStoreDelegate> |
| 54 | implements 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; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 143 | return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, |
| 144 | current, reasons); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 145 | } |
| 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 | } |