blob: 6f763ac534be28e2f8f8b0faa39a5dc1e9818584 [file] [log] [blame]
tom4774c8f2014-09-16 11:17:08 -07001package org.onlab.onos.net.trivial.path;
2
3import com.google.common.collect.Lists;
4import com.google.common.collect.Sets;
5import org.apache.felix.scr.annotations.Activate;
6import org.apache.felix.scr.annotations.Component;
7import org.apache.felix.scr.annotations.Deactivate;
8import org.apache.felix.scr.annotations.Reference;
9import org.apache.felix.scr.annotations.ReferenceCardinality;
10import org.apache.felix.scr.annotations.Service;
11import org.onlab.onos.net.ConnectPoint;
12import org.onlab.onos.net.DefaultEdgeLink;
13import org.onlab.onos.net.DefaultPath;
14import org.onlab.onos.net.DeviceId;
15import org.onlab.onos.net.EdgeLink;
16import org.onlab.onos.net.Host;
17import org.onlab.onos.net.HostId;
18import org.onlab.onos.net.Link;
19import org.onlab.onos.net.Path;
20import org.onlab.onos.net.PortNumber;
21import org.onlab.onos.net.host.HostService;
22import org.onlab.onos.net.path.PathService;
23import org.onlab.onos.net.provider.ProviderId;
24import org.onlab.onos.net.topology.LinkWeight;
25import org.onlab.onos.net.topology.Topology;
26import org.onlab.onos.net.topology.TopologyService;
27import org.slf4j.Logger;
28
29import java.util.List;
30import java.util.Set;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * Provides implementation of a path selection service atop the current
37 * topology and host services.
38 */
39@Component(immediate = true)
40@Service
41public class PathManager implements PathService {
42
43 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
44 private static final String HOST_ID_NULL = "Host ID cannot be null";
45
46 private static final ProviderId PID = new ProviderId("org.onlab.onos.core");
47 private static final PortNumber P0 = PortNumber.portNumber(0);
48
49 private final Logger log = getLogger(getClass());
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected TopologyService topologyService;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected HostService hostService;
56
57 @Activate
58 public void setUp() {
59 log.info("Started");
60 }
61
62 @Deactivate
63 public void tearDown() {
64 log.info("Stopped");
65 }
66
67 @Override
68 public Set<Path> getPaths(DeviceId src, DeviceId dst) {
69 checkNotNull(src, DEVICE_ID_NULL);
70 checkNotNull(dst, DEVICE_ID_NULL);
71 Topology topology = topologyService.currentTopology();
72 return topologyService.getPaths(topology, src, dst);
73 }
74
75 @Override
76 public Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeight weight) {
77 checkNotNull(src, DEVICE_ID_NULL);
78 checkNotNull(dst, DEVICE_ID_NULL);
79 Topology topology = topologyService.currentTopology();
80 return topologyService.getPaths(topology, src, dst, weight);
81 }
82
83 @Override
84 public Set<Path> getPaths(HostId src, HostId dst) {
85 return getPaths(src, dst, null);
86 }
87
88 @Override
89 public Set<Path> getPaths(HostId src, HostId dst, LinkWeight weight) {
90 checkNotNull(src, HOST_ID_NULL);
91 checkNotNull(dst, HOST_ID_NULL);
92
93 // Resolve the source host, bail if unable.
94 Host srcHost = hostService.getHost(src);
95 if (srcHost == null) {
96 return Sets.newHashSet();
97 }
98
99 // Resolve the destination host, bail if unable.
100 Host dstHost = hostService.getHost(dst);
101 if (dstHost == null) {
102 return Sets.newHashSet();
103 }
104
105 // Get the source and destination edge locations
106 EdgeLink srcEdge = new DefaultEdgeLink(PID, new ConnectPoint(src, P0),
107 srcHost.location(), true);
108 EdgeLink dstEdge = new DefaultEdgeLink(PID, new ConnectPoint(dst, P0),
109 dstHost.location(), false);
110
111 // If the source and destination are on the same edge device, there
112 // is just one path, so build it and return it.
113 if (srcEdge.dst().deviceId().equals(dstEdge.src().deviceId())) {
114 return edgeToEdgePaths(srcEdge, dstEdge);
115 }
116
117 // Otherwise get all paths between the source and destination edge
118 // devices.
119 Topology topology = topologyService.currentTopology();
120 Set<Path> paths = weight == null ?
121 topologyService.getPaths(topology, srcEdge.dst().deviceId(),
122 dstEdge.src().deviceId()) :
123 topologyService.getPaths(topology, srcEdge.dst().deviceId(),
124 dstEdge.src().deviceId(), weight);
125
126 return edgeToEdgePaths(srcEdge, dstEdge, paths);
127 }
128
129 // Produces a set of direct edge-to-edge paths.
130 private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink) {
131 Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(1);
132 endToEndPaths.add(edgeToEdgePath(srcLink, dstLink));
133 return endToEndPaths;
134 }
135
136 // Produces a direct edge-to-edge path.
137 private Path edgeToEdgePath(EdgeLink srcLink, EdgeLink dstLink) {
138 List<Link> links = Lists.newArrayListWithCapacity(2);
139 links.add(srcLink);
140 links.add(dstLink);
141 return new DefaultPath(PID, links, 2);
142 }
143
144 // Produces a set of edge-to-edge paths using the set of infrastructure
145 // paths and the given edge links.
146 private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink, Set<Path> paths) {
147 Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(paths.size());
148 for (Path path : paths) {
149 endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, path));
150 }
151 return endToEndPaths;
152 }
153
154 // Produces an edge-to-edge path using the specified infrastructure path
155 // and edge links.
156 private Path edgeToEdgePath(EdgeLink srcLink, EdgeLink dstLink, Path path) {
157 List<Link> links = Lists.newArrayListWithCapacity(path.links().size() + 2);
158 links.add(srcLink);
159 links.addAll(path.links());
160 links.add(dstLink);
161 return new DefaultPath(path.providerId(), links, path.cost() + 2);
162 }
163
164}