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 | |
Jonathan Hart | 29858af | 2014-10-29 15:33:18 -0700 | [diff] [blame] | 20 | import java.util.Collections; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 21 | import java.util.List; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Service; |
| 28 | import org.onlab.onos.event.Event; |
| 29 | import org.onlab.onos.net.ConnectPoint; |
Jonathan Hart | 29858af | 2014-10-29 15:33:18 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.Device; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.DeviceId; |
| 32 | import org.onlab.onos.net.Link; |
| 33 | import org.onlab.onos.net.Path; |
| 34 | import org.onlab.onos.net.provider.ProviderId; |
| 35 | import org.onlab.onos.net.topology.ClusterId; |
Jonathan Hart | 29858af | 2014-10-29 15:33:18 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.topology.DefaultGraphDescription; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.topology.GraphDescription; |
| 38 | import org.onlab.onos.net.topology.LinkWeight; |
| 39 | import org.onlab.onos.net.topology.Topology; |
| 40 | import org.onlab.onos.net.topology.TopologyCluster; |
| 41 | import org.onlab.onos.net.topology.TopologyEvent; |
| 42 | import org.onlab.onos.net.topology.TopologyGraph; |
| 43 | import org.onlab.onos.net.topology.TopologyStore; |
| 44 | import org.onlab.onos.net.topology.TopologyStoreDelegate; |
| 45 | import org.onlab.onos.store.AbstractStore; |
| 46 | import 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 |
| 55 | public class DistributedTopologyStore |
| 56 | extends AbstractStore<TopologyEvent, TopologyStoreDelegate> |
| 57 | implements TopologyStore { |
| 58 | |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
Jonathan Hart | 29858af | 2014-10-29 15:33:18 -0700 | [diff] [blame] | 61 | private volatile DefaultTopology current = |
| 62 | new DefaultTopology(ProviderId.NONE, |
| 63 | new DefaultGraphDescription(0L, |
| 64 | Collections.<Device>emptyList(), |
| 65 | Collections.<Link>emptyList())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 66 | |
| 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; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 150 | return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, |
| 151 | current, reasons); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 152 | } |
| 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 | } |