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