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.net.topology.impl; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 17 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DefaultEdgeLink; |
| 29 | import org.onosproject.net.DefaultPath; |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DisjointPath; |
| 31 | import org.onosproject.net.DefaultDisjointPath; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.EdgeLink; |
| 34 | import org.onosproject.net.ElementId; |
| 35 | import org.onosproject.net.Host; |
| 36 | import org.onosproject.net.HostId; |
| 37 | import org.onosproject.net.HostLocation; |
| 38 | import org.onosproject.net.Link; |
| 39 | import org.onosproject.net.Path; |
| 40 | import org.onosproject.net.PortNumber; |
| 41 | import org.onosproject.net.host.HostService; |
| 42 | import org.onosproject.net.provider.ProviderId; |
| 43 | import org.onosproject.net.topology.LinkWeight; |
| 44 | import org.onosproject.net.topology.PathService; |
| 45 | import org.onosproject.net.topology.Topology; |
| 46 | import org.onosproject.net.topology.TopologyService; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import java.util.List; |
| 50 | import java.util.Set; |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 51 | import java.util.Map; |
| 52 | |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 53 | |
| 54 | import static com.google.common.base.Preconditions.checkNotNull; |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 56 | import static org.onosproject.security.AppGuard.checkPermission; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 57 | import static org.onosproject.security.AppPermission.Type.*; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 58 | |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Provides implementation of a path selection service atop the current |
| 62 | * topology and host services. |
| 63 | */ |
| 64 | @Component(immediate = true) |
| 65 | @Service |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 66 | public class PathManager implements PathService { |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 67 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 68 | private static final String ELEMENT_ID_NULL = "Element ID cannot be null"; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 69 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 70 | private static final ProviderId PID = new ProviderId("core", "org.onosproject.core"); |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 71 | private static final PortNumber P0 = PortNumber.portNumber(0); |
| 72 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 73 | private static final EdgeLink NOT_HOST = new NotHost(); |
| 74 | |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 75 | private final Logger log = getLogger(getClass()); |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected TopologyService topologyService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected HostService hostService; |
| 82 | |
| 83 | @Activate |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 84 | public void activate() { |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 85 | log.info("Started"); |
| 86 | } |
| 87 | |
| 88 | @Deactivate |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 89 | public void deactivate() { |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 90 | log.info("Stopped"); |
| 91 | } |
| 92 | |
| 93 | @Override |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 94 | public Set<Path> getPaths(ElementId src, ElementId dst) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 95 | checkPermission(TOPOLOGY_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 96 | |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 97 | return getPaths(src, dst, null); |
| 98 | } |
| 99 | |
| 100 | @Override |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 101 | public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 102 | checkPermission(TOPOLOGY_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 103 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 104 | checkNotNull(src, ELEMENT_ID_NULL); |
| 105 | checkNotNull(dst, ELEMENT_ID_NULL); |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 106 | |
| 107 | // Get the source and destination edge locations |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 108 | EdgeLink srcEdge = getEdgeLink(src, true); |
| 109 | EdgeLink dstEdge = getEdgeLink(dst, false); |
| 110 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 111 | // If either edge is null, bail with no paths. |
| 112 | if (srcEdge == null || dstEdge == null) { |
| 113 | return ImmutableSet.of(); |
| 114 | } |
| 115 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 116 | DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src; |
| 117 | DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 118 | |
| 119 | // If the source and destination are on the same edge device, there |
| 120 | // is just one path, so build it and return it. |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 121 | if (srcDevice.equals(dstDevice)) { |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 122 | return edgeToEdgePaths(srcEdge, dstEdge); |
| 123 | } |
| 124 | |
| 125 | // Otherwise get all paths between the source and destination edge |
| 126 | // devices. |
| 127 | Topology topology = topologyService.currentTopology(); |
| 128 | Set<Path> paths = weight == null ? |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 129 | topologyService.getPaths(topology, srcDevice, dstDevice) : |
| 130 | topologyService.getPaths(topology, srcDevice, dstDevice, weight); |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 131 | |
| 132 | return edgeToEdgePaths(srcEdge, dstEdge, paths); |
| 133 | } |
| 134 | |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst) { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 137 | return getDisjointPaths(src, dst, (LinkWeight) null); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, LinkWeight weight) { |
| 142 | checkNotNull(src, ELEMENT_ID_NULL); |
| 143 | checkNotNull(dst, ELEMENT_ID_NULL); |
| 144 | |
| 145 | // Get the source and destination edge locations |
| 146 | EdgeLink srcEdge = getEdgeLink(src, true); |
| 147 | EdgeLink dstEdge = getEdgeLink(dst, false); |
| 148 | |
| 149 | // If either edge is null, bail with no paths. |
| 150 | if (srcEdge == null || dstEdge == null) { |
| 151 | return ImmutableSet.of(); |
| 152 | } |
| 153 | |
| 154 | DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src; |
| 155 | DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst; |
| 156 | |
| 157 | // If the source and destination are on the same edge device, there |
| 158 | // is just one path, so build it and return it. |
| 159 | if (srcDevice.equals(dstDevice)) { |
| 160 | return edgeToEdgePathsDisjoint(srcEdge, dstEdge); |
| 161 | } |
| 162 | |
| 163 | // Otherwise get all paths between the source and destination edge |
| 164 | // devices. |
| 165 | Topology topology = topologyService.currentTopology(); |
| 166 | Set<DisjointPath> paths = weight == null ? |
| 167 | topologyService.getDisjointPaths(topology, srcDevice, dstDevice) : |
| 168 | topologyService.getDisjointPaths(topology, srcDevice, dstDevice, weight); |
| 169 | |
| 170 | return edgeToEdgePathsDisjoint(srcEdge, dstEdge, paths); |
| 171 | } |
| 172 | |
| 173 | @Override |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 174 | public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, |
| 175 | Map<Link, Object> riskProfile) { |
| 176 | return getDisjointPaths(src, dst, null, riskProfile); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 180 | public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, LinkWeight weight, |
| 181 | Map<Link, Object> riskProfile) { |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 182 | checkNotNull(src, ELEMENT_ID_NULL); |
| 183 | checkNotNull(dst, ELEMENT_ID_NULL); |
| 184 | |
| 185 | // Get the source and destination edge locations |
| 186 | EdgeLink srcEdge = getEdgeLink(src, true); |
| 187 | EdgeLink dstEdge = getEdgeLink(dst, false); |
| 188 | |
| 189 | // If either edge is null, bail with no paths. |
| 190 | if (srcEdge == null || dstEdge == null) { |
| 191 | return ImmutableSet.of(); |
| 192 | } |
| 193 | |
| 194 | DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src; |
| 195 | DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst; |
| 196 | |
| 197 | // If the source and destination are on the same edge device, there |
| 198 | // is just one path, so build it and return it. |
| 199 | if (srcDevice.equals(dstDevice)) { |
| 200 | return edgeToEdgePathsDisjoint(srcEdge, dstEdge); |
| 201 | } |
| 202 | |
| 203 | // Otherwise get all paths between the source and destination edge |
| 204 | // devices. |
| 205 | Topology topology = topologyService.currentTopology(); |
| 206 | Set<DisjointPath> paths = weight == null ? |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 207 | topologyService.getDisjointPaths(topology, srcDevice, dstDevice, riskProfile) : |
| 208 | topologyService.getDisjointPaths(topology, srcDevice, dstDevice, weight, riskProfile); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 209 | |
| 210 | return edgeToEdgePathsDisjoint(srcEdge, dstEdge, paths); |
| 211 | } |
| 212 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 213 | // Finds the host edge link if the element ID is a host id of an existing |
| 214 | // host. Otherwise, if the host does not exist, it returns null and if |
| 215 | // the element ID is not a host ID, returns NOT_HOST edge link. |
| 216 | private EdgeLink getEdgeLink(ElementId elementId, boolean isIngress) { |
| 217 | if (elementId instanceof HostId) { |
| 218 | // Resolve the host, return null. |
| 219 | Host host = hostService.getHost((HostId) elementId); |
| 220 | if (host == null) { |
| 221 | return null; |
| 222 | } |
| 223 | return new DefaultEdgeLink(PID, new ConnectPoint(elementId, P0), |
| 224 | host.location(), isIngress); |
| 225 | } |
| 226 | return NOT_HOST; |
| 227 | } |
| 228 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 229 | // Produces a set of edge-to-edge paths using the set of infrastructure |
| 230 | // paths and the given edge links. |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 231 | private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink) { |
| 232 | Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(1); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 233 | endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, null)); |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 234 | return endToEndPaths; |
| 235 | } |
| 236 | |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 237 | // Produces a set of edge-to-edge paths using the set of infrastructure |
| 238 | // paths and the given edge links. |
| 239 | private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink, Set<Path> paths) { |
| 240 | Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(paths.size()); |
| 241 | for (Path path : paths) { |
| 242 | endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, path)); |
| 243 | } |
| 244 | return endToEndPaths; |
| 245 | } |
| 246 | |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 247 | private Set<DisjointPath> edgeToEdgePathsDisjoint(EdgeLink srcLink, EdgeLink dstLink) { |
| 248 | Set<DisjointPath> endToEndPaths = Sets.newHashSetWithExpectedSize(1); |
| 249 | endToEndPaths.add(edgeToEdgePathD(srcLink, dstLink, null)); |
| 250 | return endToEndPaths; |
| 251 | } |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 252 | |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 253 | private Set<DisjointPath> edgeToEdgePathsDisjoint(EdgeLink srcLink, EdgeLink dstLink, Set<DisjointPath> paths) { |
| 254 | Set<DisjointPath> endToEndPaths = Sets.newHashSetWithExpectedSize(paths.size()); |
| 255 | for (DisjointPath path : paths) { |
| 256 | endToEndPaths.add(edgeToEdgePathD(srcLink, dstLink, path)); |
| 257 | } |
| 258 | return endToEndPaths; |
| 259 | } |
| 260 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 261 | // Produces a direct edge-to-edge path. |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 262 | private Path edgeToEdgePath(EdgeLink srcLink, EdgeLink dstLink, Path path) { |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 263 | List<Link> links = Lists.newArrayListWithCapacity(2); |
| 264 | // Add source and destination edge links only if they are real and |
| 265 | // add the infrastructure path only if it is not null. |
| 266 | if (srcLink != NOT_HOST) { |
| 267 | links.add(srcLink); |
| 268 | } |
| 269 | if (path != null) { |
| 270 | links.addAll(path.links()); |
| 271 | } |
| 272 | if (dstLink != NOT_HOST) { |
| 273 | links.add(dstLink); |
| 274 | } |
| 275 | return new DefaultPath(PID, links, 2); |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 278 | // Produces a direct edge-to-edge path. |
| 279 | private DisjointPath edgeToEdgePathD(EdgeLink srcLink, EdgeLink dstLink, DisjointPath path) { |
| 280 | return new DefaultDisjointPath(PID, (DefaultPath) edgeToEdgePath(srcLink, dstLink, path.primary()), |
| 281 | (DefaultPath) edgeToEdgePath(srcLink, dstLink, path.backup())); |
| 282 | } |
| 283 | |
| 284 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 285 | // Special value for edge link to represent that this is really not an |
| 286 | // edge link since the src or dst are really an infrastructure device. |
| 287 | private static class NotHost extends DefaultEdgeLink implements EdgeLink { |
| 288 | NotHost() { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 289 | super(PID, new ConnectPoint(HostId.NONE, P0), |
| 290 | new HostLocation(DeviceId.NONE, P0, 0L), false); |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 293 | } |