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 | */ |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 16 | package org.onlab.onos.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; |
| 27 | import org.onlab.onos.net.ConnectPoint; |
| 28 | import org.onlab.onos.net.DefaultEdgeLink; |
| 29 | import org.onlab.onos.net.DefaultPath; |
| 30 | import org.onlab.onos.net.DeviceId; |
| 31 | import org.onlab.onos.net.EdgeLink; |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.ElementId; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.Host; |
| 34 | import org.onlab.onos.net.HostId; |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.HostLocation; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.Link; |
| 37 | import org.onlab.onos.net.Path; |
| 38 | import org.onlab.onos.net.PortNumber; |
| 39 | import org.onlab.onos.net.host.HostService; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.provider.ProviderId; |
| 41 | import org.onlab.onos.net.topology.LinkWeight; |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.topology.PathService; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.topology.Topology; |
| 44 | import org.onlab.onos.net.topology.TopologyService; |
| 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | import java.util.List; |
| 48 | import java.util.Set; |
| 49 | |
| 50 | import static com.google.common.base.Preconditions.checkNotNull; |
| 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
| 53 | /** |
| 54 | * Provides implementation of a path selection service atop the current |
| 55 | * topology and host services. |
| 56 | */ |
| 57 | @Component(immediate = true) |
| 58 | @Service |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 59 | public class PathManager implements PathService { |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 60 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 61 | private static final String ELEMENT_ID_NULL = "Element ID cannot be null"; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 62 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 63 | private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core"); |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 64 | private static final PortNumber P0 = PortNumber.portNumber(0); |
| 65 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 66 | private static final EdgeLink NOT_HOST = new NotHost(); |
| 67 | |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 68 | private final Logger log = getLogger(getClass()); |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected TopologyService topologyService; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected HostService hostService; |
| 75 | |
| 76 | @Activate |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 77 | public void activate() { |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 78 | log.info("Started"); |
| 79 | } |
| 80 | |
| 81 | @Deactivate |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 82 | public void deactivate() { |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 83 | log.info("Stopped"); |
| 84 | } |
| 85 | |
| 86 | @Override |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 87 | public Set<Path> getPaths(ElementId src, ElementId dst) { |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 88 | return getPaths(src, dst, null); |
| 89 | } |
| 90 | |
| 91 | @Override |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 92 | public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) { |
| 93 | checkNotNull(src, ELEMENT_ID_NULL); |
| 94 | checkNotNull(dst, ELEMENT_ID_NULL); |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 95 | |
| 96 | // Get the source and destination edge locations |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 97 | EdgeLink srcEdge = getEdgeLink(src, true); |
| 98 | EdgeLink dstEdge = getEdgeLink(dst, false); |
| 99 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 100 | // If either edge is null, bail with no paths. |
| 101 | if (srcEdge == null || dstEdge == null) { |
| 102 | return ImmutableSet.of(); |
| 103 | } |
| 104 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 105 | DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src; |
| 106 | DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst; |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 107 | |
| 108 | // If the source and destination are on the same edge device, there |
| 109 | // is just one path, so build it and return it. |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 110 | if (srcDevice.equals(dstDevice)) { |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 111 | return edgeToEdgePaths(srcEdge, dstEdge); |
| 112 | } |
| 113 | |
| 114 | // Otherwise get all paths between the source and destination edge |
| 115 | // devices. |
| 116 | Topology topology = topologyService.currentTopology(); |
| 117 | Set<Path> paths = weight == null ? |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 118 | topologyService.getPaths(topology, srcDevice, dstDevice) : |
| 119 | topologyService.getPaths(topology, srcDevice, dstDevice, weight); |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 120 | |
| 121 | return edgeToEdgePaths(srcEdge, dstEdge, paths); |
| 122 | } |
| 123 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 124 | // Finds the host edge link if the element ID is a host id of an existing |
| 125 | // host. Otherwise, if the host does not exist, it returns null and if |
| 126 | // the element ID is not a host ID, returns NOT_HOST edge link. |
| 127 | private EdgeLink getEdgeLink(ElementId elementId, boolean isIngress) { |
| 128 | if (elementId instanceof HostId) { |
| 129 | // Resolve the host, return null. |
| 130 | Host host = hostService.getHost((HostId) elementId); |
| 131 | if (host == null) { |
| 132 | return null; |
| 133 | } |
| 134 | return new DefaultEdgeLink(PID, new ConnectPoint(elementId, P0), |
| 135 | host.location(), isIngress); |
| 136 | } |
| 137 | return NOT_HOST; |
| 138 | } |
| 139 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 140 | // Produces a set of edge-to-edge paths using the set of infrastructure |
| 141 | // paths and the given edge links. |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 142 | private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink) { |
| 143 | Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(1); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 144 | endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, null)); |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 145 | return endToEndPaths; |
| 146 | } |
| 147 | |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 148 | // Produces a set of edge-to-edge paths using the set of infrastructure |
| 149 | // paths and the given edge links. |
| 150 | private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink, Set<Path> paths) { |
| 151 | Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(paths.size()); |
| 152 | for (Path path : paths) { |
| 153 | endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, path)); |
| 154 | } |
| 155 | return endToEndPaths; |
| 156 | } |
| 157 | |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 158 | // Produces a direct edge-to-edge path. |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 159 | private Path edgeToEdgePath(EdgeLink srcLink, EdgeLink dstLink, Path path) { |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 160 | List<Link> links = Lists.newArrayListWithCapacity(2); |
| 161 | // Add source and destination edge links only if they are real and |
| 162 | // add the infrastructure path only if it is not null. |
| 163 | if (srcLink != NOT_HOST) { |
| 164 | links.add(srcLink); |
| 165 | } |
| 166 | if (path != null) { |
| 167 | links.addAll(path.links()); |
| 168 | } |
| 169 | if (dstLink != NOT_HOST) { |
| 170 | links.add(dstLink); |
| 171 | } |
| 172 | return new DefaultPath(PID, links, 2); |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 173 | } |
| 174 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 175 | // Special value for edge link to represent that this is really not an |
| 176 | // edge link since the src or dst are really an infrastructure device. |
| 177 | private static class NotHost extends DefaultEdgeLink implements EdgeLink { |
| 178 | NotHost() { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 179 | super(PID, new ConnectPoint(HostId.NONE, P0), |
| 180 | new HostLocation(DeviceId.NONE, P0, 0L), false); |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
tom | 4774c8f | 2014-09-16 11:17:08 -0700 | [diff] [blame] | 183 | } |