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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.topology.impl; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.event.Event; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.Link; |
| 33 | import org.onosproject.net.Path; |
| 34 | import org.onosproject.net.provider.ProviderId; |
| 35 | import org.onosproject.net.topology.ClusterId; |
| 36 | import org.onosproject.net.topology.DefaultGraphDescription; |
| 37 | import org.onosproject.net.topology.GraphDescription; |
| 38 | import org.onosproject.net.topology.LinkWeight; |
| 39 | import org.onosproject.net.topology.Topology; |
| 40 | import org.onosproject.net.topology.TopologyCluster; |
| 41 | import org.onosproject.net.topology.TopologyEvent; |
| 42 | import org.onosproject.net.topology.TopologyGraph; |
| 43 | import org.onosproject.net.topology.TopologyStore; |
| 44 | import org.onosproject.net.topology.TopologyStoreDelegate; |
| 45 | import org.onosproject.store.AbstractStore; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | /** |
| 49 | * Manages inventory of topology snapshots using trivial in-memory |
| 50 | * structures implementation. |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 51 | * |
| 52 | * Note: This component is not distributed per-se. It runs on every |
| 53 | * instance and feeds off of other distributed stores. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 54 | */ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 55 | @Component(immediate = true) |
| 56 | @Service |
| 57 | public class DistributedTopologyStore |
| 58 | extends AbstractStore<TopologyEvent, TopologyStoreDelegate> |
| 59 | implements TopologyStore { |
| 60 | |
| 61 | private final Logger log = getLogger(getClass()); |
| 62 | |
Jonathan Hart | 29858af | 2014-10-29 15:33:18 -0700 | [diff] [blame] | 63 | private volatile DefaultTopology current = |
| 64 | new DefaultTopology(ProviderId.NONE, |
| 65 | new DefaultGraphDescription(0L, |
| 66 | Collections.<Device>emptyList(), |
| 67 | Collections.<Link>emptyList())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 68 | |
| 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; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 152 | return new TopologyEvent(TopologyEvent.Type.TOPOLOGY_CHANGED, |
| 153 | current, reasons); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 154 | } |
| 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 | } |