blob: 8347ee38514c5b6b925abf9166928fe32e74c5db [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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'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) {
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700137 return getDisjointPaths(src, dst, (LinkWeight) null);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700138 }
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 Vachuska48e64e42015-09-22 15:32:55 -0700174 public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst,
175 Map<Link, Object> riskProfile) {
176 return getDisjointPaths(src, dst, null, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700177 }
178
179 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700180 public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, LinkWeight weight,
181 Map<Link, Object> riskProfile) {
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700182 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 Vachuska48e64e42015-09-22 15:32:55 -0700207 topologyService.getDisjointPaths(topology, srcDevice, dstDevice, riskProfile) :
208 topologyService.getDisjointPaths(topology, srcDevice, dstDevice, weight, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700209
210 return edgeToEdgePathsDisjoint(srcEdge, dstEdge, paths);
211 }
212
tomcbefa232014-09-16 14:17:20 -0700213 // 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
tomca90c462014-09-22 11:40:58 -0700229 // Produces a set of edge-to-edge paths using the set of infrastructure
230 // paths and the given edge links.
tom4774c8f2014-09-16 11:17:08 -0700231 private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink) {
232 Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(1);
tomca90c462014-09-22 11:40:58 -0700233 endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, null));
tom4774c8f2014-09-16 11:17:08 -0700234 return endToEndPaths;
235 }
236
tom4774c8f2014-09-16 11:17:08 -0700237 // 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 Cheerla2ec191f2015-07-09 12:34:54 -0700247 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 Vachuska48e64e42015-09-22 15:32:55 -0700252
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700253 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
tomca90c462014-09-22 11:40:58 -0700261 // Produces a direct edge-to-edge path.
tom4774c8f2014-09-16 11:17:08 -0700262 private Path edgeToEdgePath(EdgeLink srcLink, EdgeLink dstLink, Path path) {
tomca90c462014-09-22 11:40:58 -0700263 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);
tom4774c8f2014-09-16 11:17:08 -0700276 }
277
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700278 // 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
tomcbefa232014-09-16 14:17:20 -0700285 // 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() {
tom545708e2014-10-09 17:10:02 -0700289 super(PID, new ConnectPoint(HostId.NONE, P0),
290 new HostLocation(DeviceId.NONE, P0, 0L), false);
tomcbefa232014-09-16 14:17:20 -0700291 }
292 }
tom4774c8f2014-09-16 11:17:08 -0700293}