Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
| 16 | |
| 17 | package org.onosproject.incubator.net.virtual.impl; |
| 18 | |
| 19 | import org.onosproject.common.DefaultTopology; |
| 20 | import org.onosproject.event.AbstractListenerManager; |
| 21 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 22 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 23 | import org.onosproject.incubator.net.virtual.VnetService; |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.Device; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.DisjointPath; |
| 28 | import org.onosproject.net.Link; |
| 29 | import org.onosproject.net.Path; |
| 30 | import org.onosproject.net.topology.ClusterId; |
| 31 | import org.onosproject.net.topology.DefaultGraphDescription; |
| 32 | import org.onosproject.net.topology.LinkWeight; |
| 33 | import org.onosproject.net.topology.Topology; |
| 34 | import org.onosproject.net.topology.TopologyCluster; |
| 35 | import org.onosproject.net.topology.TopologyEvent; |
| 36 | import org.onosproject.net.topology.TopologyGraph; |
| 37 | import org.onosproject.net.topology.TopologyListener; |
| 38 | import org.onosproject.net.topology.TopologyService; |
| 39 | |
| 40 | import java.util.Map; |
| 41 | import java.util.Set; |
| 42 | import java.util.stream.Collectors; |
| 43 | |
| 44 | import static com.google.common.base.Preconditions.checkArgument; |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | import static org.onosproject.incubator.net.virtual.DefaultVirtualLink.PID; |
| 47 | |
| 48 | /** |
| 49 | * Topology service implementation built on the virtual network service. |
| 50 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 51 | public class VirtualNetworkTopologyManager |
| 52 | extends AbstractListenerManager<TopologyEvent, TopologyListener> |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 53 | implements TopologyService, VnetService { |
| 54 | |
| 55 | private static final String NETWORK_NULL = "Network ID cannot be null"; |
| 56 | private static final String TOPOLOGY_NULL = "Topology cannot be null"; |
| 57 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 58 | private static final String CLUSTER_ID_NULL = "Cluster ID cannot be null"; |
| 59 | private static final String CLUSTER_NULL = "Topology cluster cannot be null"; |
| 60 | private static final String CONNECTION_POINT_NULL = "Connection point cannot be null"; |
| 61 | private static final String LINK_WEIGHT_NULL = "Link weight cannot be null"; |
| 62 | |
| 63 | private final VirtualNetwork network; |
| 64 | private final VirtualNetworkService manager; |
| 65 | |
| 66 | /** |
| 67 | * Creates a new VirtualNetworkTopologyService object. |
| 68 | * |
| 69 | * @param virtualNetworkManager virtual network manager service |
| 70 | * @param network virtual network |
| 71 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 72 | public VirtualNetworkTopologyManager(VirtualNetworkService virtualNetworkManager, |
| 73 | VirtualNetwork network) { |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 74 | checkNotNull(network, NETWORK_NULL); |
| 75 | this.network = network; |
| 76 | this.manager = virtualNetworkManager; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Topology currentTopology() { |
| 81 | Iterable<Device> devices = manager.getVirtualDevices(network().id()) |
| 82 | .stream() |
| 83 | .collect(Collectors.toSet()); |
| 84 | Iterable<Link> links = manager.getVirtualLinks(network().id()) |
| 85 | .stream() |
| 86 | .collect(Collectors.toSet()); |
| 87 | |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 88 | DefaultGraphDescription graph = |
| 89 | new DefaultGraphDescription(System.nanoTime(), |
| 90 | System.currentTimeMillis(), |
| 91 | devices, links); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 92 | return new DefaultTopology(PID, graph); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean isLatest(Topology topology) { |
| 97 | Topology currentTopology = currentTopology(); |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 98 | return defaultTopology(topology).getGraph() |
| 99 | .equals(defaultTopology(currentTopology).getGraph()); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public TopologyGraph getGraph(Topology topology) { |
| 104 | return defaultTopology(topology).getGraph(); |
| 105 | } |
| 106 | |
| 107 | // Validates the specified topology and returns it as a default |
| 108 | private DefaultTopology defaultTopology(Topology topology) { |
| 109 | checkNotNull(topology, TOPOLOGY_NULL); |
| 110 | checkArgument(topology instanceof DefaultTopology, |
| 111 | "Topology class %s not supported", topology.getClass()); |
| 112 | return (DefaultTopology) topology; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Set<TopologyCluster> getClusters(Topology topology) { |
| 117 | return defaultTopology(topology).getClusters(); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public TopologyCluster getCluster(Topology topology, ClusterId clusterId) { |
| 122 | checkNotNull(clusterId, CLUSTER_ID_NULL); |
| 123 | return defaultTopology(topology).getCluster(clusterId); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) { |
| 128 | checkNotNull(cluster, CLUSTER_NULL); |
| 129 | return defaultTopology(topology).getClusterDevices(cluster); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) { |
| 134 | checkNotNull(cluster, CLUSTER_NULL); |
| 135 | return defaultTopology(topology).getClusterLinks(cluster); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 140 | checkNotNull(src, DEVICE_ID_NULL); |
| 141 | checkNotNull(dst, DEVICE_ID_NULL); |
| 142 | return defaultTopology(topology).getPaths(src, dst); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) { |
| 147 | checkNotNull(src, DEVICE_ID_NULL); |
| 148 | checkNotNull(dst, DEVICE_ID_NULL); |
| 149 | checkNotNull(weight, LINK_WEIGHT_NULL); |
| 150 | return defaultTopology(topology).getPaths(src, dst, weight); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 155 | checkNotNull(src, DEVICE_ID_NULL); |
| 156 | checkNotNull(dst, DEVICE_ID_NULL); |
| 157 | return defaultTopology(topology).getDisjointPaths(src, dst); |
| 158 | } |
| 159 | |
| 160 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 161 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, |
| 162 | DeviceId dst, LinkWeight weight) { |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 163 | checkNotNull(src, DEVICE_ID_NULL); |
| 164 | checkNotNull(dst, DEVICE_ID_NULL); |
| 165 | checkNotNull(weight, LINK_WEIGHT_NULL); |
| 166 | return defaultTopology(topology).getDisjointPaths(src, dst, weight); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, |
| 171 | Map<Link, Object> riskProfile) { |
| 172 | checkNotNull(src, DEVICE_ID_NULL); |
| 173 | checkNotNull(dst, DEVICE_ID_NULL); |
| 174 | return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, |
| 179 | LinkWeight weight, Map<Link, Object> riskProfile) { |
| 180 | checkNotNull(src, DEVICE_ID_NULL); |
| 181 | checkNotNull(dst, DEVICE_ID_NULL); |
| 182 | checkNotNull(weight, LINK_WEIGHT_NULL); |
| 183 | return defaultTopology(topology).getDisjointPaths(src, dst, weight, riskProfile); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { |
| 188 | checkNotNull(connectPoint, CONNECTION_POINT_NULL); |
| 189 | return defaultTopology(topology).isInfrastructure(connectPoint); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) { |
| 194 | checkNotNull(connectPoint, CONNECTION_POINT_NULL); |
| 195 | return defaultTopology(topology).isBroadcastPoint(connectPoint); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public VirtualNetwork network() { |
| 200 | return network; |
| 201 | } |
| 202 | } |