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