blob: f2cf9740d3b9c3e3cad0968b5bb6d3a6fcfdf372 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.topology.impl;
tom4774c8f2014-09-16 11:17:08 -070017
tomca90c462014-09-22 11:40:58 -070018import com.google.common.collect.ImmutableSet;
tom4774c8f2014-09-16 11:17:08 -070019import com.google.common.collect.Lists;
20import com.google.common.collect.Sets;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DefaultEdgeLink;
29import org.onosproject.net.DefaultPath;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070030import org.onosproject.net.DisjointPath;
31import org.onosproject.net.DefaultDisjointPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.DeviceId;
33import org.onosproject.net.EdgeLink;
34import org.onosproject.net.ElementId;
35import org.onosproject.net.Host;
36import org.onosproject.net.HostId;
37import org.onosproject.net.HostLocation;
38import org.onosproject.net.Link;
39import org.onosproject.net.Path;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.host.HostService;
42import org.onosproject.net.provider.ProviderId;
43import org.onosproject.net.topology.LinkWeight;
44import org.onosproject.net.topology.PathService;
45import org.onosproject.net.topology.Topology;
46import org.onosproject.net.topology.TopologyService;
tom4774c8f2014-09-16 11:17:08 -070047import org.slf4j.Logger;
48
49import java.util.List;
50import java.util.Set;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070051import java.util.Map;
52
tom4774c8f2014-09-16 11:17:08 -070053
54import static com.google.common.base.Preconditions.checkNotNull;
55import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoon541ef712015-05-23 17:18:34 +090056import static org.onosproject.security.AppGuard.checkPermission;
Changhoon Yoonb856b812015-08-10 03:47:19 +090057import static org.onosproject.security.AppPermission.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090058
tom4774c8f2014-09-16 11:17:08 -070059
60/**
61 * Provides implementation of a path selection service atop the current
62 * topology and host services.
63 */
64@Component(immediate = true)
65@Service
tom10262dd2014-09-19 10:51:19 -070066public class PathManager implements PathService {
tom4774c8f2014-09-16 11:17:08 -070067
tomcbefa232014-09-16 14:17:20 -070068 private static final String ELEMENT_ID_NULL = "Element ID cannot be null";
tom4774c8f2014-09-16 11:17:08 -070069
Brian O'Connorabafb502014-12-02 22:26:20 -080070 private static final ProviderId PID = new ProviderId("core", "org.onosproject.core");
tom4774c8f2014-09-16 11:17:08 -070071 private static final PortNumber P0 = PortNumber.portNumber(0);
72
tomcbefa232014-09-16 14:17:20 -070073 private static final EdgeLink NOT_HOST = new NotHost();
74
tom4774c8f2014-09-16 11:17:08 -070075 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
tomca90c462014-09-22 11:40:58 -070084 public void activate() {
tom4774c8f2014-09-16 11:17:08 -070085 log.info("Started");
86 }
87
88 @Deactivate
tomca90c462014-09-22 11:40:58 -070089 public void deactivate() {
tom4774c8f2014-09-16 11:17:08 -070090 log.info("Stopped");
91 }
92
93 @Override
tomcbefa232014-09-16 14:17:20 -070094 public Set<Path> getPaths(ElementId src, ElementId dst) {
Changhoon Yoonb856b812015-08-10 03:47:19 +090095 checkPermission(TOPOLOGY_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +090096
tom4774c8f2014-09-16 11:17:08 -070097 return getPaths(src, dst, null);
98 }
99
100 @Override
tomcbefa232014-09-16 14:17:20 -0700101 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900102 checkPermission(TOPOLOGY_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900103
tomcbefa232014-09-16 14:17:20 -0700104 checkNotNull(src, ELEMENT_ID_NULL);
105 checkNotNull(dst, ELEMENT_ID_NULL);
tom4774c8f2014-09-16 11:17:08 -0700106
107 // Get the source and destination edge locations
tomcbefa232014-09-16 14:17:20 -0700108 EdgeLink srcEdge = getEdgeLink(src, true);
109 EdgeLink dstEdge = getEdgeLink(dst, false);
110
tomca90c462014-09-22 11:40:58 -0700111 // If either edge is null, bail with no paths.
112 if (srcEdge == null || dstEdge == null) {
113 return ImmutableSet.of();
114 }
115
tomcbefa232014-09-16 14:17:20 -0700116 DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
117 DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
tom4774c8f2014-09-16 11:17:08 -0700118
119 // If the source and destination are on the same edge device, there
120 // is just one path, so build it and return it.
tomcbefa232014-09-16 14:17:20 -0700121 if (srcDevice.equals(dstDevice)) {
tom4774c8f2014-09-16 11:17:08 -0700122 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 ?
tomcbefa232014-09-16 14:17:20 -0700129 topologyService.getPaths(topology, srcDevice, dstDevice) :
130 topologyService.getPaths(topology, srcDevice, dstDevice, weight);
tom4774c8f2014-09-16 11:17:08 -0700131
132 return edgeToEdgePaths(srcEdge, dstEdge, paths);
133 }
134
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700135 @Override
136 public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900137 checkPermission(TOPOLOGY_READ);
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700138 return getDisjointPaths(src, dst, (LinkWeight) null);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700139 }
140
141 @Override
142 public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, LinkWeight weight) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900143 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700144 checkNotNull(src, ELEMENT_ID_NULL);
145 checkNotNull(dst, ELEMENT_ID_NULL);
146
147 // Get the source and destination edge locations
148 EdgeLink srcEdge = getEdgeLink(src, true);
149 EdgeLink dstEdge = getEdgeLink(dst, false);
150
151 // If either edge is null, bail with no paths.
152 if (srcEdge == null || dstEdge == null) {
153 return ImmutableSet.of();
154 }
155
156 DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
157 DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
158
159 // If the source and destination are on the same edge device, there
160 // is just one path, so build it and return it.
161 if (srcDevice.equals(dstDevice)) {
162 return edgeToEdgePathsDisjoint(srcEdge, dstEdge);
163 }
164
165 // Otherwise get all paths between the source and destination edge
166 // devices.
167 Topology topology = topologyService.currentTopology();
168 Set<DisjointPath> paths = weight == null ?
169 topologyService.getDisjointPaths(topology, srcDevice, dstDevice) :
170 topologyService.getDisjointPaths(topology, srcDevice, dstDevice, weight);
171
172 return edgeToEdgePathsDisjoint(srcEdge, dstEdge, paths);
173 }
174
175 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700176 public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst,
177 Map<Link, Object> riskProfile) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900178 checkPermission(TOPOLOGY_READ);
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700179 return getDisjointPaths(src, dst, null, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700180 }
181
182 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700183 public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, LinkWeight weight,
184 Map<Link, Object> riskProfile) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900185 checkPermission(TOPOLOGY_READ);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700186 checkNotNull(src, ELEMENT_ID_NULL);
187 checkNotNull(dst, ELEMENT_ID_NULL);
188
189 // Get the source and destination edge locations
190 EdgeLink srcEdge = getEdgeLink(src, true);
191 EdgeLink dstEdge = getEdgeLink(dst, false);
192
193 // If either edge is null, bail with no paths.
194 if (srcEdge == null || dstEdge == null) {
195 return ImmutableSet.of();
196 }
197
198 DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
199 DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
200
201 // If the source and destination are on the same edge device, there
202 // is just one path, so build it and return it.
203 if (srcDevice.equals(dstDevice)) {
204 return edgeToEdgePathsDisjoint(srcEdge, dstEdge);
205 }
206
207 // Otherwise get all paths between the source and destination edge
208 // devices.
209 Topology topology = topologyService.currentTopology();
210 Set<DisjointPath> paths = weight == null ?
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700211 topologyService.getDisjointPaths(topology, srcDevice, dstDevice, riskProfile) :
212 topologyService.getDisjointPaths(topology, srcDevice, dstDevice, weight, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700213
214 return edgeToEdgePathsDisjoint(srcEdge, dstEdge, paths);
215 }
216
tomcbefa232014-09-16 14:17:20 -0700217 // Finds the host edge link if the element ID is a host id of an existing
218 // host. Otherwise, if the host does not exist, it returns null and if
219 // the element ID is not a host ID, returns NOT_HOST edge link.
220 private EdgeLink getEdgeLink(ElementId elementId, boolean isIngress) {
221 if (elementId instanceof HostId) {
222 // Resolve the host, return null.
223 Host host = hostService.getHost((HostId) elementId);
224 if (host == null) {
225 return null;
226 }
227 return new DefaultEdgeLink(PID, new ConnectPoint(elementId, P0),
228 host.location(), isIngress);
229 }
230 return NOT_HOST;
231 }
232
tomca90c462014-09-22 11:40:58 -0700233 // Produces a set of edge-to-edge paths using the set of infrastructure
234 // paths and the given edge links.
tom4774c8f2014-09-16 11:17:08 -0700235 private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink) {
236 Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(1);
tomca90c462014-09-22 11:40:58 -0700237 endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, null));
tom4774c8f2014-09-16 11:17:08 -0700238 return endToEndPaths;
239 }
240
tom4774c8f2014-09-16 11:17:08 -0700241 // Produces a set of edge-to-edge paths using the set of infrastructure
242 // paths and the given edge links.
243 private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink, Set<Path> paths) {
244 Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(paths.size());
245 for (Path path : paths) {
246 endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, path));
247 }
248 return endToEndPaths;
249 }
250
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700251 private Set<DisjointPath> edgeToEdgePathsDisjoint(EdgeLink srcLink, EdgeLink dstLink) {
252 Set<DisjointPath> endToEndPaths = Sets.newHashSetWithExpectedSize(1);
253 endToEndPaths.add(edgeToEdgePathD(srcLink, dstLink, null));
254 return endToEndPaths;
255 }
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700256
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700257 private Set<DisjointPath> edgeToEdgePathsDisjoint(EdgeLink srcLink, EdgeLink dstLink, Set<DisjointPath> paths) {
258 Set<DisjointPath> endToEndPaths = Sets.newHashSetWithExpectedSize(paths.size());
259 for (DisjointPath path : paths) {
260 endToEndPaths.add(edgeToEdgePathD(srcLink, dstLink, path));
261 }
262 return endToEndPaths;
263 }
264
tomca90c462014-09-22 11:40:58 -0700265 // Produces a direct edge-to-edge path.
tom4774c8f2014-09-16 11:17:08 -0700266 private Path edgeToEdgePath(EdgeLink srcLink, EdgeLink dstLink, Path path) {
tomca90c462014-09-22 11:40:58 -0700267 List<Link> links = Lists.newArrayListWithCapacity(2);
268 // Add source and destination edge links only if they are real and
269 // add the infrastructure path only if it is not null.
270 if (srcLink != NOT_HOST) {
271 links.add(srcLink);
272 }
273 if (path != null) {
274 links.addAll(path.links());
275 }
276 if (dstLink != NOT_HOST) {
277 links.add(dstLink);
278 }
279 return new DefaultPath(PID, links, 2);
tom4774c8f2014-09-16 11:17:08 -0700280 }
281
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700282 // Produces a direct edge-to-edge path.
283 private DisjointPath edgeToEdgePathD(EdgeLink srcLink, EdgeLink dstLink, DisjointPath path) {
Shashikanth VH98a88ab2016-02-12 15:03:37 +0530284 Path primary = null;
285 Path backup = null;
286 if (path != null) {
287 primary = path.primary();
288 backup = path.backup();
289 }
290 return new DefaultDisjointPath(PID, (DefaultPath) edgeToEdgePath(srcLink, dstLink, primary),
291 (DefaultPath) edgeToEdgePath(srcLink, dstLink, backup));
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700292 }
293
294
tomcbefa232014-09-16 14:17:20 -0700295 // Special value for edge link to represent that this is really not an
296 // edge link since the src or dst are really an infrastructure device.
297 private static class NotHost extends DefaultEdgeLink implements EdgeLink {
298 NotHost() {
tom545708e2014-10-09 17:10:02 -0700299 super(PID, new ConnectPoint(HostId.NONE, P0),
300 new HostLocation(DeviceId.NONE, P0, 0L), false);
tomcbefa232014-09-16 14:17:20 -0700301 }
302 }
tom4774c8f2014-09-16 11:17:08 -0700303}