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 | |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame^] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
Jonathan Hart | 29858af | 2014-10-29 15:33:18 -0700 | [diff] [blame] | 22 | import java.util.Collections; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 23 | import java.util.List; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame^] | 30 | import org.onosproject.common.DefaultTopology; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.event.Event; |
| 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.Device; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Link; |
| 36 | import org.onosproject.net.Path; |
| 37 | import org.onosproject.net.provider.ProviderId; |
| 38 | import org.onosproject.net.topology.ClusterId; |
| 39 | import org.onosproject.net.topology.DefaultGraphDescription; |
| 40 | import org.onosproject.net.topology.GraphDescription; |
| 41 | import org.onosproject.net.topology.LinkWeight; |
| 42 | import org.onosproject.net.topology.Topology; |
| 43 | import org.onosproject.net.topology.TopologyCluster; |
| 44 | import org.onosproject.net.topology.TopologyEvent; |
| 45 | import org.onosproject.net.topology.TopologyGraph; |
| 46 | import org.onosproject.net.topology.TopologyStore; |
| 47 | import org.onosproject.net.topology.TopologyStoreDelegate; |
| 48 | import org.onosproject.store.AbstractStore; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | |
| 51 | /** |
| 52 | * Manages inventory of topology snapshots using trivial in-memory |
| 53 | * structures implementation. |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame^] | 54 | * <p> |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 55 | * Note: This component is not distributed per-se. It runs on every |
| 56 | * instance and feeds off of other distributed stores. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 57 | */ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 58 | @Component(immediate = true) |
| 59 | @Service |
| 60 | public class DistributedTopologyStore |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame^] | 61 | extends AbstractStore<TopologyEvent, TopologyStoreDelegate> |
| 62 | implements TopologyStore { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 63 | |
| 64 | private final Logger log = getLogger(getClass()); |
| 65 | |
Jonathan Hart | 29858af | 2014-10-29 15:33:18 -0700 | [diff] [blame] | 66 | private volatile DefaultTopology current = |
| 67 | new DefaultTopology(ProviderId.NONE, |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame^] | 68 | new DefaultGraphDescription(0L, 0L, |
| 69 | Collections.<Device>emptyList(), |
| 70 | Collections.<Link>emptyList())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 71 | |
| 72 | @Activate |
| 73 | public void activate() { |
| 74 | log.info("Started"); |
| 75 | } |
| 76 | |
| 77 | @Deactivate |
| 78 | public void deactivate() { |
| 79 | log.info("Stopped"); |
| 80 | } |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame^] | 81 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 82 | @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 Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame^] | 125 | LinkWeight weight) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 126 | 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 Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame^] | 141 | GraphDescription graphDescription, |
| 142 | List<Event> reasons) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 143 | // 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. |
| 150 | DefaultTopology newTopology = |
| 151 | new DefaultTopology(providerId, graphDescription); |
| 152 | |
| 153 | // Promote the new topology to current and return a ready-to-send event. |
| 154 | synchronized (this) { |
| 155 | current = newTopology; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame^] | 156 | return new TopologyEvent(TOPOLOGY_CHANGED, current, reasons); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | // Validates the specified topology and returns it as a default |
| 161 | private DefaultTopology defaultTopology(Topology topology) { |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame^] | 162 | checkArgument(topology instanceof DefaultTopology, |
| 163 | "Topology class %s not supported", topology.getClass()); |
| 164 | return (DefaultTopology) topology; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | } |